多值信号量
  操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统如何实现进程之间的同步和互斥显得极为重要。比如对同一部分资源的访问是要互斥,不能在另一个进程A在访问的时候被其他的进程再访问这样两个进程相互影响就无法保证正常的访问操作。另一方面是任务间的同步,比如一个任务A依赖于另一个任务B的处理结果,在B处理完成时给A一个就绪信号让A开始使用结果进行相关处理。

  同样在使用之前要先定义一个信号量,然后调用创建OSSemCreate()函数创建信号量注意到函数内的等待队列初始化。每个信号量都有一个等待队列,当一个任务使用完公共资源部分或者完成了同步处理的一部分就会发布一个信号给等待的任务,等待任务会接到这个信号从而进行相应的操作。等待的任务由OS_PEND_LIST这个结构体变量管理TailPtr指向等待队列的最后一个Head指向第一个。

如图的数据结构:

1.创建多值信号量,这实际上是对多值信号量的一个初始化的部分这在USOC内多处都是这样叫的。

  1. #if OS_CFG_SEM_EN > 0u
  2. /*
  3. ************************************************************************************************************************
  4. * CREATE A SEMAPHORE
  5. *
  6. * Description: This function creates a semaphore.
  7. *
  8. * Arguments : p_sem is a pointer to the semaphore to initialize. Your application is responsible for
  9. * allocating storage for the semaphore.
  10. *
  11. * p_name is a pointer to the name you would like to give the semaphore.
  12. *
  13. * cnt is the initial value for the semaphore.
  14. * If used to share resources, you should initialize to the number of resources available.
  15. * If used to signal the occurrence of event(s) then you should initialize to 0.
  16. *
  17. * p_err is a pointer to a variable that will contain an error code returned by this function.
  18. *
  19. * OS_ERR_NONE if the call was successful
  20. * OS_ERR_CREATE_ISR if you called this function from an ISR
  21. * OS_ERR_ILLEGAL_CREATE_RUN_TIME if you are trying to create the semaphore after you
  22. * called OSSafetyCriticalStart().
  23. * OS_ERR_NAME if 'p_name' is a NULL pointer
  24. * OS_ERR_OBJ_CREATED if the semaphore has already been created
  25. * OS_ERR_OBJ_PTR_NULL if 'p_sem' is a NULL pointer
  26. * OS_ERR_OBJ_TYPE if 'p_sem' has already been initialized to a different
  27. * object type
  28. *
  29. * Returns : none
  30. ************************************************************************************************************************
  31. */
  32.  
  33. void OSSemCreate (OS_SEM *p_sem,
  34. CPU_CHAR *p_name,
  35. OS_SEM_CTR cnt,
  36. OS_ERR *p_err)
  37. {
  38. CPU_SR_ALLOC();
  39.  
  40. #ifdef OS_SAFETY_CRITICAL
  41. if (p_err == (OS_ERR *)0) {
  42. OS_SAFETY_CRITICAL_EXCEPTION();
  43. return;
  44. }
  45. #endif
  46.  
  47. #ifdef OS_SAFETY_CRITICAL_IEC61508
  48. if (OSSafetyCriticalStartFlag == DEF_TRUE) {
  49. *p_err = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  50. return;
  51. }
  52. #endif
  53.  
  54. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  55. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Not allowed to be called from an ISR */
  56. *p_err = OS_ERR_CREATE_ISR;
  57. return;
  58. }
  59. #endif
  60.  
  61. #if OS_CFG_ARG_CHK_EN > 0u
  62. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  63. *p_err = OS_ERR_OBJ_PTR_NULL;
  64. return;
  65. }
  66. #endif
  67.  
  68. OS_CRITICAL_ENTER();
  69. //信号量变量初始化
  70. p_sem->Type = OS_OBJ_TYPE_SEM; /* Mark the data structure as a semaphore */
  71. p_sem->Ctr = cnt; /* Set semaphore value */
  72. p_sem->TS = (CPU_TS)0;
  73. p_sem->NamePtr = p_name; /* Save the name of the semaphore */
  74. //信号量上的等待队列初始化
  75. OS_PendListInit(&p_sem->PendList); /* Initialize the waiting list */
  76. #if OS_CFG_DBG_EN > 0u
  77. OS_SemDbgListAdd(p_sem);
  78. #endif
  79. //信号量计数加1
  80. OSSemQty++;
  81. OS_CRITICAL_EXIT_NO_SCHED();
  82. *p_err = OS_ERR_NONE;
  83. }

OSSemCreate ()

2.创建完信号量,然后请求信号量,对于多值信号量先检查信号量是否可用,可用就直接返回信号量的可用数量,如果不可用选择阻塞任务,或者直接继续运行任务。

等待信号量流程:

内部调用的函数的中文注释源码:

  1. ************************************************************************************************************************
  2. * PEND ON SEMAPHORE
  3. *
  4. * Description: This function waits for a semaphore.
  5. *
  6. * Arguments : p_sem is a pointer to the semaphore
  7. *
  8. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will wait for the
  9. * resource up to the amount of time (in 'ticks') specified by this argument. If you specify
  10. * 0, however, your task will wait forever at the specified semaphore or, until the resource
  11. * becomes available (or the event occurs).
  12. *
  13. * opt determines whether the user wants to block if the semaphore is not available or not:
  14. *
  15. * OS_OPT_PEND_BLOCKING
  16. * OS_OPT_PEND_NON_BLOCKING
  17. *
  18. * p_ts is a pointer to a variable that will receive the timestamp of when the semaphore was posted
  19. * or pend aborted or the semaphore deleted. If you pass a NULL pointer (i.e. (CPU_TS*)0)
  20. * then you will not get the timestamp. In other words, passing a NULL pointer is valid
  21. * and indicates that you don't need the timestamp.
  22. *
  23. * p_err is a pointer to a variable that will contain an error code returned by this function.
  24. *
  25. * OS_ERR_NONE The call was successful and your task owns the resource
  26. * or, the event you are waiting for occurred.
  27. * OS_ERR_OBJ_DEL If 'p_sem' was deleted
  28. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  29. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing at a semaphore
  30. * OS_ERR_OPT_INVALID If you specified an invalid value for 'opt'
  31. * OS_ERR_PEND_ABORT If the pend was aborted by another task
  32. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  33. * would lead to a suspension.
  34. * OS_ERR_PEND_WOULD_BLOCK If you specified non-blocking but the semaphore was not
  35. * available.
  36. * OS_ERR_SCHED_LOCKED If you called this function when the scheduler is locked
  37. * OS_ERR_STATUS_INVALID Pend status is invalid
  38. * OS_ERR_TIMEOUT The semaphore was not received within the specified
  39. * timeout.
  40. *
  41. *
  42. * Returns : The current value of the semaphore counter or 0 if not available.
  43. ************************************************************************************************************************
  44. */
  45.  
  46. OS_SEM_CTR OSSemPend (OS_SEM *p_sem,
  47. OS_TICK timeout,
  48. OS_OPT opt,
  49. CPU_TS *p_ts,
  50. OS_ERR *p_err)
  51. {
  52. OS_SEM_CTR ctr;
  53. OS_PEND_DATA pend_data;
  54. CPU_SR_ALLOC();
  55.  
  56. #ifdef OS_SAFETY_CRITICAL
  57. if (p_err == (OS_ERR *)0) {
  58. OS_SAFETY_CRITICAL_EXCEPTION();
  59. return ((OS_SEM_CTR)0);
  60. }
  61. #endif
  62.  
  63. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  64. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Not allowed to call from an ISR */
  65. *p_err = OS_ERR_PEND_ISR;
  66. return ((OS_SEM_CTR)0);
  67. }
  68. #endif
  69.  
  70. #if OS_CFG_ARG_CHK_EN > 0u
  71. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  72. *p_err = OS_ERR_OBJ_PTR_NULL;
  73. return ((OS_SEM_CTR)0);
  74. }
  75. switch (opt) { /* Validate 'opt' */
  76. case OS_OPT_PEND_BLOCKING:
  77. case OS_OPT_PEND_NON_BLOCKING:
  78. break;
  79.  
  80. default:
  81. *p_err = OS_ERR_OPT_INVALID;
  82. return ((OS_SEM_CTR)0);
  83. }
  84. #endif
  85.  
  86. #if OS_CFG_OBJ_TYPE_CHK_EN > 0u
  87. if (p_sem->Type != OS_OBJ_TYPE_SEM) { /* Make sure semaphore was created */
  88. *p_err = OS_ERR_OBJ_TYPE;
  89. return ((OS_SEM_CTR)0);
  90. }
  91. #endif
  92.  
  93. if (p_ts != (CPU_TS *)0) {
  94. *p_ts = (CPU_TS)0; /* Initialize the returned timestamp */
  95. }
  96. CPU_CRITICAL_ENTER();
  97. //资源有效直接返回可用的数量
  98. if (p_sem->Ctr > (OS_SEM_CTR)0) { /* Resource available? */
  99. p_sem->Ctr--; /* Yes, caller may proceed */
  100. if (p_ts != (CPU_TS *)0) {
  101. *p_ts = p_sem->TS; /* get timestamp of last post */
  102. }
  103. ctr = p_sem->Ctr;
  104. CPU_CRITICAL_EXIT();
  105. *p_err = OS_ERR_NONE;
  106. return (ctr);
  107. }
  108. //是否阻塞任务
  109. if ((opt & OS_OPT_PEND_NON_BLOCKING) != (OS_OPT)0) { /* Caller wants to block if not available? */
  110. ctr = p_sem->Ctr; /* No */
  111. CPU_CRITICAL_EXIT();
  112. *p_err = OS_ERR_PEND_WOULD_BLOCK;
  113. return (ctr);
  114. } else { /* Yes */
  115. if (OSSchedLockNestingCtr > (OS_NESTING_CTR)0) { /* Can't pend when the scheduler is locked */
  116. CPU_CRITICAL_EXIT();
  117. *p_err = OS_ERR_SCHED_LOCKED;
  118. return ((OS_SEM_CTR)0);
  119. }
  120. }
  121. /* Lock the scheduler/re-enable interrupts */
  122. OS_CRITICAL_ENTER_CPU_EXIT();
  123. //阻塞任务进行等待
  124. OS_Pend(&pend_data, /* Block task pending on Semaphore */
  125. (OS_PEND_OBJ *)((void *)p_sem),
  126. OS_TASK_PEND_ON_SEM,
  127. timeout);
  128.  
  129. OS_CRITICAL_EXIT_NO_SCHED();
  130. //挂起当前任务执行其他任务
  131. OSSched(); /* Find the next highest priority task ready to run */
  132. //当前任务重新就绪检查返回等待结果
  133. CPU_CRITICAL_ENTER();
  134. switch (OSTCBCurPtr->PendStatus) {
  135. case OS_STATUS_PEND_OK: /* We got the semaphore */
  136. if (p_ts != (CPU_TS *)0) {
  137. *p_ts = OSTCBCurPtr->TS;
  138. }
  139. *p_err = OS_ERR_NONE;
  140. break;
  141.  
  142. case OS_STATUS_PEND_ABORT: /* Indicate that we aborted */
  143. if (p_ts != (CPU_TS *)0) {
  144. *p_ts = OSTCBCurPtr->TS;
  145. }
  146. *p_err = OS_ERR_PEND_ABORT;
  147. break;
  148.  
  149. case OS_STATUS_PEND_TIMEOUT: /* Indicate that we didn't get semaphore within timeout */
  150. if (p_ts != (CPU_TS *)0) {
  151. *p_ts = (CPU_TS )0;
  152. }
  153. *p_err = OS_ERR_TIMEOUT;
  154. break;
  155.  
  156. case OS_STATUS_PEND_DEL: /* Indicate that object pended on has been deleted */
  157. if (p_ts != (CPU_TS *)0) {
  158. *p_ts = OSTCBCurPtr->TS;
  159. }
  160. *p_err = OS_ERR_OBJ_DEL;
  161. break;
  162.  
  163. default:
  164. *p_err = OS_ERR_STATUS_INVALID;
  165. CPU_CRITICAL_EXIT();
  166. return ((OS_SEM_CTR)0);
  167. }
  168. ctr = p_sem->Ctr;
  169. CPU_CRITICAL_EXIT();
  170. return (ctr);
  171. }

