croutine.c 15 KB

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