1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
39
40
41
42
43
44
52
53
54
55
56
57
58
59
60
67
75
76
80
81
82
88
89
90
98
99
100
101
102
103
104
105
106
107
108
109
110
111
113
118
119
120
124
125
126
127
128
129
130
131
132
133
134
135
138
139
140
141
142
143
144
146
147
148
149
150
154
155
156
157
158
159
160
161
162
163
165
166
167
170
171
172
173
174
175
181
187
188
194
195
196
197
198
199
202
203
204
205
206
207
208
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
237
238
239
240
241
242
243
244
245
246
247
252
253
254
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
296
297
299
300
301
302
303
304
305
306
307
308
309
310
311
312
316
317
318
319
320
321
323
324
325
326
327
328
329
330
331
332
333
336
337
338
339
340
344
348
349
350
351
352
353
/* ... */
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#if( configUSE_CO_ROUTINES != 0 )
/* ... */
#ifdef portREMOVE_STATIC_QUALIFIER
#define static
#endif
static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ];
static List_t xDelayedCoRoutineList1;
static List_t xDelayedCoRoutineList2;
static List_t * pxDelayedCoRoutineList;
static List_t * pxOverflowDelayedCoRoutineList;
static List_t xPendingReadyCoRoutineList;
Lists for ready and blocked co-routines.
CRCB_t * pxCurrentCoRoutine = NULL;
static UBaseType_t uxTopCoRoutineReadyPriority = 0;
static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
#define corINITIAL_STATE ( 0 )
/* ... */
#define prvAddCoRoutineToReadyQueue( pxCRCB ) \
{ \
if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
{ \
uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
...} \
vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
...}...
/* ... */
static void prvInitialiseCoRoutineLists( void );
/* ... */
static void prvCheckPendingReadyList( void );
/* ... */
static void prvCheckDelayedList( void );
BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
{
BaseType_t xReturn;
CRCB_t *pxCoRoutine;
pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
if( pxCoRoutine )
{
/* ... */
if( pxCurrentCoRoutine == NULL )
{
pxCurrentCoRoutine = pxCoRoutine;
prvInitialiseCoRoutineLists();
}if (pxCurrentCoRoutine == NULL) { ... }
if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
{
uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
}if (uxPriority >= configMAX_CO_ROUTINE_PRIORITIES) { ... }
pxCoRoutine->uxState = corINITIAL_STATE;
pxCoRoutine->uxPriority = uxPriority;
pxCoRoutine->uxIndex = uxIndex;
pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
/* ... */
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
/* ... */
prvAddCoRoutineToReadyQueue( pxCoRoutine );
xReturn = pdPASS;
}if (pxCoRoutine) { ... }
else
{
xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
}else { ... }
return xReturn;
}xCoRoutineCreate (crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex) { ... }
void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
{
TickType_t xTimeToWake;
/* ... */
xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
/* ... */
( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
if( xTimeToWake < xCoRoutineTickCount )
{
/* ... */
vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
}if (xTimeToWake < xCoRoutineTickCount) { ... }
else
{
/* ... */
vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
}else { ... }
if( pxEventList )
{
/* ... */
vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
}if (pxEventList) { ... }
}vCoRoutineAddToDelayedList (TickType_t xTicksToDelay, List_t *pxEventList) { ... }
static void prvCheckPendingReadyList( void )
{
/* ... */
while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
{
CRCB_t *pxUnblockedCRCB;
portDISABLE_INTERRUPTS();
{
pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
...}
portENABLE_INTERRUPTS();
( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
}while (listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE) { ... }
}prvCheckPendingReadyList (void) { ... }
static void prvCheckDelayedList( void )
{
CRCB_t *pxCRCB;
xPassedTicks = xTaskGetTickCount() - xLastTickCount;
while( xPassedTicks )
{
xCoRoutineTickCount++;
xPassedTicks--;
if( xCoRoutineTickCount == 0 )
{
List_t * pxTemp;
/* ... */
pxTemp = pxDelayedCoRoutineList;
pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
pxOverflowDelayedCoRoutineList = pxTemp;
}if (xCoRoutineTickCount == 0) { ... }
while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
{
pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
{
break;
}if (xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) )) { ... }
portDISABLE_INTERRUPTS();
{
/* ... */
( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
if( pxCRCB->xEventListItem.pxContainer )
{
( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
}if (pxCRCB->xEventListItem.pxContainer) { ... }
...}
portENABLE_INTERRUPTS();
prvAddCoRoutineToReadyQueue( pxCRCB );
}while (listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE) { ... }
}while (xPassedTicks) { ... }
xLastTickCount = xCoRoutineTickCount;
}prvCheckDelayedList (void) { ... }
void vCoRoutineSchedule( void )
{
prvCheckPendingReadyList();
prvCheckDelayedList();
while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
{
if( uxTopCoRoutineReadyPriority == 0 )
{
return;
}if (uxTopCoRoutineReadyPriority == 0) { ... }
--uxTopCoRoutineReadyPriority;
}while (listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) )) { ... }
/* ... */
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
return;
}vCoRoutineSchedule (void) { ... }
static void prvInitialiseCoRoutineLists( void )
{
UBaseType_t uxPriority;
for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
{
vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
}for (uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++) { ... }
vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
/* ... */
pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
}prvInitialiseCoRoutineLists (void) { ... }
BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
{
CRCB_t *pxUnblockedCRCB;
BaseType_t xReturn;
/* ... */
pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
{
xReturn = pdTRUE;
}if (pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
return xReturn;
}xCoRoutineRemoveFromEventList (const List_t *pxEventList) { ... }
/* ... */
#endif