正点原子FreeRTOS学习笔记——任务调度器的挂起与恢复

目录

目录

目录

一、任务调度器是什么

二、API

1、挂起任务调度器函数

2、恢复任务调度器函数

三、如何使用


一、任务调度器是什么

调度器是内核中负责决定在任何特定时间应执行哪些任务的部分。内核可以在任务生命周期内多次挂起并且稍后恢复一个任务。

调度策略是调度器用来决定在任何时间点执行哪个任务的算法。 非实时多用户系统的策略极有可能使每个任务具有"公平"比例的处理时间。 之后会描述实时/嵌入式系统中使用的策略。

任务除了被迫被内核挂起之外,还可以选择将自己挂起。 如果它想要延迟(睡眠)一段固定时间,或者等待(阻塞)资源 变为可用(例如串行端口)或将要发生的事件(例如按键),它将执行此操作。 阻塞的或正在睡眠的任务无法执行,并且不会分配任何处理时间。

原文链接:https://www.freertos.org/zh-cn-cmn-s/implementation/a00005.html

                                                                                                                                                           

二、API

1、挂起任务调度器函数

void vTaskSuspendAll( void );

挂起调度器。 挂起调度器会阻止上下文切换,但会让中断处于启用状态。 如果调度器被挂起时,中断请求切换上下文,那么请求将会被挂起。而且只有在调度器恢复(取消挂起)时才会执行。

在 vTaskSuspendAll() 之后调用 xTaskResumeAll() 会转换调度器的状态,取消其阻塞状态。

vTaskSuspendAll() 可以嵌套调用。 调用 xTaskResumeAll() 的次数必须与先前调用 vTaskSuspendAll() 的次数相同,然后调度器将取消挂起状态并重新进入活动状态。

xTaskResumeAll() 只能在正在执行的任务中调用,因此不能在调度器处于初始化状态时(启动计划程序之前)调用。

不得在调度器挂起时调用其他 FreeRTOS API 函数。

调度器挂起时,禁止调用可能切换上下文的 API 函数(例如 vTaskDelayUntil()、xQueueSend() 等等) 。

2、恢复任务调度器函数

BaseType_t xTaskResumeAll( void );

恢复通过调用 vTaskSuspendAll() 挂起的调度器。

xTaskResumeAll() 仅恢复调度器, 不会取消挂起 之前通过调用 vTaskSuspend() 而挂起的任务。

返回:

如果恢复调度器导致了上下文切换,则返回 pdTRUE,否则返回 pdFALSE。

                                                                                                                                                            

三、如何使用
 

用法示例:

void vTask1( void * pvParameters )
 {
     for( ;; )
     {
         /* Task code goes here. */

         /* ... */

         /* At some point the task wants to perform a long operation
         during which it does not want to get swapped out.  It cannot
         use taskENTER_CRITICAL ()/taskEXIT_CRITICAL () as the length
         of the operation may cause interrupts to be missed -
         including the ticks.

         Prevent the RTOS kernel swapping out the task. */
         vTaskSuspendAll();

         /* Perform the operation here.  There is no need to use critical
         sections as we have all the microcontroller processing time.
         During this time interrupts will still operate and the real
         time RTOS kernel tick count will be maintained. */

         /* ... */

         /* The operation is complete.  Restart the RTOS kernel.  We want to force
         a context switch - but there is no point if resuming the scheduler
         caused a context switch already. */
         if( !xTaskResumeAll () )
         {
              taskYIELD ();
         }
     }
 }

                                                                                                                                                            

  • 23
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值