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