queue.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
  3. FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
  4. http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. ***************************************************************************
  6. * *
  7. * FreeRTOS tutorial books are available in pdf and paperback. *
  8. * Complete, revised, and edited pdf reference manuals are also *
  9. * available. *
  10. * *
  11. * Purchasing FreeRTOS documentation will not only help you, by *
  12. * ensuring you get running as quickly as possible and with an *
  13. * in-depth knowledge of how to use FreeRTOS, it will also help *
  14. * the FreeRTOS project to continue with its mission of providing *
  15. * professional grade, cross platform, de facto standard solutions *
  16. * for microcontrollers - completely free of charge! *
  17. * *
  18. * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
  19. * *
  20. * Thank you for using FreeRTOS, and thank you for your support! *
  21. * *
  22. ***************************************************************************
  23. This file is part of the FreeRTOS distribution.
  24. FreeRTOS is free software; you can redistribute it and/or modify it under
  25. the terms of the GNU General Public License (version 2) as published by the
  26. Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
  27. >>>NOTE<<< The modification to the GPL is included to allow you to
  28. distribute a combined work that includes FreeRTOS without being obliged to
  29. provide the source code for proprietary components outside of the FreeRTOS
  30. kernel. FreeRTOS is distributed in the hope that it will be useful, but
  31. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  33. more details. You should have received a copy of the GNU General Public
  34. License and the FreeRTOS license exception along with FreeRTOS; if not it
  35. can be viewed here: http://www.freertos.org/a00114.html and also obtained
  36. by writing to Richard Barry, contact details for whom are available on the
  37. FreeRTOS WEB site.
  38. 1 tab == 4 spaces!
  39. ***************************************************************************
  40. * *
  41. * Having a problem? Start by reading the FAQ "My application does *
  42. * not run, what could be wrong?" *
  43. * *
  44. * http://www.FreeRTOS.org/FAQHelp.html *
  45. * *
  46. ***************************************************************************
  47. http://www.FreeRTOS.org - Documentation, training, latest versions, license
  48. and contact details.
  49. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  50. including FreeRTOS+Trace - an indispensable productivity tool.
  51. Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
  52. the code with commercial support, indemnification, and middleware, under
  53. the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
  54. provide a safety engineered and independently SIL3 certified version under
  55. the SafeRTOS brand: http://www.SafeRTOS.com.
  56. */
  57. #include <stdlib.h>
  58. #include <string.h>
  59. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  60. all the API functions to use the MPU wrappers. That should only be done when
  61. task.h is included from an application file. */
  62. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  63. #include "FreeRTOS.h"
  64. #include "task.h"
  65. #if ( configUSE_CO_ROUTINES == 1 )
  66. #include "croutine.h"
  67. #endif
  68. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  69. /*-----------------------------------------------------------
  70. * PUBLIC LIST API documented in list.h
  71. *----------------------------------------------------------*/
  72. /* Constants used with the cRxLock and xTxLock structure members. */
  73. #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
  74. #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
  75. #define queueERRONEOUS_UNBLOCK ( -1 )
  76. /* For internal use only. */
  77. #define queueSEND_TO_BACK ( 0 )
  78. #define queueSEND_TO_FRONT ( 1 )
  79. /* Effectively make a union out of the xQUEUE structure. */
  80. #define pxMutexHolder pcTail
  81. #define uxQueueType pcHead
  82. #define uxRecursiveCallCount pcReadFrom
  83. #define queueQUEUE_IS_MUTEX NULL
  84. /* Semaphores do not actually store or copy data, so have an items size of
  85. zero. */
  86. #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
  87. #define queueDONT_BLOCK ( ( portTickType ) 0U )
  88. #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
  89. /* These definitions *must* match those in queue.h. */
  90. #define queueQUEUE_TYPE_BASE ( 0U )
  91. #define queueQUEUE_TYPE_MUTEX ( 1U )
  92. #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
  93. #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
  94. #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
  95. /*
  96. * Definition of the queue used by the scheduler.
  97. * Items are queued by copy, not reference.
  98. */
  99. typedef struct QueueDefinition
  100. {
  101. signed char *pcHead; /*< Points to the beginning of the queue storage area. */
  102. signed char *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
  103. signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
  104. signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
  105. xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
  106. xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
  107. volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
  108. unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
  109. unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
  110. volatile signed portBASE_TYPE xRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
  111. volatile signed portBASE_TYPE xTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
  112. #if ( configUSE_TRACE_FACILITY == 1 )
  113. unsigned char ucQueueNumber;
  114. unsigned char ucQueueType;
  115. #endif
  116. } xQUEUE;
  117. /*-----------------------------------------------------------*/
  118. /*
  119. * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
  120. * To keep the definition private the API header file defines it as a
  121. * pointer to void.
  122. */
  123. typedef xQUEUE * xQueueHandle;
  124. /*
  125. * Prototypes for public functions are included here so we don't have to
  126. * include the API header file (as it defines xQueueHandle differently). These
  127. * functions are documented in the API header file.
  128. */
  129. xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
  130. signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
  131. unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  132. void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
  133. signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
  134. signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
  135. signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  136. xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
  137. xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
  138. portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
  139. portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
  140. signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
  141. signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
  142. signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  143. signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  144. unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  145. void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
  146. unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  147. void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;
  148. unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  149. portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue ) PRIVILEGED_FUNCTION;
  150. xTaskHandle xQueueGetMutexHolder( xQueueHandle xSemaphore ) PRIVILEGED_FUNCTION;
  151. /*
  152. * Co-routine queue functions differ from task queue functions. Co-routines are
  153. * an optional component.
  154. */
  155. #if configUSE_CO_ROUTINES == 1
  156. signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
  157. signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
  158. signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
  159. signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
  160. #endif
  161. /*
  162. * The queue registry is just a means for kernel aware debuggers to locate
  163. * queue structures. It has no other purpose so is an optional component.
  164. */
  165. #if configQUEUE_REGISTRY_SIZE > 0
  166. /* The type stored within the queue registry array. This allows a name
  167. to be assigned to each queue making kernel aware debugging a little
  168. more user friendly. */
  169. typedef struct QUEUE_REGISTRY_ITEM
  170. {
  171. signed char *pcQueueName;
  172. xQueueHandle xHandle;
  173. } xQueueRegistryItem;
  174. /* The queue registry is simply an array of xQueueRegistryItem structures.
  175. The pcQueueName member of a structure being NULL is indicative of the
  176. array position being vacant. */
  177. xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
  178. /* Removes a queue from the registry by simply setting the pcQueueName
  179. member to NULL. */
  180. static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
  181. void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
  182. #endif
  183. /*
  184. * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
  185. * prevent an ISR from adding or removing items to the queue, but does prevent
  186. * an ISR from removing tasks from the queue event lists. If an ISR finds a
  187. * queue is locked it will instead increment the appropriate queue lock count
  188. * to indicate that a task may require unblocking. When the queue in unlocked
  189. * these lock counts are inspected, and the appropriate action taken.
  190. */
  191. static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  192. /*
  193. * Uses a critical section to determine if there is any data in a queue.
  194. *
  195. * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
  196. */
  197. static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  198. /*
  199. * Uses a critical section to determine if there is any space in a queue.
  200. *
  201. * @return pdTRUE if there is no space, otherwise pdFALSE;
  202. */
  203. static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
  204. /*
  205. * Copies an item into the queue, either at the front of the queue or the
  206. * back of the queue.
  207. */
  208. static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
  209. /*
  210. * Copies an item out of a queue.
  211. */
  212. static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
  213. /*-----------------------------------------------------------*/
  214. /*
  215. * Macro to mark a queue as locked. Locking a queue prevents an ISR from
  216. * accessing the queue event lists.
  217. */
  218. #define prvLockQueue( pxQueue ) \
  219. taskENTER_CRITICAL(); \
  220. { \
  221. if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
  222. { \
  223. ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
  224. } \
  225. if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
  226. { \
  227. ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
  228. } \
  229. } \
  230. taskEXIT_CRITICAL()
  231. /*-----------------------------------------------------------*/
  232. /*-----------------------------------------------------------
  233. * PUBLIC QUEUE MANAGEMENT API documented in queue.h
  234. *----------------------------------------------------------*/
  235. portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue )
  236. {
  237. configASSERT( pxQueue );
  238. taskENTER_CRITICAL();
  239. {
  240. pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
  241. pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
  242. pxQueue->pcWriteTo = pxQueue->pcHead;
  243. pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
  244. pxQueue->xRxLock = queueUNLOCKED;
  245. pxQueue->xTxLock = queueUNLOCKED;
  246. if( xNewQueue == pdFALSE )
  247. {
  248. /* If there are tasks blocked waiting to read from the queue, then
  249. the tasks will remain blocked as after this function exits the queue
  250. will still be empty. If there are tasks blocked waiting to write to
  251. the queue, then one should be unblocked as after this function exits
  252. it will be possible to write to it. */
  253. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  254. {
  255. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  256. {
  257. portYIELD_WITHIN_API();
  258. }
  259. }
  260. }
  261. else
  262. {
  263. /* Ensure the event queues start in the correct state. */
  264. vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
  265. vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
  266. }
  267. }
  268. taskEXIT_CRITICAL();
  269. /* A value is returned for calling semantic consistency with previous
  270. versions. */
  271. return pdPASS;
  272. }
  273. /*-----------------------------------------------------------*/
  274. xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
  275. {
  276. xQUEUE *pxNewQueue;
  277. size_t xQueueSizeInBytes;
  278. xQueueHandle xReturn = NULL;
  279. /* Remove compiler warnings about unused parameters should
  280. configUSE_TRACE_FACILITY not be set to 1. */
  281. ( void ) ucQueueType;
  282. /* Allocate the new queue structure. */
  283. if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
  284. {
  285. pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
  286. if( pxNewQueue != NULL )
  287. {
  288. /* Create the list of pointers to queue items. The queue is one byte
  289. longer than asked for to make wrap checking easier/faster. */
  290. xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
  291. pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
  292. if( pxNewQueue->pcHead != NULL )
  293. {
  294. /* Initialise the queue members as described above where the
  295. queue type is defined. */
  296. pxNewQueue->uxLength = uxQueueLength;
  297. pxNewQueue->uxItemSize = uxItemSize;
  298. xQueueGenericReset( pxNewQueue, pdTRUE );
  299. #if ( configUSE_TRACE_FACILITY == 1 )
  300. {
  301. pxNewQueue->ucQueueType = ucQueueType;
  302. }
  303. #endif /* configUSE_TRACE_FACILITY */
  304. traceQUEUE_CREATE( pxNewQueue );
  305. xReturn = pxNewQueue;
  306. }
  307. else
  308. {
  309. traceQUEUE_CREATE_FAILED( ucQueueType );
  310. vPortFree( pxNewQueue );
  311. }
  312. }
  313. }
  314. configASSERT( xReturn );
  315. return xReturn;
  316. }
  317. /*-----------------------------------------------------------*/
  318. #if ( configUSE_MUTEXES == 1 )
  319. xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
  320. {
  321. xQUEUE *pxNewQueue;
  322. /* Prevent compiler warnings about unused parameters if
  323. configUSE_TRACE_FACILITY does not equal 1. */
  324. ( void ) ucQueueType;
  325. /* Allocate the new queue structure. */
  326. pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
  327. if( pxNewQueue != NULL )
  328. {
  329. /* Information required for priority inheritance. */
  330. pxNewQueue->pxMutexHolder = NULL;
  331. pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
  332. /* Queues used as a mutex no data is actually copied into or out
  333. of the queue. */
  334. pxNewQueue->pcWriteTo = NULL;
  335. pxNewQueue->pcReadFrom = NULL;
  336. /* Each mutex has a length of 1 (like a binary semaphore) and
  337. an item size of 0 as nothing is actually copied into or out
  338. of the mutex. */
  339. pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
  340. pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
  341. pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
  342. pxNewQueue->xRxLock = queueUNLOCKED;
  343. pxNewQueue->xTxLock = queueUNLOCKED;
  344. #if ( configUSE_TRACE_FACILITY == 1 )
  345. {
  346. pxNewQueue->ucQueueType = ucQueueType;
  347. }
  348. #endif
  349. /* Ensure the event queues start with the correct state. */
  350. vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
  351. vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
  352. traceCREATE_MUTEX( pxNewQueue );
  353. /* Start with the semaphore in the expected state. */
  354. xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
  355. }
  356. else
  357. {
  358. traceCREATE_MUTEX_FAILED();
  359. }
  360. configASSERT( pxNewQueue );
  361. return pxNewQueue;
  362. }
  363. #endif /* configUSE_MUTEXES */
  364. /*-----------------------------------------------------------*/
  365. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xQueueGetMutexHolder == 1 ) )
  366. void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
  367. {
  368. void *pxReturn;
  369. /* This function is called by xSemaphoreGetMutexHolder(), and should not
  370. be called directly. Note: This is is a good way of determining if the
  371. calling task is the mutex holder, but not a good way of determining the
  372. identity of the mutex holder, as the holder may change between the
  373. following critical section exiting and the function returning. */
  374. taskENTER_CRITICAL();
  375. {
  376. if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
  377. {
  378. pxReturn = ( void * ) xSemaphore->pxMutexHolder;
  379. }
  380. else
  381. {
  382. pxReturn = NULL;
  383. }
  384. }
  385. taskEXIT_CRITICAL();
  386. return pxReturn;
  387. }
  388. #endif
  389. /*-----------------------------------------------------------*/
  390. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  391. portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
  392. {
  393. portBASE_TYPE xReturn;
  394. configASSERT( pxMutex );
  395. /* If this is the task that holds the mutex then pxMutexHolder will not
  396. change outside of this task. If this task does not hold the mutex then
  397. pxMutexHolder can never coincidentally equal the tasks handle, and as
  398. this is the only condition we are interested in it does not matter if
  399. pxMutexHolder is accessed simultaneously by another task. Therefore no
  400. mutual exclusion is required to test the pxMutexHolder variable. */
  401. if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
  402. {
  403. traceGIVE_MUTEX_RECURSIVE( pxMutex );
  404. /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
  405. the task handle, therefore no underflow check is required. Also,
  406. uxRecursiveCallCount is only modified by the mutex holder, and as
  407. there can only be one, no mutual exclusion is required to modify the
  408. uxRecursiveCallCount member. */
  409. ( pxMutex->uxRecursiveCallCount )--;
  410. /* Have we unwound the call count? */
  411. if( pxMutex->uxRecursiveCallCount == 0 )
  412. {
  413. /* Return the mutex. This will automatically unblock any other
  414. task that might be waiting to access the mutex. */
  415. xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  416. }
  417. xReturn = pdPASS;
  418. }
  419. else
  420. {
  421. /* We cannot give the mutex because we are not the holder. */
  422. xReturn = pdFAIL;
  423. traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
  424. }
  425. return xReturn;
  426. }
  427. #endif /* configUSE_RECURSIVE_MUTEXES */
  428. /*-----------------------------------------------------------*/
  429. #if configUSE_RECURSIVE_MUTEXES == 1
  430. portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
  431. {
  432. portBASE_TYPE xReturn;
  433. configASSERT( pxMutex );
  434. /* Comments regarding mutual exclusion as per those within
  435. xQueueGiveMutexRecursive(). */
  436. traceTAKE_MUTEX_RECURSIVE( pxMutex );
  437. if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
  438. {
  439. ( pxMutex->uxRecursiveCallCount )++;
  440. xReturn = pdPASS;
  441. }
  442. else
  443. {
  444. xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
  445. /* pdPASS will only be returned if we successfully obtained the mutex,
  446. we may have blocked to reach here. */
  447. if( xReturn == pdPASS )
  448. {
  449. ( pxMutex->uxRecursiveCallCount )++;
  450. }
  451. else
  452. {
  453. traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
  454. }
  455. }
  456. return xReturn;
  457. }
  458. #endif /* configUSE_RECURSIVE_MUTEXES */
  459. /*-----------------------------------------------------------*/
  460. #if configUSE_COUNTING_SEMAPHORES == 1
  461. xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
  462. {
  463. xQueueHandle pxHandle;
  464. pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  465. if( pxHandle != NULL )
  466. {
  467. pxHandle->uxMessagesWaiting = uxInitialCount;
  468. traceCREATE_COUNTING_SEMAPHORE();
  469. }
  470. else
  471. {
  472. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  473. }
  474. configASSERT( pxHandle );
  475. return pxHandle;
  476. }
  477. #endif /* configUSE_COUNTING_SEMAPHORES */
  478. /*-----------------------------------------------------------*/
  479. signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
  480. {
  481. signed portBASE_TYPE xEntryTimeSet = pdFALSE;
  482. xTimeOutType xTimeOut;
  483. configASSERT( pxQueue );
  484. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  485. /* This function relaxes the coding standard somewhat to allow return
  486. statements within the function itself. This is done in the interest
  487. of execution time efficiency. */
  488. for( ;; )
  489. {
  490. taskENTER_CRITICAL();
  491. {
  492. /* Is there room on the queue now? To be running we must be
  493. the highest priority task wanting to access the queue. */
  494. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  495. {
  496. traceQUEUE_SEND( pxQueue );
  497. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  498. /* If there was a task waiting for data to arrive on the
  499. queue then unblock it now. */
  500. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  501. {
  502. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  503. {
  504. /* The unblocked task has a priority higher than
  505. our own so yield immediately. Yes it is ok to do
  506. this from within the critical section - the kernel
  507. takes care of that. */
  508. portYIELD_WITHIN_API();
  509. }
  510. }
  511. taskEXIT_CRITICAL();
  512. /* Return to the original privilege level before exiting the
  513. function. */
  514. return pdPASS;
  515. }
  516. else
  517. {
  518. if( xTicksToWait == ( portTickType ) 0 )
  519. {
  520. /* The queue was full and no block time is specified (or
  521. the block time has expired) so leave now. */
  522. taskEXIT_CRITICAL();
  523. /* Return to the original privilege level before exiting
  524. the function. */
  525. traceQUEUE_SEND_FAILED( pxQueue );
  526. return errQUEUE_FULL;
  527. }
  528. else if( xEntryTimeSet == pdFALSE )
  529. {
  530. /* The queue was full and a block time was specified so
  531. configure the timeout structure. */
  532. vTaskSetTimeOutState( &xTimeOut );
  533. xEntryTimeSet = pdTRUE;
  534. }
  535. }
  536. }
  537. taskEXIT_CRITICAL();
  538. /* Interrupts and other tasks can send to and receive from the queue
  539. now the critical section has been exited. */
  540. vTaskSuspendAll();
  541. prvLockQueue( pxQueue );
  542. /* Update the timeout state to see if it has expired yet. */
  543. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  544. {
  545. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  546. {
  547. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  548. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  549. /* Unlocking the queue means queue events can effect the
  550. event list. It is possible that interrupts occurring now
  551. remove this task from the event list again - but as the
  552. scheduler is suspended the task will go onto the pending
  553. ready last instead of the actual ready list. */
  554. prvUnlockQueue( pxQueue );
  555. /* Resuming the scheduler will move tasks from the pending
  556. ready list into the ready list - so it is feasible that this
  557. task is already in a ready list before it yields - in which
  558. case the yield will not cause a context switch unless there
  559. is also a higher priority task in the pending ready list. */
  560. if( xTaskResumeAll() == pdFALSE )
  561. {
  562. portYIELD_WITHIN_API();
  563. }
  564. }
  565. else
  566. {
  567. /* Try again. */
  568. prvUnlockQueue( pxQueue );
  569. ( void ) xTaskResumeAll();
  570. }
  571. }
  572. else
  573. {
  574. /* The timeout has expired. */
  575. prvUnlockQueue( pxQueue );
  576. ( void ) xTaskResumeAll();
  577. /* Return to the original privilege level before exiting the
  578. function. */
  579. traceQUEUE_SEND_FAILED( pxQueue );
  580. return errQUEUE_FULL;
  581. }
  582. }
  583. }
  584. /*-----------------------------------------------------------*/
  585. #if configUSE_ALTERNATIVE_API == 1
  586. signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
  587. {
  588. signed portBASE_TYPE xEntryTimeSet = pdFALSE;
  589. xTimeOutType xTimeOut;
  590. configASSERT( pxQueue );
  591. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  592. for( ;; )
  593. {
  594. taskENTER_CRITICAL();
  595. {
  596. /* Is there room on the queue now? To be running we must be
  597. the highest priority task wanting to access the queue. */
  598. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  599. {
  600. traceQUEUE_SEND( pxQueue );
  601. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  602. /* If there was a task waiting for data to arrive on the
  603. queue then unblock it now. */
  604. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  605. {
  606. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  607. {
  608. /* The unblocked task has a priority higher than
  609. our own so yield immediately. */
  610. portYIELD_WITHIN_API();
  611. }
  612. }
  613. taskEXIT_CRITICAL();
  614. return pdPASS;
  615. }
  616. else
  617. {
  618. if( xTicksToWait == ( portTickType ) 0 )
  619. {
  620. taskEXIT_CRITICAL();
  621. return errQUEUE_FULL;
  622. }
  623. else if( xEntryTimeSet == pdFALSE )
  624. {
  625. vTaskSetTimeOutState( &xTimeOut );
  626. xEntryTimeSet = pdTRUE;
  627. }
  628. }
  629. }
  630. taskEXIT_CRITICAL();
  631. taskENTER_CRITICAL();
  632. {
  633. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  634. {
  635. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  636. {
  637. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  638. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  639. portYIELD_WITHIN_API();
  640. }
  641. }
  642. else
  643. {
  644. taskEXIT_CRITICAL();
  645. traceQUEUE_SEND_FAILED( pxQueue );
  646. return errQUEUE_FULL;
  647. }
  648. }
  649. taskEXIT_CRITICAL();
  650. }
  651. }
  652. #endif /* configUSE_ALTERNATIVE_API */
  653. /*-----------------------------------------------------------*/
  654. #if configUSE_ALTERNATIVE_API == 1
  655. signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
  656. {
  657. signed portBASE_TYPE xEntryTimeSet = pdFALSE;
  658. xTimeOutType xTimeOut;
  659. signed char *pcOriginalReadPosition;
  660. configASSERT( pxQueue );
  661. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  662. for( ;; )
  663. {
  664. taskENTER_CRITICAL();
  665. {
  666. if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
  667. {
  668. /* Remember our read position in case we are just peeking. */
  669. pcOriginalReadPosition = pxQueue->pcReadFrom;
  670. prvCopyDataFromQueue( pxQueue, pvBuffer );
  671. if( xJustPeeking == pdFALSE )
  672. {
  673. traceQUEUE_RECEIVE( pxQueue );
  674. /* We are actually removing data. */
  675. --( pxQueue->uxMessagesWaiting );
  676. #if ( configUSE_MUTEXES == 1 )
  677. {
  678. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  679. {
  680. /* Record the information required to implement
  681. priority inheritance should it become necessary. */
  682. pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
  683. }
  684. }
  685. #endif
  686. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  687. {
  688. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  689. {
  690. portYIELD_WITHIN_API();
  691. }
  692. }
  693. }
  694. else
  695. {
  696. traceQUEUE_PEEK( pxQueue );
  697. /* We are not removing the data, so reset our read
  698. pointer. */
  699. pxQueue->pcReadFrom = pcOriginalReadPosition;
  700. /* The data is being left in the queue, so see if there are
  701. any other tasks waiting for the data. */
  702. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  703. {
  704. /* Tasks that are removed from the event list will get added to
  705. the pending ready list as the scheduler is still suspended. */
  706. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  707. {
  708. /* The task waiting has a higher priority than this task. */
  709. portYIELD_WITHIN_API();
  710. }
  711. }
  712. }
  713. taskEXIT_CRITICAL();
  714. return pdPASS;
  715. }
  716. else
  717. {
  718. if( xTicksToWait == ( portTickType ) 0 )
  719. {
  720. taskEXIT_CRITICAL();
  721. traceQUEUE_RECEIVE_FAILED( pxQueue );
  722. return errQUEUE_EMPTY;
  723. }
  724. else if( xEntryTimeSet == pdFALSE )
  725. {
  726. vTaskSetTimeOutState( &xTimeOut );
  727. xEntryTimeSet = pdTRUE;
  728. }
  729. }
  730. }
  731. taskEXIT_CRITICAL();
  732. taskENTER_CRITICAL();
  733. {
  734. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  735. {
  736. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  737. {
  738. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  739. #if ( configUSE_MUTEXES == 1 )
  740. {
  741. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  742. {
  743. portENTER_CRITICAL();
  744. {
  745. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  746. }
  747. portEXIT_CRITICAL();
  748. }
  749. }
  750. #endif
  751. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  752. portYIELD_WITHIN_API();
  753. }
  754. }
  755. else
  756. {
  757. taskEXIT_CRITICAL();
  758. traceQUEUE_RECEIVE_FAILED( pxQueue );
  759. return errQUEUE_EMPTY;
  760. }
  761. }
  762. taskEXIT_CRITICAL();
  763. }
  764. }
  765. #endif /* configUSE_ALTERNATIVE_API */
  766. /*-----------------------------------------------------------*/
  767. signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
  768. {
  769. signed portBASE_TYPE xReturn;
  770. unsigned portBASE_TYPE uxSavedInterruptStatus;
  771. configASSERT( pxQueue );
  772. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  773. /* Similar to xQueueGenericSend, except we don't block if there is no room
  774. in the queue. Also we don't directly wake a task that was blocked on a
  775. queue read, instead we return a flag to say whether a context switch is
  776. required or not (i.e. has a task with a higher priority than us been woken
  777. by this post). */
  778. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  779. {
  780. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  781. {
  782. traceQUEUE_SEND_FROM_ISR( pxQueue );
  783. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  784. /* If the queue is locked we do not alter the event list. This will
  785. be done when the queue is unlocked later. */
  786. if( pxQueue->xTxLock == queueUNLOCKED )
  787. {
  788. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  789. {
  790. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  791. {
  792. /* The task waiting has a higher priority so record that a
  793. context switch is required. */
  794. if( pxHigherPriorityTaskWoken != NULL )
  795. {
  796. *pxHigherPriorityTaskWoken = pdTRUE;
  797. }
  798. }
  799. }
  800. }
  801. else
  802. {
  803. /* Increment the lock count so the task that unlocks the queue
  804. knows that data was posted while it was locked. */
  805. ++( pxQueue->xTxLock );
  806. }
  807. xReturn = pdPASS;
  808. }
  809. else
  810. {
  811. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  812. xReturn = errQUEUE_FULL;
  813. }
  814. }
  815. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  816. return xReturn;
  817. }
  818. /*-----------------------------------------------------------*/
  819. signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
  820. {
  821. signed portBASE_TYPE xEntryTimeSet = pdFALSE;
  822. xTimeOutType xTimeOut;
  823. signed char *pcOriginalReadPosition;
  824. configASSERT( pxQueue );
  825. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  826. /* This function relaxes the coding standard somewhat to allow return
  827. statements within the function itself. This is done in the interest
  828. of execution time efficiency. */
  829. for( ;; )
  830. {
  831. taskENTER_CRITICAL();
  832. {
  833. /* Is there data in the queue now? To be running we must be
  834. the highest priority task wanting to access the queue. */
  835. if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
  836. {
  837. /* Remember our read position in case we are just peeking. */
  838. pcOriginalReadPosition = pxQueue->pcReadFrom;
  839. prvCopyDataFromQueue( pxQueue, pvBuffer );
  840. if( xJustPeeking == pdFALSE )
  841. {
  842. traceQUEUE_RECEIVE( pxQueue );
  843. /* We are actually removing data. */
  844. --( pxQueue->uxMessagesWaiting );
  845. #if ( configUSE_MUTEXES == 1 )
  846. {
  847. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  848. {
  849. /* Record the information required to implement
  850. priority inheritance should it become necessary. */
  851. pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
  852. }
  853. }
  854. #endif
  855. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  856. {
  857. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  858. {
  859. portYIELD_WITHIN_API();
  860. }
  861. }
  862. }
  863. else
  864. {
  865. traceQUEUE_PEEK( pxQueue );
  866. /* We are not removing the data, so reset our read
  867. pointer. */
  868. pxQueue->pcReadFrom = pcOriginalReadPosition;
  869. /* The data is being left in the queue, so see if there are
  870. any other tasks waiting for the data. */
  871. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  872. {
  873. /* Tasks that are removed from the event list will get added to
  874. the pending ready list as the scheduler is still suspended. */
  875. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  876. {
  877. /* The task waiting has a higher priority than this task. */
  878. portYIELD_WITHIN_API();
  879. }
  880. }
  881. }
  882. taskEXIT_CRITICAL();
  883. return pdPASS;
  884. }
  885. else
  886. {
  887. if( xTicksToWait == ( portTickType ) 0 )
  888. {
  889. /* The queue was empty and no block time is specified (or
  890. the block time has expired) so leave now. */
  891. taskEXIT_CRITICAL();
  892. traceQUEUE_RECEIVE_FAILED( pxQueue );
  893. return errQUEUE_EMPTY;
  894. }
  895. else if( xEntryTimeSet == pdFALSE )
  896. {
  897. /* The queue was empty and a block time was specified so
  898. configure the timeout structure. */
  899. vTaskSetTimeOutState( &xTimeOut );
  900. xEntryTimeSet = pdTRUE;
  901. }
  902. }
  903. }
  904. taskEXIT_CRITICAL();
  905. /* Interrupts and other tasks can send to and receive from the queue
  906. now the critical section has been exited. */
  907. vTaskSuspendAll();
  908. prvLockQueue( pxQueue );
  909. /* Update the timeout state to see if it has expired yet. */
  910. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  911. {
  912. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  913. {
  914. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  915. #if ( configUSE_MUTEXES == 1 )
  916. {
  917. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  918. {
  919. portENTER_CRITICAL();
  920. {
  921. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  922. }
  923. portEXIT_CRITICAL();
  924. }
  925. }
  926. #endif
  927. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  928. prvUnlockQueue( pxQueue );
  929. if( xTaskResumeAll() == pdFALSE )
  930. {
  931. portYIELD_WITHIN_API();
  932. }
  933. }
  934. else
  935. {
  936. /* Try again. */
  937. prvUnlockQueue( pxQueue );
  938. ( void ) xTaskResumeAll();
  939. }
  940. }
  941. else
  942. {
  943. prvUnlockQueue( pxQueue );
  944. ( void ) xTaskResumeAll();
  945. traceQUEUE_RECEIVE_FAILED( pxQueue );
  946. return errQUEUE_EMPTY;
  947. }
  948. }
  949. }
  950. /*-----------------------------------------------------------*/
  951. signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )
  952. {
  953. signed portBASE_TYPE xReturn;
  954. unsigned portBASE_TYPE uxSavedInterruptStatus;
  955. configASSERT( pxQueue );
  956. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
  957. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  958. {
  959. /* We cannot block from an ISR, so check there is data available. */
  960. if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
  961. {
  962. traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
  963. prvCopyDataFromQueue( pxQueue, pvBuffer );
  964. --( pxQueue->uxMessagesWaiting );
  965. /* If the queue is locked we will not modify the event list. Instead
  966. we update the lock count so the task that unlocks the queue will know
  967. that an ISR has removed data while the queue was locked. */
  968. if( pxQueue->xRxLock == queueUNLOCKED )
  969. {
  970. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  971. {
  972. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  973. {
  974. /* The task waiting has a higher priority than us so
  975. force a context switch. */
  976. if( pxHigherPriorityTaskWoken != NULL )
  977. {
  978. *pxHigherPriorityTaskWoken = pdTRUE;
  979. }
  980. }
  981. }
  982. }
  983. else
  984. {
  985. /* Increment the lock count so the task that unlocks the queue
  986. knows that data was removed while it was locked. */
  987. ++( pxQueue->xRxLock );
  988. }
  989. xReturn = pdPASS;
  990. }
  991. else
  992. {
  993. xReturn = pdFAIL;
  994. traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
  995. }
  996. }
  997. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  998. return xReturn;
  999. }
  1000. /*-----------------------------------------------------------*/
  1001. unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
  1002. {
  1003. unsigned portBASE_TYPE uxReturn;
  1004. configASSERT( pxQueue );
  1005. taskENTER_CRITICAL();
  1006. uxReturn = pxQueue->uxMessagesWaiting;
  1007. taskEXIT_CRITICAL();
  1008. return uxReturn;
  1009. }
  1010. /*-----------------------------------------------------------*/
  1011. unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
  1012. {
  1013. unsigned portBASE_TYPE uxReturn;
  1014. configASSERT( pxQueue );
  1015. uxReturn = pxQueue->uxMessagesWaiting;
  1016. return uxReturn;
  1017. }
  1018. /*-----------------------------------------------------------*/
  1019. void vQueueDelete( xQueueHandle pxQueue )
  1020. {
  1021. configASSERT( pxQueue );
  1022. traceQUEUE_DELETE( pxQueue );
  1023. vQueueUnregisterQueue( pxQueue );
  1024. vPortFree( pxQueue->pcHead );
  1025. vPortFree( pxQueue );
  1026. }
  1027. /*-----------------------------------------------------------*/
  1028. #if ( configUSE_TRACE_FACILITY == 1 )
  1029. unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
  1030. {
  1031. return pxQueue->ucQueueNumber;
  1032. }
  1033. #endif
  1034. /*-----------------------------------------------------------*/
  1035. #if ( configUSE_TRACE_FACILITY == 1 )
  1036. void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
  1037. {
  1038. pxQueue->ucQueueNumber = ucQueueNumber;
  1039. }
  1040. #endif
  1041. /*-----------------------------------------------------------*/
  1042. #if ( configUSE_TRACE_FACILITY == 1 )
  1043. unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
  1044. {
  1045. return pxQueue->ucQueueType;
  1046. }
  1047. #endif
  1048. /*-----------------------------------------------------------*/
  1049. static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
  1050. {
  1051. if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
  1052. {
  1053. #if ( configUSE_MUTEXES == 1 )
  1054. {
  1055. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1056. {
  1057. /* The mutex is no longer being held. */
  1058. vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
  1059. pxQueue->pxMutexHolder = NULL;
  1060. }
  1061. }
  1062. #endif
  1063. }
  1064. else if( xPosition == queueSEND_TO_BACK )
  1065. {
  1066. memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
  1067. pxQueue->pcWriteTo += pxQueue->uxItemSize;
  1068. if( pxQueue->pcWriteTo >= pxQueue->pcTail )
  1069. {
  1070. pxQueue->pcWriteTo = pxQueue->pcHead;
  1071. }
  1072. }
  1073. else
  1074. {
  1075. memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
  1076. pxQueue->pcReadFrom -= pxQueue->uxItemSize;
  1077. if( pxQueue->pcReadFrom < pxQueue->pcHead )
  1078. {
  1079. pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
  1080. }
  1081. }
  1082. ++( pxQueue->uxMessagesWaiting );
  1083. }
  1084. /*-----------------------------------------------------------*/
  1085. static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
  1086. {
  1087. if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
  1088. {
  1089. pxQueue->pcReadFrom += pxQueue->uxItemSize;
  1090. if( pxQueue->pcReadFrom >= pxQueue->pcTail )
  1091. {
  1092. pxQueue->pcReadFrom = pxQueue->pcHead;
  1093. }
  1094. memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1095. }
  1096. }
  1097. /*-----------------------------------------------------------*/
  1098. static void prvUnlockQueue( xQueueHandle pxQueue )
  1099. {
  1100. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
  1101. /* The lock counts contains the number of extra data items placed or
  1102. removed from the queue while the queue was locked. When a queue is
  1103. locked items can be added or removed, but the event lists cannot be
  1104. updated. */
  1105. taskENTER_CRITICAL();
  1106. {
  1107. /* See if data was added to the queue while it was locked. */
  1108. while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
  1109. {
  1110. /* Data was posted while the queue was locked. Are any tasks
  1111. blocked waiting for data to become available? */
  1112. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1113. {
  1114. /* Tasks that are removed from the event list will get added to
  1115. the pending ready list as the scheduler is still suspended. */
  1116. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1117. {
  1118. /* The task waiting has a higher priority so record that a
  1119. context switch is required. */
  1120. vTaskMissedYield();
  1121. }
  1122. --( pxQueue->xTxLock );
  1123. }
  1124. else
  1125. {
  1126. break;
  1127. }
  1128. }
  1129. pxQueue->xTxLock = queueUNLOCKED;
  1130. }
  1131. taskEXIT_CRITICAL();
  1132. /* Do the same for the Rx lock. */
  1133. taskENTER_CRITICAL();
  1134. {
  1135. while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
  1136. {
  1137. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1138. {
  1139. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1140. {
  1141. vTaskMissedYield();
  1142. }
  1143. --( pxQueue->xRxLock );
  1144. }
  1145. else
  1146. {
  1147. break;
  1148. }
  1149. }
  1150. pxQueue->xRxLock = queueUNLOCKED;
  1151. }
  1152. taskEXIT_CRITICAL();
  1153. }
  1154. /*-----------------------------------------------------------*/
  1155. static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
  1156. {
  1157. signed portBASE_TYPE xReturn;
  1158. taskENTER_CRITICAL();
  1159. xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
  1160. taskEXIT_CRITICAL();
  1161. return xReturn;
  1162. }
  1163. /*-----------------------------------------------------------*/
  1164. signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
  1165. {
  1166. signed portBASE_TYPE xReturn;
  1167. configASSERT( pxQueue );
  1168. xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
  1169. return xReturn;
  1170. }
  1171. /*-----------------------------------------------------------*/
  1172. static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
  1173. {
  1174. signed portBASE_TYPE xReturn;
  1175. taskENTER_CRITICAL();
  1176. xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
  1177. taskEXIT_CRITICAL();
  1178. return xReturn;
  1179. }
  1180. /*-----------------------------------------------------------*/
  1181. signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
  1182. {
  1183. signed portBASE_TYPE xReturn;
  1184. configASSERT( pxQueue );
  1185. xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
  1186. return xReturn;
  1187. }
  1188. /*-----------------------------------------------------------*/
  1189. #if configUSE_CO_ROUTINES == 1
  1190. signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
  1191. {
  1192. signed portBASE_TYPE xReturn;
  1193. /* If the queue is already full we may have to block. A critical section
  1194. is required to prevent an interrupt removing something from the queue
  1195. between the check to see if the queue is full and blocking on the queue. */
  1196. portDISABLE_INTERRUPTS();
  1197. {
  1198. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  1199. {
  1200. /* The queue is full - do we want to block or just leave without
  1201. posting? */
  1202. if( xTicksToWait > ( portTickType ) 0 )
  1203. {
  1204. /* As this is called from a coroutine we cannot block directly, but
  1205. return indicating that we need to block. */
  1206. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
  1207. portENABLE_INTERRUPTS();
  1208. return errQUEUE_BLOCKED;
  1209. }
  1210. else
  1211. {
  1212. portENABLE_INTERRUPTS();
  1213. return errQUEUE_FULL;
  1214. }
  1215. }
  1216. }
  1217. portENABLE_INTERRUPTS();
  1218. portNOP();
  1219. portDISABLE_INTERRUPTS();
  1220. {
  1221. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1222. {
  1223. /* There is room in the queue, copy the data into the queue. */
  1224. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1225. xReturn = pdPASS;
  1226. /* Were any co-routines waiting for data to become available? */
  1227. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1228. {
  1229. /* In this instance the co-routine could be placed directly
  1230. into the ready list as we are within a critical section.
  1231. Instead the same pending ready list mechanism is used as if
  1232. the event were caused from within an interrupt. */
  1233. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1234. {
  1235. /* The co-routine waiting has a higher priority so record
  1236. that a yield might be appropriate. */
  1237. xReturn = errQUEUE_YIELD;
  1238. }
  1239. }
  1240. }
  1241. else
  1242. {
  1243. xReturn = errQUEUE_FULL;
  1244. }
  1245. }
  1246. portENABLE_INTERRUPTS();
  1247. return xReturn;
  1248. }
  1249. #endif
  1250. /*-----------------------------------------------------------*/
  1251. #if configUSE_CO_ROUTINES == 1
  1252. signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
  1253. {
  1254. signed portBASE_TYPE xReturn;
  1255. /* If the queue is already empty we may have to block. A critical section
  1256. is required to prevent an interrupt adding something to the queue
  1257. between the check to see if the queue is empty and blocking on the queue. */
  1258. portDISABLE_INTERRUPTS();
  1259. {
  1260. if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
  1261. {
  1262. /* There are no messages in the queue, do we want to block or just
  1263. leave with nothing? */
  1264. if( xTicksToWait > ( portTickType ) 0 )
  1265. {
  1266. /* As this is a co-routine we cannot block directly, but return
  1267. indicating that we need to block. */
  1268. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
  1269. portENABLE_INTERRUPTS();
  1270. return errQUEUE_BLOCKED;
  1271. }
  1272. else
  1273. {
  1274. portENABLE_INTERRUPTS();
  1275. return errQUEUE_FULL;
  1276. }
  1277. }
  1278. }
  1279. portENABLE_INTERRUPTS();
  1280. portNOP();
  1281. portDISABLE_INTERRUPTS();
  1282. {
  1283. if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
  1284. {
  1285. /* Data is available from the queue. */
  1286. pxQueue->pcReadFrom += pxQueue->uxItemSize;
  1287. if( pxQueue->pcReadFrom >= pxQueue->pcTail )
  1288. {
  1289. pxQueue->pcReadFrom = pxQueue->pcHead;
  1290. }
  1291. --( pxQueue->uxMessagesWaiting );
  1292. memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1293. xReturn = pdPASS;
  1294. /* Were any co-routines waiting for space to become available? */
  1295. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1296. {
  1297. /* In this instance the co-routine could be placed directly
  1298. into the ready list as we are within a critical section.
  1299. Instead the same pending ready list mechanism is used as if
  1300. the event were caused from within an interrupt. */
  1301. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1302. {
  1303. xReturn = errQUEUE_YIELD;
  1304. }
  1305. }
  1306. }
  1307. else
  1308. {
  1309. xReturn = pdFAIL;
  1310. }
  1311. }
  1312. portENABLE_INTERRUPTS();
  1313. return xReturn;
  1314. }
  1315. #endif
  1316. /*-----------------------------------------------------------*/
  1317. #if configUSE_CO_ROUTINES == 1
  1318. signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
  1319. {
  1320. /* Cannot block within an ISR so if there is no space on the queue then
  1321. exit without doing anything. */
  1322. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1323. {
  1324. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1325. /* We only want to wake one co-routine per ISR, so check that a
  1326. co-routine has not already been woken. */
  1327. if( xCoRoutinePreviouslyWoken == pdFALSE )
  1328. {
  1329. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1330. {
  1331. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1332. {
  1333. return pdTRUE;
  1334. }
  1335. }
  1336. }
  1337. }
  1338. return xCoRoutinePreviouslyWoken;
  1339. }
  1340. #endif
  1341. /*-----------------------------------------------------------*/
  1342. #if configUSE_CO_ROUTINES == 1
  1343. signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
  1344. {
  1345. signed portBASE_TYPE xReturn;
  1346. /* We cannot block from an ISR, so check there is data available. If
  1347. not then just leave without doing anything. */
  1348. if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
  1349. {
  1350. /* Copy the data from the queue. */
  1351. pxQueue->pcReadFrom += pxQueue->uxItemSize;
  1352. if( pxQueue->pcReadFrom >= pxQueue->pcTail )
  1353. {
  1354. pxQueue->pcReadFrom = pxQueue->pcHead;
  1355. }
  1356. --( pxQueue->uxMessagesWaiting );
  1357. memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1358. if( ( *pxCoRoutineWoken ) == pdFALSE )
  1359. {
  1360. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1361. {
  1362. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1363. {
  1364. *pxCoRoutineWoken = pdTRUE;
  1365. }
  1366. }
  1367. }
  1368. xReturn = pdPASS;
  1369. }
  1370. else
  1371. {
  1372. xReturn = pdFAIL;
  1373. }
  1374. return xReturn;
  1375. }
  1376. #endif
  1377. /*-----------------------------------------------------------*/
  1378. #if configQUEUE_REGISTRY_SIZE > 0
  1379. void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
  1380. {
  1381. unsigned portBASE_TYPE ux;
  1382. /* See if there is an empty space in the registry. A NULL name denotes
  1383. a free slot. */
  1384. for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
  1385. {
  1386. if( xQueueRegistry[ ux ].pcQueueName == NULL )
  1387. {
  1388. /* Store the information on this queue. */
  1389. xQueueRegistry[ ux ].pcQueueName = pcQueueName;
  1390. xQueueRegistry[ ux ].xHandle = xQueue;
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. #endif
  1396. /*-----------------------------------------------------------*/
  1397. #if configQUEUE_REGISTRY_SIZE > 0
  1398. static void vQueueUnregisterQueue( xQueueHandle xQueue )
  1399. {
  1400. unsigned portBASE_TYPE ux;
  1401. /* See if the handle of the queue being unregistered in actually in the
  1402. registry. */
  1403. for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
  1404. {
  1405. if( xQueueRegistry[ ux ].xHandle == xQueue )
  1406. {
  1407. /* Set the name to NULL to show that this slot if free again. */
  1408. xQueueRegistry[ ux ].pcQueueName = NULL;
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. #endif
  1414. /*-----------------------------------------------------------*/
  1415. #if configUSE_TIMERS == 1
  1416. void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
  1417. {
  1418. /* This function should not be called by application code hence the
  1419. 'Restricted' in its name. It is not part of the public API. It is
  1420. designed for use by kernel code, and has special calling requirements.
  1421. It can result in vListInsert() being called on a list that can only
  1422. possibly ever have one item in it, so the list will be fast, but even
  1423. so it should be called with the scheduler locked and not from a critical
  1424. section. */
  1425. /* Only do anything if there are no messages in the queue. This function
  1426. will not actually cause the task to block, just place it on a blocked
  1427. list. It will not block until the scheduler is unlocked - at which
  1428. time a yield will be performed. If an item is added to the queue while
  1429. the queue is locked, and the calling task blocks on the queue, then the
  1430. calling task will be immediately unblocked when the queue is unlocked. */
  1431. prvLockQueue( pxQueue );
  1432. if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
  1433. {
  1434. /* There is nothing in the queue, block for the specified period. */
  1435. vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1436. }
  1437. prvUnlockQueue( pxQueue );
  1438. }
  1439. #endif