OSSemPend ()

  1. ************************************************************************************************************************
  2. * BLOCK A TASK PENDING ON EVENT
  3. *
  4. * Description: This function is called to place a task in the blocked state waiting for an event to occur. This function
  5. * exist because it is common to a number of OSxxxPend() services.
  6. *
  7. * Arguments : p_pend_data is a pointer to an object used to link the task being blocked to the list of task(s)
  8. * ----------- pending on the desired object.
  9.  
  10. * p_obj is a pointer to the object to pend on. If there are no object used to pend on then
  11. * ----- the caller must pass a NULL pointer.
  12. *
  13. * pending_on Specifies what the task will be pending on:
  14. *
  15. * OS_TASK_PEND_ON_FLAG
  16. * OS_TASK_PEND_ON_TASK_Q <- No object (pending for a message sent to the task)
  17. * OS_TASK_PEND_ON_MUTEX
  18. * OS_TASK_PEND_ON_Q
  19. * OS_TASK_PEND_ON_SEM
  20. * OS_TASK_PEND_ON_TASK_SEM <- No object (pending on a signal sent to the task)
  21. *
  22. * timeout Is the amount of time the task will wait for the event to occur.
  23. *
  24. * Returns : none
  25. *
  26. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  27. ************************************************************************************************************************
  28. */
  29.  
  30. void OS_Pend (OS_PEND_DATA *p_pend_data,
  31. OS_PEND_OBJ *p_obj,
  32. OS_STATE pending_on,
  33. OS_TICK timeout)
  34. {
  35. OS_PEND_LIST *p_pend_list;
  36.  
  37. OSTCBCurPtr->PendOn = pending_on; /* Resource not available, wait until it is */
  38. OSTCBCurPtr->PendStatus = OS_STATUS_PEND_OK;
  39. //×èÈûÈÎÎñ
  40. OS_TaskBlock(OSTCBCurPtr, /* Block the task and add it to the tick list if needed */
  41. timeout);
  42.  
  43. if (p_obj != (OS_PEND_OBJ *)0) { /* Add the current task to the pend list ... */
  44. //È¡³öÐźÅÁ¿Ïµĵȴýlist
  45. p_pend_list = &p_obj->PendList; /* ... if there is an object to pend on */
  46. //ÉèÖÃdataµÄµÈ´ý¶ÔÏó
  47. p_pend_data->PendObjPtr = p_obj; /* Save the pointer to the object pending on */
  48. //p_pend_data³õʼ»¯
  49. OS_PendDataInit((OS_TCB *)OSTCBCurPtr, /* Initialize the remaining field */
  50. (OS_PEND_DATA *)p_pend_data,
  51. (OS_OBJ_QTY )1);
  52. //°´ÈÎÎñÓÅÏȼ¶ÅÅÐò½«µÈ´ýÊý¾Ý²åÈëµÈ´ý¶ÓÁÐ
  53. OS_PendListInsertPrio(p_pend_list, /* Insert in the pend list in priority order */
  54. p_pend_data);
  55. } else {
  56. OSTCBCurPtr->PendDataTblEntries = (OS_OBJ_QTY )0; /* If no object being pended on the clear these fields */
  57. OSTCBCurPtr->PendDataTblPtr = (OS_PEND_DATA *)0; /* ... in the TCB */
  58. }
  59. #if OS_CFG_DBG_EN > 0u
  60. OS_PendDbgNameAdd(p_obj,
  61. OSTCBCurPtr);
  62. #endif
  63. }

OS_Pend ()

  1. ************************************************************************************************************************
  2. * BLOCK A TASK
  3. *
  4. * Description: This function is called to remove a task from the ready list and also insert it in the timer tick list if
  5. * the specified timeout is non-zero.
  6. *
  7. * Arguments : p_tcb is a pointer to the OS_TCB of the task block
  8. * -----
  9. *
  10. * timeout is the desired timeout
  11. *
  12. * Returns : none
  13. *
  14. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  15. ************************************************************************************************************************
  16. */
  17.  
  18. void OS_TaskBlock (OS_TCB *p_tcb,
  19. OS_TICK timeout)
  20. {
  21. OS_ERR err;
  22.  
  23. //检查超时时间为0?
  24. if (timeout > (OS_TICK)0) { /* Add task to tick list if timeout non zero */
  25. //按超时时间设置延时阻塞,具体见时间管理
  26. OS_TickListInsert(p_tcb,
  27. timeout,
  28. OS_OPT_TIME_TIMEOUT,
  29. &err);
  30. //检查错误码
  31. if (err == OS_ERR_NONE) {
  32. p_tcb->TaskState = OS_TASK_STATE_PEND_TIMEOUT;
  33. } else {
  34. p_tcb->TaskState = OS_TASK_STATE_PEND;
  35. }
  36. } else {
  37. //是 永久等待
  38. p_tcb->TaskState = OS_TASK_STATE_PEND;
  39. }
  40. OS_RdyListRemove(p_tcb);
  41. }

OS_TaskBlock()

  1. ************************************************************************************************************************
  2. * INITIALIZE A WAIT LIST TABLE
  3. *
  4. * Description: This function is called to initialize the fields of a table of OS_PEND_DATA entries. It's assumed that
  5. * the .PendObjPtr field of each entry in the table is set by the caller and thus will NOT be touched by
  6. * this function.
  7. *
  8. * Arguments : p_tcb is a pointer to the TCB of the task that we want to pend abort.
  9. * -----
  10. *
  11. * p_pend_data_tbl is a pointer to a table (see below) of OS_PEND_DATA elements to initialize.
  12. * ---------------
  13. *
  14. * .PendObjPtr .RdyObjPtr .RdyMsgPtr .RdyMsgSize .RdyTS .TCBPtr .NextPtr .PrevPtr
  15. * +-----------+----------+----------+-----------+------+-------+--------+--------+ ^
  16. * p_pend_data_tbl-> | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  17. * +-----------+----------+----------+-----------+------+-------+--------+--------+ |
  18. * | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  19. * +-----------+----------+----------+-----------+------+-------+--------+--------+ |
  20. * | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  21. * +-----------+----------+----------+-----------+------+-------+--------+--------+ size
  22. * | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  23. * +-----------+----------+----------+-----------+------+-------+--------+--------+ |
  24. * | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  25. * +-----------+----------+----------+-----------+------+-------+--------+--------+ |
  26. * | ? | 0 | 0 | 0 | 0 | p_tcb | 0 | 0 | |
  27. * +-----------+----------+----------+-----------+------+-------+--------+--------+ V
  28. *
  29. * tbl_size is the size of the table in number of entries
  30. *
  31. * Returns : none
  32. *
  33. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application must not call it.
  34. *
  35. * 2) It's possible for the table to be of size 1 when multi-pend is not used
  36. *
  37. * 3) Note that the .PendObjPtr is NOT touched because it's assumed to be set by the caller.
  38. ************************************************************************************************************************
  39. */
  40.  
  41. void OS_PendDataInit (OS_TCB *p_tcb,
  42. OS_PEND_DATA *p_pend_data_tbl,
  43. OS_OBJ_QTY tbl_size)
  44. {
  45. OS_OBJ_QTY i;
  46.  
  47. //将任务控制块对应的指针参数初始化
  48. p_tcb->PendDataTblEntries = tbl_size; /* Link the TCB to the beginning of the table */
  49. p_tcb->PendDataTblPtr = p_pend_data_tbl;
  50. //这里一般调用都是执行一次
  51. for (i = 0u; i < tbl_size; i++) {
  52. p_pend_data_tbl->NextPtr = (OS_PEND_DATA *)0; /* Initialize all the fields */
  53. p_pend_data_tbl->PrevPtr = (OS_PEND_DATA *)0;
  54. p_pend_data_tbl->RdyObjPtr = (OS_PEND_OBJ *)0;
  55. p_pend_data_tbl->RdyMsgPtr = (void *)0;
  56. p_pend_data_tbl->RdyMsgSize = (OS_MSG_SIZE )0;
  57. p_pend_data_tbl->RdyTS = (CPU_TS )0;
  58. p_pend_data_tbl->TCBPtr = p_tcb; /* Every entry points back to the TCB of the task */
  59. p_pend_data_tbl++;
  60. }
  61. }

