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