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