OS_PendDataInit()

  1. ************************************************************************************************************************
  2. * INSERT PEND DATA BASED ON IT'S PRIORITY IN A LIST
  3. *
  4. * Description: This function is called to place an OS_PEND_DATA entry in a linked list based on its priority. The
  5. * highest priority being placed at the head of the list. It's assumed that the OS_PEND_DATA entry to
  6. * insert points to the TCB of the task being inserted. The TCB is also assumed to contain the priority
  7. * of the task in its .Prio field.
  8. *
  9. * CASE 0: Insert in an empty list.
  10. *
  11. * OS_PEND_LIST
  12. * +---------------+
  13. * | TailPtr |-> 0
  14. * +---------------+
  15. * | HeadPtr |-> 0
  16. * +---------------+
  17. * | NbrEntries=0 |
  18. * +---------------+
  19. *
  20. *
  21. *
  22. * CASE 1: Insert BEFORE or AFTER an OS_TCB
  23. *
  24. * OS_PEND_LIST
  25. * +--------------+ OS_PEND_DATA
  26. * | TailPtr |--+---> +------------+
  27. * +--------------+ | | NextPtr |->0
  28. * | HeadPtr |--/ +------------+
  29. * +--------------+ 0<-| PrevPtr |
  30. * | NbrEntries=1 | +------------+
  31. * +--------------+ | |
  32. * +------------+
  33. * | |
  34. * +------------+
  35. *
  36. *
  37. * OS_PEND_LIST
  38. * +--------------+
  39. * | TailPtr |-----------------------------------------------+
  40. * +--------------+ OS_PEND_DATA OS_PEND_DATA | OS_PEND_DATA
  41. * | HeadPtr |------> +------------+ +------------+ +-> +------------+
  42. * +--------------+ | NextPtr |------>| NextPtr | ...... | NextPtr |->0
  43. * | NbrEntries=N | +------------+ +------------+ +------------+
  44. * +--------------+ 0<-| PrevPtr |<------| PrevPtr | ...... | PrevPtr |
  45. * +------------+ +------------+ +------------+
  46. * | | | | | |
  47. * +------------+ +------------+ +------------+
  48. * | | | | | |
  49. * +------------+ +------------+ +------------+
  50. *
  51. *
  52. * Arguments : p_pend_list is a pointer to the OS_PEND_LIST where the OS_PEND_DATA entry will be inserted
  53. * -----------
  54. *
  55. * p_pend_data is the OS_PEND_DATA to insert in the list
  56. * -----------
  57. *
  58. * Returns : none
  59. *
  60. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  61. *
  62. * 2) 'p_pend_data->TCBPtr->Prio' contains the priority of the TCB associated with the entry to insert.
  63. * We can compare this priority with the priority of other entries in the list.
  64. ************************************************************************************************************************
  65. */
  66.  
  67. void OS_PendListInsertPrio (OS_PEND_LIST *p_pend_list,
  68. OS_PEND_DATA *p_pend_data)
  69. {
  70. OS_PRIO prio;
  71. OS_TCB *p_tcb;
  72. OS_TCB *p_tcb_next;
  73. OS_PEND_DATA *p_pend_data_prev;
  74. OS_PEND_DATA *p_pend_data_next;
  75.  
  76. p_tcb = p_pend_data->TCBPtr; /* Obtain the priority of the task to insert */
  77. prio = p_tcb->Prio;
  78. if (p_pend_list->NbrEntries == (OS_OBJ_QTY)0) { /* CASE 0: Insert when there are no entries */
  79. p_pend_list->NbrEntries = (OS_OBJ_QTY)1; /* This is the first entry */
  80. p_pend_data->NextPtr = (OS_PEND_DATA *)0; /* No other OS_PEND_DATAs in the list */
  81. p_pend_data->PrevPtr = (OS_PEND_DATA *)0;
  82. p_pend_list->HeadPtr = p_pend_data; /* */
  83. p_pend_list->TailPtr = p_pend_data;
  84. } else {
  85. p_pend_list->NbrEntries++; /* CASE 1: One more OS_PEND_DATA in the list */
  86. p_pend_data_next = p_pend_list->HeadPtr;
  87. while (p_pend_data_next != (OS_PEND_DATA *)0) { /* Find the position where to insert */
  88. p_tcb_next = p_pend_data_next->TCBPtr;
  89. if (prio < p_tcb_next->Prio) {
  90. break; /* Found! ... insert BEFORE current */
  91. } else {
  92. p_pend_data_next = p_pend_data_next->NextPtr; /* Not Found, follow the list */
  93. }
  94. }
  95. //开始插入
  96.  
  97. //末尾
  98. if (p_pend_data_next == (OS_PEND_DATA *)0) { /* TCB to insert is lower in prio */
  99. p_pend_data->NextPtr = (OS_PEND_DATA *)0; /* ... insert at the tail. */
  100. p_pend_data_prev = p_pend_list->TailPtr;
  101. p_pend_data->PrevPtr = p_pend_data_prev;
  102. p_pend_data_prev->NextPtr = p_pend_data;
  103. p_pend_list->TailPtr = p_pend_data;
  104. } else {
  105. //开头
  106. if (p_pend_data_next->PrevPtr == (OS_PEND_DATA *)0) { /* Is new TCB highest priority? */
  107. p_pend_data_next->PrevPtr = p_pend_data; /* Yes, insert as new Head of list */
  108. p_pend_data->PrevPtr = (OS_PEND_DATA *)0;
  109. p_pend_data->NextPtr = p_pend_data_next;
  110. p_pend_list->HeadPtr = p_pend_data;
  111. } else {
  112. //中间
  113. p_pend_data_prev = p_pend_data_next->PrevPtr;/* No, insert in between two entries */
  114. p_pend_data->PrevPtr = p_pend_data_prev;
  115. p_pend_data->NextPtr = p_pend_data_next;
  116. p_pend_data_prev->NextPtr = p_pend_data;
  117. p_pend_data_next->PrevPtr = p_pend_data;
  118. }
  119. }
  120. }
  121. }

OS_PendListInsertPrio()

3.发布信号量,分发布给信号量等待队列上所有等待任务还是发布全部等待任务中的最高优先级的任务,然后调用OS_Post()进行单个任务的发布而对于每个任务还要判断其是否等待多个内核对象,如果是则要仅仅发布任务的一个内核对象不能将任务脱离等待列表加入就绪任务列表,如果任务仅仅等待这一个对象就可将任务脱离等待列表并加入就绪列表下次任务调度时就会执行。

