当前位置: 首页 > article >正文

FreeRTOS的任务挂起和恢复

1.任务的挂起

void vTaskSuspend( TaskHandle_t xTaskToSuspend )
	{
	TCB_t *pxTCB;

        /* 进入临界区 */
		taskENTER_CRITICAL();
		{
			/* If null is passed in here then it is the running task that is
			being suspended. */
			 /* 获取任务控制块,若为NULL则挂起自身 */
			pxTCB = prvGetTCBFromHandle( xTaskToSuspend );

			traceTASK_SUSPEND( pxTCB );

			/* Remove task from the ready/delayed list and place in the
			suspended list. */

			/*从就绪/延迟列表中删除任务 */
			if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
			{
				taskRESET_READY_PRIORITY( pxTCB->uxPriority );
			}
			else
			{
				mtCOVERAGE_TEST_MARKER();
			}

			/* Is the task waiting on an event also? */
			/*任务是否也在等待事件,从事件链表中删除*/
			if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
			{
				( void ) uxListRemove( &( pxTCB->xEventListItem ) );
			}
			else
			{
				mtCOVERAGE_TEST_MARKER();
			}
			/* 将任务放在挂起链表中 */
			vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
		}
		taskEXIT_CRITICAL();  /* 退出临界区 */

		if( pxTCB == pxCurrentTCB )  /*若刚挂起的是正在运行的任务 */
		{
			if( xSchedulerRunning != pdFALSE )  /* 调度器正常运行 */
			{
				/* The current task has just been suspended. */
				configASSERT( uxSchedulerSuspended == 0 );
				portYIELD_WITHIN_API();  /* 直接发起任务切换 */
			}
			else
			{
				/* The scheduler is not running, but the task that was pointed
				to by pxCurrentTCB has just been suspended and pxCurrentTCB
				must be adjusted to point to a different task. */
				 /* 若任务调度器没有开启,则读取当前任务挂起列表的长度,判断所有任务是否都被挂起*/
				if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
				{
					/* No other tasks are ready, so set pxCurrentTCB back to
					NULL so when the next task is created pxCurrentTCB will
					be set to point to it no matter what its relative priority
					is. */
					/*没有其他任务准备就绪,因此将pxCurrentTCB设置回NULL*/
					pxCurrentTCB = NULL;
				}
				else
				{
					/* 若还有没被挂起的任务,则获取下一个要运行的任务 */
					vTaskSwitchContext();
				}
			}
		}
		else
		{
			if( xSchedulerRunning != pdFALSE )  /* 调度器正常运行 */
			{
				/* A task other than the currently running task was suspended,
				reset the next expected unblock time in case it referred to the
				task that is now in the Suspended state. */
				taskENTER_CRITICAL();
				{
					prvResetNextTaskUnblockTime();  /* 重置下一个预期的解锁时间 */
				}
				taskEXIT_CRITICAL();
			}
			else
			{
				mtCOVERAGE_TEST_MARKER();
			}
		}
	}

2.任务的恢复

	void vTaskResume( TaskHandle_t xTaskToResume )
	{
	TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;

		/* It does not make sense to resume the calling task. */
		configASSERT( xTaskToResume );

		/* The parameter cannot be NULL as it is impossible to resume the
		currently executing task. */
		if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )    /* 任务控制块不能为NULL和当前任务	*/
		{
			taskENTER_CRITICAL();
			{
				if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )  /* 判断任务是否被挂起 */
				{
					traceTASK_RESUME( pxTCB );

					/* As we are in a critical section we can access the ready
					lists even if the scheduler is suspended. */
					( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );  /* 从挂起列表中移除 */
					prvAddTaskToReadyList( pxTCB );   /* 添加到就绪列表中 */

					/* We may have just resumed a higher priority task. */
					if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )  /* 恢复的任务优先级更高 */
					{
						/* This yield may not cause the task just resumed to run,
						but will leave the lists in the correct state for the
						next yield. */
						taskYIELD_IF_USING_PREEMPTION();  /* 发起任务调度切换 */
					}
					else
					{
						mtCOVERAGE_TEST_MARKER();
					}
				}
				else
				{
					mtCOVERAGE_TEST_MARKER();
				}
			}
			taskEXIT_CRITICAL();
		}
		else
		{
			mtCOVERAGE_TEST_MARKER();
		}
	}

http://www.kler.cn/a/451320.html

相关文章:

  • 【ES6复习笔记】对象方法扩展(17)
  • YOLOv11 引入高效的可变形卷积网络 DCNv4 | 重新思考用于视觉应用的动态和稀疏算子
  • 如何在 Ubuntu 22.04 上安装以及使用 MongoDB
  • 低空经济的地理信息支撑:构建安全、高效的飞行管理体系
  • 搭建Elastic search群集
  • 远程控制macOS一直卡在100%,能连接上了却只显示了壁纸?
  • 高阶:基于Python paddleocr库 提取pdf 文档高亮显示的内容
  • eNSP安装教程(内含安装包)
  • 如何制作期末成绩查询小程序系统?
  • 【magic-dash】01:magic-dash创建单页面应用及二次开发
  • Cornerstone3d 基础概念
  • ECharts散点图-气泡图,附视频讲解与代码下载
  • Pytorch文件夹结构
  • 2024 年12月英语六级CET6听力原文(Long Conersation和Passage)
  • Java期末复习JDBC|网课笔记+校课总结
  • 麒麟系统修改配置镜像源地址并安装openGL
  • WebAssembly与WebGL结合:高性能图形处理
  • Python知识分享第三十五天-Pandas分组聚合
  • Linux 静默安装weblogic及JDK安装
  • chrome主页被被篡改的修复方法
  • 安全见闻(2)
  • 命令手动更新 Navigator
  • C 数组:索引魔杖点化的数据星图阵列
  • Grafana服务监控与日志查询可视化
  • 【C++】B2066救援题目分析和解决讲解
  • 随手记录第十四话 -- 在 Spring Boot 3.2.3 中使用 springdoc-openapi-starter-webmvc-ui