FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Source / croutine.c
blob7a760a6fc82e1e28a4ace7c60fa72dc9b344a26f
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
54 #include "FreeRTOS.h"
55 #include "task.h"
56 #include "croutine.h"
59 * Some kernel aware debuggers require data to be viewed to be global, rather
60 * than file scope.
62 #ifdef portREMOVE_STATIC_QUALIFIER
63 #define static
64 #endif
67 /* Lists for ready and blocked co-routines. --------------------*/
68 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
69 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
70 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
71 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
72 static xList * pxOverflowDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
73 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. */
75 /* Other file private variables. --------------------------------*/
76 corCRCB * pxCurrentCoRoutine = NULL;
77 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
78 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
80 /* The initial state of the co-routine when it is created. */
81 #define corINITIAL_STATE ( 0 )
84 * Place the co-routine represented by pxCRCB into the appropriate ready queue
85 * for the priority. It is inserted at the end of the list.
87 * This macro accesses the co-routine ready lists and therefore must not be
88 * used from within an ISR.
90 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
91 { \
92 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
93 { \
94 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
95 } \
96 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
100 * Utility to ready all the lists used by the scheduler. This is called
101 * automatically upon the creation of the first co-routine.
103 static void prvInitialiseCoRoutineLists( void );
106 * Co-routines that are readied by an interrupt cannot be placed directly into
107 * the ready lists (there is no mutual exclusion). Instead they are placed in
108 * in the pending ready list in order that they can later be moved to the ready
109 * list by the co-routine scheduler.
111 static void prvCheckPendingReadyList( void );
114 * Macro that looks at the list of co-routines that are currently delayed to
115 * see if any require waking.
117 * Co-routines are stored in the queue in the order of their wake time -
118 * meaning once one co-routine has been found whose timer has not expired
119 * we need not look any further down the list.
121 static void prvCheckDelayedList( void );
123 /*-----------------------------------------------------------*/
125 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
127 signed portBASE_TYPE xReturn;
128 corCRCB *pxCoRoutine;
130 /* Allocate the memory that will store the co-routine control block. */
131 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
132 if( pxCoRoutine )
134 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
135 be created and the co-routine data structures need initialising. */
136 if( pxCurrentCoRoutine == NULL )
138 pxCurrentCoRoutine = pxCoRoutine;
139 prvInitialiseCoRoutineLists();
142 /* Check the priority is within limits. */
143 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
145 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
148 /* Fill out the co-routine control block from the function parameters. */
149 pxCoRoutine->uxState = corINITIAL_STATE;
150 pxCoRoutine->uxPriority = uxPriority;
151 pxCoRoutine->uxIndex = uxIndex;
152 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
154 /* Initialise all the other co-routine control block parameters. */
155 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
156 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
158 /* Set the co-routine control block as a link back from the xListItem.
159 This is so we can get back to the containing CRCB from a generic item
160 in a list. */
161 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
162 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
164 /* Event lists are always in priority order. */
165 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
167 /* Now the co-routine has been initialised it can be added to the ready
168 list at the correct priority. */
169 prvAddCoRoutineToReadyQueue( pxCoRoutine );
171 xReturn = pdPASS;
173 else
175 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
178 return xReturn;
180 /*-----------------------------------------------------------*/
182 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
184 portTickType xTimeToWake;
186 /* Calculate the time to wake - this may overflow but this is
187 not a problem. */
188 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
190 /* We must remove ourselves from the ready list before adding
191 ourselves to the blocked list as the same list item is used for
192 both lists. */
193 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
195 /* The list item will be inserted in wake time order. */
196 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
198 if( xTimeToWake < xCoRoutineTickCount )
200 /* Wake time has overflowed. Place this item in the
201 overflow list. */
202 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
204 else
206 /* The wake time has not overflowed, so we can use the
207 current block list. */
208 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
211 if( pxEventList )
213 /* Also add the co-routine to an event list. If this is done then the
214 function must be called with interrupts disabled. */
215 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
218 /*-----------------------------------------------------------*/
220 static void prvCheckPendingReadyList( void )
222 /* Are there any co-routines waiting to get moved to the ready list? These
223 are co-routines that have been readied by an ISR. The ISR cannot access
224 the ready lists itself. */
225 while( !listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) )
227 corCRCB *pxUnblockedCRCB;
229 /* The pending ready list can be accessed by an ISR. */
230 portDISABLE_INTERRUPTS();
232 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
233 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
235 portENABLE_INTERRUPTS();
237 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
238 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
241 /*-----------------------------------------------------------*/
243 static void prvCheckDelayedList( void )
245 corCRCB *pxCRCB;
247 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
248 while( xPassedTicks )
250 xCoRoutineTickCount++;
251 xPassedTicks--;
253 /* If the tick count has overflowed we need to swap the ready lists. */
254 if( xCoRoutineTickCount == 0 )
256 xList * pxTemp;
258 /* Tick count has overflowed so we need to swap the delay lists. If there are
259 any items in pxDelayedCoRoutineList here then there is an error! */
260 pxTemp = pxDelayedCoRoutineList;
261 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
262 pxOverflowDelayedCoRoutineList = pxTemp;
265 /* See if this tick has made a timeout expire. */
266 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
268 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
270 /* Timeout not yet expired. */
271 break;
274 portDISABLE_INTERRUPTS();
276 /* The event could have occurred just before this critical
277 section. If this is the case then the generic list item will
278 have been moved to the pending ready list and the following
279 line is still valid. Also the pvContainer parameter will have
280 been set to NULL so the following lines are also valid. */
281 vListRemove( &( pxCRCB->xGenericListItem ) );
283 /* Is the co-routine waiting on an event also? */
284 if( pxCRCB->xEventListItem.pvContainer )
286 vListRemove( &( pxCRCB->xEventListItem ) );
289 portENABLE_INTERRUPTS();
291 prvAddCoRoutineToReadyQueue( pxCRCB );
295 xLastTickCount = xCoRoutineTickCount;
297 /*-----------------------------------------------------------*/
299 void vCoRoutineSchedule( void )
301 /* See if any co-routines readied by events need moving to the ready lists. */
302 prvCheckPendingReadyList();
304 /* See if any delayed co-routines have timed out. */
305 prvCheckDelayedList();
307 /* Find the highest priority queue that contains ready co-routines. */
308 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
310 if( uxTopCoRoutineReadyPriority == 0 )
312 /* No more co-routines to check. */
313 return;
315 --uxTopCoRoutineReadyPriority;
318 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
319 of the same priority get an equal share of the processor time. */
320 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
322 /* Call the co-routine. */
323 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
325 return;
327 /*-----------------------------------------------------------*/
329 static void prvInitialiseCoRoutineLists( void )
331 unsigned portBASE_TYPE uxPriority;
333 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
335 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
338 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
339 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
340 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
342 /* Start with pxDelayedCoRoutineList using list1 and the
343 pxOverflowDelayedCoRoutineList using list2. */
344 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
345 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
347 /*-----------------------------------------------------------*/
349 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
351 corCRCB *pxUnblockedCRCB;
352 signed portBASE_TYPE xReturn;
354 /* This function is called from within an interrupt. It can only access
355 event lists and the pending ready list. */
356 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
357 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
358 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
360 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
362 xReturn = pdTRUE;
364 else
366 xReturn = pdFALSE;
369 return xReturn;