先关函数的中文注释:

  1. ************************************************************************************************************************
  2. * POST TO A SEMAPHORE
  3. *
  4. * Description: This function signals a semaphore
  5. *
  6. * Arguments : p_sem is a pointer to the semaphore
  7. *
  8. * opt determines the type of POST performed:
  9. *
  10. * OS_OPT_POST_1 POST and ready only the highest priority task waiting on semaphore
  11. * (if tasks are waiting).
  12. * OS_OPT_POST_ALL POST to ALL tasks that are waiting on the semaphore
  13. *
  14. * OS_OPT_POST_NO_SCHED Do not call the scheduler
  15. *
  16. * Note(s): 1) OS_OPT_POST_NO_SCHED can be added with one of the other options.
  17. *
  18. * p_err is a pointer to a variable that will contain an error code returned by this function.
  19. *
  20. * OS_ERR_NONE The call was successful and the semaphore was signaled.
  21. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  22. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing at a semaphore
  23. * OS_ERR_SEM_OVF If the post would cause the semaphore count to overflow.
  24. *
  25. * Returns : The current value of the semaphore counter or 0 upon error.
  26. ************************************************************************************************************************
  27. */
  28.  
  29. OS_SEM_CTR OSSemPost (OS_SEM *p_sem,
  30. OS_OPT opt,
  31. OS_ERR *p_err)
  32. {
  33. OS_SEM_CTR ctr;
  34. CPU_TS ts;
  35.  
  36. #ifdef OS_SAFETY_CRITICAL
  37. if (p_err == (OS_ERR *)0) {
  38. OS_SAFETY_CRITICAL_EXCEPTION();
  39. return ((OS_SEM_CTR)0);
  40. }
  41. #endif
  42.  
  43. #if OS_CFG_ARG_CHK_EN > 0u
  44. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  45. *p_err = OS_ERR_OBJ_PTR_NULL;
  46. return ((OS_SEM_CTR)0);
  47. }
  48. switch (opt) { /* Validate 'opt' */
  49. case OS_OPT_POST_1:
  50. case OS_OPT_POST_ALL:
  51. case OS_OPT_POST_1 | OS_OPT_POST_NO_SCHED:
  52. case OS_OPT_POST_ALL | OS_OPT_POST_NO_SCHED:
  53. break;
  54.  
  55. default:
  56. *p_err = OS_ERR_OPT_INVALID;
  57. return ((OS_SEM_CTR)0u);
  58. }
  59. #endif
  60.  
  61. #if OS_CFG_OBJ_TYPE_CHK_EN > 0u
  62. if (p_sem->Type != OS_OBJ_TYPE_SEM) { /* Make sure semaphore was created */
  63. *p_err = OS_ERR_OBJ_TYPE;
  64. return ((OS_SEM_CTR)0);
  65. }
  66. #endif
  67.  
  68. ts = OS_TS_GET(); /* Get timestamp */
  69. //延迟发布
  70. #if OS_CFG_ISR_POST_DEFERRED_EN > 0u
  71. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* See if called from an ISR */
  72. OS_IntQPost((OS_OBJ_TYPE)OS_OBJ_TYPE_SEM, /* Post to ISR queue */
  73. (void *)p_sem,
  74. (void *)0,
  75. (OS_MSG_SIZE)0,
  76. (OS_FLAGS )0,
  77. (OS_OPT )opt,
  78. (CPU_TS )ts,
  79. (OS_ERR *)p_err);
  80. return ((OS_SEM_CTR)0);
  81. }
  82. #endif
  83. //正常发布
  84. ctr = OS_SemPost(p_sem, /* Post to semaphore */
  85. opt,
  86. ts,
  87. p_err);
  88.  
  89. return (ctr);
  90. }

OSSemPost()

  1. ************************************************************************************************************************
  2. * POST TO A SEMAPHORE
  3. *
  4. * Description: This function signals a semaphore
  5. *
  6. * Arguments : p_sem is a pointer to the semaphore
  7. *
  8. * opt determines the type of POST performed:
  9. *
  10. * OS_OPT_POST_1 POST to a single waiting task
  11. * OS_OPT_POST_ALL POST to ALL tasks that are waiting on the semaphore
  12. *
  13. * OS_OPT_POST_NO_SCHED Do not call the scheduler
  14. *
  15. * Note(s): 1) OS_OPT_POST_NO_SCHED can be added with one of the other options.
  16. *
  17. * ts is a timestamp indicating when the post occurred.
  18. *
  19. * p_err is a pointer to a variable that will contain an error code returned by this function.
  20. *
  21. * OS_ERR_NONE The call was successful and the semaphore was signaled.
  22. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  23. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing at a semaphore
  24. * OS_ERR_SEM_OVF If the post would cause the semaphore count to overflow.
  25. *
  26. * Returns : The current value of the semaphore counter or 0 upon error.
  27. *
  28. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  29. ************************************************************************************************************************
  30. */
  31.  
  32. OS_SEM_CTR OS_SemPost (OS_SEM *p_sem,
  33. OS_OPT opt,
  34. CPU_TS ts,
  35. OS_ERR *p_err)
  36. {
  37. OS_OBJ_QTY cnt;
  38. OS_SEM_CTR ctr;
  39. OS_PEND_LIST *p_pend_list;
  40. OS_PEND_DATA *p_pend_data;
  41. OS_PEND_DATA *p_pend_data_next;
  42. OS_TCB *p_tcb;
  43. CPU_SR_ALLOC();
  44.  
  45. CPU_CRITICAL_ENTER();
  46. //取出信号量内的等待队列
  47. p_pend_list = &p_sem->PendList;
  48. //没有任何任务等待此信号量
  49. if (p_pend_list->NbrEntries == (OS_OBJ_QTY)0) { /* Any task waiting on semaphore? */
  50. //变量范围检查
  51. switch (sizeof(OS_SEM_CTR)) {
  52. case 1u:
  53. if (p_sem->Ctr == DEF_INT_08U_MAX_VAL) {
  54. CPU_CRITICAL_EXIT();
  55. *p_err = OS_ERR_SEM_OVF;
  56. return ((OS_SEM_CTR)0);
  57. }
  58. break;
  59.  
  60. case 2u:
  61. if (p_sem->Ctr == DEF_INT_16U_MAX_VAL) {
  62. CPU_CRITICAL_EXIT();
  63. *p_err = OS_ERR_SEM_OVF;
  64. return ((OS_SEM_CTR)0);
  65. }
  66. break;
  67.  
  68. case 4u:
  69. if (p_sem->Ctr == DEF_INT_32U_MAX_VAL) {
  70. CPU_CRITICAL_EXIT();
  71. *p_err = OS_ERR_SEM_OVF;
  72. return ((OS_SEM_CTR)0);
  73. }
  74. break;
  75.  
  76. default:
  77. break;
  78. }
  79. //信号量可用加1
  80. p_sem->Ctr++; /* No */
  81. ctr = p_sem->Ctr;
  82. p_sem->TS = ts; /* Save timestamp in semaphore control block */
  83. CPU_CRITICAL_EXIT();
  84. *p_err = OS_ERR_NONE;
  85. return (ctr);
  86. }
  87.  
  88. OS_CRITICAL_ENTER_CPU_EXIT();
  89. //发布选项给等待队列上的所有任务还是一个?
  90. if ((opt & OS_OPT_POST_ALL) != (OS_OPT)0) { /* Post message to all tasks waiting? */
  91. //全部
  92. cnt = p_pend_list->NbrEntries; /* Yes */
  93. } else {
  94. //优先级最高的一个
  95. cnt = (OS_OBJ_QTY)1; /* No */
  96. }
  97. //取出list头
  98. p_pend_data = p_pend_list->HeadPtr;
  99. //循环从头开始操作任务
  100. while (cnt > 0u) {
  101. p_tcb = p_pend_data->TCBPtr;
  102. p_pend_data_next = p_pend_data->NextPtr;
  103. //发布给等待任务
  104. OS_Post((OS_PEND_OBJ *)((void *)p_sem),
  105. p_tcb,
  106. (void *)0,
  107. (OS_MSG_SIZE)0,
  108. ts);
  109. p_pend_data = p_pend_data_next;
  110. cnt--;
  111. }
  112. ctr = p_sem->Ctr;
  113. OS_CRITICAL_EXIT_NO_SCHED();
  114. //发布后启动调度?
  115. if ((opt & OS_OPT_POST_NO_SCHED) == (OS_OPT)0) {
  116. //是
  117. OSSched(); /* Run the scheduler */
  118. }
  119. *p_err = OS_ERR_NONE;
  120. return (ctr);
  121. }
  122.  
  123. #endif

OS_SemPost ()

  1. ************************************************************************************************************************
  2. * POST TO A TASK
  3. *
  4. * Description: This function is called to post to a task. This function exist because it is common to a number of
  5. * OSxxxPost() services.
  6. *
  7. * Arguments : p_obj Is a pointer to the object being posted to or NULL pointer if there is no object
  8. * -----
  9. *
  10. * p_tcb Is a pointer to the OS_TCB that will receive the 'post'
  11. * -----
  12. *
  13. * p_void If we are posting a message to a task, this is the message that the task will receive
  14. *
  15. * msg_size If we are posting a message to a task, this is the size of the message
  16. *
  17. * ts The timestamp as to when the post occurred
  18. *
  19. * Returns : none
  20. *
  21. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  22. ************************************************************************************************************************
  23. */
  24.  
  25. void OS_Post (OS_PEND_OBJ *p_obj,
  26. OS_TCB *p_tcb,
  27. void *p_void,
  28. OS_MSG_SIZE msg_size,
  29. CPU_TS ts)
  30. {
  31. //检查任务状态
  32. switch (p_tcb->TaskState) {
  33. //非该有状态
  34. case OS_TASK_STATE_RDY: /* Cannot Pend Abort a task that is ready */
  35. case OS_TASK_STATE_DLY: /* Cannot Pend Abort a task that is delayed */
  36. case OS_TASK_STATE_SUSPENDED: /* Cannot Post a suspended task */
  37. case OS_TASK_STATE_DLY_SUSPENDED: /* Cannot Post a suspended task that was also dly'd */
  38. break;
  39. //任务同时仅仅是等待信号量状态的
  40. case OS_TASK_STATE_PEND:
  41. case OS_TASK_STATE_PEND_TIMEOUT:
  42. //任务等待多个内核对象?
  43. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  44. //是 发布一个内核对象给任务控制块
  45. OS_Post1(p_obj, /* Indicate which object was posted to */
  46. p_tcb,
  47. p_void,
  48. msg_size,
  49. ts);
  50. } else {
  51. //否 发布一个内核对象给任务控制块
  52. #if (OS_MSG_EN > 0u)
  53. p_tcb->MsgPtr = p_void; /* Deposit message in OS_TCB of task waiting */
  54. p_tcb->MsgSize = msg_size; /* ... assuming posting a message */
  55. #endif
  56. p_tcb->TS = ts;
  57. }
  58. if (p_obj != (OS_PEND_OBJ *)0) {
  59. //将任务从任务从等待list移除(这里是一个双向链表额删除操作)
  60. OS_PendListRemove(p_tcb); /* Remove task from wait list(s) */
  61. #if OS_CFG_DBG_EN > 0u
  62. OS_PendDbgNameRemove(p_obj,
  63. p_tcb);
  64. #endif
  65. }
  66. //将任务加入就绪list下次任务调度就会被执行
  67. OS_TaskRdy(p_tcb); /* Make task ready to run */
  68. //修改任务状态标记值
  69. p_tcb->TaskState = OS_TASK_STATE_RDY;
  70. p_tcb->PendStatus = OS_STATUS_PEND_OK; /* Clear pend status */
  71. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING; /* Indicate no longer pending */
  72. break;
  73. //任务同时是等待挂起状态的设置为挂起状态
  74. case OS_TASK_STATE_PEND_SUSPENDED:
  75. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  76. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  77. //这里和等待一个内核对象不同的是没有将任务加入到就绪list
  78. OS_Post1(p_obj, /* Indicate which object was posted to */
  79. p_tcb,
  80. p_void,
  81. msg_size,
  82. ts);
  83. } else {
  84. #if (OS_MSG_EN > 0u)
  85. p_tcb->MsgPtr = p_void; /* Deposit message in OS_TCB of task waiting */
  86. p_tcb->MsgSize = msg_size; /* ... assuming posting a message */
  87. #endif
  88. p_tcb->TS = ts;
  89. }
  90. //将任务从任务从等待list移除(这里是一个双向链表额删除操作)
  91. OS_TickListRemove(p_tcb); /* Cancel any timeout */
  92. if (p_obj != (OS_PEND_OBJ *)0) {
  93. OS_PendListRemove(p_tcb); /* Remove task from wait list(s) */
  94. #if OS_CFG_DBG_EN > 0u
  95. OS_PendDbgNameRemove(p_obj,
  96. p_tcb);
  97. #endif
  98. }
  99. //注意这里和上面的不同
  100. p_tcb->TaskState = OS_TASK_STATE_SUSPENDED;
  101. p_tcb->PendStatus = OS_STATUS_PEND_OK; /* Clear pend status */
  102. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING; /* Indicate no longer pending */
  103. break;
  104.  
  105. default:
  106. break;
  107. }
  108. }

