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