Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_List.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_LIST.C
  5. * Purpose: Functions for the management of different lists
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Conf.h"
  36. #include "rt_System.h"
  37. #include "rt_List.h"
  38. #include "rt_Task.h"
  39. #include "rt_Time.h"
  40. #include "rt_HAL_CM.h"
  41. /*----------------------------------------------------------------------------
  42. * Global Variables
  43. *---------------------------------------------------------------------------*/
  44. /* List head of chained ready tasks */
  45. struct OS_XCB os_rdy;
  46. /* List head of chained delay tasks */
  47. struct OS_XCB os_dly;
  48. /*----------------------------------------------------------------------------
  49. * Functions
  50. *---------------------------------------------------------------------------*/
  51. /*--------------------------- rt_put_prio -----------------------------------*/
  52. void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
  53. /* Put task identified with "p_task" into list ordered by priority. */
  54. /* "p_CB" points to head of list; list has always an element at end with */
  55. /* a priority less than "p_task->prio". */
  56. P_TCB p_CB2;
  57. U32 prio;
  58. BOOL sem_mbx = __FALSE;
  59. if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
  60. sem_mbx = __TRUE;
  61. }
  62. prio = p_task->prio;
  63. p_CB2 = p_CB->p_lnk;
  64. /* Search for an entry in the list */
  65. while (p_CB2 != NULL && prio <= p_CB2->prio) {
  66. p_CB = (P_XCB)p_CB2;
  67. p_CB2 = p_CB2->p_lnk;
  68. }
  69. /* Entry found, insert the task into the list */
  70. p_task->p_lnk = p_CB2;
  71. p_CB->p_lnk = p_task;
  72. if (sem_mbx) {
  73. if (p_CB2 != NULL) {
  74. p_CB2->p_rlnk = p_task;
  75. }
  76. p_task->p_rlnk = (P_TCB)p_CB;
  77. }
  78. else {
  79. p_task->p_rlnk = NULL;
  80. }
  81. }
  82. /*--------------------------- rt_get_first ----------------------------------*/
  83. P_TCB rt_get_first (P_XCB p_CB) {
  84. /* Get task at head of list: it is the task with highest priority. */
  85. /* "p_CB" points to head of list. */
  86. P_TCB p_first;
  87. p_first = p_CB->p_lnk;
  88. p_CB->p_lnk = p_first->p_lnk;
  89. if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
  90. if (p_first->p_lnk != NULL) {
  91. p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
  92. p_first->p_lnk = NULL;
  93. }
  94. p_first->p_rlnk = NULL;
  95. }
  96. else {
  97. p_first->p_lnk = NULL;
  98. }
  99. return (p_first);
  100. }
  101. /*--------------------------- rt_put_rdy_first ------------------------------*/
  102. void rt_put_rdy_first (P_TCB p_task) {
  103. /* Put task identified with "p_task" at the head of the ready list. The */
  104. /* task must have at least a priority equal to highest priority in list. */
  105. p_task->p_lnk = os_rdy.p_lnk;
  106. p_task->p_rlnk = NULL;
  107. os_rdy.p_lnk = p_task;
  108. }
  109. /*--------------------------- rt_get_same_rdy_prio --------------------------*/
  110. P_TCB rt_get_same_rdy_prio (void) {
  111. /* Remove a task of same priority from ready list if any exists. Other- */
  112. /* wise return NULL. */
  113. P_TCB p_first;
  114. p_first = os_rdy.p_lnk;
  115. if (p_first->prio == os_tsk.run->prio) {
  116. os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
  117. return (p_first);
  118. }
  119. return (NULL);
  120. }
  121. /*--------------------------- rt_resort_prio --------------------------------*/
  122. void rt_resort_prio (P_TCB p_task) {
  123. /* Re-sort ordered lists after the priority of 'p_task' has changed. */
  124. P_TCB p_CB;
  125. if (p_task->p_rlnk == NULL) {
  126. if (p_task->state == READY) {
  127. /* Task is chained into READY list. */
  128. p_CB = (P_TCB)&os_rdy;
  129. goto res;
  130. }
  131. }
  132. else {
  133. p_CB = p_task->p_rlnk;
  134. while (p_CB->cb_type == TCB) {
  135. /* Find a header of this task chain list. */
  136. p_CB = p_CB->p_rlnk;
  137. }
  138. res:rt_rmv_list (p_task);
  139. rt_put_prio ((P_XCB)p_CB, p_task);
  140. }
  141. }
  142. /*--------------------------- rt_put_dly ------------------------------------*/
  143. void rt_put_dly (P_TCB p_task, U16 delay) {
  144. /* Put a task identified with "p_task" into chained delay wait list using */
  145. /* a delay value of "delay". */
  146. P_TCB p;
  147. U32 delta,idelay = delay;
  148. p = (P_TCB)&os_dly;
  149. if (p->p_dlnk == NULL) {
  150. /* Delay list empty */
  151. delta = 0;
  152. goto last;
  153. }
  154. delta = os_dly.delta_time;
  155. while (delta < idelay) {
  156. if (p->p_dlnk == NULL) {
  157. /* End of list found */
  158. last: p_task->p_dlnk = NULL;
  159. p->p_dlnk = p_task;
  160. p_task->p_blnk = p;
  161. p->delta_time = (U16)(idelay - delta);
  162. p_task->delta_time = 0;
  163. return;
  164. }
  165. p = p->p_dlnk;
  166. delta += p->delta_time;
  167. }
  168. /* Right place found */
  169. p_task->p_dlnk = p->p_dlnk;
  170. p->p_dlnk = p_task;
  171. p_task->p_blnk = p;
  172. if (p_task->p_dlnk != NULL) {
  173. p_task->p_dlnk->p_blnk = p_task;
  174. }
  175. p_task->delta_time = (U16)(delta - idelay);
  176. p->delta_time -= p_task->delta_time;
  177. }
  178. /*--------------------------- rt_dec_dly ------------------------------------*/
  179. void rt_dec_dly (void) {
  180. /* Decrement delta time of list head: remove tasks having a value of zero.*/
  181. P_TCB p_rdy;
  182. if (os_dly.p_dlnk == NULL) {
  183. return;
  184. }
  185. os_dly.delta_time--;
  186. while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
  187. p_rdy = os_dly.p_dlnk;
  188. if (p_rdy->p_rlnk != NULL) {
  189. /* Task is really enqueued, remove task from semaphore/mailbox */
  190. /* timeout waiting list. */
  191. p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
  192. if (p_rdy->p_lnk != NULL) {
  193. p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
  194. p_rdy->p_lnk = NULL;
  195. }
  196. p_rdy->p_rlnk = NULL;
  197. }
  198. rt_put_prio (&os_rdy, p_rdy);
  199. os_dly.delta_time = p_rdy->delta_time;
  200. if (p_rdy->state == WAIT_ITV) {
  201. /* Calculate the next time for interval wait. */
  202. p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
  203. }
  204. p_rdy->state = READY;
  205. os_dly.p_dlnk = p_rdy->p_dlnk;
  206. if (p_rdy->p_dlnk != NULL) {
  207. p_rdy->p_dlnk->p_blnk = (P_TCB)&os_dly;
  208. p_rdy->p_dlnk = NULL;
  209. }
  210. p_rdy->p_blnk = NULL;
  211. }
  212. }
  213. /*--------------------------- rt_rmv_list -----------------------------------*/
  214. void rt_rmv_list (P_TCB p_task) {
  215. /* Remove task identified with "p_task" from ready, semaphore or mailbox */
  216. /* waiting list if enqueued. */
  217. P_TCB p_b;
  218. if (p_task->p_rlnk != NULL) {
  219. /* A task is enqueued in semaphore / mailbox waiting list. */
  220. p_task->p_rlnk->p_lnk = p_task->p_lnk;
  221. if (p_task->p_lnk != NULL) {
  222. p_task->p_lnk->p_rlnk = p_task->p_rlnk;
  223. }
  224. return;
  225. }
  226. p_b = (P_TCB)&os_rdy;
  227. while (p_b != NULL) {
  228. /* Search the ready list for task "p_task" */
  229. if (p_b->p_lnk == p_task) {
  230. p_b->p_lnk = p_task->p_lnk;
  231. return;
  232. }
  233. p_b = p_b->p_lnk;
  234. }
  235. }
  236. /*--------------------------- rt_rmv_dly ------------------------------------*/
  237. void rt_rmv_dly (P_TCB p_task) {
  238. /* Remove task identified with "p_task" from delay list if enqueued. */
  239. P_TCB p_b;
  240. p_b = p_task->p_blnk;
  241. if (p_b != NULL) {
  242. /* Task is really enqueued */
  243. p_b->p_dlnk = p_task->p_dlnk;
  244. if (p_task->p_dlnk != NULL) {
  245. /* 'p_task' is in the middle of list */
  246. p_b->delta_time += p_task->delta_time;
  247. p_task->p_dlnk->p_blnk = p_b;
  248. p_task->p_dlnk = NULL;
  249. }
  250. else {
  251. /* 'p_task' is at the end of list */
  252. p_b->delta_time = 0;
  253. }
  254. p_task->p_blnk = NULL;
  255. }
  256. }
  257. /*--------------------------- rt_psq_enq ------------------------------------*/
  258. void rt_psq_enq (OS_ID entry, U32 arg) {
  259. /* Insert post service request "entry" into ps-queue. */
  260. U32 idx;
  261. idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
  262. if (idx < os_psq->size) {
  263. os_psq->q[idx].id = entry;
  264. os_psq->q[idx].arg = arg;
  265. }
  266. else {
  267. os_error (OS_ERR_FIFO_OVF);
  268. }
  269. }
  270. /*----------------------------------------------------------------------------
  271. * end of file
  272. *---------------------------------------------------------------------------*/