OS_Post ()

  1. ************************************************************************************************************************
  2. * POST TO A TASK PENDING ON MULTIPLE OBJECTS
  3. *
  4. * Description: This function is called when a task is pending on multiple objects and the object has been posted to.
  5. * This function needs to indicate to the caller which object was posted to by placing the address of the
  6. * object in the OS_PEND_DATA table corresponding to the posted object.
  7. *
  8. * For example, if the task pends on six (6) objects, the address of those 6 objects are placed in the
  9. * .PendObjPtr field of the OS_PEND_DATA table as shown below. Note that the .PendDataTblEntries would be
  10. * set to six (6) in this case. As shown, when the pend call returns because a task or an ISR posted to
  11. * 'Obj C' then, only the one entry contains the filled in data and the other entries contains NULL pointers
  12. * and zero data.
  13. *
  14. * You should note that the NULL pointers are zero data values are actually filled in by the pend call.
  15. *
  16. *
  17. * .PendObjPtr .RdyObjPtr .RdyMsgPtr .RdyMsgSize .RdyTS
  18. * +--------------+--------------+--------------+--------------+--------------+
  19. * p_tcb->PendDataTblPtr -> | Obj A | 0 | 0 | 0 | 0 |
  20. * +--------------+--------------+--------------+--------------+--------------+
  21. * | Obj B | 0 | 0 | 0 | 0 |
  22. * +--------------+--------------+--------------+--------------+--------------+
  23. * | Obj C | Obj C | Msg Ptr | Msg Size | TS |
  24. * +--------------+--------------+--------------+--------------+--------------+
  25. * | Obj D | 0 | 0 | 0 | 0 |
  26. * +--------------+--------------+--------------+--------------+--------------+
  27. * | Obj E | 0 | 0 | 0 | 0 |
  28. * +--------------+--------------+--------------+--------------+--------------+
  29. * | Obj F | 0 | 0 | 0 | 0 |
  30. * +--------------+--------------+--------------+--------------+--------------+
  31. *
  32. *
  33. * Arguments : p_obj is a pointer to the object being posted to
  34. * -----
  35. *
  36. * p_tcb is the OS_TCB of the task receiving the signal or the message
  37. * -----
  38. *
  39. * p_void is the actual message (assuming posting to a message queue). A NULL pointer otherwise.
  40. *
  41. * msg_size is the size of the message sent (if posted to a message queue)
  42. *
  43. * ts is the time stamp of when the post occurred
  44. *
  45. * Returns : none
  46. *
  47. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  48. ************************************************************************************************************************
  49. */
  50.  
  51. void OS_Post1 (OS_PEND_OBJ *p_obj,
  52. OS_TCB *p_tcb,
  53. void *p_void,
  54. OS_MSG_SIZE msg_size,
  55. CPU_TS ts)
  56. {
  57. OS_OBJ_QTY n_pend_list; /* Number of pend lists */
  58. OS_PEND_DATA *p_pend_data;
  59.  
  60. //取出任务等待的内核对象数据指向OSSemPend()时的那个参数
  61. //虽然p_pend_data定义的时候是一个函数内的临时变量但有UCOS的进
  62. //程调度机制就知道这个临时变量的寿命可以保留到等待结束且被巧妙的释放
  63. p_pend_data = p_tcb->PendDataTblPtr; /* Point to the first OS_PEND_DATA to remove */
  64. //任务等待内核对象的个数是?
  65. n_pend_list = p_tcb->PendDataTblEntries; /* Get number of entries in the table */
  66.  
  67. while (n_pend_list > (OS_OBJ_QTY)0) { /* Mark posted object in OS_PEND_DATA table */
  68. //查找和等待当前内核的pend_data,一般任务就只有一个
  69. //因此这个循环一般情况也是执行一次,任务等待多个内核对象时就会循环至少一次
  70. if (p_obj == p_pend_data->PendObjPtr) { /* Did we find the object posted to? */
  71. p_pend_data->RdyObjPtr = p_obj; /* Yes, indicate the object in the .RdyObjPtr */
  72. p_pend_data->RdyMsgPtr = p_void; /* store the message posted */
  73. p_pend_data->RdyMsgSize = msg_size; /* store the size of the message posted */
  74. p_pend_data->RdyTS = ts; /* save the timestamp of the post */
  75. break;
  76. }
  77. p_pend_data++;
  78. n_pend_list--;
  79. }
  80. }

OS_Post1()

4.以上就是多值信号量基本工作原理,还有一些相关的操作函数有以下几个:

强制解除等待状态

  1. ************************************************************************************************************************
  2. * ABORT WAITING ON A SEMAPHORE
  3. *
  4. * Description: This function aborts & readies any tasks currently waiting on a semaphore. This function should be used
  5. * to fault-abort the wait on the semaphore, rather than to normally signal the semaphore via OSSemPost().
  6. *
  7. * Arguments : p_sem is a pointer to the semaphore
  8. *
  9. * opt determines the type of ABORT performed:
  10. *
  11. * OS_OPT_PEND_ABORT_1 ABORT wait for a single task (HPT) waiting on the semaphore
  12. * OS_OPT_PEND_ABORT_ALL ABORT wait for ALL tasks that are waiting on the semaphore
  13. * OS_OPT_POST_NO_SCHED Do not call the scheduler
  14. *
  15. * p_err is a pointer to a variable that will contain an error code returned by this function.
  16. *
  17. * OS_ERR_NONE At least one task waiting on the semaphore was readied and
  18. * informed of the aborted wait; check return value for the
  19. * number of tasks whose wait on the semaphore was aborted.
  20. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  21. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing at a semaphore
  22. * OS_ERR_OPT_INVALID If you specified an invalid option
  23. * OS_ERR_PEND_ABORT_ISR If you called this function from an ISR
  24. * OS_ERR_PEND_ABORT_NONE No task were pending
  25. *
  26. * Returns : == 0 if no tasks were waiting on the semaphore, or upon error.
  27. * > 0 if one or more tasks waiting on the semaphore are now readied and informed.
  28. ************************************************************************************************************************
  29. */
  30.  
  31. #if OS_CFG_SEM_PEND_ABORT_EN > 0u
  32. OS_OBJ_QTY OSSemPendAbort (OS_SEM *p_sem,
  33. OS_OPT opt,
  34. OS_ERR *p_err)
  35. {
  36. OS_PEND_LIST *p_pend_list;
  37. OS_TCB *p_tcb;
  38. CPU_TS ts;
  39. OS_OBJ_QTY nbr_tasks;
  40. CPU_SR_ALLOC();
  41.  
  42. #ifdef OS_SAFETY_CRITICAL
  43. if (p_err == (OS_ERR *)0) {
  44. OS_SAFETY_CRITICAL_EXCEPTION();
  45. return ((OS_OBJ_QTY)0u);
  46. }
  47. #endif
  48.  
  49. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  50. if (OSIntNestingCtr > (OS_NESTING_CTR)0u) { /* Not allowed to Pend Abort from an ISR */
  51. *p_err = OS_ERR_PEND_ABORT_ISR;
  52. return ((OS_OBJ_QTY)0u);
  53. }
  54. #endif
  55.  
  56. #if OS_CFG_ARG_CHK_EN > 0u
  57. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  58. *p_err = OS_ERR_OBJ_PTR_NULL;
  59. return ((OS_OBJ_QTY)0u);
  60. }
  61. switch (opt) { /* Validate 'opt' */
  62. //选项检查
  63. case OS_OPT_PEND_ABORT_1:
  64. case OS_OPT_PEND_ABORT_ALL:
  65. case OS_OPT_PEND_ABORT_1 | OS_OPT_POST_NO_SCHED:
  66. case OS_OPT_PEND_ABORT_ALL | OS_OPT_POST_NO_SCHED:
  67. break;
  68.  
  69. default:
  70. *p_err = OS_ERR_OPT_INVALID;
  71. return ((OS_OBJ_QTY)0u);
  72. }
  73. #endif
  74.  
  75. #if OS_CFG_OBJ_TYPE_CHK_EN > 0u
  76. if (p_sem->Type != OS_OBJ_TYPE_SEM) { /* Make sure semaphore was created */
  77. *p_err = OS_ERR_OBJ_TYPE;
  78. return ((OS_OBJ_QTY)0u);
  79. }
  80. #endif
  81.  
  82. CPU_CRITICAL_ENTER();
  83. //取出等待队列管理变量
  84. p_pend_list = &p_sem->PendList;
  85. if (p_pend_list->NbrEntries == (OS_OBJ_QTY)0u) { /* Any task waiting on semaphore? */
  86. CPU_CRITICAL_EXIT(); /* No */
  87. *p_err = OS_ERR_PEND_ABORT_NONE;
  88. return ((OS_OBJ_QTY)0u);
  89. }
  90.  
  91. OS_CRITICAL_ENTER_CPU_EXIT();
  92. nbr_tasks = 0u;
  93. ts = OS_TS_GET(); /* Get local time stamp so all tasks get the same time */
  94. while (p_pend_list->NbrEntries > (OS_OBJ_QTY)0u) {
  95. p_tcb = p_pend_list->HeadPtr->TCBPtr;
  96. //解除内核对象上的一个等待任务的等待状态
  97. OS_PendAbort((OS_PEND_OBJ *)((void *)p_sem),
  98. p_tcb,
  99. ts);
  100. nbr_tasks++;
  101. if (opt != OS_OPT_PEND_ABORT_ALL) { /* Pend abort all tasks waiting? */
  102. break; /* No */
  103. }
  104. }
  105. OS_CRITICAL_EXIT_NO_SCHED();
  106.  
  107. if ((opt & OS_OPT_POST_NO_SCHED) == (OS_OPT)0u) {
  108. OSSched(); /* Run the scheduler */
  109. }
  110.  
  111. *p_err = OS_ERR_NONE;
  112. return (nbr_tasks);
  113. }
  114. #endif

