2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 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 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
50 #include "FreeRTOS.h"
\r
52 #include "croutine.h"
\r
54 /* Lists for ready and blocked co-routines. --------------------*/
\r
55 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
56 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
57 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
58 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
59 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
60 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
62 /* Other file private variables. --------------------------------*/
\r
63 corCRCB * pxCurrentCoRoutine = NULL;
\r
64 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
65 static portTickType xCoRoutineTickCount = 0;
\r
67 /* The initial state of the co-routine when it is created. */
\r
68 #define corINITIAL_STATE ( 0 )
\r
71 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
72 * for the priority. It is inserted at the end of the list.
\r
74 * This macro accesses the co-routine ready lists and therefore must not be
\r
75 * used from within an ISR.
\r
77 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
79 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
81 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
83 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
87 * Utility to ready all the lists used by the scheduler. This is called
\r
88 * automatically upon the creation of the first co-routine.
\r
90 static void prvInitialiseCoRoutineLists( void );
\r
93 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
94 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
95 * in the pending ready list in order that they can later be moved to the ready
\r
96 * list by the co-routine scheduler.
\r
98 static void prvCheckPendingReadyList( void );
\r
101 * Macro that looks at the list of co-routines that are currently delayed to
\r
102 * see if any require waking.
\r
104 * Co-routines are stored in the queue in the order of their wake time -
\r
105 * meaning once one co-routine has been found whose timer has not expired
\r
106 * we need not look any further down the list.
\r
108 static void prvCheckDelayedList( void );
\r
110 /*-----------------------------------------------------------*/
\r
112 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
114 signed portBASE_TYPE xReturn;
\r
115 corCRCB *pxCoRoutine;
\r
117 /* Allocate the memory that will store the co-routine control block. */
\r
118 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
121 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
122 be created and the co-routine data structures need initialising. */
\r
123 if( pxCurrentCoRoutine == NULL )
\r
125 pxCurrentCoRoutine = pxCoRoutine;
\r
126 prvInitialiseCoRoutineLists();
\r
129 /* Check the priority is within limits. */
\r
130 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
132 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
135 /* Fill out the co-routine control block from the function parameters. */
\r
136 pxCoRoutine->uxState = corINITIAL_STATE;
\r
137 pxCoRoutine->uxPriority = uxPriority;
\r
138 pxCoRoutine->uxIndex = uxIndex;
\r
139 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
141 /* Initialise all the other co-routine control block parameters. */
\r
142 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
143 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
145 /* Set the co-routine control block as a link back from the xListItem.
\r
146 This is so we can get back to the containing CRCB from a generic item
\r
148 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
149 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
151 /* Event lists are always in priority order. */
\r
152 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
154 /* Now the co-routine has been initialised it can be added to the ready
\r
155 list at the correct priority. */
\r
156 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
162 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
167 /*-----------------------------------------------------------*/
\r
169 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
171 portTickType xTimeToWake;
\r
173 /* Calculate the time to wake - this may overflow but this is
\r
175 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
177 /* We must remove ourselves from the ready list before adding
\r
178 ourselves to the blocked list as the same list item is used for
\r
180 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
182 /* The list item will be inserted in wake time order. */
\r
183 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
185 if( xTimeToWake < xCoRoutineTickCount )
\r
187 /* Wake time has overflowed. Place this item in the
\r
189 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
193 /* The wake time has not overflowed, so we can use the
\r
194 current block list. */
\r
195 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
200 /* Also add the co-routine to an event list. If this is done then the
\r
201 function must be called with interrupts disabled. */
\r
202 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvCheckPendingReadyList( void )
\r
209 /* Are there any co-routines waiting to get moved to the ready list? These
\r
210 are co-routines that have been readied by an ISR. The ISR cannot access
\r
211 the ready lists itself. */
\r
212 while( !listLIST_IS_EMPTY( &xPendingReadyList ) )
\r
214 corCRCB *pxUnblockedCRCB;
\r
216 /* The pending ready list can be accessed by an ISR. */
\r
217 portDISABLE_INTERRUPTS();
\r
219 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyList) );
\r
220 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
222 portENABLE_INTERRUPTS();
\r
224 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
225 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
228 /*-----------------------------------------------------------*/
\r
230 static void prvCheckDelayedList( void )
\r
232 static portTickType xLastTickCount, xPassedTicks;
\r
235 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
236 while( xPassedTicks )
\r
238 xCoRoutineTickCount++;
\r
241 /* If the tick count has overflowed we need to swap the ready lists. */
\r
242 if( xCoRoutineTickCount == 0 )
\r
246 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
247 any items in pxDelayedCoRoutineList here then there is an error! */
\r
248 pxTemp = pxDelayedCoRoutineList;
\r
249 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
250 pxOverflowDelayedCoRoutineList = pxTemp;
\r
253 /* See if this tick has made a timeout expire. */
\r
254 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
256 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
258 /* Timeout not yet expired. */
\r
262 portDISABLE_INTERRUPTS();
\r
264 /* The event could have occurred just before this critical
\r
265 section. If this is the case then the generic list item will
\r
266 have been moved to the pending ready list and the following
\r
267 line is still valid. Also the pvContainer parameter will have
\r
268 been set to NULL so the following lines are also valid. */
\r
269 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
271 /* Is the co-routine waiting on an event also? */
\r
272 if( pxCRCB->xEventListItem.pvContainer )
\r
274 vListRemove( &( pxCRCB->xEventListItem ) );
\r
277 portENABLE_INTERRUPTS();
\r
279 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
283 xLastTickCount = xCoRoutineTickCount;
\r
285 /*-----------------------------------------------------------*/
\r
287 void vCoRoutineSchedule( void )
\r
289 /* See if any co-routines readied by events need moving to the ready lists. */
\r
290 prvCheckPendingReadyList();
\r
292 /* See if any delayed co-routines have timed out. */
\r
293 prvCheckDelayedList();
\r
295 /* Find the highest priority queue that contains ready co-routines. */
\r
296 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
298 if( uxTopCoRoutineReadyPriority == 0 )
\r
300 /* No more co-routines to check. */
\r
303 --uxTopCoRoutineReadyPriority;
\r
306 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
307 of the same priority get an equal share of the processor time. */
\r
308 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
310 /* Call the co-routine. */
\r
311 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvInitialiseCoRoutineLists( void )
\r
319 unsigned portBASE_TYPE uxPriority;
\r
321 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
323 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
326 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
327 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
328 vListInitialise( ( xList * ) &xPendingReadyList );
\r
330 /* Start with pxDelayedCoRoutineList using list1 and the
\r
331 pxOverflowDelayedCoRoutineList using list2. */
\r
332 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
333 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
335 /*-----------------------------------------------------------*/
\r
337 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
339 corCRCB *pxUnblockedCRCB;
\r
340 signed portBASE_TYPE xReturn;
\r
342 /* This function is called from within an interrupt. It can only access
\r
343 event lists and the pending ready list. */
\r
344 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
345 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
346 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
348 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r