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