]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/croutine.c
Replace the need for taskCHECK_READY_LIST() by instead making vListRemove() return...
[freertos] / FreeRTOS / Source / croutine.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 #include "croutine.h"\r
70 \r
71 /*\r
72  * Some kernel aware debuggers require data to be viewed to be global, rather\r
73  * than file scope.\r
74  */\r
75 #ifdef portREMOVE_STATIC_QUALIFIER\r
76         #define static\r
77 #endif\r
78 \r
79 \r
80 /* Lists for ready and blocked co-routines. --------------------*/\r
81 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ];  /*< Prioritised ready co-routines. */\r
82 static xList xDelayedCoRoutineList1;                                                                    /*< Delayed co-routines. */\r
83 static xList xDelayedCoRoutineList2;                                                                    /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */\r
84 static xList * pxDelayedCoRoutineList;                                                                  /*< Points to the delayed co-routine list currently being used. */\r
85 static xList * pxOverflowDelayedCoRoutineList;                                                  /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */\r
86 static xList xPendingReadyCoRoutineList;                                                                                        /*< Holds co-routines that have been readied by an external event.  They cannot be added directly to the ready lists as the ready lists cannot be accessed by interrupts. */\r
87 \r
88 /* Other file private variables. --------------------------------*/\r
89 corCRCB * pxCurrentCoRoutine = NULL;\r
90 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;\r
91 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;\r
92 \r
93 /* The initial state of the co-routine when it is created. */\r
94 #define corINITIAL_STATE        ( 0 )\r
95 \r
96 /*\r
97  * Place the co-routine represented by pxCRCB into the appropriate ready queue\r
98  * for the priority.  It is inserted at the end of the list.\r
99  *\r
100  * This macro accesses the co-routine ready lists and therefore must not be\r
101  * used from within an ISR.\r
102  */\r
103 #define prvAddCoRoutineToReadyQueue( pxCRCB )                                                                                                                                           \\r
104 {                                                                                                                                                                                                                                       \\r
105         if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority )                                                                                                                  \\r
106         {                                                                                                                                                                                                                               \\r
107                 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority;                                                                                                                       \\r
108         }                                                                                                                                                                                                                               \\r
109         vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) );  \\r
110 }       \r
111 \r
112 /*\r
113  * Utility to ready all the lists used by the scheduler.  This is called\r
114  * automatically upon the creation of the first co-routine.\r
115  */\r
116 static void prvInitialiseCoRoutineLists( void );\r
117 \r
118 /*\r
119  * Co-routines that are readied by an interrupt cannot be placed directly into\r
120  * the ready lists (there is no mutual exclusion).  Instead they are placed in\r
121  * in the pending ready list in order that they can later be moved to the ready\r
122  * list by the co-routine scheduler.\r
123  */\r
124 static void prvCheckPendingReadyList( void );\r
125 \r
126 /*\r
127  * Macro that looks at the list of co-routines that are currently delayed to\r
128  * see if any require waking.\r
129  *\r
130  * Co-routines are stored in the queue in the order of their wake time -\r
131  * meaning once one co-routine has been found whose timer has not expired\r
132  * we need not look any further down the list.\r
133  */\r
134 static void prvCheckDelayedList( void );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )\r
139 {\r
140 signed portBASE_TYPE xReturn;\r
141 corCRCB *pxCoRoutine;\r
142 \r
143         /* Allocate the memory that will store the co-routine control block. */\r
144         pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );\r
145         if( pxCoRoutine )\r
146         {\r
147                 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to\r
148                 be created and the co-routine data structures need initialising. */\r
149                 if( pxCurrentCoRoutine == NULL )\r
150                 {\r
151                         pxCurrentCoRoutine = pxCoRoutine;\r
152                         prvInitialiseCoRoutineLists();\r
153                 }\r
154 \r
155                 /* Check the priority is within limits. */\r
156                 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )\r
157                 {\r
158                         uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;\r
159                 }\r
160 \r
161                 /* Fill out the co-routine control block from the function parameters. */\r
162                 pxCoRoutine->uxState = corINITIAL_STATE;\r
163                 pxCoRoutine->uxPriority = uxPriority;\r
164                 pxCoRoutine->uxIndex = uxIndex;\r
165                 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;\r
166 \r
167                 /* Initialise all the other co-routine control block parameters. */\r
168                 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );\r
169                 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );\r
170 \r
171                 /* Set the co-routine control block as a link back from the xListItem.\r
172                 This is so we can get back to the containing CRCB from a generic item\r
173                 in a list. */\r
174                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );\r
175                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );\r
176         \r
177                 /* Event lists are always in priority order. */\r
178                 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );\r
179                 \r
180                 /* Now the co-routine has been initialised it can be added to the ready\r
181                 list at the correct priority. */\r
182                 prvAddCoRoutineToReadyQueue( pxCoRoutine );\r
183 \r
184                 xReturn = pdPASS;\r
185         }\r
186         else\r
187         {               \r
188                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
189         }\r
190         \r
191         return xReturn; \r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )\r
196 {\r
197 portTickType xTimeToWake;\r
198 \r
199         /* Calculate the time to wake - this may overflow but this is\r
200         not a problem. */\r
201         xTimeToWake = xCoRoutineTickCount + xTicksToDelay;\r
202 \r
203         /* We must remove ourselves from the ready list before adding\r
204         ourselves to the blocked list as the same list item is used for\r
205         both lists. */\r
206         uxListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
207 \r
208         /* The list item will be inserted in wake time order. */\r
209         listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );\r
210 \r
211         if( xTimeToWake < xCoRoutineTickCount )\r
212         {\r
213                 /* Wake time has overflowed.  Place this item in the\r
214                 overflow list. */\r
215                 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
216         }\r
217         else\r
218         {\r
219                 /* The wake time has not overflowed, so we can use the\r
220                 current block list. */\r
221                 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
222         }\r
223 \r
224         if( pxEventList )\r
225         {\r
226                 /* Also add the co-routine to an event list.  If this is done then the\r
227                 function must be called with interrupts disabled. */\r
228                 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvCheckPendingReadyList( void )\r
234 {\r
235         /* Are there any co-routines waiting to get moved to the ready list?  These\r
236         are co-routines that have been readied by an ISR.  The ISR cannot access\r
237         the     ready lists itself. */\r
238         while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )\r
239         {\r
240                 corCRCB *pxUnblockedCRCB;\r
241 \r
242                 /* The pending ready list can be accessed by an ISR. */\r
243                 portDISABLE_INTERRUPTS();\r
244                 {       \r
245                         pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );                   \r
246                         uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
247                 }\r
248                 portENABLE_INTERRUPTS();\r
249 \r
250                 uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );\r
251                 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB ); \r
252         }\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvCheckDelayedList( void )\r
257 {\r
258 corCRCB *pxCRCB;\r
259 \r
260         xPassedTicks = xTaskGetTickCount() - xLastTickCount;\r
261         while( xPassedTicks )\r
262         {\r
263                 xCoRoutineTickCount++;\r
264                 xPassedTicks--;\r
265 \r
266                 /* If the tick count has overflowed we need to swap the ready lists. */\r
267                 if( xCoRoutineTickCount == 0 )\r
268                 {\r
269                         xList * pxTemp;\r
270 \r
271                         /* Tick count has overflowed so we need to swap the delay lists.  If there are\r
272                         any items in pxDelayedCoRoutineList here then there is an error! */\r
273                         pxTemp = pxDelayedCoRoutineList;\r
274                         pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;\r
275                         pxOverflowDelayedCoRoutineList = pxTemp;\r
276                 }\r
277 \r
278                 /* See if this tick has made a timeout expire. */\r
279                 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )\r
280                 {\r
281                         pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );\r
282 \r
283                         if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )                            \r
284                         {                       \r
285                                 /* Timeout not yet expired. */                                                                                                                                                  \r
286                                 break;                                                                                                                                                          \r
287                         }                                                                                                                                                                               \r
288 \r
289                         portDISABLE_INTERRUPTS();\r
290                         {\r
291                                 /* The event could have occurred just before this critical\r
292                                 section.  If this is the case then the generic list item will\r
293                                 have been moved to the pending ready list and the following\r
294                                 line is still valid.  Also the pvContainer parameter will have\r
295                                 been set to NULL so the following lines are also valid. */\r
296                                 uxListRemove( &( pxCRCB->xGenericListItem ) );                                                                                  \r
297 \r
298                                 /* Is the co-routine waiting on an event also? */                                                                                               \r
299                                 if( pxCRCB->xEventListItem.pvContainer )                                                                                                        \r
300                                 {                                                                                                                       \r
301                                         uxListRemove( &( pxCRCB->xEventListItem ) );                                                                                    \r
302                                 }\r
303                         }\r
304                         portENABLE_INTERRUPTS();\r
305 \r
306                         prvAddCoRoutineToReadyQueue( pxCRCB );                                                                                                  \r
307                 }                                                                                                                                                                                                       \r
308         }\r
309 \r
310         xLastTickCount = xCoRoutineTickCount;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 void vCoRoutineSchedule( void )\r
315 {\r
316         /* See if any co-routines readied by events need moving to the ready lists. */\r
317         prvCheckPendingReadyList();\r
318 \r
319         /* See if any delayed co-routines have timed out. */\r
320         prvCheckDelayedList();\r
321 \r
322         /* Find the highest priority queue that contains ready co-routines. */\r
323         while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )\r
324         {\r
325                 if( uxTopCoRoutineReadyPriority == 0 )\r
326                 {\r
327                         /* No more co-routines to check. */\r
328                         return;\r
329                 }\r
330                 --uxTopCoRoutineReadyPriority;\r
331         }\r
332 \r
333         /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines\r
334          of the same priority get an equal share of the processor time. */\r
335         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );\r
336 \r
337         /* Call the co-routine. */\r
338         ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );\r
339 \r
340         return;\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 static void prvInitialiseCoRoutineLists( void )\r
345 {\r
346 unsigned portBASE_TYPE uxPriority;\r
347 \r
348         for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )\r
349         {\r
350                 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );\r
351         }\r
352 \r
353         vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );\r
354         vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );\r
355         vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );\r
356 \r
357         /* Start with pxDelayedCoRoutineList using list1 and the\r
358         pxOverflowDelayedCoRoutineList using list2. */\r
359         pxDelayedCoRoutineList = &xDelayedCoRoutineList1;\r
360         pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )\r
365 {\r
366 corCRCB *pxUnblockedCRCB;\r
367 signed portBASE_TYPE xReturn;\r
368 \r
369         /* This function is called from within an interrupt.  It can only access\r
370         event lists and the pending ready list.  This function assumes that a\r
371         check has already been made to ensure pxEventList is not empty. */\r
372         pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
373         uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
374         vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );\r
375 \r
376         if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )\r
377         {\r
378                 xReturn = pdTRUE;\r
379         }\r
380         else\r
381         {\r
382                 xReturn = pdFALSE;\r
383         }\r
384 \r
385         return xReturn;\r
386 }\r
387 \r