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