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