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