1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Functions for the management of different lists
7 *----------------------------------------------------------------------------
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.
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 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
37 #include "rt_System.h"
41 #include "rt_HAL_CM.h"
43 /*----------------------------------------------------------------------------
45 *---------------------------------------------------------------------------*/
47 /* List head of chained ready tasks */
49 /* List head of chained delay tasks */
53 /*----------------------------------------------------------------------------
55 *---------------------------------------------------------------------------*/
58 /*--------------------------- rt_put_prio -----------------------------------*/
60 void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
61 /* Put task identified with "p_task" into list ordered by priority. */
62 /* "p_CB" points to head of list; list has always an element at end with */
63 /* a priority less than "p_task->prio". */
66 BOOL sem_mbx = __FALSE;
68 if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
73 /* Search for an entry in the list */
74 while (p_CB2 != NULL && prio <= p_CB2->prio) {
78 /* Entry found, insert the task into the list */
79 p_task->p_lnk = p_CB2;
83 p_CB2->p_rlnk = p_task;
85 p_task->p_rlnk = (P_TCB)p_CB;
88 p_task->p_rlnk = NULL;
93 /*--------------------------- rt_get_first ----------------------------------*/
95 P_TCB rt_get_first (P_XCB p_CB) {
96 /* Get task at head of list: it is the task with highest priority. */
97 /* "p_CB" points to head of list. */
100 p_first = p_CB->p_lnk;
101 p_CB->p_lnk = p_first->p_lnk;
102 if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
103 if (p_first->p_lnk != NULL) {
104 p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
105 p_first->p_lnk = NULL;
107 p_first->p_rlnk = NULL;
110 p_first->p_lnk = NULL;
116 /*--------------------------- rt_put_rdy_first ------------------------------*/
118 void rt_put_rdy_first (P_TCB p_task) {
119 /* Put task identified with "p_task" at the head of the ready list. The */
120 /* task must have at least a priority equal to highest priority in list. */
121 p_task->p_lnk = os_rdy.p_lnk;
122 p_task->p_rlnk = NULL;
123 os_rdy.p_lnk = p_task;
127 /*--------------------------- rt_get_same_rdy_prio --------------------------*/
129 P_TCB rt_get_same_rdy_prio (void) {
130 /* Remove a task of same priority from ready list if any exists. Other- */
131 /* wise return NULL. */
134 p_first = os_rdy.p_lnk;
135 if (p_first->prio == os_tsk.run->prio) {
136 os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
143 /*--------------------------- rt_resort_prio --------------------------------*/
145 void rt_resort_prio (P_TCB p_task) {
146 /* Re-sort ordered lists after the priority of 'p_task' has changed. */
149 if (p_task->p_rlnk == NULL) {
150 if (p_task->state == READY) {
151 /* Task is chained into READY list. */
152 p_CB = (P_TCB)&os_rdy;
157 p_CB = p_task->p_rlnk;
158 while (p_CB->cb_type == TCB) {
159 /* Find a header of this task chain list. */
162 res:rt_rmv_list (p_task);
163 rt_put_prio ((P_XCB)p_CB, p_task);
168 /*--------------------------- rt_put_dly ------------------------------------*/
170 void rt_put_dly (P_TCB p_task, U16 delay) {
171 /* Put a task identified with "p_task" into chained delay wait list using */
172 /* a delay value of "delay". */
174 U32 delta,idelay = delay;
177 if (p->p_dlnk == NULL) {
178 /* Delay list empty */
182 delta = os_dly.delta_time;
183 while (delta < idelay) {
184 if (p->p_dlnk == NULL) {
185 /* End of list found */
186 last: p_task->p_dlnk = NULL;
189 p->delta_time = (U16)(idelay - delta);
190 p_task->delta_time = 0;
194 delta += p->delta_time;
196 /* Right place found */
197 p_task->p_dlnk = p->p_dlnk;
200 if (p_task->p_dlnk != NULL) {
201 p_task->p_dlnk->p_blnk = p_task;
203 p_task->delta_time = (U16)(delta - idelay);
204 p->delta_time -= p_task->delta_time;
208 /*--------------------------- rt_dec_dly ------------------------------------*/
210 void rt_dec_dly (void) {
211 /* Decrement delta time of list head: remove tasks having a value of zero.*/
214 if (os_dly.p_dlnk == NULL) {
218 while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
219 p_rdy = os_dly.p_dlnk;
220 if (p_rdy->p_rlnk != NULL) {
221 /* Task is really enqueued, remove task from semaphore/mailbox */
222 /* timeout waiting list. */
223 p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
224 if (p_rdy->p_lnk != NULL) {
225 p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
228 p_rdy->p_rlnk = NULL;
230 rt_put_prio (&os_rdy, p_rdy);
231 os_dly.delta_time = p_rdy->delta_time;
232 if (p_rdy->state == WAIT_ITV) {
233 /* Calculate the next time for interval wait. */
234 p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
236 p_rdy->state = READY;
237 os_dly.p_dlnk = p_rdy->p_dlnk;
238 if (p_rdy->p_dlnk != NULL) {
239 p_rdy->p_dlnk->p_blnk = (P_TCB)&os_dly;
240 p_rdy->p_dlnk = NULL;
242 p_rdy->p_blnk = NULL;
247 /*--------------------------- rt_rmv_list -----------------------------------*/
249 void rt_rmv_list (P_TCB p_task) {
250 /* Remove task identified with "p_task" from ready, semaphore or mailbox */
251 /* waiting list if enqueued. */
254 if (p_task->p_rlnk != NULL) {
255 /* A task is enqueued in semaphore / mailbox waiting list. */
256 p_task->p_rlnk->p_lnk = p_task->p_lnk;
257 if (p_task->p_lnk != NULL) {
258 p_task->p_lnk->p_rlnk = p_task->p_rlnk;
263 p_b = (P_TCB)&os_rdy;
264 while (p_b != NULL) {
265 /* Search the ready list for task "p_task" */
266 if (p_b->p_lnk == p_task) {
267 p_b->p_lnk = p_task->p_lnk;
275 /*--------------------------- rt_rmv_dly ------------------------------------*/
277 void rt_rmv_dly (P_TCB p_task) {
278 /* Remove task identified with "p_task" from delay list if enqueued. */
281 p_b = p_task->p_blnk;
283 /* Task is really enqueued */
284 p_b->p_dlnk = p_task->p_dlnk;
285 if (p_task->p_dlnk != NULL) {
286 /* 'p_task' is in the middle of list */
287 p_b->delta_time += p_task->delta_time;
288 p_task->p_dlnk->p_blnk = p_b;
289 p_task->p_dlnk = NULL;
292 /* 'p_task' is at the end of list */
295 p_task->p_blnk = NULL;
300 /*--------------------------- rt_psq_enq ------------------------------------*/
302 void rt_psq_enq (OS_ID entry, U32 arg) {
303 /* Insert post service request "entry" into ps-queue. */
306 idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
307 if (idx < os_psq->size) {
308 os_psq->q[idx].id = entry;
309 os_psq->q[idx].arg = arg;
312 os_error (OS_ERR_FIFO_OVF);
317 /*----------------------------------------------------------------------------
319 *---------------------------------------------------------------------------*/