2 FreeRTOS.org V4.5.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
36 #include "FreeRTOS.h"
\r
38 #include "croutine.h"
\r
40 /* Lists for ready and blocked co-routines. --------------------*/
\r
41 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
42 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
43 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
44 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
45 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
46 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
48 /* Other file private variables. --------------------------------*/
\r
49 corCRCB * pxCurrentCoRoutine = NULL;
\r
50 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
51 static portTickType xCoRoutineTickCount = 0;
\r
53 /* The initial state of the co-routine when it is created. */
\r
54 #define corINITIAL_STATE ( 0 )
\r
57 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
58 * for the priority. It is inserted at the end of the list.
\r
60 * This macro accesses the co-routine ready lists and therefore must not be
\r
61 * used from within an ISR.
\r
63 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
65 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
67 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
69 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
73 * Utility to ready all the lists used by the scheduler. This is called
\r
74 * automatically upon the creation of the first co-routine.
\r
76 static void prvInitialiseCoRoutineLists( void );
\r
79 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
80 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
81 * in the pending ready list in order that they can later be moved to the ready
\r
82 * list by the co-routine scheduler.
\r
84 static inline void prvCheckPendingReadyList( void );
\r
87 * Macro that looks at the list of co-routines that are currently delayed to
\r
88 * see if any require waking.
\r
90 * Co-routines are stored in the queue in the order of their wake time -
\r
91 * meaning once one co-routine has been found whose timer has not expired
\r
92 * we need not look any further down the list.
\r
94 static inline void prvCheckDelayedList( void );
\r
96 /*-----------------------------------------------------------*/
\r
98 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
100 signed portBASE_TYPE xReturn;
\r
101 corCRCB *pxCoRoutine;
\r
103 /* Allocate the memory that will store the co-routine control block. */
\r
104 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
107 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
108 be created and the co-routine data structures need initialising. */
\r
109 if( pxCurrentCoRoutine == NULL )
\r
111 pxCurrentCoRoutine = pxCoRoutine;
\r
112 prvInitialiseCoRoutineLists();
\r
115 /* Check the priority is within limits. */
\r
116 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
118 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
121 /* Fill out the co-routine control block from the function parameters. */
\r
122 pxCoRoutine->uxState = corINITIAL_STATE;
\r
123 pxCoRoutine->uxPriority = uxPriority;
\r
124 pxCoRoutine->uxIndex = uxIndex;
\r
125 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
127 /* Initialise all the other co-routine control block parameters. */
\r
128 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
129 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
131 /* Set the co-routine control block as a link back from the xListItem.
\r
132 This is so we can get back to the containing CRCB from a generic item
\r
134 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
135 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
137 /* Event lists are always in priority order. */
\r
138 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
140 /* Now the co-routine has been initialised it can be added to the ready
\r
141 list at the correct priority. */
\r
142 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
148 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
153 /*-----------------------------------------------------------*/
\r
155 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
157 portTickType xTimeToWake;
\r
159 /* Calculate the time to wake - this may overflow but this is
\r
161 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
163 /* We must remove ourselves from the ready list before adding
\r
164 ourselves to the blocked list as the same list item is used for
\r
166 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
168 /* The list item will be inserted in wake time order. */
\r
169 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
171 if( xTimeToWake < xCoRoutineTickCount )
\r
173 /* Wake time has overflowed. Place this item in the
\r
175 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
179 /* The wake time has not overflowed, so we can use the
\r
180 current block list. */
\r
181 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
186 /* Also add the co-routine to an event list. If this is done then the
\r
187 function must be called with interrupts disabled. */
\r
188 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
191 /*-----------------------------------------------------------*/
\r
193 static inline void prvCheckPendingReadyList( void )
\r
195 /* Are there any co-routines waiting to get moved to the ready list? These
\r
196 are co-routines that have been readied by an ISR. The ISR cannot access
\r
197 the ready lists itself. */
\r
198 while( !listLIST_IS_EMPTY( &xPendingReadyList ) )
\r
200 corCRCB *pxUnblockedCRCB;
\r
202 /* The pending ready list can be accessed by an ISR. */
\r
203 portDISABLE_INTERRUPTS();
\r
205 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyList) );
\r
206 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
208 portENABLE_INTERRUPTS();
\r
210 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
211 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
214 /*-----------------------------------------------------------*/
\r
216 static inline void prvCheckDelayedList( void )
\r
218 static portTickType xLastTickCount, xPassedTicks;
\r
221 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
222 while( xPassedTicks )
\r
224 xCoRoutineTickCount++;
\r
227 /* If the tick count has overflowed we need to swap the ready lists. */
\r
228 if( xCoRoutineTickCount == 0 )
\r
232 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
233 any items in pxDelayedCoRoutineList here then there is an error! */
\r
234 pxTemp = pxDelayedCoRoutineList;
\r
235 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
236 pxOverflowDelayedCoRoutineList = pxTemp;
\r
239 /* See if this tick has made a timeout expire. */
\r
240 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
242 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
244 /* Timeout not yet expired. */
\r
248 portDISABLE_INTERRUPTS();
\r
250 /* The event could have occurred just before this critical
\r
251 section. If this is the case then the generic list item will
\r
252 have been moved to the pending ready list and the following
\r
253 line is still valid. Also the pvContainer parameter will have
\r
254 been set to NULL so the following lines are also valid. */
\r
255 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
257 /* Is the co-routine waiting on an event also? */
\r
258 if( pxCRCB->xEventListItem.pvContainer )
\r
260 vListRemove( &( pxCRCB->xEventListItem ) );
\r
263 portENABLE_INTERRUPTS();
\r
265 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
269 xLastTickCount = xCoRoutineTickCount;
\r
271 /*-----------------------------------------------------------*/
\r
273 void vCoRoutineSchedule( void )
\r
275 /* See if any co-routines readied by events need moving to the ready lists. */
\r
276 prvCheckPendingReadyList();
\r
278 /* See if any delayed co-routines have timed out. */
\r
279 prvCheckDelayedList();
\r
281 /* Find the highest priority queue that contains ready co-routines. */
\r
282 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
284 if( uxTopCoRoutineReadyPriority == 0 )
\r
286 /* No more co-routines to check. */
\r
289 --uxTopCoRoutineReadyPriority;
\r
292 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
293 of the same priority get an equal share of the processor time. */
\r
294 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
296 /* Call the co-routine. */
\r
297 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
301 /*-----------------------------------------------------------*/
\r
303 static void prvInitialiseCoRoutineLists( void )
\r
305 unsigned portBASE_TYPE uxPriority;
\r
307 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
309 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
312 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
313 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
314 vListInitialise( ( xList * ) &xPendingReadyList );
\r
316 /* Start with pxDelayedCoRoutineList using list1 and the
\r
317 pxOverflowDelayedCoRoutineList using list2. */
\r
318 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
319 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
321 /*-----------------------------------------------------------*/
\r
323 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
325 corCRCB *pxUnblockedCRCB;
\r
326 signed portBASE_TYPE xReturn;
\r
328 /* This function is called from within an interrupt. It can only access
\r
329 event lists and the pending ready list. */
\r
330 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
331 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
332 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
334 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r