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