前言

  • 20201009
  • LiteOS 2018
  • 需要会通用链表

链接

参考

  • 上面链接

笔录草稿

部分源码分析

源码分析

LOS_TaskCreate函数

  • 需要一个 任务初始化参数结构体 TSK_INIT_PARAM_S 和 一个任务句柄。
  • TSK_INIT_PARAM_S 源码
  1. /**
  2. * @ingroup los_task
  3. * Define the structure of the parameters used for task creation.
  4. *
  5. * Information of specified parameters passed in during task creation.
  6. */
  7. typedef struct tagTskInitParam
  8. {
  9. TSK_ENTRY_FUNC pfnTaskEntry; /**< Task entrance function */
  10. UINT16 usTaskPrio; /**< Task priority */
  11. UINT32 uwArg; /**< Task parameters */
  12. UINT32 uwStackSize; /**< Task stack size */
  13. CHAR *pcName; /**< Task name */
  14. UINT32 uwResved; /**< Reserved */
  15. } TSK_INIT_PARAM_S;
  • LOS_TaskCreate 函数源码

    • 内含解读
  1. /*****************************************************************************
  2. Function : LOS_TaskCreate
  3. Description : Create a task
  4. Input : pstInitParam --- Task init parameters
  5. Output : puwTaskID --- Save task ID
  6. Return : LOS_OK on success or error code on failure
  7. *****************************************************************************/
  8. LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *puwTaskID, TSK_INIT_PARAM_S *pstInitParam)
  9. {
  10. UINT32 uwRet = LOS_OK;
  11. UINTPTR uvIntSave;
  12. LOS_TASK_CB *pstTaskCB; // 定义一个任务控制块
  13. uwRet = LOS_TaskCreateOnly(puwTaskID, pstInitParam); // 以挂起的方式创建一个新的任务(即是阻塞态)
  14. if (LOS_OK != uwRet)
  15. {
  16. return uwRet;
  17. }
  18. pstTaskCB = OS_TCB_FROM_TID(*puwTaskID); // 通过任务 ID 获取 TCB 句柄
  19. uvIntSave = LOS_IntLock(); // 锁任务
  20. pstTaskCB->usTaskStatus &= (~OS_TASK_STATUS_SUSPEND);
  21. pstTaskCB->usTaskStatus |= OS_TASK_STATUS_READY; // 解除阻塞-->进入就绪
  22. #if (LOSCFG_BASE_CORE_CPUP == YES) // CPU 利用率模块代码段
  23. g_pstCpup[pstTaskCB->uwTaskID].uwID = pstTaskCB->uwTaskID;
  24. g_pstCpup[pstTaskCB->uwTaskID].usStatus = pstTaskCB->usTaskStatus;
  25. #endif
  26. osPriqueueEnqueue(&pstTaskCB->stPendList, pstTaskCB->usPriority); // 将该任务插入 优先级队列 ,即是 就绪列表
  27. g_stLosTask.pstNewTask = LOS_DL_LIST_ENTRY(osPriqueueTop(), LOS_TASK_CB, stPendList); /*lint !e413*/ /* 找出已经创建的任务中最高优先级、最先插入的 TCB */
  28. /* 以下只是判断是否需要调度而已 */
  29. /* 如果系统开启了调度,且,锁任务关闭,则,进入二次判断 */
  30. if ((g_bTaskScheduled) && (g_usLosTaskLock == 0))
  31. {
  32. /* 如果最高优先级、最先插入的任务不在运行态,则进行调度 */
  33. if (g_stLosTask.pstRunTask != g_stLosTask.pstNewTask)
  34. {
  35. if (LOS_CHECK_SCHEDULE)
  36. {
  37. (VOID)LOS_IntRestore(uvIntSave);
  38. osSchedule();
  39. return LOS_OK;
  40. }
  41. }
  42. }
  43. /* 解锁任务调度 */
  44. (VOID)LOS_IntRestore(uvIntSave);
  45. return LOS_OK;
  46. }

