]> granicus.if.org Git - esp-idf/blob - components/freertos/croutine.c
sleep: fix checking length of RTC data sections
[esp-idf] / components / freertos / croutine.c
1 /*
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
12
13         ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18         ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40         the FAQ page "My application does not run, what could be wrong?".  Have you
41         defined configASSERT()?
42
43         http://www.FreeRTOS.org/support - In return for receiving this top quality
44         embedded software for free we request you assist our global community by
45         participating in the support forum.
46
47         http://www.FreeRTOS.org/training - Investing in training allows your team to
48         be as productive as possible as early as possible.  Now you can receive
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50         Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 #include "FreeRTOS.h"
71 #include "task.h"
72 #include "croutine.h"
73
74 /*
75  * Some kernel aware debuggers require data to be viewed to be global, rather
76  * than file scope.
77  */
78 #ifdef portREMOVE_STATIC_QUALIFIER
79         #define static
80 #endif
81
82
83 /* Lists for ready and blocked co-routines. --------------------*/
84 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
85 static List_t xDelayedCoRoutineList1;                                                                   /*< Delayed co-routines. */
86 static List_t xDelayedCoRoutineList2;                                                                   /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
87 static List_t * pxDelayedCoRoutineList;                                                                 /*< Points to the delayed co-routine list currently being used. */
88 static List_t * pxOverflowDelayedCoRoutineList;                                                 /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
89 static List_t 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. */
90
91 /* Other file private variables. --------------------------------*/
92 CRCB_t * pxCurrentCoRoutine = NULL;
93 static UBaseType_t uxTopCoRoutineReadyPriority = 0;
94 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
95
96 /* The initial state of the co-routine when it is created. */
97 #define corINITIAL_STATE        ( 0 )
98
99 /*
100  * Place the co-routine represented by pxCRCB into the appropriate ready queue
101  * for the priority.  It is inserted at the end of the list.
102  *
103  * This macro accesses the co-routine ready lists and therefore must not be
104  * used from within an ISR.
105  */
106 #define prvAddCoRoutineToReadyQueue( pxCRCB )                                                                                                                                           \
107 {                                                                                                                                                                                                                                       \
108         if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority )                                                                                                                  \
109         {                                                                                                                                                                                                                               \
110                 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority;                                                                                                                       \
111         }                                                                                                                                                                                                                               \
112         vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
113 }
114
115 /*
116  * Utility to ready all the lists used by the scheduler.  This is called
117  * automatically upon the creation of the first co-routine.
118  */
119 static void prvInitialiseCoRoutineLists( void );
120
121 /*
122  * Co-routines that are readied by an interrupt cannot be placed directly into
123  * the ready lists (there is no mutual exclusion).  Instead they are placed in
124  * in the pending ready list in order that they can later be moved to the ready
125  * list by the co-routine scheduler.
126  */
127 static void prvCheckPendingReadyList( void );
128
129 /*
130  * Macro that looks at the list of co-routines that are currently delayed to
131  * see if any require waking.
132  *
133  * Co-routines are stored in the queue in the order of their wake time -
134  * meaning once one co-routine has been found whose timer has not expired
135  * we need not look any further down the list.
136  */
137 static void prvCheckDelayedList( void );
138
139 /*-----------------------------------------------------------*/
140
141 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
142 {
143 BaseType_t xReturn;
144 CRCB_t *pxCoRoutine;
145
146         UNTESTED_FUNCTION(); //Actually, coroutines are entirely unsupported
147         /* Allocate the memory that will store the co-routine control block. */
148         pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
149         if( pxCoRoutine )
150         {
151                 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
152                 be created and the co-routine data structures need initialising. */
153                 if( pxCurrentCoRoutine == NULL )
154                 {
155                         pxCurrentCoRoutine = pxCoRoutine;
156                         prvInitialiseCoRoutineLists();
157                 }
158
159                 /* Check the priority is within limits. */
160                 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
161                 {
162                         uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
163                 }
164
165                 /* Fill out the co-routine control block from the function parameters. */
166                 pxCoRoutine->uxState = corINITIAL_STATE;
167                 pxCoRoutine->uxPriority = uxPriority;
168                 pxCoRoutine->uxIndex = uxIndex;
169                 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
170
171                 /* Initialise all the other co-routine control block parameters. */
172                 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
173                 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
174
175                 /* Set the co-routine control block as a link back from the ListItem_t.
176                 This is so we can get back to the containing CRCB from a generic item
177                 in a list. */
178                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
179                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
180
181                 /* Event lists are always in priority order. */
182                 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
183
184                 /* Now the co-routine has been initialised it can be added to the ready
185                 list at the correct priority. */
186                 prvAddCoRoutineToReadyQueue( pxCoRoutine );
187
188                 xReturn = pdPASS;
189         }
190         else
191         {
192                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
193         }
194
195         return xReturn;
196 }
197 /*-----------------------------------------------------------*/
198
199 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
200 {
201 TickType_t xTimeToWake;
202
203         /* Calculate the time to wake - this may overflow but this is
204         not a problem. */
205         xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
206
207         /* We must remove ourselves from the ready list before adding
208         ourselves to the blocked list as the same list item is used for
209         both lists. */
210         ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
211
212         /* The list item will be inserted in wake time order. */
213         listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
214
215         if( xTimeToWake < xCoRoutineTickCount )
216         {
217                 /* Wake time has overflowed.  Place this item in the
218                 overflow list. */
219                 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
220         }
221         else
222         {
223                 /* The wake time has not overflowed, so we can use the
224                 current block list. */
225                 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
226         }
227
228         if( pxEventList )
229         {
230                 /* Also add the co-routine to an event list.  If this is done then the
231                 function must be called with interrupts disabled. */
232                 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
233         }
234 }
235 /*-----------------------------------------------------------*/
236
237 static void prvCheckPendingReadyList( void )
238 {
239         /* Are there any co-routines waiting to get moved to the ready list?  These
240         are co-routines that have been readied by an ISR.  The ISR cannot access
241         the     ready lists itself. */
242         while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
243         {
244                 CRCB_t *pxUnblockedCRCB;
245
246                 /* The pending ready list can be accessed by an ISR. */
247                 portDISABLE_INTERRUPTS();
248                 {
249                         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
250                         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
251                 }
252                 portENABLE_INTERRUPTS();
253
254                 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
255                 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
256         }
257 }
258 /*-----------------------------------------------------------*/
259
260 static void prvCheckDelayedList( void )
261 {
262 CRCB_t *pxCRCB;
263
264         xPassedTicks = xTaskGetTickCount() - xLastTickCount;
265         while( xPassedTicks )
266         {
267                 xCoRoutineTickCount++;
268                 xPassedTicks--;
269
270                 /* If the tick count has overflowed we need to swap the ready lists. */
271                 if( xCoRoutineTickCount == 0 )
272                 {
273                         List_t * pxTemp;
274
275                         /* Tick count has overflowed so we need to swap the delay lists.  If there are
276                         any items in pxDelayedCoRoutineList here then there is an error! */
277                         pxTemp = pxDelayedCoRoutineList;
278                         pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
279                         pxOverflowDelayedCoRoutineList = pxTemp;
280                 }
281
282                 /* See if this tick has made a timeout expire. */
283                 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
284                 {
285                         pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
286
287                         if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
288                         {
289                                 /* Timeout not yet expired. */
290                                 break;
291                         }
292
293                         portDISABLE_INTERRUPTS();
294                         {
295                                 /* The event could have occurred just before this critical
296                                 section.  If this is the case then the generic list item will
297                                 have been moved to the pending ready list and the following
298                                 line is still valid.  Also the pvContainer parameter will have
299                                 been set to NULL so the following lines are also valid. */
300                                 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
301
302                                 /* Is the co-routine waiting on an event also? */
303                                 if( pxCRCB->xEventListItem.pvContainer )
304                                 {
305                                         ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
306                                 }
307                         }
308                         portENABLE_INTERRUPTS();
309
310                         prvAddCoRoutineToReadyQueue( pxCRCB );
311                 }
312         }
313
314         xLastTickCount = xCoRoutineTickCount;
315 }
316 /*-----------------------------------------------------------*/
317
318 void vCoRoutineSchedule( void )
319 {
320         /* See if any co-routines readied by events need moving to the ready lists. */
321         prvCheckPendingReadyList();
322
323         /* See if any delayed co-routines have timed out. */
324         prvCheckDelayedList();
325
326         /* Find the highest priority queue that contains ready co-routines. */
327         while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
328         {
329                 if( uxTopCoRoutineReadyPriority == 0 )
330                 {
331                         /* No more co-routines to check. */
332                         return;
333                 }
334                 --uxTopCoRoutineReadyPriority;
335         }
336
337         /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
338          of the same priority get an equal share of the processor time. */
339         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
340
341         /* Call the co-routine. */
342         ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
343
344         return;
345 }
346 /*-----------------------------------------------------------*/
347
348 static void prvInitialiseCoRoutineLists( void )
349 {
350 UBaseType_t uxPriority;
351
352         for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
353         {
354                 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
355         }
356
357         vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
358         vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
359         vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
360
361         /* Start with pxDelayedCoRoutineList using list1 and the
362         pxOverflowDelayedCoRoutineList using list2. */
363         pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
364         pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
365 }
366 /*-----------------------------------------------------------*/
367
368 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
369 {
370 CRCB_t *pxUnblockedCRCB;
371 BaseType_t xReturn;
372
373         /* This function is called from within an interrupt.  It can only access
374         event lists and the pending ready list.  This function assumes that a
375         check has already been made to ensure pxEventList is not empty. */
376         pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
377         ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
378         vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
379
380         if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
381         {
382                 xReturn = pdTRUE;
383         }
384         else
385         {
386                 xReturn = pdFALSE;
387         }
388
389         return xReturn;
390 }
391