queue.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /*
  2. FreeRTOS V8.2.0 - 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. /*
  56. ToDo: The multicore implementation of this uses taskENTER_CRITICAL etc to make sure the
  57. queue structures aren't accessed by another processor or core. It would be useful to have
  58. IRQs be able to schedule stuff while doing task-related stuff, meaning we have to convert
  59. the taskENTER_CRITICAL stuff to a lock + a scheduler suspend instead.
  60. */
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include "rom/ets_sys.h"
  64. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  65. all the API functions to use the MPU wrappers. That should only be done when
  66. task.h is included from an application file. */
  67. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  68. #include "FreeRTOS.h"
  69. #include "task.h"
  70. #include "queue.h"
  71. #if ( configUSE_CO_ROUTINES == 1 )
  72. #include "croutine.h"
  73. #endif
  74. /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
  75. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  76. header files above, but not in this file, in order to generate the correct
  77. privileged Vs unprivileged linkage and placement. */
  78. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  79. /* When the Queue_t structure is used to represent a base queue its pcHead and
  80. pcTail members are used as pointers into the queue storage area. When the
  81. Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
  82. not necessary, and the pcHead pointer is set to NULL to indicate that the
  83. pcTail pointer actually points to the mutex holder (if any). Map alternative
  84. names to the pcHead and pcTail structure members to ensure the readability of
  85. the code is maintained despite this dual use of two structure members. An
  86. alternative implementation would be to use a union, but use of a union is
  87. against the coding standard (although an exception to the standard has been
  88. permitted where the dual use also significantly changes the type of the
  89. structure member). */
  90. #define pxMutexHolder pcTail
  91. #define uxQueueType pcHead
  92. #define queueQUEUE_IS_MUTEX NULL
  93. /* Semaphores do not actually store or copy data, so have an item size of
  94. zero. */
  95. #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
  96. #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
  97. #if( configUSE_PREEMPTION == 0 )
  98. /* If the cooperative scheduler is being used then a yield should not be
  99. performed just because a higher priority task has been woken. */
  100. #define queueYIELD_IF_USING_PREEMPTION_MUX()
  101. #define queueYIELD_IF_USING_PREEMPTION()
  102. #else
  103. #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  104. #define queueYIELD_IF_USING_PREEMPTION_MUX(mux) { \
  105. taskEXIT_CRITICAL(mux); \
  106. portYIELD_WITHIN_API(); \
  107. taskENTER_CRITICAL(mux); \
  108. } while(0)
  109. #endif
  110. /*
  111. * Definition of the queue used by the scheduler.
  112. * Items are queued by copy, not reference. See the following link for the
  113. * rationale: http://www.freertos.org/Embedded-RTOS-Queues.html
  114. */
  115. typedef struct QueueDefinition
  116. {
  117. int8_t *pcHead; /*< Points to the beginning of the queue storage area. */
  118. int8_t *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. */
  119. int8_t *pcWriteTo; /*< Points to the free next place in the storage area. */
  120. union /* Use of a union is an exception to the coding standard to ensure two mutually exclusive structure members don't appear simultaneously (wasting RAM). */
  121. {
  122. int8_t *pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
  123. UBaseType_t uxRecursiveCallCount;/*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
  124. } u;
  125. List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
  126. List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
  127. volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. */
  128. UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
  129. UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. */
  130. #if ( configUSE_TRACE_FACILITY == 1 )
  131. UBaseType_t uxQueueNumber;
  132. uint8_t ucQueueType;
  133. #endif
  134. #if ( configUSE_QUEUE_SETS == 1 )
  135. struct QueueDefinition *pxQueueSetContainer;
  136. #endif
  137. portMUX_TYPE mux;
  138. } xQUEUE;
  139. /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
  140. name below to enable the use of older kernel aware debuggers. */
  141. typedef xQUEUE Queue_t;
  142. /*-----------------------------------------------------------*/
  143. /*
  144. * The queue registry is just a means for kernel aware debuggers to locate
  145. * queue structures. It has no other purpose so is an optional component.
  146. */
  147. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  148. /* The type stored within the queue registry array. This allows a name
  149. to be assigned to each queue making kernel aware debugging a little
  150. more user friendly. */
  151. typedef struct QUEUE_REGISTRY_ITEM
  152. {
  153. const char *pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  154. QueueHandle_t xHandle;
  155. } xQueueRegistryItem;
  156. /* The old xQueueRegistryItem name is maintained above then typedefed to the
  157. new xQueueRegistryItem name below to enable the use of older kernel aware
  158. debuggers. */
  159. typedef xQueueRegistryItem QueueRegistryItem_t;
  160. /* The queue registry is simply an array of QueueRegistryItem_t structures.
  161. The pcQueueName member of a structure being NULL is indicative of the
  162. array position being vacant. */
  163. QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
  164. #endif /* configQUEUE_REGISTRY_SIZE */
  165. /*
  166. * Uses a critical section to determine if there is any data in a queue.
  167. *
  168. * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
  169. */
  170. static BaseType_t prvIsQueueEmpty( Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  171. /*
  172. * Uses a critical section to determine if there is any space in a queue.
  173. *
  174. * @return pdTRUE if there is no space, otherwise pdFALSE;
  175. */
  176. static BaseType_t prvIsQueueFull( Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  177. /*
  178. * Copies an item into the queue, either at the front of the queue or the
  179. * back of the queue.
  180. */
  181. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
  182. /*
  183. * Copies an item out of a queue.
  184. */
  185. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer ) PRIVILEGED_FUNCTION;
  186. #if ( configUSE_QUEUE_SETS == 1 )
  187. /*
  188. * Checks to see if a queue is a member of a queue set, and if so, notifies
  189. * the queue set that the queue contains data.
  190. */
  191. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION;
  192. #endif
  193. BaseType_t xQueueGenericReset( QueueHandle_t xQueue, BaseType_t xNewQueue )
  194. {
  195. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  196. configASSERT( pxQueue );
  197. if ( xNewQueue == pdTRUE )
  198. {
  199. vPortCPUInitializeMutex(&pxQueue->mux);
  200. }
  201. taskENTER_CRITICAL(&pxQueue->mux);
  202. {
  203. pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
  204. pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
  205. pxQueue->pcWriteTo = pxQueue->pcHead;
  206. pxQueue->u.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( UBaseType_t ) 1U ) * pxQueue->uxItemSize );
  207. if( xNewQueue == pdFALSE )
  208. {
  209. /* If there are tasks blocked waiting to read from the queue, then
  210. the tasks will remain blocked as after this function exits the queue
  211. will still be empty. If there are tasks blocked waiting to write to
  212. the queue, then one should be unblocked as after this function exits
  213. it will be possible to write to it. */
  214. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  215. {
  216. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  217. {
  218. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  219. }
  220. else
  221. {
  222. mtCOVERAGE_TEST_MARKER();
  223. }
  224. }
  225. else
  226. {
  227. mtCOVERAGE_TEST_MARKER();
  228. }
  229. }
  230. else
  231. {
  232. /* Ensure the event queues start in the correct state. */
  233. vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
  234. vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
  235. }
  236. }
  237. taskEXIT_CRITICAL(&pxQueue->mux);
  238. /* A value is returned for calling semantic consistency with previous
  239. versions. */
  240. return pdPASS;
  241. }
  242. /*-----------------------------------------------------------*/
  243. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
  244. {
  245. Queue_t *pxNewQueue;
  246. size_t xQueueSizeInBytes;
  247. QueueHandle_t xReturn = NULL;
  248. int8_t *pcAllocatedBuffer;
  249. /* Remove compiler warnings about unused parameters should
  250. configUSE_TRACE_FACILITY not be set to 1. */
  251. ( void ) ucQueueType;
  252. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  253. if( uxItemSize == ( UBaseType_t ) 0 )
  254. {
  255. /* There is not going to be a queue storage area. */
  256. xQueueSizeInBytes = ( size_t ) 0;
  257. }
  258. else
  259. {
  260. /* The queue is one byte longer than asked for to make wrap checking
  261. easier/faster. */
  262. xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  263. }
  264. /* Allocate the new queue structure and storage area. */
  265. pcAllocatedBuffer = ( int8_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
  266. if( pcAllocatedBuffer != NULL )
  267. {
  268. pxNewQueue = ( Queue_t * ) pcAllocatedBuffer; /*lint !e826 MISRA The buffer cannot be to small because it was dimensioned by sizeof( Queue_t ) + xQueueSizeInBytes. */
  269. if( uxItemSize == ( UBaseType_t ) 0 )
  270. {
  271. /* No RAM was allocated for the queue storage area, but PC head
  272. cannot be set to NULL because NULL is used as a key to say the queue
  273. is used as a mutex. Therefore just set pcHead to point to the queue
  274. as a benign value that is known to be within the memory map. */
  275. pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
  276. }
  277. else
  278. {
  279. /* Jump past the queue structure to find the location of the queue
  280. storage area - adding the padding bytes to get a better alignment. */
  281. pxNewQueue->pcHead = pcAllocatedBuffer + sizeof( Queue_t );
  282. }
  283. /* Initialise the queue members as described above where the queue type
  284. is defined. */
  285. pxNewQueue->uxLength = uxQueueLength;
  286. pxNewQueue->uxItemSize = uxItemSize;
  287. ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
  288. #if ( configUSE_TRACE_FACILITY == 1 )
  289. {
  290. pxNewQueue->ucQueueType = ucQueueType;
  291. }
  292. #endif /* configUSE_TRACE_FACILITY */
  293. #if( configUSE_QUEUE_SETS == 1 )
  294. {
  295. pxNewQueue->pxQueueSetContainer = NULL;
  296. }
  297. #endif /* configUSE_QUEUE_SETS */
  298. traceQUEUE_CREATE( pxNewQueue );
  299. xReturn = pxNewQueue;
  300. }
  301. else
  302. {
  303. mtCOVERAGE_TEST_MARKER();
  304. }
  305. configASSERT( xReturn );
  306. return xReturn;
  307. }
  308. /*-----------------------------------------------------------*/
  309. #if ( configUSE_MUTEXES == 1 )
  310. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  311. {
  312. Queue_t *pxNewQueue;
  313. /* Prevent compiler warnings about unused parameters if
  314. configUSE_TRACE_FACILITY does not equal 1. */
  315. ( void ) ucQueueType;
  316. /* Allocate the new queue structure. */
  317. pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) );
  318. if( pxNewQueue != NULL )
  319. {
  320. /* Information required for priority inheritance. */
  321. pxNewQueue->pxMutexHolder = NULL;
  322. pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
  323. /* Queues used as a mutex no data is actually copied into or out
  324. of the queue. */
  325. pxNewQueue->pcWriteTo = NULL;
  326. pxNewQueue->u.pcReadFrom = NULL;
  327. /* Each mutex has a length of 1 (like a binary semaphore) and
  328. an item size of 0 as nothing is actually copied into or out
  329. of the mutex. */
  330. pxNewQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
  331. pxNewQueue->uxLength = ( UBaseType_t ) 1U;
  332. pxNewQueue->uxItemSize = ( UBaseType_t ) 0U;
  333. #if ( configUSE_TRACE_FACILITY == 1 )
  334. {
  335. pxNewQueue->ucQueueType = ucQueueType;
  336. }
  337. #endif
  338. #if ( configUSE_QUEUE_SETS == 1 )
  339. {
  340. pxNewQueue->pxQueueSetContainer = NULL;
  341. }
  342. #endif
  343. /* Ensure the event queues start with the correct state. */
  344. vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
  345. vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
  346. vPortCPUInitializeMutex(&pxNewQueue->mux);
  347. traceCREATE_MUTEX( pxNewQueue );
  348. /* Start with the semaphore in the expected state. */
  349. ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
  350. }
  351. else
  352. {
  353. traceCREATE_MUTEX_FAILED();
  354. }
  355. configASSERT( pxNewQueue );
  356. return pxNewQueue;
  357. }
  358. #endif /* configUSE_MUTEXES */
  359. /*-----------------------------------------------------------*/
  360. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
  361. void* xQueueGetMutexHolder( QueueHandle_t xSemaphore )
  362. {
  363. Queue_t * const pxQueue = ( Queue_t * ) xSemaphore;
  364. void *pxReturn;
  365. /* This function is called by xSemaphoreGetMutexHolder(), and should not
  366. be called directly. Note: This is a good way of determining if the
  367. calling task is the mutex holder, but not a good way of determining the
  368. identity of the mutex holder, as the holder may change between the
  369. following critical section exiting and the function returning. */
  370. taskENTER_CRITICAL(&pxQueue->mux);
  371. {
  372. if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
  373. {
  374. pxReturn = ( void * ) ( ( Queue_t * ) xSemaphore )->pxMutexHolder;
  375. }
  376. else
  377. {
  378. pxReturn = NULL;
  379. }
  380. }
  381. taskEXIT_CRITICAL(&pxQueue->mux);
  382. return pxReturn;
  383. } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
  384. #endif
  385. /*-----------------------------------------------------------*/
  386. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  387. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  388. {
  389. BaseType_t xReturn;
  390. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  391. configASSERT( pxMutex );
  392. /* If this is the task that holds the mutex then pxMutexHolder will not
  393. change outside of this task. If this task does not hold the mutex then
  394. pxMutexHolder can never coincidentally equal the tasks handle, and as
  395. this is the only condition we are interested in it does not matter if
  396. pxMutexHolder is accessed simultaneously by another task. Therefore no
  397. mutual exclusion is required to test the pxMutexHolder variable. */
  398. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Not a redundant cast as TaskHandle_t is a typedef. */
  399. {
  400. traceGIVE_MUTEX_RECURSIVE( pxMutex );
  401. /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
  402. the task handle, therefore no underflow check is required. Also,
  403. uxRecursiveCallCount is only modified by the mutex holder, and as
  404. there can only be one, no mutual exclusion is required to modify the
  405. uxRecursiveCallCount member. */
  406. ( pxMutex->u.uxRecursiveCallCount )--;
  407. /* Have we unwound the call count? */
  408. if( pxMutex->u.uxRecursiveCallCount == ( UBaseType_t ) 0 )
  409. {
  410. /* Return the mutex. This will automatically unblock any other
  411. task that might be waiting to access the mutex. */
  412. ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  413. }
  414. else
  415. {
  416. mtCOVERAGE_TEST_MARKER();
  417. }
  418. xReturn = pdPASS;
  419. }
  420. else
  421. {
  422. /* The mutex cannot be given because the calling task is not the
  423. holder. */
  424. xReturn = pdFAIL;
  425. traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
  426. }
  427. return xReturn;
  428. }
  429. #endif /* configUSE_RECURSIVE_MUTEXES */
  430. /*-----------------------------------------------------------*/
  431. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  432. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait )
  433. {
  434. BaseType_t xReturn;
  435. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  436. configASSERT( pxMutex );
  437. /* Comments regarding mutual exclusion as per those within
  438. xQueueGiveMutexRecursive(). */
  439. traceTAKE_MUTEX_RECURSIVE( pxMutex );
  440. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  441. {
  442. ( pxMutex->u.uxRecursiveCallCount )++;
  443. xReturn = pdPASS;
  444. }
  445. else
  446. {
  447. xReturn = xQueueGenericReceive( pxMutex, NULL, xTicksToWait, pdFALSE );
  448. /* pdPASS will only be returned if the mutex was successfully
  449. obtained. The calling task may have entered the Blocked state
  450. before reaching here. */
  451. if( xReturn == pdPASS )
  452. {
  453. ( pxMutex->u.uxRecursiveCallCount )++;
  454. }
  455. else
  456. {
  457. traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
  458. }
  459. }
  460. return xReturn;
  461. }
  462. #endif /* configUSE_RECURSIVE_MUTEXES */
  463. /*-----------------------------------------------------------*/
  464. #if ( configUSE_COUNTING_SEMAPHORES == 1 )
  465. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )
  466. {
  467. QueueHandle_t xHandle;
  468. configASSERT( uxMaxCount != 0 );
  469. configASSERT( uxInitialCount <= uxMaxCount );
  470. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  471. if( xHandle != NULL )
  472. {
  473. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  474. traceCREATE_COUNTING_SEMAPHORE();
  475. }
  476. else
  477. {
  478. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  479. }
  480. configASSERT( xHandle );
  481. return xHandle;
  482. }
  483. #endif /* configUSE_COUNTING_SEMAPHORES */
  484. /*-----------------------------------------------------------*/
  485. BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
  486. {
  487. BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
  488. TimeOut_t xTimeOut;
  489. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  490. configASSERT( pxQueue );
  491. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  492. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  493. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  494. {
  495. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  496. }
  497. #endif
  498. /* This function relaxes the coding standard somewhat to allow return
  499. statements within the function itself. This is done in the interest
  500. of execution time efficiency. */
  501. for( ;; )
  502. {
  503. taskENTER_CRITICAL(&pxQueue->mux);
  504. {
  505. /* Is there room on the queue now? The running task must be
  506. the highest priority task wanting to access the queue. If
  507. the head item in the queue is to be overwritten then it does
  508. not matter if the queue is full. */
  509. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  510. {
  511. traceQUEUE_SEND( pxQueue );
  512. xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  513. #if ( configUSE_QUEUE_SETS == 1 )
  514. {
  515. if( pxQueue->pxQueueSetContainer != NULL )
  516. {
  517. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  518. {
  519. /* The queue is a member of a queue set, and posting
  520. to the queue set caused a higher priority task to
  521. unblock. A context switch is required. */
  522. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  523. }
  524. else
  525. {
  526. mtCOVERAGE_TEST_MARKER();
  527. }
  528. }
  529. else
  530. {
  531. /* If there was a task waiting for data to arrive on the
  532. queue then unblock it now. */
  533. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  534. {
  535. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  536. {
  537. /* The unblocked task has a priority higher than
  538. our own so yield immediately. Yes it is ok to
  539. do this from within the critical section - the
  540. kernel takes care of that. */
  541. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  542. }
  543. else
  544. {
  545. mtCOVERAGE_TEST_MARKER();
  546. }
  547. }
  548. else if( xYieldRequired != pdFALSE )
  549. {
  550. /* This path is a special case that will only get
  551. executed if the task was holding multiple mutexes
  552. and the mutexes were given back in an order that is
  553. different to that in which they were taken. */
  554. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  555. }
  556. else
  557. {
  558. mtCOVERAGE_TEST_MARKER();
  559. }
  560. }
  561. }
  562. #else /* configUSE_QUEUE_SETS */
  563. {
  564. /* If there was a task waiting for data to arrive on the
  565. queue then unblock it now. */
  566. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  567. {
  568. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  569. {
  570. /* The unblocked task has a priority higher than
  571. our own so yield immediately. Yes it is ok to do
  572. this from within the critical section - the kernel
  573. takes care of that. */
  574. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  575. }
  576. else
  577. {
  578. mtCOVERAGE_TEST_MARKER();
  579. }
  580. }
  581. else if( xYieldRequired != pdFALSE )
  582. {
  583. /* This path is a special case that will only get
  584. executed if the task was holding multiple mutexes and
  585. the mutexes were given back in an order that is
  586. different to that in which they were taken. */
  587. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  588. }
  589. else
  590. {
  591. mtCOVERAGE_TEST_MARKER();
  592. }
  593. }
  594. #endif /* configUSE_QUEUE_SETS */
  595. taskEXIT_CRITICAL(&pxQueue->mux);
  596. return pdPASS;
  597. }
  598. else
  599. {
  600. if( xTicksToWait == ( TickType_t ) 0 )
  601. {
  602. /* The queue was full and no block time is specified (or
  603. the block time has expired) so leave now. */
  604. taskEXIT_CRITICAL(&pxQueue->mux);
  605. /* Return to the original privilege level before exiting
  606. the function. */
  607. traceQUEUE_SEND_FAILED( pxQueue );
  608. return errQUEUE_FULL;
  609. }
  610. else if( xEntryTimeSet == pdFALSE )
  611. {
  612. /* The queue was full and a block time was specified so
  613. configure the timeout structure. */
  614. vTaskSetTimeOutState( &xTimeOut );
  615. xEntryTimeSet = pdTRUE;
  616. }
  617. else
  618. {
  619. /* Entry time was already set. */
  620. mtCOVERAGE_TEST_MARKER();
  621. }
  622. }
  623. }
  624. taskEXIT_CRITICAL(&pxQueue->mux);
  625. /* Interrupts and other tasks can send to and receive from the queue
  626. now the critical section has been exited. */
  627. taskENTER_CRITICAL(&pxQueue->mux);
  628. /* Update the timeout state to see if it has expired yet. */
  629. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  630. {
  631. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  632. {
  633. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  634. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  635. /* Resuming the scheduler will move tasks from the pending
  636. ready list into the ready list - so it is feasible that this
  637. task is already in a ready list before it yields - in which
  638. case the yield will not cause a context switch unless there
  639. is also a higher priority task in the pending ready list. */
  640. taskEXIT_CRITICAL(&pxQueue->mux);
  641. portYIELD_WITHIN_API();
  642. }
  643. else
  644. {
  645. /* Try again. */
  646. taskEXIT_CRITICAL(&pxQueue->mux);
  647. }
  648. }
  649. else
  650. {
  651. /* The timeout has expired. */
  652. taskEXIT_CRITICAL(&pxQueue->mux);
  653. /* Return to the original privilege level before exiting the
  654. function. */
  655. traceQUEUE_SEND_FAILED( pxQueue );
  656. return errQUEUE_FULL;
  657. }
  658. }
  659. }
  660. /*-----------------------------------------------------------*/
  661. #if ( configUSE_ALTERNATIVE_API == 1 )
  662. BaseType_t xQueueAltGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, BaseType_t xCopyPosition )
  663. {
  664. BaseType_t xEntryTimeSet = pdFALSE;
  665. TimeOut_t xTimeOut;
  666. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  667. configASSERT( pxQueue );
  668. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  669. for( ;; )
  670. {
  671. taskENTER_CRITICAL(&pxQueue->mux);
  672. {
  673. /* Is there room on the queue now? To be running we must be
  674. the highest priority task wanting to access the queue. */
  675. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  676. {
  677. traceQUEUE_SEND( pxQueue );
  678. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  679. /* If there was a task waiting for data to arrive on the
  680. queue then unblock it now. */
  681. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  682. {
  683. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  684. {
  685. /* The unblocked task has a priority higher than
  686. our own so yield immediately. */
  687. taskEXIT_CRITICAL(&pxQueue->mux);
  688. portYIELD_WITHIN_API();
  689. taskENTER_CRITICAL(&pxQueue->mux);
  690. }
  691. else
  692. {
  693. mtCOVERAGE_TEST_MARKER();
  694. }
  695. }
  696. else
  697. {
  698. mtCOVERAGE_TEST_MARKER();
  699. }
  700. taskEXIT_CRITICAL(&pxQueue->mux);
  701. return pdPASS;
  702. }
  703. else
  704. {
  705. if( xTicksToWait == ( TickType_t ) 0 )
  706. {
  707. taskEXIT_CRITICAL(&pxQueue->mux);
  708. return errQUEUE_FULL;
  709. }
  710. else if( xEntryTimeSet == pdFALSE )
  711. {
  712. vTaskSetTimeOutState( &xTimeOut );
  713. xEntryTimeSet = pdTRUE;
  714. }
  715. }
  716. }
  717. taskEXIT_CRITICAL(&pxQueue->mux);
  718. taskENTER_CRITICAL(&pxQueue->mux);
  719. {
  720. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  721. {
  722. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  723. {
  724. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  725. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  726. taskEXIT_CRITICAL(&pxQueue->mux);
  727. portYIELD_WITHIN_API();
  728. taskENTER_CRITICAL(&pxQueue->mux);
  729. }
  730. else
  731. {
  732. mtCOVERAGE_TEST_MARKER();
  733. }
  734. }
  735. else
  736. {
  737. taskEXIT_CRITICAL(&pxQueue->mux);
  738. traceQUEUE_SEND_FAILED( pxQueue );
  739. return errQUEUE_FULL;
  740. }
  741. }
  742. taskEXIT_CRITICAL(&pxQueue->mux);
  743. }
  744. }
  745. #endif /* configUSE_ALTERNATIVE_API */
  746. /*-----------------------------------------------------------*/
  747. #if ( configUSE_ALTERNATIVE_API == 1 )
  748. BaseType_t xQueueAltGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, BaseType_t xJustPeeking )
  749. {
  750. BaseType_t xEntryTimeSet = pdFALSE;
  751. TimeOut_t xTimeOut;
  752. int8_t *pcOriginalReadPosition;
  753. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  754. configASSERT( pxQueue );
  755. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  756. UNTESTED_FUNCTION();
  757. for( ;; )
  758. {
  759. taskENTER_CRITICAL();
  760. {
  761. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  762. {
  763. /* Remember our read position in case we are just peeking. */
  764. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  765. prvCopyDataFromQueue( pxQueue, pvBuffer );
  766. if( xJustPeeking == pdFALSE )
  767. {
  768. traceQUEUE_RECEIVE( pxQueue );
  769. /* Data is actually being removed (not just peeked). */
  770. --( pxQueue->uxMessagesWaiting );
  771. #if ( configUSE_MUTEXES == 1 )
  772. {
  773. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  774. {
  775. /* Record the information required to implement
  776. priority inheritance should it become necessary. */
  777. pxQueue->pxMutexHolder = ( int8_t * ) xTaskGetCurrentTaskHandle();
  778. }
  779. else
  780. {
  781. mtCOVERAGE_TEST_MARKER();
  782. }
  783. }
  784. #endif
  785. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  786. {
  787. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  788. {
  789. portYIELD_WITHIN_API();
  790. }
  791. else
  792. {
  793. mtCOVERAGE_TEST_MARKER();
  794. }
  795. }
  796. }
  797. else
  798. {
  799. traceQUEUE_PEEK( pxQueue );
  800. /* The data is not being removed, so reset our read
  801. pointer. */
  802. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  803. /* The data is being left in the queue, so see if there are
  804. any other tasks waiting for the data. */
  805. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  806. {
  807. /* Tasks that are removed from the event list will get added to
  808. the pending ready list as the scheduler is still suspended. */
  809. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  810. {
  811. /* The task waiting has a higher priority than this task. */
  812. portYIELD_WITHIN_API();
  813. }
  814. else
  815. {
  816. mtCOVERAGE_TEST_MARKER();
  817. }
  818. }
  819. else
  820. {
  821. mtCOVERAGE_TEST_MARKER();
  822. }
  823. }
  824. taskEXIT_CRITICAL();
  825. return pdPASS;
  826. }
  827. else
  828. {
  829. if( xTicksToWait == ( TickType_t ) 0 )
  830. {
  831. taskEXIT_CRITICAL();
  832. traceQUEUE_RECEIVE_FAILED( pxQueue );
  833. return errQUEUE_EMPTY;
  834. }
  835. else if( xEntryTimeSet == pdFALSE )
  836. {
  837. vTaskSetTimeOutState( &xTimeOut );
  838. xEntryTimeSet = pdTRUE;
  839. }
  840. }
  841. }
  842. taskEXIT_CRITICAL();
  843. taskENTER_CRITICAL();
  844. {
  845. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  846. {
  847. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  848. {
  849. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  850. #if ( configUSE_MUTEXES == 1 )
  851. {
  852. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  853. {
  854. taskENTER_CRITICAL();
  855. {
  856. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  857. }
  858. taskEXIT_CRITICAL();
  859. }
  860. else
  861. {
  862. mtCOVERAGE_TEST_MARKER();
  863. }
  864. }
  865. #endif
  866. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  867. portYIELD_WITHIN_API();
  868. }
  869. else
  870. {
  871. mtCOVERAGE_TEST_MARKER();
  872. }
  873. }
  874. else
  875. {
  876. taskEXIT_CRITICAL();
  877. traceQUEUE_RECEIVE_FAILED( pxQueue );
  878. return errQUEUE_EMPTY;
  879. }
  880. }
  881. taskEXIT_CRITICAL();
  882. }
  883. }
  884. #endif /* configUSE_ALTERNATIVE_API */
  885. /*-----------------------------------------------------------*/
  886. BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )
  887. {
  888. BaseType_t xReturn;
  889. UBaseType_t uxSavedInterruptStatus;
  890. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  891. configASSERT( pxQueue );
  892. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  893. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  894. /* RTOS ports that support interrupt nesting have the concept of a maximum
  895. system call (or maximum API call) interrupt priority. Interrupts that are
  896. above the maximum system call priority are kept permanently enabled, even
  897. when the RTOS kernel is in a critical section, but cannot make any calls to
  898. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  899. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  900. failure if a FreeRTOS API function is called from an interrupt that has been
  901. assigned a priority above the configured maximum system call priority.
  902. Only FreeRTOS functions that end in FromISR can be called from interrupts
  903. that have been assigned a priority at or (logically) below the maximum
  904. system call interrupt priority. FreeRTOS maintains a separate interrupt
  905. safe API to ensure interrupt entry is as fast and as simple as possible.
  906. More information (albeit Cortex-M specific) is provided on the following
  907. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  908. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  909. /* Similar to xQueueGenericSend, except without blocking if there is no room
  910. in the queue. Also don't directly wake a task that was blocked on a queue
  911. read, instead return a flag to say whether a context switch is required or
  912. not (i.e. has a task with a higher priority than us been woken by this
  913. post). */
  914. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  915. {
  916. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  917. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  918. {
  919. traceQUEUE_SEND_FROM_ISR( pxQueue );
  920. /* A task can only have an inherited priority if it is a mutex
  921. holder - and if there is a mutex holder then the mutex cannot be
  922. given from an ISR. Therefore, unlike the xQueueGenericGive()
  923. function, there is no need to determine the need for priority
  924. disinheritance here or to clear the mutex holder TCB member. */
  925. ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  926. #if ( configUSE_QUEUE_SETS == 1 )
  927. {
  928. if( pxQueue->pxQueueSetContainer != NULL )
  929. {
  930. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  931. {
  932. /* The queue is a member of a queue set, and posting
  933. to the queue set caused a higher priority task to
  934. unblock. A context switch is required. */
  935. if( pxHigherPriorityTaskWoken != NULL )
  936. {
  937. *pxHigherPriorityTaskWoken = pdTRUE;
  938. }
  939. else
  940. {
  941. mtCOVERAGE_TEST_MARKER();
  942. }
  943. }
  944. else
  945. {
  946. mtCOVERAGE_TEST_MARKER();
  947. }
  948. }
  949. else
  950. {
  951. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  952. {
  953. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  954. {
  955. /* The task waiting has a higher priority so
  956. record that a context switch is required. */
  957. if( pxHigherPriorityTaskWoken != NULL )
  958. {
  959. *pxHigherPriorityTaskWoken = pdTRUE;
  960. }
  961. else
  962. {
  963. mtCOVERAGE_TEST_MARKER();
  964. }
  965. }
  966. else
  967. {
  968. mtCOVERAGE_TEST_MARKER();
  969. }
  970. }
  971. else
  972. {
  973. mtCOVERAGE_TEST_MARKER();
  974. }
  975. }
  976. }
  977. #else /* configUSE_QUEUE_SETS */
  978. {
  979. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  980. {
  981. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  982. {
  983. /* The task waiting has a higher priority so record that a
  984. context switch is required. */
  985. if( pxHigherPriorityTaskWoken != NULL )
  986. {
  987. *pxHigherPriorityTaskWoken = pdTRUE;
  988. }
  989. else
  990. {
  991. mtCOVERAGE_TEST_MARKER();
  992. }
  993. }
  994. else
  995. {
  996. mtCOVERAGE_TEST_MARKER();
  997. }
  998. }
  999. else
  1000. {
  1001. mtCOVERAGE_TEST_MARKER();
  1002. }
  1003. }
  1004. #endif /* configUSE_QUEUE_SETS */
  1005. xReturn = pdPASS;
  1006. }
  1007. else
  1008. {
  1009. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1010. xReturn = errQUEUE_FULL;
  1011. }
  1012. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1013. }
  1014. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1015. return xReturn;
  1016. }
  1017. /*-----------------------------------------------------------*/
  1018. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )
  1019. {
  1020. BaseType_t xReturn;
  1021. UBaseType_t uxSavedInterruptStatus;
  1022. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1023. configASSERT( pxQueue );
  1024. /* xQueueGenericSendFromISR() should be used in the item size is not 0. */
  1025. configASSERT( pxQueue->uxItemSize == 0 );
  1026. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1027. system call (or maximum API call) interrupt priority. Interrupts that are
  1028. above the maximum system call priority are kept permanently enabled, even
  1029. when the RTOS kernel is in a critical section, but cannot make any calls to
  1030. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1031. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1032. failure if a FreeRTOS API function is called from an interrupt that has been
  1033. assigned a priority above the configured maximum system call priority.
  1034. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1035. that have been assigned a priority at or (logically) below the maximum
  1036. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1037. safe API to ensure interrupt entry is as fast and as simple as possible.
  1038. More information (albeit Cortex-M specific) is provided on the following
  1039. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1040. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1041. /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
  1042. item size is 0. Don't directly wake a task that was blocked on a queue
  1043. read, instead return a flag to say whether a context switch is required or
  1044. not (i.e. has a task with a higher priority than us been woken by this
  1045. post). */
  1046. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1047. {
  1048. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1049. /* When the queue is used to implement a semaphore no data is ever
  1050. moved through the queue but it is still valid to see if the queue 'has
  1051. space'. */
  1052. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1053. {
  1054. traceQUEUE_SEND_FROM_ISR( pxQueue );
  1055. /* A task can only have an inherited priority if it is a mutex
  1056. holder - and if there is a mutex holder then the mutex cannot be
  1057. given from an ISR. Therefore, unlike the xQueueGenericGive()
  1058. function, there is no need to determine the need for priority
  1059. disinheritance here or to clear the mutex holder TCB member. */
  1060. ++( pxQueue->uxMessagesWaiting );
  1061. #if ( configUSE_QUEUE_SETS == 1 )
  1062. {
  1063. if( pxQueue->pxQueueSetContainer != NULL )
  1064. {
  1065. if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )
  1066. {
  1067. /* The semaphore is a member of a queue set, and
  1068. posting to the queue set caused a higher priority
  1069. task to unblock. A context switch is required. */
  1070. if( pxHigherPriorityTaskWoken != NULL )
  1071. {
  1072. *pxHigherPriorityTaskWoken = pdTRUE;
  1073. }
  1074. else
  1075. {
  1076. mtCOVERAGE_TEST_MARKER();
  1077. }
  1078. }
  1079. else
  1080. {
  1081. mtCOVERAGE_TEST_MARKER();
  1082. }
  1083. }
  1084. else
  1085. {
  1086. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1087. {
  1088. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1089. {
  1090. /* The task waiting has a higher priority so
  1091. record that a context switch is required. */
  1092. if( pxHigherPriorityTaskWoken != NULL )
  1093. {
  1094. *pxHigherPriorityTaskWoken = pdTRUE;
  1095. }
  1096. else
  1097. {
  1098. mtCOVERAGE_TEST_MARKER();
  1099. }
  1100. }
  1101. else
  1102. {
  1103. mtCOVERAGE_TEST_MARKER();
  1104. }
  1105. }
  1106. else
  1107. {
  1108. mtCOVERAGE_TEST_MARKER();
  1109. }
  1110. }
  1111. }
  1112. #else /* configUSE_QUEUE_SETS */
  1113. {
  1114. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1115. {
  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. if( pxHigherPriorityTaskWoken != NULL )
  1121. {
  1122. *pxHigherPriorityTaskWoken = pdTRUE;
  1123. }
  1124. else
  1125. {
  1126. mtCOVERAGE_TEST_MARKER();
  1127. }
  1128. }
  1129. else
  1130. {
  1131. mtCOVERAGE_TEST_MARKER();
  1132. }
  1133. }
  1134. else
  1135. {
  1136. mtCOVERAGE_TEST_MARKER();
  1137. }
  1138. }
  1139. #endif /* configUSE_QUEUE_SETS */
  1140. xReturn = pdPASS;
  1141. }
  1142. else
  1143. {
  1144. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1145. xReturn = errQUEUE_FULL;
  1146. }
  1147. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1148. }
  1149. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1150. return xReturn;
  1151. }
  1152. /*-----------------------------------------------------------*/
  1153. BaseType_t xQueueGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, const BaseType_t xJustPeeking )
  1154. {
  1155. BaseType_t xEntryTimeSet = pdFALSE;
  1156. TimeOut_t xTimeOut;
  1157. int8_t *pcOriginalReadPosition;
  1158. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1159. configASSERT( pxQueue );
  1160. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1161. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1162. {
  1163. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1164. }
  1165. #endif
  1166. /* This function relaxes the coding standard somewhat to allow return
  1167. statements within the function itself. This is done in the interest
  1168. of execution time efficiency. */
  1169. for( ;; )
  1170. {
  1171. taskENTER_CRITICAL(&pxQueue->mux);
  1172. {
  1173. /* Is there data in the queue now? To be running the calling task
  1174. must be the highest priority task wanting to access the queue. */
  1175. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1176. {
  1177. /* Remember the read position in case the queue is only being
  1178. peeked. */
  1179. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1180. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1181. if( xJustPeeking == pdFALSE )
  1182. {
  1183. traceQUEUE_RECEIVE( pxQueue );
  1184. /* Actually removing data, not just peeking. */
  1185. --( pxQueue->uxMessagesWaiting );
  1186. #if ( configUSE_MUTEXES == 1 )
  1187. {
  1188. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1189. {
  1190. /* Record the information required to implement
  1191. priority inheritance should it become necessary. */
  1192. pxQueue->pxMutexHolder = ( int8_t * ) pvTaskIncrementMutexHeldCount(); /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  1193. }
  1194. else
  1195. {
  1196. mtCOVERAGE_TEST_MARKER();
  1197. }
  1198. }
  1199. #endif /* configUSE_MUTEXES */
  1200. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1201. {
  1202. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  1203. {
  1204. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  1205. }
  1206. else
  1207. {
  1208. mtCOVERAGE_TEST_MARKER();
  1209. }
  1210. }
  1211. else
  1212. {
  1213. mtCOVERAGE_TEST_MARKER();
  1214. }
  1215. }
  1216. else
  1217. {
  1218. traceQUEUE_PEEK( pxQueue );
  1219. /* The data is not being removed, so reset the read
  1220. pointer. */
  1221. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1222. /* The data is being left in the queue, so see if there are
  1223. any other tasks waiting for the data. */
  1224. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1225. {
  1226. /* Tasks that are removed from the event list will get added to
  1227. the pending ready list as the scheduler is still suspended. */
  1228. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1229. {
  1230. /* The task waiting has a higher priority than this task. */
  1231. queueYIELD_IF_USING_PREEMPTION_MUX(&pxQueue->mux);
  1232. }
  1233. else
  1234. {
  1235. mtCOVERAGE_TEST_MARKER();
  1236. }
  1237. }
  1238. else
  1239. {
  1240. mtCOVERAGE_TEST_MARKER();
  1241. }
  1242. }
  1243. taskEXIT_CRITICAL(&pxQueue->mux);
  1244. return pdPASS;
  1245. }
  1246. else
  1247. {
  1248. if( xTicksToWait == ( TickType_t ) 0 )
  1249. {
  1250. /* The queue was empty and no block time is specified (or
  1251. the block time has expired) so leave now. */
  1252. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1253. taskEXIT_CRITICAL(&pxQueue->mux);
  1254. return errQUEUE_EMPTY;
  1255. }
  1256. else if( xEntryTimeSet == pdFALSE )
  1257. {
  1258. /* The queue was empty and a block time was specified so
  1259. configure the timeout structure. */
  1260. vTaskSetTimeOutState( &xTimeOut );
  1261. xEntryTimeSet = pdTRUE;
  1262. }
  1263. else
  1264. {
  1265. /* Entry time was already set. */
  1266. mtCOVERAGE_TEST_MARKER();
  1267. }
  1268. }
  1269. }
  1270. taskEXIT_CRITICAL(&pxQueue->mux);
  1271. /* Interrupts and other tasks can send to and receive from the queue
  1272. now the critical section has been exited. */
  1273. taskENTER_CRITICAL(&pxQueue->mux);
  1274. /* Update the timeout state to see if it has expired yet. */
  1275. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1276. {
  1277. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1278. {
  1279. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  1280. #if ( configUSE_MUTEXES == 1 )
  1281. {
  1282. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1283. {
  1284. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  1285. }
  1286. else
  1287. {
  1288. mtCOVERAGE_TEST_MARKER();
  1289. }
  1290. }
  1291. #endif
  1292. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1293. taskEXIT_CRITICAL(&pxQueue->mux);
  1294. portYIELD_WITHIN_API();
  1295. }
  1296. else
  1297. {
  1298. /* Try again. */
  1299. taskEXIT_CRITICAL(&pxQueue->mux);
  1300. }
  1301. }
  1302. else
  1303. {
  1304. taskEXIT_CRITICAL(&pxQueue->mux);
  1305. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1306. return errQUEUE_EMPTY;
  1307. }
  1308. }
  1309. }
  1310. /*-----------------------------------------------------------*/
  1311. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, BaseType_t * const pxHigherPriorityTaskWoken )
  1312. {
  1313. BaseType_t xReturn;
  1314. UBaseType_t uxSavedInterruptStatus;
  1315. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1316. configASSERT( pxQueue );
  1317. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1318. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1319. system call (or maximum API call) interrupt priority. Interrupts that are
  1320. above the maximum system call priority are kept permanently enabled, even
  1321. when the RTOS kernel is in a critical section, but cannot make any calls to
  1322. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1323. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1324. failure if a FreeRTOS API function is called from an interrupt that has been
  1325. assigned a priority above the configured maximum system call priority.
  1326. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1327. that have been assigned a priority at or (logically) below the maximum
  1328. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1329. safe API to ensure interrupt entry is as fast and as simple as possible.
  1330. More information (albeit Cortex-M specific) is provided on the following
  1331. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1332. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1333. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1334. {
  1335. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1336. /* Cannot block in an ISR, so check there is data available. */
  1337. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1338. {
  1339. traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
  1340. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1341. --( pxQueue->uxMessagesWaiting );
  1342. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1343. {
  1344. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1345. {
  1346. /* The task waiting has a higher priority than us so
  1347. force a context switch. */
  1348. if( pxHigherPriorityTaskWoken != NULL )
  1349. {
  1350. *pxHigherPriorityTaskWoken = pdTRUE;
  1351. }
  1352. else
  1353. {
  1354. mtCOVERAGE_TEST_MARKER();
  1355. }
  1356. }
  1357. else
  1358. {
  1359. mtCOVERAGE_TEST_MARKER();
  1360. }
  1361. }
  1362. else
  1363. {
  1364. mtCOVERAGE_TEST_MARKER();
  1365. }
  1366. xReturn = pdPASS;
  1367. }
  1368. else
  1369. {
  1370. xReturn = pdFAIL;
  1371. traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
  1372. }
  1373. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1374. }
  1375. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1376. return xReturn;
  1377. }
  1378. /*-----------------------------------------------------------*/
  1379. BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void * const pvBuffer )
  1380. {
  1381. BaseType_t xReturn;
  1382. UBaseType_t uxSavedInterruptStatus;
  1383. int8_t *pcOriginalReadPosition;
  1384. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1385. configASSERT( pxQueue );
  1386. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1387. configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
  1388. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1389. system call (or maximum API call) interrupt priority. Interrupts that are
  1390. above the maximum system call priority are kept permanently enabled, even
  1391. when the RTOS kernel is in a critical section, but cannot make any calls to
  1392. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1393. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1394. failure if a FreeRTOS API function is called from an interrupt that has been
  1395. assigned a priority above the configured maximum system call priority.
  1396. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1397. that have been assigned a priority at or (logically) below the maximum
  1398. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1399. safe API to ensure interrupt entry is as fast and as simple as possible.
  1400. More information (albeit Cortex-M specific) is provided on the following
  1401. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1402. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1403. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1404. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1405. {
  1406. /* Cannot block in an ISR, so check there is data available. */
  1407. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1408. {
  1409. traceQUEUE_PEEK_FROM_ISR( pxQueue );
  1410. /* Remember the read position so it can be reset as nothing is
  1411. actually being removed from the queue. */
  1412. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1413. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1414. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1415. xReturn = pdPASS;
  1416. }
  1417. else
  1418. {
  1419. xReturn = pdFAIL;
  1420. traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
  1421. }
  1422. }
  1423. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1424. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1425. return xReturn;
  1426. }
  1427. /*-----------------------------------------------------------*/
  1428. UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
  1429. {
  1430. UBaseType_t uxReturn;
  1431. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1432. configASSERT( xQueue );
  1433. taskENTER_CRITICAL(&pxQueue->mux);
  1434. {
  1435. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1436. }
  1437. taskEXIT_CRITICAL(&pxQueue->mux);
  1438. return uxReturn;
  1439. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1440. /*-----------------------------------------------------------*/
  1441. UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
  1442. {
  1443. UBaseType_t uxReturn;
  1444. Queue_t *pxQueue;
  1445. pxQueue = ( Queue_t * ) xQueue;
  1446. configASSERT( pxQueue );
  1447. taskENTER_CRITICAL(&pxQueue->mux);
  1448. {
  1449. uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
  1450. }
  1451. taskEXIT_CRITICAL(&pxQueue->mux);
  1452. return uxReturn;
  1453. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1454. /*-----------------------------------------------------------*/
  1455. UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
  1456. {
  1457. UBaseType_t uxReturn;
  1458. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1459. configASSERT( xQueue );
  1460. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1461. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1462. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1463. return uxReturn;
  1464. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1465. /*-----------------------------------------------------------*/
  1466. void vQueueDelete( QueueHandle_t xQueue )
  1467. {
  1468. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1469. configASSERT( pxQueue );
  1470. traceQUEUE_DELETE( pxQueue );
  1471. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1472. {
  1473. vQueueUnregisterQueue( pxQueue );
  1474. }
  1475. #endif
  1476. vPortFree( pxQueue );
  1477. }
  1478. /*-----------------------------------------------------------*/
  1479. #if ( configUSE_TRACE_FACILITY == 1 )
  1480. UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
  1481. {
  1482. return ( ( Queue_t * ) xQueue )->uxQueueNumber;
  1483. }
  1484. #endif /* configUSE_TRACE_FACILITY */
  1485. /*-----------------------------------------------------------*/
  1486. #if ( configUSE_TRACE_FACILITY == 1 )
  1487. void vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber )
  1488. {
  1489. ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
  1490. }
  1491. #endif /* configUSE_TRACE_FACILITY */
  1492. /*-----------------------------------------------------------*/
  1493. #if ( configUSE_TRACE_FACILITY == 1 )
  1494. uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
  1495. {
  1496. return ( ( Queue_t * ) xQueue )->ucQueueType;
  1497. }
  1498. #endif /* configUSE_TRACE_FACILITY */
  1499. /*-----------------------------------------------------------*/
  1500. //This routine assumes the queue has already been locked.
  1501. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition )
  1502. {
  1503. BaseType_t xReturn = pdFALSE;
  1504. if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
  1505. {
  1506. #if ( configUSE_MUTEXES == 1 )
  1507. {
  1508. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1509. {
  1510. /* The mutex is no longer being held. */
  1511. xReturn = xTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
  1512. pxQueue->pxMutexHolder = NULL;
  1513. }
  1514. else
  1515. {
  1516. mtCOVERAGE_TEST_MARKER();
  1517. }
  1518. }
  1519. #endif /* configUSE_MUTEXES */
  1520. }
  1521. else if( xPosition == queueSEND_TO_BACK )
  1522. {
  1523. ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0. */
  1524. pxQueue->pcWriteTo += pxQueue->uxItemSize;
  1525. if( pxQueue->pcWriteTo >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1526. {
  1527. pxQueue->pcWriteTo = pxQueue->pcHead;
  1528. }
  1529. else
  1530. {
  1531. mtCOVERAGE_TEST_MARKER();
  1532. }
  1533. }
  1534. else
  1535. {
  1536. ( void ) memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1537. pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;
  1538. if( pxQueue->u.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1539. {
  1540. pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
  1541. }
  1542. else
  1543. {
  1544. mtCOVERAGE_TEST_MARKER();
  1545. }
  1546. if( xPosition == queueOVERWRITE )
  1547. {
  1548. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1549. {
  1550. /* An item is not being added but overwritten, so subtract
  1551. one from the recorded number of items in the queue so when
  1552. one is added again below the number of recorded items remains
  1553. correct. */
  1554. --( pxQueue->uxMessagesWaiting );
  1555. }
  1556. else
  1557. {
  1558. mtCOVERAGE_TEST_MARKER();
  1559. }
  1560. }
  1561. else
  1562. {
  1563. mtCOVERAGE_TEST_MARKER();
  1564. }
  1565. }
  1566. ++( pxQueue->uxMessagesWaiting );
  1567. return xReturn;
  1568. }
  1569. /*-----------------------------------------------------------*/
  1570. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer )
  1571. {
  1572. if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
  1573. {
  1574. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1575. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
  1576. {
  1577. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1578. }
  1579. else
  1580. {
  1581. mtCOVERAGE_TEST_MARKER();
  1582. }
  1583. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 MISRA exception as the casts are only redundant for some ports. Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0. */
  1584. }
  1585. }
  1586. /*-----------------------------------------------------------*/
  1587. static BaseType_t prvIsQueueEmpty( Queue_t *pxQueue )
  1588. {
  1589. BaseType_t xReturn;
  1590. //No lock needed: we read a base type.
  1591. {
  1592. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1593. {
  1594. xReturn = pdTRUE;
  1595. }
  1596. else
  1597. {
  1598. xReturn = pdFALSE;
  1599. }
  1600. }
  1601. return xReturn;
  1602. }
  1603. /*-----------------------------------------------------------*/
  1604. BaseType_t xQueueIsQueueEmptyFromISR( QueueHandle_t xQueue )
  1605. {
  1606. BaseType_t xReturn;
  1607. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1608. configASSERT( xQueue );
  1609. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1610. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1611. {
  1612. xReturn = pdTRUE;
  1613. }
  1614. else
  1615. {
  1616. xReturn = pdFALSE;
  1617. }
  1618. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1619. return xReturn;
  1620. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1621. /*-----------------------------------------------------------*/
  1622. static BaseType_t prvIsQueueFull( Queue_t *pxQueue )
  1623. {
  1624. BaseType_t xReturn;
  1625. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1626. {
  1627. if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
  1628. {
  1629. xReturn = pdTRUE;
  1630. }
  1631. else
  1632. {
  1633. xReturn = pdFALSE;
  1634. }
  1635. }
  1636. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1637. return xReturn;
  1638. }
  1639. /*-----------------------------------------------------------*/
  1640. BaseType_t xQueueIsQueueFullFromISR( QueueHandle_t xQueue )
  1641. {
  1642. BaseType_t xReturn;
  1643. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1644. configASSERT( xQueue );
  1645. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1646. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( ( Queue_t * ) xQueue )->uxLength )
  1647. {
  1648. xReturn = pdTRUE;
  1649. }
  1650. else
  1651. {
  1652. xReturn = pdFALSE;
  1653. }
  1654. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1655. return xReturn;
  1656. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1657. /*-----------------------------------------------------------*/
  1658. #if ( configUSE_CO_ROUTINES == 1 )
  1659. BaseType_t xQueueCRSend( QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait )
  1660. {
  1661. BaseType_t xReturn;
  1662. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1663. UNTESTED_FUNCTION();
  1664. /* If the queue is already full we may have to block. A critical section
  1665. is required to prevent an interrupt removing something from the queue
  1666. between the check to see if the queue is full and blocking on the queue. */
  1667. portDISABLE_INTERRUPTS();
  1668. {
  1669. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  1670. {
  1671. /* The queue is full - do we want to block or just leave without
  1672. posting? */
  1673. if( xTicksToWait > ( TickType_t ) 0 )
  1674. {
  1675. /* As this is called from a coroutine we cannot block directly, but
  1676. return indicating that we need to block. */
  1677. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
  1678. portENABLE_INTERRUPTS();
  1679. return errQUEUE_BLOCKED;
  1680. }
  1681. else
  1682. {
  1683. portENABLE_INTERRUPTS();
  1684. return errQUEUE_FULL;
  1685. }
  1686. }
  1687. }
  1688. portENABLE_INTERRUPTS();
  1689. portDISABLE_INTERRUPTS();
  1690. {
  1691. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1692. {
  1693. /* There is room in the queue, copy the data into the queue. */
  1694. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1695. xReturn = pdPASS;
  1696. /* Were any co-routines waiting for data to become available? */
  1697. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1698. {
  1699. /* In this instance the co-routine could be placed directly
  1700. into the ready list as we are within a critical section.
  1701. Instead the same pending ready list mechanism is used as if
  1702. the event were caused from within an interrupt. */
  1703. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1704. {
  1705. /* The co-routine waiting has a higher priority so record
  1706. that a yield might be appropriate. */
  1707. xReturn = errQUEUE_YIELD;
  1708. }
  1709. else
  1710. {
  1711. mtCOVERAGE_TEST_MARKER();
  1712. }
  1713. }
  1714. else
  1715. {
  1716. mtCOVERAGE_TEST_MARKER();
  1717. }
  1718. }
  1719. else
  1720. {
  1721. xReturn = errQUEUE_FULL;
  1722. }
  1723. }
  1724. portENABLE_INTERRUPTS();
  1725. return xReturn;
  1726. }
  1727. #endif /* configUSE_CO_ROUTINES */
  1728. /*-----------------------------------------------------------*/
  1729. #if ( configUSE_CO_ROUTINES == 1 )
  1730. BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait )
  1731. {
  1732. BaseType_t xReturn;
  1733. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1734. /* If the queue is already empty we may have to block. A critical section
  1735. is required to prevent an interrupt adding something to the queue
  1736. between the check to see if the queue is empty and blocking on the queue. */
  1737. portDISABLE_INTERRUPTS();
  1738. {
  1739. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1740. {
  1741. /* There are no messages in the queue, do we want to block or just
  1742. leave with nothing? */
  1743. if( xTicksToWait > ( TickType_t ) 0 )
  1744. {
  1745. /* As this is a co-routine we cannot block directly, but return
  1746. indicating that we need to block. */
  1747. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
  1748. portENABLE_INTERRUPTS();
  1749. return errQUEUE_BLOCKED;
  1750. }
  1751. else
  1752. {
  1753. portENABLE_INTERRUPTS();
  1754. return errQUEUE_FULL;
  1755. }
  1756. }
  1757. else
  1758. {
  1759. mtCOVERAGE_TEST_MARKER();
  1760. }
  1761. }
  1762. portENABLE_INTERRUPTS();
  1763. portDISABLE_INTERRUPTS();
  1764. {
  1765. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1766. {
  1767. /* Data is available from the queue. */
  1768. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1769. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1770. {
  1771. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1772. }
  1773. else
  1774. {
  1775. mtCOVERAGE_TEST_MARKER();
  1776. }
  1777. --( pxQueue->uxMessagesWaiting );
  1778. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1779. xReturn = pdPASS;
  1780. /* Were any co-routines waiting for space to become available? */
  1781. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1782. {
  1783. /* In this instance the co-routine could be placed directly
  1784. into the ready list as we are within a critical section.
  1785. Instead the same pending ready list mechanism is used as if
  1786. the event were caused from within an interrupt. */
  1787. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1788. {
  1789. xReturn = errQUEUE_YIELD;
  1790. }
  1791. else
  1792. {
  1793. mtCOVERAGE_TEST_MARKER();
  1794. }
  1795. }
  1796. else
  1797. {
  1798. mtCOVERAGE_TEST_MARKER();
  1799. }
  1800. }
  1801. else
  1802. {
  1803. xReturn = pdFAIL;
  1804. }
  1805. }
  1806. portENABLE_INTERRUPTS();
  1807. return xReturn;
  1808. }
  1809. #endif /* configUSE_CO_ROUTINES */
  1810. /*-----------------------------------------------------------*/
  1811. #if ( configUSE_CO_ROUTINES == 1 )
  1812. BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t xCoRoutinePreviouslyWoken )
  1813. {
  1814. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1815. /* Cannot block within an ISR so if there is no space on the queue then
  1816. exit without doing anything. */
  1817. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1818. {
  1819. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1820. /* We only want to wake one co-routine per ISR, so check that a
  1821. co-routine has not already been woken. */
  1822. if( xCoRoutinePreviouslyWoken == pdFALSE )
  1823. {
  1824. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1825. {
  1826. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1827. {
  1828. return pdTRUE;
  1829. }
  1830. else
  1831. {
  1832. mtCOVERAGE_TEST_MARKER();
  1833. }
  1834. }
  1835. else
  1836. {
  1837. mtCOVERAGE_TEST_MARKER();
  1838. }
  1839. }
  1840. else
  1841. {
  1842. mtCOVERAGE_TEST_MARKER();
  1843. }
  1844. }
  1845. else
  1846. {
  1847. mtCOVERAGE_TEST_MARKER();
  1848. }
  1849. return xCoRoutinePreviouslyWoken;
  1850. }
  1851. #endif /* configUSE_CO_ROUTINES */
  1852. /*-----------------------------------------------------------*/
  1853. #if ( configUSE_CO_ROUTINES == 1 )
  1854. BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxCoRoutineWoken )
  1855. {
  1856. BaseType_t xReturn;
  1857. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1858. /* We cannot block from an ISR, so check there is data available. If
  1859. not then just leave without doing anything. */
  1860. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1861. {
  1862. /* Copy the data from the queue. */
  1863. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1864. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1865. {
  1866. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1867. }
  1868. else
  1869. {
  1870. mtCOVERAGE_TEST_MARKER();
  1871. }
  1872. --( pxQueue->uxMessagesWaiting );
  1873. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1874. if( ( *pxCoRoutineWoken ) == pdFALSE )
  1875. {
  1876. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1877. {
  1878. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1879. {
  1880. *pxCoRoutineWoken = pdTRUE;
  1881. }
  1882. else
  1883. {
  1884. mtCOVERAGE_TEST_MARKER();
  1885. }
  1886. }
  1887. else
  1888. {
  1889. mtCOVERAGE_TEST_MARKER();
  1890. }
  1891. }
  1892. else
  1893. {
  1894. mtCOVERAGE_TEST_MARKER();
  1895. }
  1896. xReturn = pdPASS;
  1897. }
  1898. else
  1899. {
  1900. xReturn = pdFAIL;
  1901. }
  1902. return xReturn;
  1903. }
  1904. #endif /* configUSE_CO_ROUTINES */
  1905. /*-----------------------------------------------------------*/
  1906. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1907. void vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  1908. {
  1909. UBaseType_t ux;
  1910. UNTESTED_FUNCTION();
  1911. /* See if there is an empty space in the registry. A NULL name denotes
  1912. a free slot. */
  1913. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  1914. {
  1915. if( xQueueRegistry[ ux ].pcQueueName == NULL )
  1916. {
  1917. /* Store the information on this queue. */
  1918. xQueueRegistry[ ux ].pcQueueName = pcQueueName;
  1919. xQueueRegistry[ ux ].xHandle = xQueue;
  1920. traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
  1921. break;
  1922. }
  1923. else
  1924. {
  1925. mtCOVERAGE_TEST_MARKER();
  1926. }
  1927. }
  1928. }
  1929. #endif /* configQUEUE_REGISTRY_SIZE */
  1930. /*-----------------------------------------------------------*/
  1931. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1932. void vQueueUnregisterQueue( QueueHandle_t xQueue )
  1933. {
  1934. UBaseType_t ux;
  1935. /* See if the handle of the queue being unregistered in actually in the
  1936. registry. */
  1937. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  1938. {
  1939. if( xQueueRegistry[ ux ].xHandle == xQueue )
  1940. {
  1941. /* Set the name to NULL to show that this slot if free again. */
  1942. xQueueRegistry[ ux ].pcQueueName = NULL;
  1943. break;
  1944. }
  1945. else
  1946. {
  1947. mtCOVERAGE_TEST_MARKER();
  1948. }
  1949. }
  1950. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1951. #endif /* configQUEUE_REGISTRY_SIZE */
  1952. /*-----------------------------------------------------------*/
  1953. #if ( configUSE_TIMERS == 1 )
  1954. void vQueueWaitForMessageRestricted( QueueHandle_t xQueue, TickType_t xTicksToWait )
  1955. {
  1956. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1957. /* This function should not be called by application code hence the
  1958. 'Restricted' in its name. It is not part of the public API. It is
  1959. designed for use by kernel code, and has special calling requirements.
  1960. It can result in vListInsert() being called on a list that can only
  1961. possibly ever have one item in it, so the list will be fast, but even
  1962. so it should be called with the scheduler locked and not from a critical
  1963. section. */
  1964. /* Only do anything if there are no messages in the queue. This function
  1965. will not actually cause the task to block, just place it on a blocked
  1966. list. It will not block until the scheduler is unlocked - at which
  1967. time a yield will be performed. */
  1968. taskENTER_CRITICAL(&pxQueue->mux);
  1969. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
  1970. {
  1971. /* There is nothing in the queue, block for the specified period. */
  1972. vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1973. }
  1974. else
  1975. {
  1976. mtCOVERAGE_TEST_MARKER();
  1977. }
  1978. taskEXIT_CRITICAL(&pxQueue->mux);
  1979. }
  1980. #endif /* configUSE_TIMERS */
  1981. /*-----------------------------------------------------------*/
  1982. #if ( configUSE_QUEUE_SETS == 1 )
  1983. QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
  1984. {
  1985. QueueSetHandle_t pxQueue;
  1986. pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
  1987. return pxQueue;
  1988. }
  1989. #endif /* configUSE_QUEUE_SETS */
  1990. /*-----------------------------------------------------------*/
  1991. #if ( configUSE_QUEUE_SETS == 1 )
  1992. BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  1993. {
  1994. BaseType_t xReturn;
  1995. //ToDo: figure out locking
  1996. // taskENTER_CRITICAL(&pxQueue->mux);
  1997. {
  1998. if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
  1999. {
  2000. /* Cannot add a queue/semaphore to more than one queue set. */
  2001. xReturn = pdFAIL;
  2002. }
  2003. else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2004. {
  2005. /* Cannot add a queue/semaphore to a queue set if there are already
  2006. items in the queue/semaphore. */
  2007. xReturn = pdFAIL;
  2008. }
  2009. else
  2010. {
  2011. ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
  2012. xReturn = pdPASS;
  2013. }
  2014. }
  2015. // taskEXIT_CRITICAL(&pxQueue->mux);
  2016. return xReturn;
  2017. }
  2018. #endif /* configUSE_QUEUE_SETS */
  2019. /*-----------------------------------------------------------*/
  2020. #if ( configUSE_QUEUE_SETS == 1 )
  2021. BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2022. {
  2023. BaseType_t xReturn;
  2024. Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
  2025. if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
  2026. {
  2027. /* The queue was not a member of the set. */
  2028. xReturn = pdFAIL;
  2029. }
  2030. else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2031. {
  2032. /* It is dangerous to remove a queue from a set when the queue is
  2033. not empty because the queue set will still hold pending events for
  2034. the queue. */
  2035. xReturn = pdFAIL;
  2036. }
  2037. else
  2038. {
  2039. // taskENTER_CRITICAL(&pxQueue->mux);
  2040. {
  2041. /* The queue is no longer contained in the set. */
  2042. pxQueueOrSemaphore->pxQueueSetContainer = NULL;
  2043. }
  2044. // taskEXIT_CRITICAL(&pxQueue->mux);
  2045. xReturn = pdPASS;
  2046. }
  2047. return xReturn;
  2048. } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
  2049. #endif /* configUSE_QUEUE_SETS */
  2050. /*-----------------------------------------------------------*/
  2051. #if ( configUSE_QUEUE_SETS == 1 )
  2052. QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, TickType_t const xTicksToWait )
  2053. {
  2054. QueueSetMemberHandle_t xReturn = NULL;
  2055. ( void ) xQueueGenericReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait, pdFALSE ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2056. return xReturn;
  2057. }
  2058. #endif /* configUSE_QUEUE_SETS */
  2059. /*-----------------------------------------------------------*/
  2060. #if ( configUSE_QUEUE_SETS == 1 )
  2061. QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
  2062. {
  2063. QueueSetMemberHandle_t xReturn = NULL;
  2064. ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2065. return xReturn;
  2066. }
  2067. #endif /* configUSE_QUEUE_SETS */
  2068. /*-----------------------------------------------------------*/
  2069. #if ( configUSE_QUEUE_SETS == 1 )
  2070. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition )
  2071. {
  2072. Queue_t *pxQueueSetContainer = pxQueue->pxQueueSetContainer;
  2073. BaseType_t xReturn = pdFALSE;
  2074. /* This function must be called form a critical section. */
  2075. configASSERT( pxQueueSetContainer );
  2076. configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
  2077. if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
  2078. {
  2079. traceQUEUE_SEND( pxQueueSetContainer );
  2080. /* The data copied is the handle of the queue that contains data. */
  2081. xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
  2082. if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
  2083. {
  2084. if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
  2085. {
  2086. /* The task waiting has a higher priority */
  2087. xReturn = pdTRUE;
  2088. }
  2089. else
  2090. {
  2091. mtCOVERAGE_TEST_MARKER();
  2092. }
  2093. }
  2094. else
  2095. {
  2096. mtCOVERAGE_TEST_MARKER();
  2097. }
  2098. }
  2099. else
  2100. {
  2101. mtCOVERAGE_TEST_MARKER();
  2102. }
  2103. return xReturn;
  2104. }
  2105. #endif /* configUSE_QUEUE_SETS */