OSSemPendAbort

强制解除内核对象上的一个任务

  1. ************************************************************************************************************************
  2. * ABORT PENDING
  3. *
  4. * Description: This function is called by OSxxxPendAbort() functions to abort pending on an event.
  5. *
  6. * Arguments : p_obj Is a pointer to the object to pend abort.
  7. * -----
  8. *
  9. * p_tcb Is a pointer to the OS_TCB of the task that we'll abort the pend for
  10. * -----
  11. *
  12. * ts The is a timestamp as to when the pend abort occurred
  13. *
  14. * Returns : none
  15. *
  16. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  17. ************************************************************************************************************************
  18. */
  19.  
  20. void OS_PendAbort (OS_PEND_OBJ *p_obj,
  21. OS_TCB *p_tcb,
  22. CPU_TS ts)
  23. {
  24. switch (p_tcb->TaskState) {
  25. case OS_TASK_STATE_RDY: /* Cannot Pend Abort a task that is ready */
  26. case OS_TASK_STATE_DLY: /* Cannot Pend Abort a task that is delayed */
  27. case OS_TASK_STATE_SUSPENDED: /* Cannot Pend Abort a suspended task */
  28. case OS_TASK_STATE_DLY_SUSPENDED: /* Cannot Pend Abort a suspended task that was also dly'd */
  29. break;
  30.  
  31. case OS_TASK_STATE_PEND:
  32. case OS_TASK_STATE_PEND_TIMEOUT:
  33. //等待做个内核对象?
  34. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  35. //是 标记任务某一内核对象被强制解除等待状态
  36. OS_PendAbort1(p_obj, /* Indicate which object was pend aborted */
  37. p_tcb,
  38. ts);
  39. }
  40. #if (OS_MSG_EN > 0u)
  41. p_tcb->MsgPtr = (void *)0;
  42. p_tcb->MsgSize = (OS_MSG_SIZE)0u;
  43. #endif
  44. //否
  45. p_tcb->TS = ts;
  46. if (p_obj != (OS_PEND_OBJ *)0) {
  47. //从等待列表移除
  48. OS_PendListRemove(p_tcb); /* Remove task from all pend lists */
  49. }
  50. //加入就素list
  51. OS_TaskRdy(p_tcb);
  52. p_tcb->TaskState = OS_TASK_STATE_RDY; /* Task will be ready */
  53. p_tcb->PendStatus = OS_STATUS_PEND_ABORT; /* Indicate pend was aborted */
  54. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING; /* Indicate no longer pending */
  55. break;
  56.  
  57. case OS_TASK_STATE_PEND_SUSPENDED:
  58. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  59. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  60. OS_PendAbort1(p_obj, /* Indicate which object was pend aborted */
  61. p_tcb,
  62. ts);
  63. }
  64. #if (OS_MSG_EN > 0u)
  65. p_tcb->MsgPtr = (void *)0;
  66. p_tcb->MsgSize = (OS_MSG_SIZE)0u;
  67. #endif
  68. p_tcb->TS = ts;
  69. if (p_obj != (OS_PEND_OBJ *)0) {
  70. OS_PendListRemove(p_tcb); /* Remove task from all pend lists */
  71. }
  72. OS_TickListRemove(p_tcb); /* Cancel the timeout */
  73. p_tcb->TaskState = OS_TASK_STATE_SUSPENDED; /* Pend Aborted task is still suspended */
  74. p_tcb->PendStatus = OS_STATUS_PEND_ABORT; /* Indicate pend was aborted */
  75. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING; /* Indicate no longer pending */
  76. break;
  77.  
  78. default:
  79. break;
  80. }
  81. }

OS_PendAbort ()

标记某一内核对象上的任务被强制解除

  1. ************************************************************************************************************************
  2. * PEND ABORT A TASK PENDING ON MULTIPLE OBJECTS
  3. *
  4. * Description: This function is called when a task is pending on multiple objects and one of the objects has been pend
  5. * aborted. This function needs to indicate to the caller which object was pend aborted by placing the
  6. * address of the object in the OS_PEND_DATA table corresponding to the pend aborted object.
  7. *
  8. * For example, if the task pends on six (6) objects, the address of those 6 objects are placed in the
  9. * .PendObjPtr field of the OS_PEND_DATA table as shown below. Note that the .PendDataTblEntries of the
  10. * OS_TCB would be set to six (6) in this case. As shown, when the pend call returns because a task pend
  11. * aborted 'Obj C' then, only the one entry contains the .RdyObjPtr filled in data and the other entries
  12. * contains NULL pointers and zero data.
  13. *
  14. * You should note that the NULL pointers are zero data values are actually filled in by the pend call.
  15. *
  16. *
  17. * .PendObjPtr .RdyObjPtr .RdyMsgPtr .RdyMsgSize .RdyTS
  18. * +--------------+--------------+--------------+--------------+--------------+
  19. * p_tcb->PendDataTblPtr -> | Obj A | 0 | 0 | 0 | 0 |
  20. * +--------------+--------------+--------------+--------------+--------------+
  21. * | Obj B | 0 | 0 | 0 | 0 |
  22. * +--------------+--------------+--------------+--------------+--------------+
  23. * | Obj C | Obj C | 0 | 0 | TS |
  24. * +--------------+--------------+--------------+--------------+--------------+
  25. * | Obj D | 0 | 0 | 0 | 0 |
  26. * +--------------+--------------+--------------+--------------+--------------+
  27. * | Obj E | 0 | 0 | 0 | 0 |
  28. * +--------------+--------------+--------------+--------------+--------------+
  29. * | Obj F | 0 | 0 | 0 | 0 |
  30. * +--------------+--------------+--------------+--------------+--------------+
  31. *
  32. *
  33. * Arguments : p_obj is a pointer to the object being pend aborted to
  34. * -----
  35. *
  36. * p_tcb is a pointer to the OS_TCB of the task that we'll abort he pend for
  37. * -----
  38. *
  39. * ts is the time stamp of when the pend abort occurred
  40. *
  41. * Returns : none
  42. *
  43. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  44. ************************************************************************************************************************
  45. */
  46.  
  47. void OS_PendAbort1 (OS_PEND_OBJ *p_obj,
  48. OS_TCB *p_tcb,
  49. CPU_TS ts)
  50. {
  51. OS_OBJ_QTY n_pend_list; /* Number of pend lists */
  52. OS_PEND_DATA *p_pend_data;
  53.  
  54. p_pend_data = p_tcb->PendDataTblPtr; /* Point to the first OS_PEND_DATA to remove */
  55. n_pend_list = p_tcb->PendDataTblEntries; /* Get number of entries in the table */
  56.  
  57. while (n_pend_list > (OS_OBJ_QTY)0) { /* Mark posted object in OS_PEND_DATA table */
  58. if (p_obj == p_pend_data->PendObjPtr) { /* Did we find the object pend aborted? */
  59. p_pend_data->RdyObjPtr = p_obj; /* Yes, indicate the object in the .RdyObjPtr */
  60. p_pend_data->RdyTS = ts; /* save the timestamp of the pend abort */
  61. break;
  62. }
  63. p_pend_data++;
  64. n_pend_list--;
  65. }
  66. }

OS_PendAbort1 ()

