rt_List.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_LIST.C
  5. * Purpose: Functions for the management of different lists
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "RTX_Config.h"
  27. #include "rt_System.h"
  28. #include "rt_List.h"
  29. #include "rt_Task.h"
  30. #include "rt_Time.h"
  31. #include "rt_HAL_CM.h"
  32. /*----------------------------------------------------------------------------
  33. * Global Variables
  34. *---------------------------------------------------------------------------*/
  35. /* List head of chained ready tasks */
  36. struct OS_XCB os_rdy;
  37. /* List head of chained delay tasks */
  38. struct OS_XCB os_dly;
  39. /*----------------------------------------------------------------------------
  40. * Functions
  41. *---------------------------------------------------------------------------*/
  42. /*--------------------------- rt_put_prio -----------------------------------*/
  43. void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
  44. /* Put task identified with "p_task" into list ordered by priority. */
  45. /* "p_CB" points to head of list; list has always an element at end with */
  46. /* a priority less than "p_task->prio". */
  47. P_TCB p_CB2;
  48. U32 prio;
  49. BOOL sem_mbx = __FALSE;
  50. if ((p_CB->cb_type == SCB) || (p_CB->cb_type == MCB) || (p_CB->cb_type == MUCB)) {
  51. sem_mbx = __TRUE;
  52. }
  53. prio = p_task->prio;
  54. p_CB2 = p_CB->p_lnk;
  55. /* Search for an entry in the list */
  56. while ((p_CB2 != NULL) && (prio <= p_CB2->prio)) {
  57. p_CB = (P_XCB)p_CB2;
  58. p_CB2 = p_CB2->p_lnk;
  59. }
  60. /* Entry found, insert the task into the list */
  61. p_task->p_lnk = p_CB2;
  62. p_CB->p_lnk = p_task;
  63. if (sem_mbx) {
  64. if (p_CB2 != NULL) {
  65. p_CB2->p_rlnk = p_task;
  66. }
  67. p_task->p_rlnk = (P_TCB)p_CB;
  68. }
  69. else {
  70. p_task->p_rlnk = NULL;
  71. }
  72. }
  73. /*--------------------------- rt_get_first ----------------------------------*/
  74. P_TCB rt_get_first (P_XCB p_CB) {
  75. /* Get task at head of list: it is the task with highest priority. */
  76. /* "p_CB" points to head of list. */
  77. P_TCB p_first;
  78. p_first = p_CB->p_lnk;
  79. p_CB->p_lnk = p_first->p_lnk;
  80. if ((p_CB->cb_type == SCB) || (p_CB->cb_type == MCB) || (p_CB->cb_type == MUCB)) {
  81. if (p_first->p_lnk != NULL) {
  82. p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
  83. p_first->p_lnk = NULL;
  84. }
  85. p_first->p_rlnk = NULL;
  86. }
  87. else {
  88. p_first->p_lnk = NULL;
  89. }
  90. return (p_first);
  91. }
  92. /*--------------------------- rt_put_rdy_first ------------------------------*/
  93. void rt_put_rdy_first (P_TCB p_task) {
  94. /* Put task identified with "p_task" at the head of the ready list. The */
  95. /* task must have at least a priority equal to highest priority in list. */
  96. p_task->p_lnk = os_rdy.p_lnk;
  97. p_task->p_rlnk = NULL;
  98. os_rdy.p_lnk = p_task;
  99. }
  100. /*--------------------------- rt_get_same_rdy_prio --------------------------*/
  101. P_TCB rt_get_same_rdy_prio (void) {
  102. /* Remove a task of same priority from ready list if any exists. Other- */
  103. /* wise return NULL. */
  104. P_TCB p_first;
  105. p_first = os_rdy.p_lnk;
  106. if (p_first->prio == os_tsk.run->prio) {
  107. os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
  108. return (p_first);
  109. }
  110. return (NULL);
  111. }
  112. /*--------------------------- rt_resort_prio --------------------------------*/
  113. void rt_resort_prio (P_TCB p_task) {
  114. /* Re-sort ordered lists after the priority of 'p_task' has changed. */
  115. P_TCB p_CB;
  116. if (p_task->p_rlnk == NULL) {
  117. if (p_task->state == READY) {
  118. /* Task is chained into READY list. */
  119. p_CB = (P_TCB)&os_rdy;
  120. goto res;
  121. }
  122. }
  123. else {
  124. p_CB = p_task->p_rlnk;
  125. while (p_CB->cb_type == TCB) {
  126. /* Find a header of this task chain list. */
  127. p_CB = p_CB->p_rlnk;
  128. }
  129. res:rt_rmv_list (p_task);
  130. rt_put_prio ((P_XCB)p_CB, p_task);
  131. }
  132. }
  133. /*--------------------------- rt_put_dly ------------------------------------*/
  134. void rt_put_dly (P_TCB p_task, U16 delay) {
  135. /* Put a task identified with "p_task" into chained delay wait list using */
  136. /* a delay value of "delay". */
  137. P_TCB p;
  138. U32 delta,idelay = delay;
  139. p = (P_TCB)&os_dly;
  140. if (p->p_dlnk == NULL) {
  141. /* Delay list empty */
  142. delta = 0U;
  143. goto last;
  144. }
  145. delta = os_dly.delta_time;
  146. while (delta < idelay) {
  147. if (p->p_dlnk == NULL) {
  148. /* End of list found */
  149. last: p_task->p_dlnk = NULL;
  150. p->p_dlnk = p_task;
  151. p_task->p_blnk = p;
  152. p->delta_time = (U16)(idelay - delta);
  153. p_task->delta_time = 0U;
  154. return;
  155. }
  156. p = p->p_dlnk;
  157. delta += p->delta_time;
  158. }
  159. /* Right place found */
  160. p_task->p_dlnk = p->p_dlnk;
  161. p->p_dlnk = p_task;
  162. p_task->p_blnk = p;
  163. if (p_task->p_dlnk != NULL) {
  164. p_task->p_dlnk->p_blnk = p_task;
  165. }
  166. p_task->delta_time = (U16)(delta - idelay);
  167. p->delta_time -= p_task->delta_time;
  168. }
  169. /*--------------------------- rt_dec_dly ------------------------------------*/
  170. void rt_dec_dly (void) {
  171. /* Decrement delta time of list head: remove tasks having a value of zero.*/
  172. P_TCB p_rdy;
  173. if (os_dly.p_dlnk == NULL) {
  174. return;
  175. }
  176. os_dly.delta_time--;
  177. while ((os_dly.delta_time == 0U) && (os_dly.p_dlnk != NULL)) {
  178. p_rdy = os_dly.p_dlnk;
  179. if (p_rdy->p_rlnk != NULL) {
  180. /* Task is really enqueued, remove task from semaphore/mailbox */
  181. /* timeout waiting list. */
  182. p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
  183. if (p_rdy->p_lnk != NULL) {
  184. p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
  185. p_rdy->p_lnk = NULL;
  186. }
  187. p_rdy->p_rlnk = NULL;
  188. }
  189. rt_put_prio (&os_rdy, p_rdy);
  190. os_dly.delta_time = p_rdy->delta_time;
  191. if (p_rdy->state == WAIT_ITV) {
  192. /* Calculate the next time for interval wait. */
  193. p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
  194. }
  195. p_rdy->state = READY;
  196. os_dly.p_dlnk = p_rdy->p_dlnk;
  197. if (p_rdy->p_dlnk != NULL) {
  198. p_rdy->p_dlnk->p_blnk = (P_TCB)&os_dly;
  199. p_rdy->p_dlnk = NULL;
  200. }
  201. p_rdy->p_blnk = NULL;
  202. }
  203. }
  204. /*--------------------------- rt_rmv_list -----------------------------------*/
  205. void rt_rmv_list (P_TCB p_task) {
  206. /* Remove task identified with "p_task" from ready, semaphore or mailbox */
  207. /* waiting list if enqueued. */
  208. P_TCB p_b;
  209. if (p_task->p_rlnk != NULL) {
  210. /* A task is enqueued in semaphore / mailbox waiting list. */
  211. p_task->p_rlnk->p_lnk = p_task->p_lnk;
  212. if (p_task->p_lnk != NULL) {
  213. p_task->p_lnk->p_rlnk = p_task->p_rlnk;
  214. }
  215. return;
  216. }
  217. p_b = (P_TCB)&os_rdy;
  218. while (p_b != NULL) {
  219. /* Search the ready list for task "p_task" */
  220. if (p_b->p_lnk == p_task) {
  221. p_b->p_lnk = p_task->p_lnk;
  222. return;
  223. }
  224. p_b = p_b->p_lnk;
  225. }
  226. }
  227. /*--------------------------- rt_rmv_dly ------------------------------------*/
  228. void rt_rmv_dly (P_TCB p_task) {
  229. /* Remove task identified with "p_task" from delay list if enqueued. */
  230. P_TCB p_b;
  231. p_b = p_task->p_blnk;
  232. if (p_b != NULL) {
  233. /* Task is really enqueued */
  234. p_b->p_dlnk = p_task->p_dlnk;
  235. if (p_task->p_dlnk != NULL) {
  236. /* 'p_task' is in the middle of list */
  237. p_b->delta_time += p_task->delta_time;
  238. p_task->p_dlnk->p_blnk = p_b;
  239. p_task->p_dlnk = NULL;
  240. }
  241. else {
  242. /* 'p_task' is at the end of list */
  243. p_b->delta_time = 0U;
  244. }
  245. p_task->p_blnk = NULL;
  246. }
  247. }
  248. /*--------------------------- rt_psq_enq ------------------------------------*/
  249. void rt_psq_enq (OS_ID entry, U32 arg) {
  250. /* Insert post service request "entry" into ps-queue. */
  251. U32 idx;
  252. idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
  253. if (idx < os_psq->size) {
  254. os_psq->q[idx].id = entry;
  255. os_psq->q[idx].arg = arg;
  256. }
  257. else {
  258. os_error (OS_ERR_FIFO_OVF);
  259. }
  260. }
  261. /*----------------------------------------------------------------------------
  262. * end of file
  263. *---------------------------------------------------------------------------*/