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