]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/croutine.c
Update version number in preparation for official V8.2.0 release.
[freertos] / FreeRTOS / Source / croutine.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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 /*\r
75  * Some kernel aware debuggers require data to be viewed to be global, rather\r
76  * than file scope.\r
77  */\r
78 #ifdef portREMOVE_STATIC_QUALIFIER\r
79         #define static\r
80 #endif\r
81 \r
82 \r
83 /* Lists for ready and blocked co-routines. --------------------*/\r
84 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */\r
85 static List_t xDelayedCoRoutineList1;                                                                   /*< Delayed co-routines. */\r
86 static List_t xDelayedCoRoutineList2;                                                                   /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */\r
87 static List_t * pxDelayedCoRoutineList;                                                                 /*< Points to the delayed co-routine list currently being used. */\r
88 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
89 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
90 \r
91 /* Other file private variables. --------------------------------*/\r
92 CRCB_t * pxCurrentCoRoutine = NULL;\r
93 static UBaseType_t uxTopCoRoutineReadyPriority = 0;\r
94 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;\r
95 \r
96 /* The initial state of the co-routine when it is created. */\r
97 #define corINITIAL_STATE        ( 0 )\r
98 \r
99 /*\r
100  * Place the co-routine represented by pxCRCB into the appropriate ready queue\r
101  * for the priority.  It is inserted at the end of the list.\r
102  *\r
103  * This macro accesses the co-routine ready lists and therefore must not be\r
104  * used from within an ISR.\r
105  */\r
106 #define prvAddCoRoutineToReadyQueue( pxCRCB )                                                                                                                                           \\r
107 {                                                                                                                                                                                                                                       \\r
108         if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority )                                                                                                                  \\r
109         {                                                                                                                                                                                                                               \\r
110                 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority;                                                                                                                       \\r
111         }                                                                                                                                                                                                                               \\r
112         vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \\r
113 }\r
114 \r
115 /*\r
116  * Utility to ready all the lists used by the scheduler.  This is called\r
117  * automatically upon the creation of the first co-routine.\r
118  */\r
119 static void prvInitialiseCoRoutineLists( void );\r
120 \r
121 /*\r
122  * Co-routines that are readied by an interrupt cannot be placed directly into\r
123  * the ready lists (there is no mutual exclusion).  Instead they are placed in\r
124  * in the pending ready list in order that they can later be moved to the ready\r
125  * list by the co-routine scheduler.\r
126  */\r
127 static void prvCheckPendingReadyList( void );\r
128 \r
129 /*\r
130  * Macro that looks at the list of co-routines that are currently delayed to\r
131  * see if any require waking.\r
132  *\r
133  * Co-routines are stored in the queue in the order of their wake time -\r
134  * meaning once one co-routine has been found whose timer has not expired\r
135  * we need not look any further down the list.\r
136  */\r
137 static void prvCheckDelayedList( void );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )\r
142 {\r
143 BaseType_t xReturn;\r
144 CRCB_t *pxCoRoutine;\r
145 \r
146         /* Allocate the memory that will store the co-routine control block. */\r
147         pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );\r
148         if( pxCoRoutine )\r
149         {\r
150                 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to\r
151                 be created and the co-routine data structures need initialising. */\r
152                 if( pxCurrentCoRoutine == NULL )\r
153                 {\r
154                         pxCurrentCoRoutine = pxCoRoutine;\r
155                         prvInitialiseCoRoutineLists();\r
156                 }\r
157 \r
158                 /* Check the priority is within limits. */\r
159                 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )\r
160                 {\r
161                         uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;\r
162                 }\r
163 \r
164                 /* Fill out the co-routine control block from the function parameters. */\r
165                 pxCoRoutine->uxState = corINITIAL_STATE;\r
166                 pxCoRoutine->uxPriority = uxPriority;\r
167                 pxCoRoutine->uxIndex = uxIndex;\r
168                 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;\r
169 \r
170                 /* Initialise all the other co-routine control block parameters. */\r
171                 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );\r
172                 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );\r
173 \r
174                 /* Set the co-routine control block as a link back from the ListItem_t.\r
175                 This is so we can get back to the containing CRCB from a generic item\r
176                 in a list. */\r
177                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );\r
178                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );\r
179 \r
180                 /* Event lists are always in priority order. */\r
181                 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );\r
182 \r
183                 /* Now the co-routine has been initialised it can be added to the ready\r
184                 list at the correct priority. */\r
185                 prvAddCoRoutineToReadyQueue( pxCoRoutine );\r
186 \r
187                 xReturn = pdPASS;\r
188         }\r
189         else\r
190         {\r
191                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
192         }\r
193 \r
194         return xReturn;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )\r
199 {\r
200 TickType_t xTimeToWake;\r
201 \r
202         /* Calculate the time to wake - this may overflow but this is\r
203         not a problem. */\r
204         xTimeToWake = xCoRoutineTickCount + xTicksToDelay;\r
205 \r
206         /* We must remove ourselves from the ready list before adding\r
207         ourselves to the blocked list as the same list item is used for\r
208         both lists. */\r
209         ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
210 \r
211         /* The list item will be inserted in wake time order. */\r
212         listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );\r
213 \r
214         if( xTimeToWake < xCoRoutineTickCount )\r
215         {\r
216                 /* Wake time has overflowed.  Place this item in the\r
217                 overflow list. */\r
218                 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
219         }\r
220         else\r
221         {\r
222                 /* The wake time has not overflowed, so we can use the\r
223                 current block list. */\r
224                 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );\r
225         }\r
226 \r
227         if( pxEventList )\r
228         {\r
229                 /* Also add the co-routine to an event list.  If this is done then the\r
230                 function must be called with interrupts disabled. */\r
231                 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 static void prvCheckPendingReadyList( void )\r
237 {\r
238         /* Are there any co-routines waiting to get moved to the ready list?  These\r
239         are co-routines that have been readied by an ISR.  The ISR cannot access\r
240         the     ready lists itself. */\r
241         while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )\r
242         {\r
243                 CRCB_t *pxUnblockedCRCB;\r
244 \r
245                 /* The pending ready list can be accessed by an ISR. */\r
246                 portDISABLE_INTERRUPTS();\r
247                 {\r
248                         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );\r
249                         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
250                 }\r
251                 portENABLE_INTERRUPTS();\r
252 \r
253                 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );\r
254                 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void prvCheckDelayedList( void )\r
260 {\r
261 CRCB_t *pxCRCB;\r
262 \r
263         xPassedTicks = xTaskGetTickCount() - xLastTickCount;\r
264         while( xPassedTicks )\r
265         {\r
266                 xCoRoutineTickCount++;\r
267                 xPassedTicks--;\r
268 \r
269                 /* If the tick count has overflowed we need to swap the ready lists. */\r
270                 if( xCoRoutineTickCount == 0 )\r
271                 {\r
272                         List_t * pxTemp;\r
273 \r
274                         /* Tick count has overflowed so we need to swap the delay lists.  If there are\r
275                         any items in pxDelayedCoRoutineList here then there is an error! */\r
276                         pxTemp = pxDelayedCoRoutineList;\r
277                         pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;\r
278                         pxOverflowDelayedCoRoutineList = pxTemp;\r
279                 }\r
280 \r
281                 /* See if this tick has made a timeout expire. */\r
282                 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )\r
283                 {\r
284                         pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );\r
285 \r
286                         if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )\r
287                         {\r
288                                 /* Timeout not yet expired. */\r
289                                 break;\r
290                         }\r
291 \r
292                         portDISABLE_INTERRUPTS();\r
293                         {\r
294                                 /* The event could have occurred just before this critical\r
295                                 section.  If this is the case then the generic list item will\r
296                                 have been moved to the pending ready list and the following\r
297                                 line is still valid.  Also the pvContainer parameter will have\r
298                                 been set to NULL so the following lines are also valid. */\r
299                                 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );\r
300 \r
301                                 /* Is the co-routine waiting on an event also? */\r
302                                 if( pxCRCB->xEventListItem.pvContainer )\r
303                                 {\r
304                                         ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );\r
305                                 }\r
306                         }\r
307                         portENABLE_INTERRUPTS();\r
308 \r
309                         prvAddCoRoutineToReadyQueue( pxCRCB );\r
310                 }\r
311         }\r
312 \r
313         xLastTickCount = xCoRoutineTickCount;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vCoRoutineSchedule( void )\r
318 {\r
319         /* See if any co-routines readied by events need moving to the ready lists. */\r
320         prvCheckPendingReadyList();\r
321 \r
322         /* See if any delayed co-routines have timed out. */\r
323         prvCheckDelayedList();\r
324 \r
325         /* Find the highest priority queue that contains ready co-routines. */\r
326         while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )\r
327         {\r
328                 if( uxTopCoRoutineReadyPriority == 0 )\r
329                 {\r
330                         /* No more co-routines to check. */\r
331                         return;\r
332                 }\r
333                 --uxTopCoRoutineReadyPriority;\r
334         }\r
335 \r
336         /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines\r
337          of the same priority get an equal share of the processor time. */\r
338         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );\r
339 \r
340         /* Call the co-routine. */\r
341         ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );\r
342 \r
343         return;\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 static void prvInitialiseCoRoutineLists( void )\r
348 {\r
349 UBaseType_t uxPriority;\r
350 \r
351         for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )\r
352         {\r
353                 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );\r
354         }\r
355 \r
356         vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );\r
357         vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );\r
358         vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );\r
359 \r
360         /* Start with pxDelayedCoRoutineList using list1 and the\r
361         pxOverflowDelayedCoRoutineList using list2. */\r
362         pxDelayedCoRoutineList = &xDelayedCoRoutineList1;\r
363         pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;\r
364 }\r
365 /*-----------------------------------------------------------*/\r
366 \r
367 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )\r
368 {\r
369 CRCB_t *pxUnblockedCRCB;\r
370 BaseType_t xReturn;\r
371 \r
372         /* This function is called from within an interrupt.  It can only access\r
373         event lists and the pending ready list.  This function assumes that a\r
374         check has already been made to ensure pxEventList is not empty. */\r
375         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
376         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );\r
377         vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );\r
378 \r
379         if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )\r
380         {\r
381                 xReturn = pdTRUE;\r
382         }\r
383         else\r
384         {\r
385                 xReturn = pdFALSE;\r
386         }\r
387 \r
388         return xReturn;\r
389 }\r
390 \r