2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\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
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
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
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 #include "FreeRTOS.h"
\r
50 #include "croutine.h"
\r
53 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
56 #ifdef portREMOVE_STATIC_QUALIFIER
\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
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
74 /* The initial state of the co-routine when it is created. */
\r
75 #define corINITIAL_STATE ( 0 )
\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
81 * This macro accesses the co-routine ready lists and therefore must not be
\r
82 * used from within an ISR.
\r
84 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
86 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
88 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
90 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\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
97 static void prvInitialiseCoRoutineLists( void );
\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
105 static void prvCheckPendingReadyList( void );
\r
108 * Macro that looks at the list of co-routines that are currently delayed to
\r
109 * see if any require waking.
\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
115 static void prvCheckDelayedList( void );
\r
117 /*-----------------------------------------------------------*/
\r
119 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
121 signed portBASE_TYPE xReturn;
\r
122 corCRCB *pxCoRoutine;
\r
124 /* Allocate the memory that will store the co-routine control block. */
\r
125 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\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
132 pxCurrentCoRoutine = pxCoRoutine;
\r
133 prvInitialiseCoRoutineLists();
\r
136 /* Check the priority is within limits. */
\r
137 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
139 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\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
148 /* Initialise all the other co-routine control block parameters. */
\r
149 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
150 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\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
155 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
156 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
158 /* Event lists are always in priority order. */
\r
159 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\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
169 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
174 /*-----------------------------------------------------------*/
\r
176 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
178 portTickType xTimeToWake;
\r
180 /* Calculate the time to wake - this may overflow but this is
\r
182 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\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
187 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
189 /* The list item will be inserted in wake time order. */
\r
190 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
192 if( xTimeToWake < xCoRoutineTickCount )
\r
194 /* Wake time has overflowed. Place this item in the
\r
196 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\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
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
212 /*-----------------------------------------------------------*/
\r
214 static void prvCheckPendingReadyList( void )
\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
221 corCRCB *pxUnblockedCRCB;
\r
223 /* The pending ready list can be accessed by an ISR. */
\r
224 portDISABLE_INTERRUPTS();
\r
226 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
227 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
229 portENABLE_INTERRUPTS();
\r
231 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
232 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
235 /*-----------------------------------------------------------*/
\r
237 static void prvCheckDelayedList( void )
\r
241 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
242 while( xPassedTicks )
\r
244 xCoRoutineTickCount++;
\r
247 /* If the tick count has overflowed we need to swap the ready lists. */
\r
248 if( xCoRoutineTickCount == 0 )
\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
259 /* See if this tick has made a timeout expire. */
\r
260 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
262 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
264 /* Timeout not yet expired. */
\r
268 portDISABLE_INTERRUPTS();
\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
277 /* Is the co-routine waiting on an event also? */
\r
278 if( pxCRCB->xEventListItem.pvContainer )
\r
280 vListRemove( &( pxCRCB->xEventListItem ) );
\r
283 portENABLE_INTERRUPTS();
\r
285 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
289 xLastTickCount = xCoRoutineTickCount;
\r
291 /*-----------------------------------------------------------*/
\r
293 void vCoRoutineSchedule( void )
\r
295 /* See if any co-routines readied by events need moving to the ready lists. */
\r
296 prvCheckPendingReadyList();
\r
298 /* See if any delayed co-routines have timed out. */
\r
299 prvCheckDelayedList();
\r
301 /* Find the highest priority queue that contains ready co-routines. */
\r
302 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
304 if( uxTopCoRoutineReadyPriority == 0 )
\r
306 /* No more co-routines to check. */
\r
309 --uxTopCoRoutineReadyPriority;
\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
316 /* Call the co-routine. */
\r
317 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
321 /*-----------------------------------------------------------*/
\r
323 static void prvInitialiseCoRoutineLists( void )
\r
325 unsigned portBASE_TYPE uxPriority;
\r
327 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
329 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
332 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
333 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
334 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
336 /* Start with pxDelayedCoRoutineList using list1 and the
\r
337 pxOverflowDelayedCoRoutineList using list2. */
\r
338 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
339 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
341 /*-----------------------------------------------------------*/
\r
343 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
345 corCRCB *pxUnblockedCRCB;
\r
346 signed portBASE_TYPE xReturn;
\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
354 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r