taskENTER_CRITICAL()
taskEXIT_CRITICAL()
[ 内核控制RTOS]
task. h
void taskENTER_CRITICAL( void );
void taskEXIT_CRITICAL( void );
通过调用 taskENTER_CRITICAL() 进入临界区,随后
通过调用 taskEXIT_CRITICAL() 退出临界区。
宏 taskENTER_CRITICAL() 和 taskEXIT_CRITICAL() 提供了一个基本
临界区实现,只需禁用中断即可使其全局运作,
或在特定的中断优先级范围内运作。 请参阅 vTaskSuspendAll()
RTOS API 函数,获取有关在不禁用中断的情况下创建临界区的
信息。
如果所使用的 FreeRTOS 移植未使用
configMAX_SYSCALL_INTERRUPT_PRIORITY
内核配置常量(也称为
configMAX_API_CALL_INTERRUPT_PRIORITY),则调用 taskENTER_CRITICAL() 将
全局禁用中断。 如果所使用的 FreeRTOS 移植
使用了 configMAX_SYSCALL_INTERRUPT_PRIORITY 内核配置常量,
则调用 taskENTER_CRITICAL() 会将中断保留在
由已禁用的 configMAX_SYSCALL_INTERRUPT_PRIORITY 设置的中断优先级一下,
并启用所有更高优先级的中断。
抢占式上下文切换仅在中断内发生,
在中断被禁用时不会发生。 因此,可保证
调用 taskENTER_CRITICAL() 的任务维持在运行状态,直到
退出临界区,除非任务明确试图阻塞或让出
(它不应在临界区的内部进行该操作)。
对 taskENTER_CRITICAL() 和 taskEXIT_CRITICAL() 的调用旨在嵌套。
因此,只有在执行了一次对 taskEXIT_CRITICAL() 的调用,
用于所有先前的 taskENTER_CRITICAL() 调用之后,
才会退出临界区。
临界区必须保持非常短,否则将影响
中断响应时间。 每次 taskENTER_CRITICAL() 调用都必须紧密配合
taskEXIT_CRITICAL() 调用。
不得从临界区调用 FreeRTOS API 函数。
taskENTER_CRITICAL() 和 taskEXIT_CRITICAL()
不得从中断服务程序 (ISR) 调用——请参阅
taskENTER_CRITICAL_FROM_ISR()
和 taskEXIT_CRITICAL_FROM_ISR(),获取中断安全等效项。
- 参数:
-
-
返回:
-
无
用法示例:
/* A function that makes use of a critical section. */
void vDemoFunction( void )
{
/* Enter the critical section. In this example, this function is itself called
from within a critical section, so entering this critical section will result
in a nesting depth of 2. */
taskENTER_CRITICAL();
/* Perform the action that is being protected by the critical section here. */
/* Exit the critical section. In this example, this function is itself called
from a critical section, so this call to taskEXIT_CRITICAL() will decrement the
nesting count by one, but not result in interrupts becoming enabled. */
taskEXIT_CRITICAL();
}
/* A task that calls vDemoFunction() from within a critical section. */
void vTask1( void * pvParameters )
{
for( ;; )
{
/* Perform some functionality here. */
/* Call taskENTER_CRITICAL() to create a critical section. */
taskENTER_CRITICAL();
/* Execute the code that requires the critical section here. */
/* Calls to taskENTER_CRITICAL() can be nested so it is safe to call a
function that includes its own calls to taskENTER_CRITICAL() and
taskEXIT_CRITICAL(). */
vDemoFunction();
/* The operation that required the critical section is complete so exit the
critical section. After this call to taskEXIT_CRITICAL(), the nesting depth
will be zero, so interrupts will have been re-enabled. */
taskEXIT_CRITICAL();
}
}
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.