croutine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * FreeRTOS Kernel V10.0.1
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. #include "FreeRTOS.h"
  28. #include "task.h"
  29. #include "croutine.h"
  30. /* Remove the whole file is co-routines are not being used. */
  31. #if( configUSE_CO_ROUTINES != 0 )
  32. /*
  33. * Some kernel aware debuggers require data to be viewed to be global, rather
  34. * than file scope.
  35. */
  36. #ifdef portREMOVE_STATIC_QUALIFIER
  37. #define static
  38. #endif
  39. /* Lists for ready and blocked co-routines. --------------------*/
  40. static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
  41. static List_t xDelayedCoRoutineList1; /*< Delayed co-routines. */
  42. static List_t xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
  43. static List_t * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
  44. 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. */
  45. 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. */
  46. /* Other file private variables. --------------------------------*/
  47. CRCB_t * pxCurrentCoRoutine = NULL;
  48. static UBaseType_t uxTopCoRoutineReadyPriority = 0;
  49. static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
  50. /* The initial state of the co-routine when it is created. */
  51. #define corINITIAL_STATE ( 0 )
  52. /*
  53. * Place the co-routine represented by pxCRCB into the appropriate ready queue
  54. * for the priority. It is inserted at the end of the list.
  55. *
  56. * This macro accesses the co-routine ready lists and therefore must not be
  57. * used from within an ISR.
  58. */
  59. #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
  60. { \
  61. if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
  62. { \
  63. uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
  64. } \
  65. vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
  66. }
  67. /*
  68. * Utility to ready all the lists used by the scheduler. This is called
  69. * automatically upon the creation of the first co-routine.
  70. */
  71. static void prvInitialiseCoRoutineLists( void );
  72. /*
  73. * Co-routines that are readied by an interrupt cannot be placed directly into
  74. * the ready lists (there is no mutual exclusion). Instead they are placed in
  75. * in the pending ready list in order that they can later be moved to the ready
  76. * list by the co-routine scheduler.
  77. */
  78. static void prvCheckPendingReadyList( void );
  79. /*
  80. * Macro that looks at the list of co-routines that are currently delayed to
  81. * see if any require waking.
  82. *
  83. * Co-routines are stored in the queue in the order of their wake time -
  84. * meaning once one co-routine has been found whose timer has not expired
  85. * we need not look any further down the list.
  86. */
  87. static void prvCheckDelayedList( void );
  88. /*-----------------------------------------------------------*/
  89. BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
  90. {
  91. BaseType_t xReturn;
  92. CRCB_t *pxCoRoutine;
  93. /* Allocate the memory that will store the co-routine control block. */
  94. pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
  95. if( pxCoRoutine )
  96. {
  97. /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
  98. be created and the co-routine data structures need initialising. */
  99. if( pxCurrentCoRoutine == NULL )
  100. {
  101. pxCurrentCoRoutine = pxCoRoutine;
  102. prvInitialiseCoRoutineLists();
  103. }
  104. /* Check the priority is within limits. */
  105. if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
  106. {
  107. uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
  108. }
  109. /* Fill out the co-routine control block from the function parameters. */
  110. pxCoRoutine->uxState = corINITIAL_STATE;
  111. pxCoRoutine->uxPriority = uxPriority;
  112. pxCoRoutine->uxIndex = uxIndex;
  113. pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
  114. /* Initialise all the other co-routine control block parameters. */
  115. vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
  116. vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
  117. /* Set the co-routine control block as a link back from the ListItem_t.
  118. This is so we can get back to the containing CRCB from a generic item
  119. in a list. */
  120. listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
  121. listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
  122. /* Event lists are always in priority order. */
  123. listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
  124. /* Now the co-routine has been initialised it can be added to the ready
  125. list at the correct priority. */
  126. prvAddCoRoutineToReadyQueue( pxCoRoutine );
  127. xReturn = pdPASS;
  128. }
  129. else
  130. {
  131. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  132. }
  133. return xReturn;
  134. }
  135. /*-----------------------------------------------------------*/
  136. void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
  137. {
  138. TickType_t xTimeToWake;
  139. /* Calculate the time to wake - this may overflow but this is
  140. not a problem. */
  141. xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
  142. /* We must remove ourselves from the ready list before adding
  143. ourselves to the blocked list as the same list item is used for
  144. both lists. */
  145. ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
  146. /* The list item will be inserted in wake time order. */
  147. listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
  148. if( xTimeToWake < xCoRoutineTickCount )
  149. {
  150. /* Wake time has overflowed. Place this item in the
  151. overflow list. */
  152. vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
  153. }
  154. else
  155. {
  156. /* The wake time has not overflowed, so we can use the
  157. current block list. */
  158. vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
  159. }
  160. if( pxEventList )
  161. {
  162. /* Also add the co-routine to an event list. If this is done then the
  163. function must be called with interrupts disabled. */
  164. vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
  165. }
  166. }
  167. /*-----------------------------------------------------------*/
  168. static void prvCheckPendingReadyList( void )
  169. {
  170. /* Are there any co-routines waiting to get moved to the ready list? These
  171. are co-routines that have been readied by an ISR. The ISR cannot access
  172. the ready lists itself. */
  173. while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
  174. {
  175. CRCB_t *pxUnblockedCRCB;
  176. /* The pending ready list can be accessed by an ISR. */
  177. portDISABLE_INTERRUPTS();
  178. {
  179. pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
  180. ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
  181. }
  182. portENABLE_INTERRUPTS();
  183. ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
  184. prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
  185. }
  186. }
  187. /*-----------------------------------------------------------*/
  188. static void prvCheckDelayedList( void )
  189. {
  190. CRCB_t *pxCRCB;
  191. xPassedTicks = xTaskGetTickCount() - xLastTickCount;
  192. while( xPassedTicks )
  193. {
  194. xCoRoutineTickCount++;
  195. xPassedTicks--;
  196. /* If the tick count has overflowed we need to swap the ready lists. */
  197. if( xCoRoutineTickCount == 0 )
  198. {
  199. List_t * pxTemp;
  200. /* Tick count has overflowed so we need to swap the delay lists. If there are
  201. any items in pxDelayedCoRoutineList here then there is an error! */
  202. pxTemp = pxDelayedCoRoutineList;
  203. pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
  204. pxOverflowDelayedCoRoutineList = pxTemp;
  205. }
  206. /* See if this tick has made a timeout expire. */
  207. while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
  208. {
  209. pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
  210. if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
  211. {
  212. /* Timeout not yet expired. */
  213. break;
  214. }
  215. portDISABLE_INTERRUPTS();
  216. {
  217. /* The event could have occurred just before this critical
  218. section. If this is the case then the generic list item will
  219. have been moved to the pending ready list and the following
  220. line is still valid. Also the pvContainer parameter will have
  221. been set to NULL so the following lines are also valid. */
  222. ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
  223. /* Is the co-routine waiting on an event also? */
  224. if( pxCRCB->xEventListItem.pvContainer )
  225. {
  226. ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
  227. }
  228. }
  229. portENABLE_INTERRUPTS();
  230. prvAddCoRoutineToReadyQueue( pxCRCB );
  231. }
  232. }
  233. xLastTickCount = xCoRoutineTickCount;
  234. }
  235. /*-----------------------------------------------------------*/
  236. void vCoRoutineSchedule( void )
  237. {
  238. /* See if any co-routines readied by events need moving to the ready lists. */
  239. prvCheckPendingReadyList();
  240. /* See if any delayed co-routines have timed out. */
  241. prvCheckDelayedList();
  242. /* Find the highest priority queue that contains ready co-routines. */
  243. while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
  244. {
  245. if( uxTopCoRoutineReadyPriority == 0 )
  246. {
  247. /* No more co-routines to check. */
  248. return;
  249. }
  250. --uxTopCoRoutineReadyPriority;
  251. }
  252. /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
  253. of the same priority get an equal share of the processor time. */
  254. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
  255. /* Call the co-routine. */
  256. ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
  257. return;
  258. }
  259. /*-----------------------------------------------------------*/
  260. static void prvInitialiseCoRoutineLists( void )
  261. {
  262. UBaseType_t uxPriority;
  263. for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
  264. {
  265. vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
  266. }
  267. vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
  268. vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
  269. vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
  270. /* Start with pxDelayedCoRoutineList using list1 and the
  271. pxOverflowDelayedCoRoutineList using list2. */
  272. pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
  273. pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
  274. }
  275. /*-----------------------------------------------------------*/
  276. BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
  277. {
  278. CRCB_t *pxUnblockedCRCB;
  279. BaseType_t xReturn;
  280. /* This function is called from within an interrupt. It can only access
  281. event lists and the pending ready list. This function assumes that a
  282. check has already been made to ensure pxEventList is not empty. */
  283. pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
  284. ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
  285. vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
  286. if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
  287. {
  288. xReturn = pdTRUE;
  289. }
  290. else
  291. {
  292. xReturn = pdFALSE;
  293. }
  294. return xReturn;
  295. }
  296. #endif /* configUSE_CO_ROUTINES == 0 */