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