]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/croutine.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Source / croutine.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 #include "FreeRTOS.h"\r
29 #include "task.h"\r
30 #include "croutine.h"\r
31 \r
32 /* Remove the whole file is co-routines are not being used. */\r
33 #if( configUSE_CO_ROUTINES != 0 )\r
34 \r
35 /*\r
36  * Some kernel aware debuggers require data to be viewed to be global, rather\r
37  * than file scope.\r
38  */\r
39 #ifdef portREMOVE_STATIC_QUALIFIER\r
40         #define static\r
41 #endif\r
42 \r
43 \r
44 /* Lists for ready and blocked co-routines. --------------------*/\r
45 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */\r
46 static List_t xDelayedCoRoutineList1;                                                                   /*< Delayed co-routines. */\r
47 static List_t xDelayedCoRoutineList2;                                                                   /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */\r
48 static List_t * pxDelayedCoRoutineList;                                                                 /*< Points to the delayed co-routine list currently being used. */\r
49 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
50 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
51 \r
52 /* Other file private variables. --------------------------------*/\r
53 CRCB_t * pxCurrentCoRoutine = NULL;\r
54 static UBaseType_t uxTopCoRoutineReadyPriority = 0;\r
55 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;\r
56 \r
57 /* The initial state of the co-routine when it is created. */\r
58 #define corINITIAL_STATE        ( 0 )\r
59 \r
60 /*\r
61  * Place the co-routine represented by pxCRCB into the appropriate ready queue\r
62  * for the priority.  It is inserted at the end of the list.\r
63  *\r
64  * This macro accesses the co-routine ready lists and therefore must not be\r
65  * used from within an ISR.\r
66  */\r
67 #define prvAddCoRoutineToReadyQueue( pxCRCB )                                                                                                                                           \\r
68 {                                                                                                                                                                                                                                       \\r
69         if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority )                                                                                                                  \\r
70         {                                                                                                                                                                                                                               \\r
71                 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority;                                                                                                                       \\r
72         }                                                                                                                                                                                                                               \\r
73         vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \\r
74 }\r
75 \r
76 /*\r
77  * Utility to ready all the lists used by the scheduler.  This is called\r
78  * automatically upon the creation of the first co-routine.\r
79  */\r
80 static void prvInitialiseCoRoutineLists( void );\r
81 \r
82 /*\r
83  * Co-routines that are readied by an interrupt cannot be placed directly into\r
84  * the ready lists (there is no mutual exclusion).  Instead they are placed in\r
85  * in the pending ready list in order that they can later be moved to the ready\r
86  * list by the co-routine scheduler.\r
87  */\r
88 static void prvCheckPendingReadyList( void );\r
89 \r
90 /*\r
91  * Macro that looks at the list of co-routines that are currently delayed to\r
92  * see if any require waking.\r
93  *\r
94  * Co-routines are stored in the queue in the order of their wake time -\r
95  * meaning once one co-routine has been found whose timer has not expired\r
96  * we need not look any further down the list.\r
97  */\r
98 static void prvCheckDelayedList( void );\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )\r
103 {\r
104 BaseType_t xReturn;\r
105 CRCB_t *pxCoRoutine;\r
106 \r
107         /* Allocate the memory that will store the co-routine control block. */\r
108         pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );\r
109         if( pxCoRoutine )\r
110         {\r
111                 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to\r
112                 be created and the co-routine data structures need initialising. */\r
113                 if( pxCurrentCoRoutine == NULL )\r
114                 {\r
115                         pxCurrentCoRoutine = pxCoRoutine;\r
116                         prvInitialiseCoRoutineLists();\r
117                 }\r
118 \r
119                 /* Check the priority is within limits. */\r
120                 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )\r
121                 {\r
122                         uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;\r
123                 }\r
124 \r
125                 /* Fill out the co-routine control block from the function parameters. */\r
126                 pxCoRoutine->uxState = corINITIAL_STATE;\r
127                 pxCoRoutine->uxPriority = uxPriority;\r
128                 pxCoRoutine->uxIndex = uxIndex;\r
129                 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;\r
130 \r
131                 /* Initialise all the other co-routine control block parameters. */\r
132                 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );\r
133                 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );\r
134 \r
135                 /* Set the co-routine control block as a link back from the ListItem_t.\r
136                 This is so we can get back to the containing CRCB from a generic item\r
137                 in a list. */\r
138                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );\r
139                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );\r
140 \r
141                 /* Event lists are always in priority order. */\r
142                 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );\r
143 \r
144                 /* Now the co-routine has been initialised it can be added to the ready\r
145                 list at the correct priority. */\r
146                 prvAddCoRoutineToReadyQueue( pxCoRoutine );\r
147 \r
148                 xReturn = pdPASS;\r
149         }\r
150         else\r
151         {\r
152                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
153         }\r
154 \r
155         return xReturn;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )\r
160 {\r
161 TickType_t xTimeToWake;\r
162 \r
163         /* Calculate the time to wake - this may overflow but this is\r
164         not a problem. */\r
165         xTimeToWake = xCoRoutineTickCount + xTicksToDelay;\r
166 \r
167         /* We must remove ourselves from the ready list before adding\r
168         ourselves to the blocked list as the same list item is used for\r
169         both lists. */\r
170         ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
171 \r
172         /* The list item will be inserted in wake time order. */\r
173         listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );\r
174 \r
175         if( xTimeToWake < xCoRoutineTickCount )\r
176         {\r
177                 /* Wake time has overflowed.  Place this item in the\r
178                 overflow list. */\r
179                 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
180         }\r
181         else\r
182         {\r
183                 /* The wake time has not overflowed, so we can use the\r
184                 current block list. */\r
185                 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
186         }\r
187 \r
188         if( pxEventList )\r
189         {\r
190                 /* Also add the co-routine to an event list.  If this is done then the\r
191                 function must be called with interrupts disabled. */\r
192                 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvCheckPendingReadyList( void )\r
198 {\r
199         /* Are there any co-routines waiting to get moved to the ready list?  These\r
200         are co-routines that have been readied by an ISR.  The ISR cannot access\r
201         the     ready lists itself. */\r
202         while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )\r
203         {\r
204                 CRCB_t *pxUnblockedCRCB;\r
205 \r
206                 /* The pending ready list can be accessed by an ISR. */\r
207                 portDISABLE_INTERRUPTS();\r
208                 {\r
209                         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );\r
210                         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
211                 }\r
212                 portENABLE_INTERRUPTS();\r
213 \r
214                 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );\r
215                 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void prvCheckDelayedList( void )\r
221 {\r
222 CRCB_t *pxCRCB;\r
223 \r
224         xPassedTicks = xTaskGetTickCount() - xLastTickCount;\r
225         while( xPassedTicks )\r
226         {\r
227                 xCoRoutineTickCount++;\r
228                 xPassedTicks--;\r
229 \r
230                 /* If the tick count has overflowed we need to swap the ready lists. */\r
231                 if( xCoRoutineTickCount == 0 )\r
232                 {\r
233                         List_t * pxTemp;\r
234 \r
235                         /* Tick count has overflowed so we need to swap the delay lists.  If there are\r
236                         any items in pxDelayedCoRoutineList here then there is an error! */\r
237                         pxTemp = pxDelayedCoRoutineList;\r
238                         pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;\r
239                         pxOverflowDelayedCoRoutineList = pxTemp;\r
240                 }\r
241 \r
242                 /* See if this tick has made a timeout expire. */\r
243                 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )\r
244                 {\r
245                         pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );\r
246 \r
247                         if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )\r
248                         {\r
249                                 /* Timeout not yet expired. */\r
250                                 break;\r
251                         }\r
252 \r
253                         portDISABLE_INTERRUPTS();\r
254                         {\r
255                                 /* The event could have occurred just before this critical\r
256                                 section.  If this is the case then the generic list item will\r
257                                 have been moved to the pending ready list and the following\r
258                                 line is still valid.  Also the pvContainer parameter will have\r
259                                 been set to NULL so the following lines are also valid. */\r
260                                 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );\r
261 \r
262                                 /* Is the co-routine waiting on an event also? */\r
263                                 if( pxCRCB->xEventListItem.pvContainer )\r
264                                 {\r
265                                         ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );\r
266                                 }\r
267                         }\r
268                         portENABLE_INTERRUPTS();\r
269 \r
270                         prvAddCoRoutineToReadyQueue( pxCRCB );\r
271                 }\r
272         }\r
273 \r
274         xLastTickCount = xCoRoutineTickCount;\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vCoRoutineSchedule( void )\r
279 {\r
280         /* See if any co-routines readied by events need moving to the ready lists. */\r
281         prvCheckPendingReadyList();\r
282 \r
283         /* See if any delayed co-routines have timed out. */\r
284         prvCheckDelayedList();\r
285 \r
286         /* Find the highest priority queue that contains ready co-routines. */\r
287         while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )\r
288         {\r
289                 if( uxTopCoRoutineReadyPriority == 0 )\r
290                 {\r
291                         /* No more co-routines to check. */\r
292                         return;\r
293                 }\r
294                 --uxTopCoRoutineReadyPriority;\r
295         }\r
296 \r
297         /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines\r
298          of the same priority get an equal share of the processor time. */\r
299         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );\r
300 \r
301         /* Call the co-routine. */\r
302         ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );\r
303 \r
304         return;\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static void prvInitialiseCoRoutineLists( void )\r
309 {\r
310 UBaseType_t uxPriority;\r
311 \r
312         for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )\r
313         {\r
314                 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );\r
315         }\r
316 \r
317         vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );\r
318         vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );\r
319         vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );\r
320 \r
321         /* Start with pxDelayedCoRoutineList using list1 and the\r
322         pxOverflowDelayedCoRoutineList using list2. */\r
323         pxDelayedCoRoutineList = &xDelayedCoRoutineList1;\r
324         pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )\r
329 {\r
330 CRCB_t *pxUnblockedCRCB;\r
331 BaseType_t xReturn;\r
332 \r
333         /* This function is called from within an interrupt.  It can only access\r
334         event lists and the pending ready list.  This function assumes that a\r
335         check has already been made to ensure pxEventList is not empty. */\r
336         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
337         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
338         vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );\r
339 \r
340         if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )\r
341         {\r
342                 xReturn = pdTRUE;\r
343         }\r
344         else\r
345         {\r
346                 xReturn = pdFALSE;\r
347         }\r
348 \r
349         return xReturn;\r
350 }\r
351 \r
352 #endif /* configUSE_CO_ROUTINES == 0 */\r
353 \r