删除信号量

  1. ************************************************************************************************************************
  2. * DELETE A SEMAPHORE
  3. *
  4. * Description: This function deletes a semaphore.
  5. *
  6. * Arguments : p_sem is a pointer to the semaphore to delete
  7. *
  8. * opt determines delete options as follows:
  9. *
  10. * OS_OPT_DEL_NO_PEND Delete semaphore ONLY if no task pending
  11. * OS_OPT_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
  12. * In this case, all the tasks pending will be readied.
  13. *
  14. * p_err is a pointer to a variable that will contain an error code returned by this function.
  15. *
  16. * OS_ERR_NONE The call was successful and the semaphore was deleted
  17. * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an ISR
  18. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  19. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing at a semaphore
  20. * OS_ERR_OPT_INVALID An invalid option was specified
  21. * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
  22. *
  23. * Returns : == 0 if no tasks were waiting on the semaphore, or upon error.
  24. * > 0 if one or more tasks waiting on the semaphore are now readied and informed.
  25. *
  26. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of the semaphore
  27. * MUST check the return code of OSSemPend().
  28. * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted.
  29. * 3) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in applications where
  30. * the semaphore is used for mutual exclusion because the resource(s) will no longer be guarded by the
  31. * semaphore.
  32. ************************************************************************************************************************
  33. */
  34.  
  35. #if OS_CFG_SEM_DEL_EN > 0u
  36. OS_OBJ_QTY OSSemDel (OS_SEM *p_sem,
  37. OS_OPT opt,
  38. OS_ERR *p_err)
  39. {
  40. OS_OBJ_QTY cnt;
  41. OS_OBJ_QTY nbr_tasks;
  42. OS_PEND_DATA *p_pend_data;
  43. OS_PEND_LIST *p_pend_list;
  44. OS_TCB *p_tcb;
  45. CPU_TS ts;
  46. CPU_SR_ALLOC();
  47.  
  48. #ifdef OS_SAFETY_CRITICAL
  49. if (p_err == (OS_ERR *)0) {
  50. OS_SAFETY_CRITICAL_EXCEPTION();
  51. return ((OS_OBJ_QTY)0);
  52. }
  53. #endif
  54.  
  55. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  56. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Not allowed to delete a semaphore from an ISR */
  57. *p_err = OS_ERR_DEL_ISR;
  58. return ((OS_OBJ_QTY)0);
  59. }
  60. #endif
  61.  
  62. #if OS_CFG_ARG_CHK_EN > 0u
  63. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  64. *p_err = OS_ERR_OBJ_PTR_NULL;
  65. return ((OS_OBJ_QTY)0);
  66. }
  67. switch (opt) { /* Validate 'opt' */
  68. //选项和法性检查
  69. case OS_OPT_DEL_NO_PEND:
  70. case OS_OPT_DEL_ALWAYS:
  71. break;
  72.  
  73. default:
  74. *p_err = OS_ERR_OPT_INVALID;
  75. return ((OS_OBJ_QTY)0);
  76. }
  77. #endif
  78.  
  79. #if OS_CFG_OBJ_TYPE_CHK_EN > 0u
  80. if (p_sem->Type != OS_OBJ_TYPE_SEM) { /* Make sure semaphore was created */
  81. *p_err = OS_ERR_OBJ_TYPE;
  82. return ((OS_OBJ_QTY)0);
  83. }
  84. #endif
  85.  
  86. CPU_CRITICAL_ENTER();
  87. //取出等待队列
  88. p_pend_list = &p_sem->PendList;
  89. //取出等待队列数量
  90. cnt = p_pend_list->NbrEntries;
  91. nbr_tasks = cnt;
  92. switch (opt) {
  93. //无等待任务
  94. case OS_OPT_DEL_NO_PEND: /* Delete semaphore only if no task waiting */
  95. if (nbr_tasks == (OS_OBJ_QTY)0) {
  96. #if OS_CFG_DBG_EN > 0u
  97. OS_SemDbgListRemove(p_sem);
  98. #endif
  99. //信号量--
  100. OSSemQty--;
  101. //清除信号量
  102. OS_SemClr(p_sem);
  103. CPU_CRITICAL_EXIT();
  104. *p_err = OS_ERR_NONE;
  105. } else {
  106. CPU_CRITICAL_EXIT();
  107. *p_err = OS_ERR_TASK_WAITING;
  108. }
  109. break;
  110.  
  111. case OS_OPT_DEL_ALWAYS: /* Always delete the semaphore */
  112. //有任务等待依然删除
  113. OS_CRITICAL_ENTER_CPU_EXIT();
  114. ts = OS_TS_GET(); /* Get local time stamp so all tasks get the same time */
  115. while (cnt > 0u) { /* Remove all tasks on the pend list */
  116. p_pend_data = p_pend_list->HeadPtr;
  117. p_tcb = p_pend_data->TCBPtr;
  118. //删除一个信号量即删除一个等待任务
  119. OS_PendObjDel((OS_PEND_OBJ *)((void *)p_sem),
  120. p_tcb,
  121. ts);
  122. cnt--;
  123. }
  124. #if OS_CFG_DBG_EN > 0u
  125. OS_SemDbgListRemove(p_sem);
  126. #endif
  127. OSSemQty--;
  128. OS_SemClr(p_sem);
  129. OS_CRITICAL_EXIT_NO_SCHED();
  130. OSSched(); /* Find highest priority task ready to run */
  131. *p_err = OS_ERR_NONE;
  132. break;
  133.  
  134. default:
  135. CPU_CRITICAL_EXIT();
  136. *p_err = OS_ERR_OPT_INVALID;
  137. break;
  138. }
  139. return ((OS_OBJ_QTY)nbr_tasks);
  140. }
  141. #endif

OSSemDel ()

信号量清除

  1. ************************************************************************************************************************
  2. * CLEAR THE CONTENTS OF A SEMAPHORE
  3. *
  4. * Description: This function is called by OSSemDel() to clear the contents of a semaphore
  5. *
  6.  
  7. * Argument(s): p_sem is a pointer to the semaphore to clear
  8. * -----
  9. *
  10. * Returns : none
  11. *
  12. * Note(s) : 1) This function is INTERNAL to uC/OS-III and your application MUST NOT call it.
  13. ************************************************************************************************************************
  14. */
  15.  
  16. void OS_SemClr (OS_SEM *p_sem)
  17. {
  18. p_sem->Type = OS_OBJ_TYPE_NONE; /* Mark the data structure as a NONE */
  19. p_sem->Ctr = (OS_SEM_CTR)0; /* Set semaphore value */
  20. p_sem->TS = (CPU_TS )0; /* Clear the time stamp */
  21. p_sem->NamePtr = (CPU_CHAR *)((void *)"?SEM");
  22. OS_PendListInit(&p_sem->PendList); /* Initialize the waiting list */
  23. }

OS_SemClr ()

删除信号量节点

  1. ************************************************************************************************************************
  2. * READY A TASK THAT WAS PENDING ON AN OBJECT BEING DELETED
  3. *
  4. * Description: This function is called to make a task ready-to-run because an object is being deleted
  5. *
  6. * Arguments : p_obj is a pointer to the object being deleted
  7. * -----
  8. *
  9. * p_tcb is a pointer to the OS_TCB of the task to make ready-to-run
  10. * -----
  11. *
  12. * ts is a timestamp to indicate when the object was deleted
  13. *
  14. * Returns : none
  15. *
  16. * Note(s) : This function is INTERNAL to uC/OS-III and your application should not call it.
  17. ************************************************************************************************************************
  18. */
  19.  
  20. void OS_PendObjDel (OS_PEND_OBJ *p_obj,
  21. OS_TCB *p_tcb,
  22. CPU_TS ts)
  23. {
  24. switch (p_tcb->TaskState) {
  25. case OS_TASK_STATE_RDY: /* These states should never occur */
  26. case OS_TASK_STATE_DLY:
  27. case OS_TASK_STATE_SUSPENDED:
  28. case OS_TASK_STATE_DLY_SUSPENDED:
  29. break;
  30.  
  31. case OS_TASK_STATE_PEND:
  32. case OS_TASK_STATE_PEND_TIMEOUT:
  33. //等待多个内核对象 ?
  34. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  35. //yes
  36. //等待对象删除后加入就绪
  37. OS_PendObjDel1(p_obj, /* Indicate which object was pend aborted */
  38. p_tcb,
  39. ts);
  40. }
  41. #if (OS_MSG_EN > 0u)
  42. p_tcb->MsgPtr = (void *)0;
  43. p_tcb->MsgSize = (OS_MSG_SIZE)0u;
  44. #endif
  45. // No
  46. p_tcb->TS = ts;
  47. //将任务从等待list删除
  48. OS_PendListRemove(p_tcb); /* Remove task from all wait lists */
  49. //将任务从加入就绪list
  50. OS_TaskRdy(p_tcb);
  51. p_tcb->TaskState = OS_TASK_STATE_RDY; /* Task is readied because object is deleted */
  52. p_tcb->PendStatus = OS_STATUS_PEND_DEL;
  53. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING;
  54. break;
  55.  
  56. case OS_TASK_STATE_PEND_SUSPENDED:
  57. case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
  58. if (p_tcb->PendOn == OS_TASK_PEND_ON_MULTI) {
  59. OS_PendObjDel1(p_obj, /* Indicate which object was pend aborted */
  60. p_tcb,
  61. ts);
  62. }
  63. #if (OS_MSG_EN > 0u)
  64. p_tcb->MsgPtr = (void *)0;
  65. p_tcb->MsgSize = (OS_MSG_SIZE)0u;
  66. #endif
  67. p_tcb->TS = ts;
  68. OS_TickListRemove(p_tcb); /* Cancel the timeout */
  69. OS_PendListRemove(p_tcb); /* Remove task from all wait lists */
  70. p_tcb->TaskState = OS_TASK_STATE_SUSPENDED; /* Task needs to remain suspended */
  71. p_tcb->PendStatus = OS_STATUS_PEND_DEL;
  72. p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING; /* Indicate no longer pending */
  73. break;
  74.  
  75. default:
  76. break;
  77. }
  78. }

