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