LOS_TaskCreateOnly函数

  • g_stTskRecyleList

    • 任务可回收链表
  • g_stLosFreeTask
    • 任务可用链表
  1. /*****************************************************************************
  2. Function : LOS_TaskCreateOnly
  3. Description : Create a task and suspend
  4. Input : pstInitParam --- Task init parameters
  5. Output : puwTaskID --- Save task ID
  6. Return : LOS_OK on success or error code on failure
  7. *****************************************************************************/
  8. LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreateOnly(UINT32 *puwTaskID, TSK_INIT_PARAM_S *pstInitParam)
  9. {
  10. UINT32 uwTaskID = 0;
  11. UINTPTR uvIntSave;
  12. VOID *pTopStack;
  13. VOID *pStackPtr;
  14. LOS_TASK_CB *pstTaskCB;
  15. UINT32 uwErrRet = OS_ERROR;
  16. /* 元素合法检查 [start][A] */
  17. if (NULL == puwTaskID)
  18. {
  19. return LOS_ERRNO_TSK_ID_INVALID;
  20. }
  21. if (NULL == pstInitParam)
  22. {
  23. return LOS_ERRNO_TSK_PTR_NULL;
  24. }
  25. if (NULL == pstInitParam->pcName)
  26. {
  27. return LOS_ERRNO_TSK_NAME_EMPTY;
  28. }
  29. if (NULL == pstInitParam->pfnTaskEntry)
  30. {
  31. return LOS_ERRNO_TSK_ENTRY_NULL;
  32. }
  33. if ((pstInitParam->usTaskPrio) > OS_TASK_PRIORITY_LOWEST)
  34. {
  35. return LOS_ERRNO_TSK_PRIOR_ERROR;
  36. }
  37. if (((pstInitParam->usTaskPrio) == OS_TASK_PRIORITY_LOWEST)
  38. && (pstInitParam->pfnTaskEntry != OS_IDLE_TASK_ENTRY))
  39. {
  40. return LOS_ERRNO_TSK_PRIOR_ERROR;
  41. }
  42. if (pstInitParam->uwStackSize > OS_SYS_MEM_SIZE)
  43. {
  44. return LOS_ERRNO_TSK_STKSZ_TOO_LARGE;
  45. }
  46. if (0 == pstInitParam->uwStackSize)
  47. {
  48. pstInitParam->uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
  49. }
  50. pstInitParam->uwStackSize = ALIGN(pstInitParam->uwStackSize , 8);
  51. if (pstInitParam->uwStackSize < LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE)
  52. {
  53. return LOS_ERRNO_TSK_STKSZ_TOO_SMALL;
  54. }
  55. /* 元素合法检查 [end][A] */
  56. uvIntSave = LOS_IntLock(); // 锁任务
  57. /* 处理任务可回收链表,释放出可用任务空间 */
  58. while (!LOS_ListEmpty(&g_stTskRecyleList))
  59. {
  60. pstTaskCB = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&g_stTskRecyleList)); /*lint !e413*/ /* 获取任务 可回收链表 中的 第一个 TCB。 *后面有源码分析* */
  61. LOS_ListDelete(LOS_DL_LIST_FIRST(&g_stTskRecyleList)); // 从该链表中删除该节点
  62. LOS_ListAdd(&g_stLosFreeTask, &pstTaskCB->stPendList); // 把该任块放到 可用链表 中
  63. (VOID)LOS_MemFree(m_aucSysMem0, (VOID *)pstTaskCB->uwTopOfStack); // 释放该任务申请的任务栈空间
  64. pstTaskCB->uwTopOfStack = (UINT32)NULL;// 初始化该任务块栈顶指针
  65. }
  66. /* 判断是否还有可用任务块使用,若没有,则,退出 */
  67. if (LOS_ListEmpty(&g_stLosFreeTask))
  68. {
  69. uwErrRet = LOS_ERRNO_TSK_TCB_UNAVAILABLE;
  70. OS_GOTO_ERREND();
  71. }
  72. pstTaskCB = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&g_stLosFreeTask)); /*lint !e413*/ /* 获取 可用链表 中第一个节点对应的 TCB。*后面有源码分析* */
  73. LOS_ListDelete(LOS_DL_LIST_FIRST(&g_stLosFreeTask)); // 上面获取后,便从 可用链表 中删除该节点。
  74. (VOID)LOS_IntRestore(uvIntSave); // 解锁任务
  75. uwTaskID = pstTaskCB->uwTaskID; // 获取 ID
  76. pTopStack = (VOID *)LOS_MemAllocAlign(m_aucSysMem0, pstInitParam->uwStackSize, 8); // 申请动态内存,8 字节对齐
  77. if (NULL == pTopStack) // 申请失败
  78. {
  79. uvIntSave = LOS_IntLock();
  80. LOS_ListAdd(&g_stLosFreeTask, &pstTaskCB->stPendList);
  81. uwErrRet = LOS_ERRNO_TSK_NO_MEMORY;
  82. OS_GOTO_ERREND();
  83. }
  84. /* 申请成功 */
  85. /* 初始化 TCB */
  86. pStackPtr = osTskStackInit(uwTaskID, pstInitParam->uwStackSize, pTopStack);
  87. pstTaskCB->pStackPointer = pStackPtr;
  88. pstTaskCB->uwArg = pstInitParam->uwArg;
  89. pstTaskCB->uwTopOfStack = (UINT32)pTopStack;
  90. pstTaskCB->uwStackSize = pstInitParam->uwStackSize;
  91. pstTaskCB->pTaskSem = NULL;
  92. pstTaskCB->pTaskMux = NULL;
  93. pstTaskCB->usTaskStatus = OS_TASK_STATUS_SUSPEND;
  94. pstTaskCB->usPriority = pstInitParam->usTaskPrio;
  95. pstTaskCB->pfnTaskEntry = pstInitParam->pfnTaskEntry;
  96. pstTaskCB->uwEvent.uwEventID = 0xFFFFFFFF;
  97. pstTaskCB->uwEventMask = 0;
  98. pstTaskCB->pcTaskName = pstInitParam->pcName;
  99. pstTaskCB->puwMsg = NULL;
  100. *puwTaskID = uwTaskID; /* 更新 ID,返回给上层 */
  101. return LOS_OK; /* 创建成功 */
  102. LOS_ERREND:
  103. (VOID)LOS_IntRestore(uvIntSave);
  104. return uwErrRet;
  105. }

