2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\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 * 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
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
49 #include "FreeRTOS.h"
\r
51 #include "croutine.h"
\r
54 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
57 #ifdef portREMOVE_STATIC_QUALIFIER
\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
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
75 /* The initial state of the co-routine when it is created. */
\r
76 #define corINITIAL_STATE ( 0 )
\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
82 * This macro accesses the co-routine ready lists and therefore must not be
\r
83 * used from within an ISR.
\r
85 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
87 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
89 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
91 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\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
98 static void prvInitialiseCoRoutineLists( void );
\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
106 static void prvCheckPendingReadyList( void );
\r
109 * Macro that looks at the list of co-routines that are currently delayed to
\r
110 * see if any require waking.
\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
116 static void prvCheckDelayedList( void );
\r
118 /*-----------------------------------------------------------*/
\r
120 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
122 signed portBASE_TYPE xReturn;
\r
123 corCRCB *pxCoRoutine;
\r
125 /* Allocate the memory that will store the co-routine control block. */
\r
126 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\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
133 pxCurrentCoRoutine = pxCoRoutine;
\r
134 prvInitialiseCoRoutineLists();
\r
137 /* Check the priority is within limits. */
\r
138 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
140 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\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
149 /* Initialise all the other co-routine control block parameters. */
\r
150 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
151 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\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
156 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
157 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
159 /* Event lists are always in priority order. */
\r
160 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\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
170 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
175 /*-----------------------------------------------------------*/
\r
177 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
179 portTickType xTimeToWake;
\r
181 /* Calculate the time to wake - this may overflow but this is
\r
183 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\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
188 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
190 /* The list item will be inserted in wake time order. */
\r
191 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
193 if( xTimeToWake < xCoRoutineTickCount )
\r
195 /* Wake time has overflowed. Place this item in the
\r
197 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\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
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
213 /*-----------------------------------------------------------*/
\r
215 static void prvCheckPendingReadyList( void )
\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
222 corCRCB *pxUnblockedCRCB;
\r
224 /* The pending ready list can be accessed by an ISR. */
\r
225 portDISABLE_INTERRUPTS();
\r
227 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
228 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
230 portENABLE_INTERRUPTS();
\r
232 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
233 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
236 /*-----------------------------------------------------------*/
\r
238 static void prvCheckDelayedList( void )
\r
242 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
243 while( xPassedTicks )
\r
245 xCoRoutineTickCount++;
\r
248 /* If the tick count has overflowed we need to swap the ready lists. */
\r
249 if( xCoRoutineTickCount == 0 )
\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
260 /* See if this tick has made a timeout expire. */
\r
261 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
263 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
265 /* Timeout not yet expired. */
\r
269 portDISABLE_INTERRUPTS();
\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
278 /* Is the co-routine waiting on an event also? */
\r
279 if( pxCRCB->xEventListItem.pvContainer )
\r
281 vListRemove( &( pxCRCB->xEventListItem ) );
\r
284 portENABLE_INTERRUPTS();
\r
286 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
290 xLastTickCount = xCoRoutineTickCount;
\r
292 /*-----------------------------------------------------------*/
\r
294 void vCoRoutineSchedule( void )
\r
296 /* See if any co-routines readied by events need moving to the ready lists. */
\r
297 prvCheckPendingReadyList();
\r
299 /* See if any delayed co-routines have timed out. */
\r
300 prvCheckDelayedList();
\r
302 /* Find the highest priority queue that contains ready co-routines. */
\r
303 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
305 if( uxTopCoRoutineReadyPriority == 0 )
\r
307 /* No more co-routines to check. */
\r
310 --uxTopCoRoutineReadyPriority;
\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
317 /* Call the co-routine. */
\r
318 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvInitialiseCoRoutineLists( void )
\r
326 unsigned portBASE_TYPE uxPriority;
\r
328 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
330 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
333 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
334 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
335 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
337 /* Start with pxDelayedCoRoutineList using list1 and the
\r
338 pxOverflowDelayedCoRoutineList using list2. */
\r
339 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
340 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
342 /*-----------------------------------------------------------*/
\r
344 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
346 corCRCB *pxUnblockedCRCB;
\r
347 signed portBASE_TYPE xReturn;
\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
355 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r