croutine.c 13 KB

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