OS_PendObjDel ()

设置信号量值

  1. ************************************************************************************************************************
  2. * SET SEMAPHORE
  3. *
  4. * Description: This function sets the semaphore count to the value specified as an argument. Typically, this value
  5. * would be 0 but of course, we can set the semaphore to any value.
  6. *
  7. * You would typically use this function when a semaphore is used as a signaling mechanism
  8. * and, you want to reset the count value.
  9. *
  10. * Arguments : p_sem is a pointer to the semaphore
  11. *
  12. * cnt is the new value for the semaphore count. You would pass 0 to reset the semaphore count.
  13. *
  14. * p_err is a pointer to a variable that will contain an error code returned by this function.
  15. *
  16. * OS_ERR_NONE The call was successful and the semaphore value was set.
  17. * OS_ERR_OBJ_PTR_NULL If 'p_sem' is a NULL pointer.
  18. * OS_ERR_OBJ_TYPE If 'p_sem' is not pointing to a semaphore.
  19. * OS_ERR_TASK_WAITING If tasks are waiting on the semaphore.
  20. *
  21. * Returns : None
  22. ************************************************************************************************************************
  23. */
  24.  
  25. #if OS_CFG_SEM_SET_EN > 0u
  26. void OSSemSet (OS_SEM *p_sem,
  27. OS_SEM_CTR cnt,
  28. OS_ERR *p_err)
  29. {
  30. OS_PEND_LIST *p_pend_list;
  31. CPU_SR_ALLOC();
  32.  
  33. #ifdef OS_SAFETY_CRITICAL
  34. if (p_err == (OS_ERR *)0) {
  35. OS_SAFETY_CRITICAL_EXCEPTION();
  36. return;
  37. }
  38. #endif
  39.  
  40. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  41. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Can't call this function from an ISR */
  42. *p_err = OS_ERR_SET_ISR;
  43. return;
  44. }
  45. #endif
  46.  
  47. #if OS_CFG_ARG_CHK_EN > 0u
  48. if (p_sem == (OS_SEM *)0) { /* Validate 'p_sem' */
  49. *p_err = OS_ERR_OBJ_PTR_NULL;
  50. return;
  51. }
  52. #endif
  53.  
  54. #if OS_CFG_OBJ_TYPE_CHK_EN > 0u
  55. if (p_sem->Type != OS_OBJ_TYPE_SEM) { /* Make sure semaphore was created */
  56. *p_err = OS_ERR_OBJ_TYPE;
  57. return;
  58. }
  59. #endif
  60.  
  61. *p_err = OS_ERR_NONE;
  62. CPU_CRITICAL_ENTER();
  63. //´Ë״̬²»¿ÉÄÜÓÐÈÎÎñÔڵȴýÐźÅÁ¿Ö±½ÓÐÞ¸Ä
  64. if (p_sem->Ctr > (OS_SEM_CTR)0) { /* See if semaphore already has a count */
  65. p_sem->Ctr = cnt; /* Yes, set it to the new value specified. */
  66. } else {
  67. //·ñÔò ÅжÏÊDz»ÓÐÈÎÎñÔڵȴýÐźÅÁ¿£¿
  68. p_pend_list = &p_sem->PendList; /* No */
  69. if (p_pend_list->NbrEntries == (OS_OBJ_QTY)0) { /* See if task(s) waiting? */
  70. //No Ö±½ÓÐÞ¸Ä+
  71. p_sem->Ctr = cnt; /* No, OK to set the value */
  72. } else {
  73. //Yes ²»ÐíÐÞ¸ÄÐźÅÁ¿Öµ
  74. *p_err = OS_ERR_TASK_WAITING;
  75. }
  76. }
  77. CPU_CRITICAL_EXIT();
  78. }
  79. #endif

OSSemSet ()

信号量有很多个操作,但都是对数据结构的操作,因此理清数据结构就知道各个函数都是如何工作的,多值信号量在请求任务内调用在无法请求到信号量时会将当前任务加入等待列表,在post信号量时在等待队列里找到对应的发布任务将其就绪,对于等待列表则是一个按照任务优先级排序的双向链表。

μC/OS-III---I笔记5---多值信号量的更多相关文章

  1. uc/os iii移植到STM32F4---IAR开发环境

    也许是先入为主的原因,时钟用不惯Keil环境,大多数的教程都是拿keil写的,尝试将官方的uc/os iii 移植到IAR环境. 1.首先尝试从官网上下载的官方移植的代码,编译通过,但是执行会报堆栈溢 ...

  2. 基于μC/OS—III的CC1120驱动程序设计

    基于μC/OS—III的CC1120驱动程序设计 时间:2014-01-21 来源:电子设计工程 作者:张绍游,张贻雄,石江宏 关键字:CC1120   嵌入式操作系统   STM32F103ZE   ...

  3. 机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记

    机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记 关键字:k-均值.kMeans.聚类.非监督学习作者:米仓山下时间: ...

  4. freertos知识点笔记——队列、二值信号量、计数信号量

    队列1.队列queue通常用于任务之间的通信,一个任务写缓存,另一个任务读缓存.队列还会有等待时间,2.阻塞超时时间.如果在发送时队列已满,这个时间即是任务处于阻塞态等待队列空间有效的最长等待时间.如 ...

  5. FreeRTOS 二值信号量,互斥信号量,递归互斥信号量

    以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节讲解 FreeRTOS 任务间的同步和资源共享机制,二值信号量. 二值信号量是计数信号量的一种特殊形式 ...

  6. 15.4-uC/OS-III资源管理(二值信号量)

    互斥信号量是 uC/OS 操作系统的一个内核对象, 与多值信号量非常相似,但它是二值的,只能是 0 或 1,所以也叫二值信号量, 主要用于保护资源. 1.如果想要使用互斥信号量,就必须事先使能互斥信号 ...

  7. 15.3-uC/OS-III资源管理(多值信号量)

    多值信号量是 uC/OS 操作系统的一个内核对象, 主要用于标志事件的发生和共享资源管理. 1.如果想要使用多值信号量,就必须事先使能多值信号量. 多值信号量的使能位于“os_cfg.h”. 2.OS ...

  8. Linux学习笔记(15)-信号量

    在多线程或者多进程编程中,有一个非常需要关注的东西,那就是同步以及互斥问题. 同步是指多个进程之间的协作,而互斥是指多个进程之间,为了争夺有限的资源,而进行的竞争. 理论很高端,但经过自己几天的学习, ...

  9. FreeRTOS 任务计数信号量,任务二值信号量,任务事件标志组,任务消息邮箱

    以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 计数信号量的另一种实现方式----基于任务通知(Task Not ...

随机推荐

  1. 论super().__init__()的用法

    当我们调用 super() 的时候,实际上是实例化了一个 super 类. super 是个类,既不是关键字也不是函数等其他数据结构,该对象就是专门用来访问父类中的属性的(严格按照继承的关系). -- ...

  2. Cisco IOS

    IOS Internetwork Operating System 互联网操作系统(基于UNIX系统) Cisco IOS 软件提供多种网络服务进而支持各种网络应用. Cisco IOS用户界面的基本 ...

  3. 新型赌博黑产攻击肆虐网吧: LOL博彩引流+棋牌盗号

    https://mp.weixin.qq.com/s/BxnovV6jKqPkYfHEzjd_FA 新型赌博黑产攻击肆虐网吧: LOL博彩引流+棋牌盗号 看雪学院 2019-04-21

  4. Centos 7 安装 erlang

    https://www.cnblogs.com/swyy/p/11582309.html https://blog.csdn.net/qq_20492999/article/details/81254 ...

  5. Jackson学习

    Jackson 是一个能够将java对象序列化为JSON字符串,也能够将JSON字符串反序列化为java对象的框架. 本文的所有内容都可以从 Java JSON Jackson Introductio ...

  6. 正则re高级用法

    search 需求:匹配出文章阅读的次数 #coding=utf-8 import re ret = re.search(r"\d+", "阅读次数为 9999" ...

  7. http、https比较

    HTTP 超文本传输协议,是一个基于请求与响应,无状态的,应用层的协议,常基于TCP/IP协议传输数据, 互联网上应用最为广泛的一种网络协议,所有的WWW文件都必须遵守这个标准. 设计HTTP的初衷是 ...

  8. Language Guide (proto3) | proto3 语言指南(四)枚举类型

    枚举类型 定义消息类型时,可能希望其中一个字段只包含预定义值列表中的一个.例如,假设您想为每个SearchRequest添加一个corpus(语料库)字段,其中语料库的值可以是UNIVERSAL.WE ...

  9. Golang之垃圾回收

    本篇主要是参考了: http://legendtkl.com/2017/04/28/golang-gc/ 说是参考,但其实基本上是原封不动. GC算法简介: 1. 引用计数 引用计数的思想非常简单:每 ...

  10. 使用Docker Registry管理Docker镜像

    文章目录 使用Docker Registry管理Docker镜像 1.使用Docker Hub管理镜像 1.1注册与登录 1.2创建仓库 1.3推送镜像 2. 使用私有仓库管理镜像 2.1 搭建私有仓 ...