OS_TCB_FROM_PENDLIST 和 宏 LOS_DL_LIST_FIRST

  • OS_TCB_FROM_PENDLIST

    • 获取 链接因子 ptr 中所在的任务控制块的首地址
    • 意思就是:获取某条 stPendList 链表 中 ptr 节点的 TCB句柄
  1. /**
  2. * @ingroup los_task
  3. * @brief Obtain the pointer to a task control block.
  4. * @par Description:
  5. * This API is used to obtain the pointer to a task control block using a corresponding parameter.
  6. * @param ptr [IN] Parameter used for obtaining the task control block.
  7. * @retval Pointer to the task control block.
  8. */
  9. #define OS_TCB_FROM_PENDLIST(ptr) LOS_DL_LIST_ENTRY(ptr, LOS_TASK_CB, stPendList)
  • LOS_DL_LIST_ENTRY

    • 通用链表的算法
    • 获取链接因子 item 所在的数据块的首地址
  1. /**
  2. *@ingroup los_list
  3. *@brief Obtain the pointer to a structure that contains a doubly linked list.
  4. *@par Description:
  5. *This API is used to obtain the pointer to a structure that contains a doubly linked list.
  6. *@param item [IN] Current node's pointer to the next node.
  7. *@param type [IN] Structure name.
  8. *@param member [IN] Member name of the doubly linked list in the structure.
  9. *@retval Pointer to the structure that contains the doubly linked list.
  10. */
  11. #define LOS_DL_LIST_ENTRY(item, type, member) \
  12. ((type *)((char *)item - LOS_OFF_SET_OF(type, member))) \
  • LOS_OFF_SET_OF

    • 通用链表的算法
    • 算出 结构体首地址成员 之间的便宜。
  1. /**
  2. *@ingroup los_list
  3. *@brief Obtain the pointer to a doubly linked list in a structure.
  4. *@par Description:
  5. *This API is used to obtain the pointer to a doubly linked list in a structure.
  6. *@param type [IN] Structure name.
  7. *@param member [IN] Member name of the doubly linked list in the structure.
  8. *@retval Pointer to the doubly linked list in the structure.
  9. */
  10. #define LOS_OFF_SET_OF(type, member) ((long)&((type *)0)->member) /*lint -e(413) */

