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