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