]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_List.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_List.c
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
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
38 #include "rt_List.h"
39 #include "rt_Task.h"
40 #include "rt_Time.h"
41 #ifdef __CORTEX_A9
42 #include "rt_HAL_CA.h"
43 #else
44 #include "rt_HAL_CM.h"
45 #endif
46
47 /*----------------------------------------------------------------------------
48  *      Global Variables
49  *---------------------------------------------------------------------------*/
50
51 /* List head of chained ready tasks */
52 struct OS_XCB  os_rdy;
53 /* List head of chained delay tasks */
54 struct OS_XCB  os_dly;
55
56
57 /*----------------------------------------------------------------------------
58  *      Functions
59  *---------------------------------------------------------------------------*/
60
61
62 /*--------------------------- rt_put_prio -----------------------------------*/
63
64 void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
65   /* Put task identified with "p_task" into list ordered by priority.       */
66   /* "p_CB" points to head of list; list has always an element at end with  */
67   /* a priority less than "p_task->prio".                                   */
68   P_TCB p_CB2;
69   U32 prio;
70   BOOL sem_mbx = __FALSE;
71
72   if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
73     sem_mbx = __TRUE;
74   }
75   prio = p_task->prio;
76   p_CB2 = p_CB->p_lnk;
77   /* Search for an entry in the list */
78   while (p_CB2 != NULL && prio <= p_CB2->prio) {
79     p_CB = (P_XCB)p_CB2;
80     p_CB2 = p_CB2->p_lnk;
81   }
82   /* Entry found, insert the task into the list */
83   p_task->p_lnk = p_CB2;
84   p_CB->p_lnk = p_task;
85   if (sem_mbx) {
86     if (p_CB2 != NULL) {
87       p_CB2->p_rlnk = p_task;
88     }
89     p_task->p_rlnk = (P_TCB)p_CB;
90   }
91   else {
92     p_task->p_rlnk = NULL;
93   }
94 }
95
96
97 /*--------------------------- rt_get_first ----------------------------------*/
98
99 P_TCB rt_get_first (P_XCB p_CB) {
100   /* Get task at head of list: it is the task with highest priority. */
101   /* "p_CB" points to head of list. */
102   P_TCB p_first;
103
104   p_first = p_CB->p_lnk;
105   p_CB->p_lnk = p_first->p_lnk;
106   if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
107     if (p_first->p_lnk != NULL) {
108       p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
109       p_first->p_lnk = NULL;
110     }
111     p_first->p_rlnk = NULL;
112   }
113   else {
114     p_first->p_lnk = NULL;
115   }
116   return (p_first);
117 }
118
119
120 /*--------------------------- rt_put_rdy_first ------------------------------*/
121
122 void rt_put_rdy_first (P_TCB p_task) {
123   /* Put task identified with "p_task" at the head of the ready list. The   */
124   /* task must have at least a priority equal to highest priority in list.  */
125   p_task->p_lnk = os_rdy.p_lnk;
126   p_task->p_rlnk = NULL;
127   os_rdy.p_lnk = p_task;
128 }
129
130
131 /*--------------------------- rt_get_same_rdy_prio --------------------------*/
132
133 P_TCB rt_get_same_rdy_prio (void) {
134   /* Remove a task of same priority from ready list if any exists. Other-   */
135   /* wise return NULL.                                                      */
136   P_TCB p_first;
137
138   p_first = os_rdy.p_lnk;
139   if (p_first->prio == os_tsk.run->prio) {
140     os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
141     return (p_first);
142   }
143   return (NULL);
144 }
145
146
147 /*--------------------------- rt_resort_prio --------------------------------*/
148
149 void rt_resort_prio (P_TCB p_task) {
150   /* Re-sort ordered lists after the priority of 'p_task' has changed.      */
151   P_TCB p_CB;
152
153   if (p_task->p_rlnk == NULL) {
154     if (p_task->state == READY) {
155       /* Task is chained into READY list. */
156       p_CB = (P_TCB)&os_rdy;
157       goto res;
158     }
159   }
160   else {
161     p_CB = p_task->p_rlnk;
162     while (p_CB->cb_type == TCB) {
163       /* Find a header of this task chain list. */
164       p_CB = p_CB->p_rlnk;
165     }
166 res:rt_rmv_list (p_task);
167     rt_put_prio ((P_XCB)p_CB, p_task);
168   }
169 }
170
171
172 /*--------------------------- rt_put_dly ------------------------------------*/
173
174 void rt_put_dly (P_TCB p_task, U16 delay) {
175   /* Put a task identified with "p_task" into chained delay wait list using */
176   /* a delay value of "delay".                                              */
177   P_TCB p;
178   U32 delta,idelay = delay;
179
180   p = (P_TCB)&os_dly;
181   if (p->p_dlnk == NULL) {
182     /* Delay list empty */
183     delta = 0;
184     goto last;
185   }
186   delta = os_dly.delta_time;
187   while (delta < idelay) {
188     if (p->p_dlnk == NULL) {
189       /* End of list found */
190 last: p_task->p_dlnk = NULL;
191       p->p_dlnk = p_task;
192       p_task->p_blnk = p;
193       p->delta_time = (U16)(idelay - delta);
194       p_task->delta_time = 0;
195       return;
196     }
197     p = p->p_dlnk;
198     delta += p->delta_time;
199   }
200   /* Right place found */
201   p_task->p_dlnk = p->p_dlnk;
202   p->p_dlnk = p_task;
203   p_task->p_blnk = p;
204   if (p_task->p_dlnk != NULL) {
205     p_task->p_dlnk->p_blnk = p_task;
206   }
207   p_task->delta_time = (U16)(delta - idelay);
208   p->delta_time -= p_task->delta_time;
209 }
210
211
212 /*--------------------------- rt_dec_dly ------------------------------------*/
213
214 void rt_dec_dly (void) {
215   /* Decrement delta time of list head: remove tasks having a value of zero.*/
216   P_TCB p_rdy;
217
218   if (os_dly.p_dlnk == NULL) {
219     return;
220   }
221   os_dly.delta_time--;
222   while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
223     p_rdy = os_dly.p_dlnk;
224     if (p_rdy->p_rlnk != NULL) {
225       /* Task is really enqueued, remove task from semaphore/mailbox */
226       /* timeout waiting list. */
227       p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
228       if (p_rdy->p_lnk != NULL) {
229         p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
230         p_rdy->p_lnk = NULL;
231       }
232       p_rdy->p_rlnk = NULL;
233     }
234     rt_put_prio (&os_rdy, p_rdy);
235     os_dly.delta_time = p_rdy->delta_time;
236     if (p_rdy->state == WAIT_ITV) {
237       /* Calculate the next time for interval wait. */
238       p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
239     }
240     p_rdy->state   = READY;
241     os_dly.p_dlnk = p_rdy->p_dlnk;
242     if (p_rdy->p_dlnk != NULL) {
243       p_rdy->p_dlnk->p_blnk =  (P_TCB)&os_dly;
244       p_rdy->p_dlnk = NULL;
245     }
246     p_rdy->p_blnk = NULL;
247   }
248 }
249
250
251 /*--------------------------- rt_rmv_list -----------------------------------*/
252
253 void rt_rmv_list (P_TCB p_task) {
254   /* Remove task identified with "p_task" from ready, semaphore or mailbox  */
255   /* waiting list if enqueued.                                              */
256   P_TCB p_b;
257
258   if (p_task->p_rlnk != NULL) {
259     /* A task is enqueued in semaphore / mailbox waiting list. */
260     p_task->p_rlnk->p_lnk = p_task->p_lnk;
261     if (p_task->p_lnk != NULL) {
262       p_task->p_lnk->p_rlnk = p_task->p_rlnk;
263     }
264     return;
265   }
266
267   p_b = (P_TCB)&os_rdy;
268   while (p_b != NULL) {
269     /* Search the ready list for task "p_task" */
270     if (p_b->p_lnk == p_task) {
271       p_b->p_lnk = p_task->p_lnk;
272       return;
273     }
274     p_b = p_b->p_lnk;
275   }
276 }
277
278
279 /*--------------------------- rt_rmv_dly ------------------------------------*/
280
281 void rt_rmv_dly (P_TCB p_task) {
282   /* Remove task identified with "p_task" from delay list if enqueued.      */
283   P_TCB p_b;
284
285   p_b = p_task->p_blnk;
286   if (p_b != NULL) {
287     /* Task is really enqueued */
288     p_b->p_dlnk = p_task->p_dlnk;
289     if (p_task->p_dlnk != NULL) {
290       /* 'p_task' is in the middle of list */
291       p_b->delta_time += p_task->delta_time;
292       p_task->p_dlnk->p_blnk = p_b;
293       p_task->p_dlnk = NULL;
294     }
295     else {
296       /* 'p_task' is at the end of list */
297       p_b->delta_time = 0;
298     }
299     p_task->p_blnk = NULL;
300   }
301 }
302
303
304 /*--------------------------- rt_psq_enq ------------------------------------*/
305
306 void rt_psq_enq (OS_ID entry, U32 arg) {
307   /* Insert post service request "entry" into ps-queue. */
308   U32 idx;
309
310   idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
311   if (idx < os_psq->size) {
312     os_psq->q[idx].id  = entry;
313     os_psq->q[idx].arg = arg;
314   }
315   else {
316     os_error (OS_ERR_FIFO_OVF);
317   }
318 }
319
320
321 /*----------------------------------------------------------------------------
322  * end of file
323  *---------------------------------------------------------------------------*/
324