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