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