任务控制块 LOS_TASK_CB

  1. /**
  2. * @ingroup los_task
  3. * Define the task control block structure.
  4. */
  5. typedef struct tagTaskCB
  6. {
  7. VOID *pStackPointer; /**< Task stack pointer */
  8. UINT16 usTaskStatus;
  9. UINT16 usPriority;
  10. UINT32 uwStackSize; /**< Task stack size */
  11. UINT32 uwTopOfStack; /**< Task stack top */
  12. UINT32 uwTaskID; /**< Task ID */
  13. TSK_ENTRY_FUNC pfnTaskEntry; /**< Task entrance function */
  14. VOID *pTaskSem; /**< Task-held semaphore */
  15. VOID *pTaskMux; /**< Task-held mutex */
  16. UINT32 uwArg; /**< Parameter */
  17. CHAR *pcTaskName; /**< Task name */
  18. LOS_DL_LIST stPendList;
  19. LOS_DL_LIST stTimerList;
  20. UINT32 uwIdxRollNum;
  21. EVENT_CB_S uwEvent;
  22. UINT32 uwEventMask; /**< Event mask */
  23. UINT32 uwEventMode; /**< Event mode */
  24. VOID *puwMsg; /**< Memory allocated to queues */
  25. } LOS_TASK_CB;

【LiteOS】LiteOS任务篇-源码分析-创建任务函数的更多相关文章

  1. LiteOS-任务篇-源码分析-删除任务函数

    目录 前言 笔录草稿 源码分析 LOS_TaskDelete函数源码分析 完整源码 参考 链接 前言 20201009 LiteOS 2018 需要会通用链表 笔录草稿 源码分析 LOS_TaskDe ...

  2. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

  3. Spring IOC 容器源码分析 - 创建原始 bean 对象

    1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...

  4. Spring IOC 容器源码分析 - 创建单例 bean 的过程

    1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...

  5. LiteOS-任务篇-源码分析-系统启动函数

    目录 前言 链接 参考 开启调度 LOS_Start 函数源码 osTickStart 函数源码 LOS_StartToRun 函数源码 前言 20201009 移植好内核后,开始实战内核. 源码分析 ...

  6. LiteOS-任务篇-源码分析-任务调度函数

    目录 前言 笔录草稿 核心源码分析 osTaskSchedule函数源码分析 osPendSV函数源码分析 TaskSwitch函数源码分析 调度上层源码分析 osSchedule函数源码分析 LOS ...

  7. SequoiaDB 系列之五 :源码分析之main函数

    好久好久没有写博客了,因为一直要做各种事,工作上的,生活上的,这一下就是半年. 时光如梭. 这两天回头看了看写的博客,感觉都是贻笑大方. 但是还是想坚持把SequoiaDB系列写完. 初步的打算已经确 ...

  8. jQuery 源码分析(五) map函数 $.map和$.fn.map函数 详解

    $.map() 函数用于使用指定函数处理数组中的每个元素(或对象的每个属性),并将处理结果封装为新的数组返回,该函数有三个参数,如下: elems Array/Object类型 指定的需要处理的数组或 ...

  9. jQuery 源码分析(四) each函数 $.each和$.fn.each方法 详解

    $.each一般用来遍历一个数组或对象,$.fn.each()就是指jQuery实例可以执行的操作(因为$.fn是jQuery对象的原型) $.each用来遍历一个数组或对象,并依次执行回掉函数,最后 ...

随机推荐

  1. 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释

    如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python  注释 PIP $ pip install beauti ...

  2. JS Object Deep Copy & 深拷贝 & 浅拷贝

    JS Object Deep Copy & 深拷贝 & 浅拷贝 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Refe ...

  3. Upcoming Browser Behavior Changes & Chrome & SameSite

    Upcoming Browser Behavior Changes & Chrome & SameSite Chrome 80 https://auth0.com/blog/brows ...

  4. Redis 博文索引

    博文索引 Redis 对象与编码 Redis 持久化 Redis 主从复制 Redis 哨兵 Redis 缓存淘汰 Redis 集合统计 Redis 简介

  5. 比起USDT,我为什么建议你选择USDN

    2018年1月16日,USDT(泰达币)进入了很多人的视野.因为在这一天,在全球价值排名前50的加密货币中,包括比特币.莱特币以及以太坊在内的大多数的数字虚拟加密货币都遭遇了价格大幅下跌,只有泰达币价 ...

  6. 「NGK每日快讯」11.20日NGK公链第17期官方快讯!

  7. alpakka-kafka(1)-producer

    alpakka项目是一个基于akka-streams流处理编程工具的scala/java开源项目,通过提供connector连接各种数据源并在akka-streams里进行数据处理.alpakka-k ...

  8. 死磕Spring之IoC篇 - BeanDefinition 的解析阶段(XML 文件)

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  9. 学习js、jquery、vue实现部分组件

    通过js实现radio小组件,最终效果如下 html代码: <!DOCTYPE html> <html lang="en"> <head> &l ...

  10. 使用 xunit 编写测试代码

    使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...