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