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