queue.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  31. all the API functions to use the MPU wrappers. That should only be done when
  32. task.h is included from an application file. */
  33. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. #include "queue.h"
  37. #if ( configUSE_CO_ROUTINES == 1 )
  38. #include "croutine.h"
  39. #endif
  40. /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
  41. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  42. header files above, but not in this file, in order to generate the correct
  43. privileged Vs unprivileged linkage and placement. */
  44. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  45. /* Constants used with the cRxLock and cTxLock structure members. */
  46. #define queueUNLOCKED ( ( int8_t ) -1 )
  47. #define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
  48. /* When the Queue_t structure is used to represent a base queue its pcHead and
  49. pcTail members are used as pointers into the queue storage area. When the
  50. Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
  51. not necessary, and the pcHead pointer is set to NULL to indicate that the
  52. pcTail pointer actually points to the mutex holder (if any). Map alternative
  53. names to the pcHead and pcTail structure members to ensure the readability of
  54. the code is maintained despite this dual use of two structure members. An
  55. alternative implementation would be to use a union, but use of a union is
  56. against the coding standard (although an exception to the standard has been
  57. permitted where the dual use also significantly changes the type of the
  58. structure member). */
  59. #define pxMutexHolder pcTail
  60. #define uxQueueType pcHead
  61. #define queueQUEUE_IS_MUTEX NULL
  62. /* Semaphores do not actually store or copy data, so have an item size of
  63. zero. */
  64. #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
  65. #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
  66. #if( configUSE_PREEMPTION == 0 )
  67. /* If the cooperative scheduler is being used then a yield should not be
  68. performed just because a higher priority task has been woken. */
  69. #define queueYIELD_IF_USING_PREEMPTION()
  70. #else
  71. #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  72. #endif
  73. /*
  74. * Definition of the queue used by the scheduler.
  75. * Items are queued by copy, not reference. See the following link for the
  76. * rationale: http://www.freertos.org/Embedded-RTOS-Queues.html
  77. */
  78. typedef struct QueueDefinition
  79. {
  80. int8_t *pcHead; /*< Points to the beginning of the queue storage area. */
  81. 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. */
  82. int8_t *pcWriteTo; /*< Points to the free next place in the storage area. */
  83. 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). */
  84. {
  85. int8_t *pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
  86. 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. */
  87. } u;
  88. List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
  89. List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
  90. volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. */
  91. UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
  92. UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. */
  93. volatile int8_t cRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
  94. volatile int8_t cTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
  95. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  96. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
  97. #endif
  98. #if ( configUSE_QUEUE_SETS == 1 )
  99. struct QueueDefinition *pxQueueSetContainer;
  100. #endif
  101. #if ( configUSE_TRACE_FACILITY == 1 )
  102. UBaseType_t uxQueueNumber;
  103. uint8_t ucQueueType;
  104. #endif
  105. } xQUEUE;
  106. /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
  107. name below to enable the use of older kernel aware debuggers. */
  108. typedef xQUEUE Queue_t;
  109. /*-----------------------------------------------------------*/
  110. /*
  111. * The queue registry is just a means for kernel aware debuggers to locate
  112. * queue structures. It has no other purpose so is an optional component.
  113. */
  114. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  115. /* The type stored within the queue registry array. This allows a name
  116. to be assigned to each queue making kernel aware debugging a little
  117. more user friendly. */
  118. typedef struct QUEUE_REGISTRY_ITEM
  119. {
  120. const char *pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  121. QueueHandle_t xHandle;
  122. } xQueueRegistryItem;
  123. /* The old xQueueRegistryItem name is maintained above then typedefed to the
  124. new xQueueRegistryItem name below to enable the use of older kernel aware
  125. debuggers. */
  126. typedef xQueueRegistryItem QueueRegistryItem_t;
  127. /* The queue registry is simply an array of QueueRegistryItem_t structures.
  128. The pcQueueName member of a structure being NULL is indicative of the
  129. array position being vacant. */
  130. PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
  131. #endif /* configQUEUE_REGISTRY_SIZE */
  132. /*
  133. * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
  134. * prevent an ISR from adding or removing items to the queue, but does prevent
  135. * an ISR from removing tasks from the queue event lists. If an ISR finds a
  136. * queue is locked it will instead increment the appropriate queue lock count
  137. * to indicate that a task may require unblocking. When the queue in unlocked
  138. * these lock counts are inspected, and the appropriate action taken.
  139. */
  140. static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
  141. /*
  142. * Uses a critical section to determine if there is any data in a queue.
  143. *
  144. * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
  145. */
  146. static BaseType_t prvIsQueueEmpty( const Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  147. /*
  148. * Uses a critical section to determine if there is any space in a queue.
  149. *
  150. * @return pdTRUE if there is no space, otherwise pdFALSE;
  151. */
  152. static BaseType_t prvIsQueueFull( const Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  153. /*
  154. * Copies an item into the queue, either at the front of the queue or the
  155. * back of the queue.
  156. */
  157. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
  158. /*
  159. * Copies an item out of a queue.
  160. */
  161. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer ) PRIVILEGED_FUNCTION;
  162. #if ( configUSE_QUEUE_SETS == 1 )
  163. /*
  164. * Checks to see if a queue is a member of a queue set, and if so, notifies
  165. * the queue set that the queue contains data.
  166. */
  167. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION;
  168. #endif
  169. /*
  170. * Called after a Queue_t structure has been allocated either statically or
  171. * dynamically to fill in the structure's members.
  172. */
  173. static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
  174. /*
  175. * Mutexes are a special type of queue. When a mutex is created, first the
  176. * queue is created, then prvInitialiseMutex() is called to configure the queue
  177. * as a mutex.
  178. */
  179. #if( configUSE_MUTEXES == 1 )
  180. static void prvInitialiseMutex( Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
  181. #endif
  182. #if( configUSE_MUTEXES == 1 )
  183. /*
  184. * If a task waiting for a mutex causes the mutex holder to inherit a
  185. * priority, but the waiting task times out, then the holder should
  186. * disinherit the priority - but only down to the highest priority of any
  187. * other tasks that are waiting for the same mutex. This function returns
  188. * that priority.
  189. */
  190. static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
  191. #endif
  192. /*-----------------------------------------------------------*/
  193. /*
  194. * Macro to mark a queue as locked. Locking a queue prevents an ISR from
  195. * accessing the queue event lists.
  196. */
  197. #define prvLockQueue( pxQueue ) \
  198. taskENTER_CRITICAL(); \
  199. { \
  200. if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
  201. { \
  202. ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
  203. } \
  204. if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
  205. { \
  206. ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
  207. } \
  208. } \
  209. taskEXIT_CRITICAL()
  210. /*-----------------------------------------------------------*/
  211. BaseType_t xQueueGenericReset( QueueHandle_t xQueue, BaseType_t xNewQueue )
  212. {
  213. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  214. configASSERT( pxQueue );
  215. taskENTER_CRITICAL();
  216. {
  217. pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
  218. pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
  219. pxQueue->pcWriteTo = pxQueue->pcHead;
  220. pxQueue->u.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( UBaseType_t ) 1U ) * pxQueue->uxItemSize );
  221. pxQueue->cRxLock = queueUNLOCKED;
  222. pxQueue->cTxLock = queueUNLOCKED;
  223. if( xNewQueue == pdFALSE )
  224. {
  225. /* If there are tasks blocked waiting to read from the queue, then
  226. the tasks will remain blocked as after this function exits the queue
  227. will still be empty. If there are tasks blocked waiting to write to
  228. the queue, then one should be unblocked as after this function exits
  229. it will be possible to write to it. */
  230. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  231. {
  232. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  233. {
  234. queueYIELD_IF_USING_PREEMPTION();
  235. }
  236. else
  237. {
  238. mtCOVERAGE_TEST_MARKER();
  239. }
  240. }
  241. else
  242. {
  243. mtCOVERAGE_TEST_MARKER();
  244. }
  245. }
  246. else
  247. {
  248. /* Ensure the event queues start in the correct state. */
  249. vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
  250. vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
  251. }
  252. }
  253. taskEXIT_CRITICAL();
  254. /* A value is returned for calling semantic consistency with previous
  255. versions. */
  256. return pdPASS;
  257. }
  258. /*-----------------------------------------------------------*/
  259. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  260. QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType )
  261. {
  262. Queue_t *pxNewQueue;
  263. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  264. /* The StaticQueue_t structure and the queue storage area must be
  265. supplied. */
  266. configASSERT( pxStaticQueue != NULL );
  267. /* A queue storage area should be provided if the item size is not 0, and
  268. should not be provided if the item size is 0. */
  269. configASSERT( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) );
  270. configASSERT( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) );
  271. #if( configASSERT_DEFINED == 1 )
  272. {
  273. /* Sanity check that the size of the structure used to declare a
  274. variable of type StaticQueue_t or StaticSemaphore_t equals the size of
  275. the real queue and semaphore structures. */
  276. volatile size_t xSize = sizeof( StaticQueue_t );
  277. configASSERT( xSize == sizeof( Queue_t ) );
  278. }
  279. #endif /* configASSERT_DEFINED */
  280. /* The address of a statically allocated queue was passed in, use it.
  281. The address of a statically allocated storage area was also passed in
  282. but is already set. */
  283. pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
  284. if( pxNewQueue != NULL )
  285. {
  286. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  287. {
  288. /* Queues can be allocated wither statically or dynamically, so
  289. note this queue was allocated statically in case the queue is
  290. later deleted. */
  291. pxNewQueue->ucStaticallyAllocated = pdTRUE;
  292. }
  293. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  294. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  295. }
  296. else
  297. {
  298. traceQUEUE_CREATE_FAILED( ucQueueType );
  299. }
  300. return pxNewQueue;
  301. }
  302. #endif /* configSUPPORT_STATIC_ALLOCATION */
  303. /*-----------------------------------------------------------*/
  304. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  305. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
  306. {
  307. Queue_t *pxNewQueue;
  308. size_t xQueueSizeInBytes;
  309. uint8_t *pucQueueStorage;
  310. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  311. if( uxItemSize == ( UBaseType_t ) 0 )
  312. {
  313. /* There is not going to be a queue storage area. */
  314. xQueueSizeInBytes = ( size_t ) 0;
  315. }
  316. else
  317. {
  318. /* Allocate enough space to hold the maximum number of items that
  319. can be in the queue at any time. */
  320. xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  321. }
  322. pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
  323. if( pxNewQueue != NULL )
  324. {
  325. /* Jump past the queue structure to find the location of the queue
  326. storage area. */
  327. pucQueueStorage = ( ( uint8_t * ) pxNewQueue ) + sizeof( Queue_t );
  328. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  329. {
  330. /* Queues can be created either statically or dynamically, so
  331. note this task was created dynamically in case it is later
  332. deleted. */
  333. pxNewQueue->ucStaticallyAllocated = pdFALSE;
  334. }
  335. #endif /* configSUPPORT_STATIC_ALLOCATION */
  336. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  337. }
  338. else
  339. {
  340. traceQUEUE_CREATE_FAILED( ucQueueType );
  341. }
  342. return pxNewQueue;
  343. }
  344. #endif /* configSUPPORT_STATIC_ALLOCATION */
  345. /*-----------------------------------------------------------*/
  346. static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue )
  347. {
  348. /* Remove compiler warnings about unused parameters should
  349. configUSE_TRACE_FACILITY not be set to 1. */
  350. ( void ) ucQueueType;
  351. if( uxItemSize == ( UBaseType_t ) 0 )
  352. {
  353. /* No RAM was allocated for the queue storage area, but PC head cannot
  354. be set to NULL because NULL is used as a key to say the queue is used as
  355. a mutex. Therefore just set pcHead to point to the queue as a benign
  356. value that is known to be within the memory map. */
  357. pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
  358. }
  359. else
  360. {
  361. /* Set the head to the start of the queue storage area. */
  362. pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
  363. }
  364. /* Initialise the queue members as described where the queue type is
  365. defined. */
  366. pxNewQueue->uxLength = uxQueueLength;
  367. pxNewQueue->uxItemSize = uxItemSize;
  368. ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
  369. #if ( configUSE_TRACE_FACILITY == 1 )
  370. {
  371. pxNewQueue->ucQueueType = ucQueueType;
  372. }
  373. #endif /* configUSE_TRACE_FACILITY */
  374. #if( configUSE_QUEUE_SETS == 1 )
  375. {
  376. pxNewQueue->pxQueueSetContainer = NULL;
  377. }
  378. #endif /* configUSE_QUEUE_SETS */
  379. traceQUEUE_CREATE( pxNewQueue );
  380. }
  381. /*-----------------------------------------------------------*/
  382. #if( configUSE_MUTEXES == 1 )
  383. static void prvInitialiseMutex( Queue_t *pxNewQueue )
  384. {
  385. if( pxNewQueue != NULL )
  386. {
  387. /* The queue create function will set all the queue structure members
  388. correctly for a generic queue, but this function is creating a
  389. mutex. Overwrite those members that need to be set differently -
  390. in particular the information required for priority inheritance. */
  391. pxNewQueue->pxMutexHolder = NULL;
  392. pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
  393. /* In case this is a recursive mutex. */
  394. pxNewQueue->u.uxRecursiveCallCount = 0;
  395. traceCREATE_MUTEX( pxNewQueue );
  396. /* Start with the semaphore in the expected state. */
  397. ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
  398. }
  399. else
  400. {
  401. traceCREATE_MUTEX_FAILED();
  402. }
  403. }
  404. #endif /* configUSE_MUTEXES */
  405. /*-----------------------------------------------------------*/
  406. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  407. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  408. {
  409. Queue_t *pxNewQueue;
  410. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  411. pxNewQueue = ( Queue_t * ) xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
  412. prvInitialiseMutex( pxNewQueue );
  413. return pxNewQueue;
  414. }
  415. #endif /* configUSE_MUTEXES */
  416. /*-----------------------------------------------------------*/
  417. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  418. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue )
  419. {
  420. Queue_t *pxNewQueue;
  421. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  422. /* Prevent compiler warnings about unused parameters if
  423. configUSE_TRACE_FACILITY does not equal 1. */
  424. ( void ) ucQueueType;
  425. pxNewQueue = ( Queue_t * ) xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
  426. prvInitialiseMutex( pxNewQueue );
  427. return pxNewQueue;
  428. }
  429. #endif /* configUSE_MUTEXES */
  430. /*-----------------------------------------------------------*/
  431. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
  432. void* xQueueGetMutexHolder( QueueHandle_t xSemaphore )
  433. {
  434. void *pxReturn;
  435. /* This function is called by xSemaphoreGetMutexHolder(), and should not
  436. be called directly. Note: This is a good way of determining if the
  437. calling task is the mutex holder, but not a good way of determining the
  438. identity of the mutex holder, as the holder may change between the
  439. following critical section exiting and the function returning. */
  440. taskENTER_CRITICAL();
  441. {
  442. if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
  443. {
  444. pxReturn = ( void * ) ( ( Queue_t * ) xSemaphore )->pxMutexHolder;
  445. }
  446. else
  447. {
  448. pxReturn = NULL;
  449. }
  450. }
  451. taskEXIT_CRITICAL();
  452. return pxReturn;
  453. } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
  454. #endif
  455. /*-----------------------------------------------------------*/
  456. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
  457. void* xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
  458. {
  459. void *pxReturn;
  460. configASSERT( xSemaphore );
  461. /* Mutexes cannot be used in interrupt service routines, so the mutex
  462. holder should not change in an ISR, and therefore a critical section is
  463. not required here. */
  464. if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
  465. {
  466. pxReturn = ( void * ) ( ( Queue_t * ) xSemaphore )->pxMutexHolder;
  467. }
  468. else
  469. {
  470. pxReturn = NULL;
  471. }
  472. return pxReturn;
  473. } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
  474. #endif
  475. /*-----------------------------------------------------------*/
  476. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  477. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  478. {
  479. BaseType_t xReturn;
  480. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  481. configASSERT( pxMutex );
  482. /* If this is the task that holds the mutex then pxMutexHolder will not
  483. change outside of this task. If this task does not hold the mutex then
  484. pxMutexHolder can never coincidentally equal the tasks handle, and as
  485. this is the only condition we are interested in it does not matter if
  486. pxMutexHolder is accessed simultaneously by another task. Therefore no
  487. mutual exclusion is required to test the pxMutexHolder variable. */
  488. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Not a redundant cast as TaskHandle_t is a typedef. */
  489. {
  490. traceGIVE_MUTEX_RECURSIVE( pxMutex );
  491. /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
  492. the task handle, therefore no underflow check is required. Also,
  493. uxRecursiveCallCount is only modified by the mutex holder, and as
  494. there can only be one, no mutual exclusion is required to modify the
  495. uxRecursiveCallCount member. */
  496. ( pxMutex->u.uxRecursiveCallCount )--;
  497. /* Has the recursive call count unwound to 0? */
  498. if( pxMutex->u.uxRecursiveCallCount == ( UBaseType_t ) 0 )
  499. {
  500. /* Return the mutex. This will automatically unblock any other
  501. task that might be waiting to access the mutex. */
  502. ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  503. }
  504. else
  505. {
  506. mtCOVERAGE_TEST_MARKER();
  507. }
  508. xReturn = pdPASS;
  509. }
  510. else
  511. {
  512. /* The mutex cannot be given because the calling task is not the
  513. holder. */
  514. xReturn = pdFAIL;
  515. traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
  516. }
  517. return xReturn;
  518. }
  519. #endif /* configUSE_RECURSIVE_MUTEXES */
  520. /*-----------------------------------------------------------*/
  521. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  522. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait )
  523. {
  524. BaseType_t xReturn;
  525. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  526. configASSERT( pxMutex );
  527. /* Comments regarding mutual exclusion as per those within
  528. xQueueGiveMutexRecursive(). */
  529. traceTAKE_MUTEX_RECURSIVE( pxMutex );
  530. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  531. {
  532. ( pxMutex->u.uxRecursiveCallCount )++;
  533. xReturn = pdPASS;
  534. }
  535. else
  536. {
  537. xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
  538. /* pdPASS will only be returned if the mutex was successfully
  539. obtained. The calling task may have entered the Blocked state
  540. before reaching here. */
  541. if( xReturn != pdFAIL )
  542. {
  543. ( pxMutex->u.uxRecursiveCallCount )++;
  544. }
  545. else
  546. {
  547. traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
  548. }
  549. }
  550. return xReturn;
  551. }
  552. #endif /* configUSE_RECURSIVE_MUTEXES */
  553. /*-----------------------------------------------------------*/
  554. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  555. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue )
  556. {
  557. QueueHandle_t xHandle;
  558. configASSERT( uxMaxCount != 0 );
  559. configASSERT( uxInitialCount <= uxMaxCount );
  560. xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  561. if( xHandle != NULL )
  562. {
  563. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  564. traceCREATE_COUNTING_SEMAPHORE();
  565. }
  566. else
  567. {
  568. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  569. }
  570. return xHandle;
  571. }
  572. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  573. /*-----------------------------------------------------------*/
  574. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  575. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )
  576. {
  577. QueueHandle_t xHandle;
  578. configASSERT( uxMaxCount != 0 );
  579. configASSERT( uxInitialCount <= uxMaxCount );
  580. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  581. if( xHandle != NULL )
  582. {
  583. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  584. traceCREATE_COUNTING_SEMAPHORE();
  585. }
  586. else
  587. {
  588. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  589. }
  590. return xHandle;
  591. }
  592. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  593. /*-----------------------------------------------------------*/
  594. BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
  595. {
  596. BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
  597. TimeOut_t xTimeOut;
  598. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  599. configASSERT( pxQueue );
  600. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  601. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  602. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  603. {
  604. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  605. }
  606. #endif
  607. /* This function relaxes the coding standard somewhat to allow return
  608. statements within the function itself. This is done in the interest
  609. of execution time efficiency. */
  610. for( ;; )
  611. {
  612. taskENTER_CRITICAL();
  613. {
  614. /* Is there room on the queue now? The running task must be the
  615. highest priority task wanting to access the queue. If the head item
  616. in the queue is to be overwritten then it does not matter if the
  617. queue is full. */
  618. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  619. {
  620. traceQUEUE_SEND( pxQueue );
  621. xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  622. #if ( configUSE_QUEUE_SETS == 1 )
  623. {
  624. if( pxQueue->pxQueueSetContainer != NULL )
  625. {
  626. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) != pdFALSE )
  627. {
  628. /* The queue is a member of a queue set, and posting
  629. to the queue set caused a higher priority task to
  630. unblock. A context switch is required. */
  631. queueYIELD_IF_USING_PREEMPTION();
  632. }
  633. else
  634. {
  635. mtCOVERAGE_TEST_MARKER();
  636. }
  637. }
  638. else
  639. {
  640. /* If there was a task waiting for data to arrive on the
  641. queue then unblock it now. */
  642. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  643. {
  644. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  645. {
  646. /* The unblocked task has a priority higher than
  647. our own so yield immediately. Yes it is ok to
  648. do this from within the critical section - the
  649. kernel takes care of that. */
  650. queueYIELD_IF_USING_PREEMPTION();
  651. }
  652. else
  653. {
  654. mtCOVERAGE_TEST_MARKER();
  655. }
  656. }
  657. else if( xYieldRequired != pdFALSE )
  658. {
  659. /* This path is a special case that will only get
  660. executed if the task was holding multiple mutexes
  661. and the mutexes were given back in an order that is
  662. different to that in which they were taken. */
  663. queueYIELD_IF_USING_PREEMPTION();
  664. }
  665. else
  666. {
  667. mtCOVERAGE_TEST_MARKER();
  668. }
  669. }
  670. }
  671. #else /* configUSE_QUEUE_SETS */
  672. {
  673. /* If there was a task waiting for data to arrive on the
  674. queue then unblock it now. */
  675. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  676. {
  677. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  678. {
  679. /* The unblocked task has a priority higher than
  680. our own so yield immediately. Yes it is ok to do
  681. this from within the critical section - the kernel
  682. takes care of that. */
  683. queueYIELD_IF_USING_PREEMPTION();
  684. }
  685. else
  686. {
  687. mtCOVERAGE_TEST_MARKER();
  688. }
  689. }
  690. else if( xYieldRequired != pdFALSE )
  691. {
  692. /* This path is a special case that will only get
  693. executed if the task was holding multiple mutexes and
  694. the mutexes were given back in an order that is
  695. different to that in which they were taken. */
  696. queueYIELD_IF_USING_PREEMPTION();
  697. }
  698. else
  699. {
  700. mtCOVERAGE_TEST_MARKER();
  701. }
  702. }
  703. #endif /* configUSE_QUEUE_SETS */
  704. taskEXIT_CRITICAL();
  705. return pdPASS;
  706. }
  707. else
  708. {
  709. if( xTicksToWait == ( TickType_t ) 0 )
  710. {
  711. /* The queue was full and no block time is specified (or
  712. the block time has expired) so leave now. */
  713. taskEXIT_CRITICAL();
  714. /* Return to the original privilege level before exiting
  715. the function. */
  716. traceQUEUE_SEND_FAILED( pxQueue );
  717. return errQUEUE_FULL;
  718. }
  719. else if( xEntryTimeSet == pdFALSE )
  720. {
  721. /* The queue was full and a block time was specified so
  722. configure the timeout structure. */
  723. vTaskInternalSetTimeOutState( &xTimeOut );
  724. xEntryTimeSet = pdTRUE;
  725. }
  726. else
  727. {
  728. /* Entry time was already set. */
  729. mtCOVERAGE_TEST_MARKER();
  730. }
  731. }
  732. }
  733. taskEXIT_CRITICAL();
  734. /* Interrupts and other tasks can send to and receive from the queue
  735. now the critical section has been exited. */
  736. vTaskSuspendAll();
  737. prvLockQueue( pxQueue );
  738. /* Update the timeout state to see if it has expired yet. */
  739. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  740. {
  741. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  742. {
  743. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  744. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  745. /* Unlocking the queue means queue events can effect the
  746. event list. It is possible that interrupts occurring now
  747. remove this task from the event list again - but as the
  748. scheduler is suspended the task will go onto the pending
  749. ready last instead of the actual ready list. */
  750. prvUnlockQueue( pxQueue );
  751. /* Resuming the scheduler will move tasks from the pending
  752. ready list into the ready list - so it is feasible that this
  753. task is already in a ready list before it yields - in which
  754. case the yield will not cause a context switch unless there
  755. is also a higher priority task in the pending ready list. */
  756. if( xTaskResumeAll() == pdFALSE )
  757. {
  758. portYIELD_WITHIN_API();
  759. }
  760. }
  761. else
  762. {
  763. /* Try again. */
  764. prvUnlockQueue( pxQueue );
  765. ( void ) xTaskResumeAll();
  766. }
  767. }
  768. else
  769. {
  770. /* The timeout has expired. */
  771. prvUnlockQueue( pxQueue );
  772. ( void ) xTaskResumeAll();
  773. traceQUEUE_SEND_FAILED( pxQueue );
  774. return errQUEUE_FULL;
  775. }
  776. }
  777. }
  778. /*-----------------------------------------------------------*/
  779. BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )
  780. {
  781. BaseType_t xReturn;
  782. UBaseType_t uxSavedInterruptStatus;
  783. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  784. configASSERT( pxQueue );
  785. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  786. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  787. /* RTOS ports that support interrupt nesting have the concept of a maximum
  788. system call (or maximum API call) interrupt priority. Interrupts that are
  789. above the maximum system call priority are kept permanently enabled, even
  790. when the RTOS kernel is in a critical section, but cannot make any calls to
  791. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  792. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  793. failure if a FreeRTOS API function is called from an interrupt that has been
  794. assigned a priority above the configured maximum system call priority.
  795. Only FreeRTOS functions that end in FromISR can be called from interrupts
  796. that have been assigned a priority at or (logically) below the maximum
  797. system call interrupt priority. FreeRTOS maintains a separate interrupt
  798. safe API to ensure interrupt entry is as fast and as simple as possible.
  799. More information (albeit Cortex-M specific) is provided on the following
  800. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  801. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  802. /* Similar to xQueueGenericSend, except without blocking if there is no room
  803. in the queue. Also don't directly wake a task that was blocked on a queue
  804. read, instead return a flag to say whether a context switch is required or
  805. not (i.e. has a task with a higher priority than us been woken by this
  806. post). */
  807. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  808. {
  809. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  810. {
  811. const int8_t cTxLock = pxQueue->cTxLock;
  812. traceQUEUE_SEND_FROM_ISR( pxQueue );
  813. /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
  814. semaphore or mutex. That means prvCopyDataToQueue() cannot result
  815. in a task disinheriting a priority and prvCopyDataToQueue() can be
  816. called here even though the disinherit function does not check if
  817. the scheduler is suspended before accessing the ready lists. */
  818. ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  819. /* The event list is not altered if the queue is locked. This will
  820. be done when the queue is unlocked later. */
  821. if( cTxLock == queueUNLOCKED )
  822. {
  823. #if ( configUSE_QUEUE_SETS == 1 )
  824. {
  825. if( pxQueue->pxQueueSetContainer != NULL )
  826. {
  827. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) != pdFALSE )
  828. {
  829. /* The queue is a member of a queue set, and posting
  830. to the queue set caused a higher priority task to
  831. unblock. A context switch is required. */
  832. if( pxHigherPriorityTaskWoken != NULL )
  833. {
  834. *pxHigherPriorityTaskWoken = pdTRUE;
  835. }
  836. else
  837. {
  838. mtCOVERAGE_TEST_MARKER();
  839. }
  840. }
  841. else
  842. {
  843. mtCOVERAGE_TEST_MARKER();
  844. }
  845. }
  846. else
  847. {
  848. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  849. {
  850. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  851. {
  852. /* The task waiting has a higher priority so
  853. record that a context switch is required. */
  854. if( pxHigherPriorityTaskWoken != NULL )
  855. {
  856. *pxHigherPriorityTaskWoken = pdTRUE;
  857. }
  858. else
  859. {
  860. mtCOVERAGE_TEST_MARKER();
  861. }
  862. }
  863. else
  864. {
  865. mtCOVERAGE_TEST_MARKER();
  866. }
  867. }
  868. else
  869. {
  870. mtCOVERAGE_TEST_MARKER();
  871. }
  872. }
  873. }
  874. #else /* configUSE_QUEUE_SETS */
  875. {
  876. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  877. {
  878. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  879. {
  880. /* The task waiting has a higher priority so record that a
  881. context switch is required. */
  882. if( pxHigherPriorityTaskWoken != NULL )
  883. {
  884. *pxHigherPriorityTaskWoken = pdTRUE;
  885. }
  886. else
  887. {
  888. mtCOVERAGE_TEST_MARKER();
  889. }
  890. }
  891. else
  892. {
  893. mtCOVERAGE_TEST_MARKER();
  894. }
  895. }
  896. else
  897. {
  898. mtCOVERAGE_TEST_MARKER();
  899. }
  900. }
  901. #endif /* configUSE_QUEUE_SETS */
  902. }
  903. else
  904. {
  905. /* Increment the lock count so the task that unlocks the queue
  906. knows that data was posted while it was locked. */
  907. pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
  908. }
  909. xReturn = pdPASS;
  910. }
  911. else
  912. {
  913. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  914. xReturn = errQUEUE_FULL;
  915. }
  916. }
  917. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  918. return xReturn;
  919. }
  920. /*-----------------------------------------------------------*/
  921. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )
  922. {
  923. BaseType_t xReturn;
  924. UBaseType_t uxSavedInterruptStatus;
  925. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  926. /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
  927. item size is 0. Don't directly wake a task that was blocked on a queue
  928. read, instead return a flag to say whether a context switch is required or
  929. not (i.e. has a task with a higher priority than us been woken by this
  930. post). */
  931. configASSERT( pxQueue );
  932. /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
  933. if the item size is not 0. */
  934. configASSERT( pxQueue->uxItemSize == 0 );
  935. /* Normally a mutex would not be given from an interrupt, especially if
  936. there is a mutex holder, as priority inheritance makes no sense for an
  937. interrupts, only tasks. */
  938. configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->pxMutexHolder != NULL ) ) );
  939. /* RTOS ports that support interrupt nesting have the concept of a maximum
  940. system call (or maximum API call) interrupt priority. Interrupts that are
  941. above the maximum system call priority are kept permanently enabled, even
  942. when the RTOS kernel is in a critical section, but cannot make any calls to
  943. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  944. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  945. failure if a FreeRTOS API function is called from an interrupt that has been
  946. assigned a priority above the configured maximum system call priority.
  947. Only FreeRTOS functions that end in FromISR can be called from interrupts
  948. that have been assigned a priority at or (logically) below the maximum
  949. system call interrupt priority. FreeRTOS maintains a separate interrupt
  950. safe API to ensure interrupt entry is as fast and as simple as possible.
  951. More information (albeit Cortex-M specific) is provided on the following
  952. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  953. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  954. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  955. {
  956. const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
  957. /* When the queue is used to implement a semaphore no data is ever
  958. moved through the queue but it is still valid to see if the queue 'has
  959. space'. */
  960. if( uxMessagesWaiting < pxQueue->uxLength )
  961. {
  962. const int8_t cTxLock = pxQueue->cTxLock;
  963. traceQUEUE_SEND_FROM_ISR( pxQueue );
  964. /* A task can only have an inherited priority if it is a mutex
  965. holder - and if there is a mutex holder then the mutex cannot be
  966. given from an ISR. As this is the ISR version of the function it
  967. can be assumed there is no mutex holder and no need to determine if
  968. priority disinheritance is needed. Simply increase the count of
  969. messages (semaphores) available. */
  970. pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
  971. /* The event list is not altered if the queue is locked. This will
  972. be done when the queue is unlocked later. */
  973. if( cTxLock == queueUNLOCKED )
  974. {
  975. #if ( configUSE_QUEUE_SETS == 1 )
  976. {
  977. if( pxQueue->pxQueueSetContainer != NULL )
  978. {
  979. if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) != pdFALSE )
  980. {
  981. /* The semaphore is a member of a queue set, and
  982. posting to the queue set caused a higher priority
  983. task to unblock. A context switch is required. */
  984. if( pxHigherPriorityTaskWoken != NULL )
  985. {
  986. *pxHigherPriorityTaskWoken = pdTRUE;
  987. }
  988. else
  989. {
  990. mtCOVERAGE_TEST_MARKER();
  991. }
  992. }
  993. else
  994. {
  995. mtCOVERAGE_TEST_MARKER();
  996. }
  997. }
  998. else
  999. {
  1000. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1001. {
  1002. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1003. {
  1004. /* The task waiting has a higher priority so
  1005. record that a context switch is required. */
  1006. if( pxHigherPriorityTaskWoken != NULL )
  1007. {
  1008. *pxHigherPriorityTaskWoken = pdTRUE;
  1009. }
  1010. else
  1011. {
  1012. mtCOVERAGE_TEST_MARKER();
  1013. }
  1014. }
  1015. else
  1016. {
  1017. mtCOVERAGE_TEST_MARKER();
  1018. }
  1019. }
  1020. else
  1021. {
  1022. mtCOVERAGE_TEST_MARKER();
  1023. }
  1024. }
  1025. }
  1026. #else /* configUSE_QUEUE_SETS */
  1027. {
  1028. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1029. {
  1030. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1031. {
  1032. /* The task waiting has a higher priority so record that a
  1033. context switch is required. */
  1034. if( pxHigherPriorityTaskWoken != NULL )
  1035. {
  1036. *pxHigherPriorityTaskWoken = pdTRUE;
  1037. }
  1038. else
  1039. {
  1040. mtCOVERAGE_TEST_MARKER();
  1041. }
  1042. }
  1043. else
  1044. {
  1045. mtCOVERAGE_TEST_MARKER();
  1046. }
  1047. }
  1048. else
  1049. {
  1050. mtCOVERAGE_TEST_MARKER();
  1051. }
  1052. }
  1053. #endif /* configUSE_QUEUE_SETS */
  1054. }
  1055. else
  1056. {
  1057. /* Increment the lock count so the task that unlocks the queue
  1058. knows that data was posted while it was locked. */
  1059. pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
  1060. }
  1061. xReturn = pdPASS;
  1062. }
  1063. else
  1064. {
  1065. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1066. xReturn = errQUEUE_FULL;
  1067. }
  1068. }
  1069. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1070. return xReturn;
  1071. }
  1072. /*-----------------------------------------------------------*/
  1073. BaseType_t xQueueReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )
  1074. {
  1075. BaseType_t xEntryTimeSet = pdFALSE;
  1076. TimeOut_t xTimeOut;
  1077. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1078. /* Check the pointer is not NULL. */
  1079. configASSERT( ( pxQueue ) );
  1080. /* The buffer into which data is received can only be NULL if the data size
  1081. is zero (so no data is copied into the buffer. */
  1082. configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1083. /* Cannot block if the scheduler is suspended. */
  1084. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1085. {
  1086. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1087. }
  1088. #endif
  1089. /* This function relaxes the coding standard somewhat to allow return
  1090. statements within the function itself. This is done in the interest
  1091. of execution time efficiency. */
  1092. for( ;; )
  1093. {
  1094. taskENTER_CRITICAL();
  1095. {
  1096. const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
  1097. /* Is there data in the queue now? To be running the calling task
  1098. must be the highest priority task wanting to access the queue. */
  1099. if( uxMessagesWaiting > ( UBaseType_t ) 0 )
  1100. {
  1101. /* Data available, remove one item. */
  1102. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1103. traceQUEUE_RECEIVE( pxQueue );
  1104. pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
  1105. /* There is now space in the queue, were any tasks waiting to
  1106. post to the queue? If so, unblock the highest priority waiting
  1107. task. */
  1108. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1109. {
  1110. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1111. {
  1112. queueYIELD_IF_USING_PREEMPTION();
  1113. }
  1114. else
  1115. {
  1116. mtCOVERAGE_TEST_MARKER();
  1117. }
  1118. }
  1119. else
  1120. {
  1121. mtCOVERAGE_TEST_MARKER();
  1122. }
  1123. taskEXIT_CRITICAL();
  1124. return pdPASS;
  1125. }
  1126. else
  1127. {
  1128. if( xTicksToWait == ( TickType_t ) 0 )
  1129. {
  1130. /* The queue was empty and no block time is specified (or
  1131. the block time has expired) so leave now. */
  1132. taskEXIT_CRITICAL();
  1133. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1134. return errQUEUE_EMPTY;
  1135. }
  1136. else if( xEntryTimeSet == pdFALSE )
  1137. {
  1138. /* The queue was empty and a block time was specified so
  1139. configure the timeout structure. */
  1140. vTaskInternalSetTimeOutState( &xTimeOut );
  1141. xEntryTimeSet = pdTRUE;
  1142. }
  1143. else
  1144. {
  1145. /* Entry time was already set. */
  1146. mtCOVERAGE_TEST_MARKER();
  1147. }
  1148. }
  1149. }
  1150. taskEXIT_CRITICAL();
  1151. /* Interrupts and other tasks can send to and receive from the queue
  1152. now the critical section has been exited. */
  1153. vTaskSuspendAll();
  1154. prvLockQueue( pxQueue );
  1155. /* Update the timeout state to see if it has expired yet. */
  1156. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1157. {
  1158. /* The timeout has not expired. If the queue is still empty place
  1159. the task on the list of tasks waiting to receive from the queue. */
  1160. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1161. {
  1162. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  1163. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1164. prvUnlockQueue( pxQueue );
  1165. if( xTaskResumeAll() == pdFALSE )
  1166. {
  1167. portYIELD_WITHIN_API();
  1168. }
  1169. else
  1170. {
  1171. mtCOVERAGE_TEST_MARKER();
  1172. }
  1173. }
  1174. else
  1175. {
  1176. /* The queue contains data again. Loop back to try and read the
  1177. data. */
  1178. prvUnlockQueue( pxQueue );
  1179. ( void ) xTaskResumeAll();
  1180. }
  1181. }
  1182. else
  1183. {
  1184. /* Timed out. If there is no data in the queue exit, otherwise loop
  1185. back and attempt to read the data. */
  1186. prvUnlockQueue( pxQueue );
  1187. ( void ) xTaskResumeAll();
  1188. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1189. {
  1190. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1191. return errQUEUE_EMPTY;
  1192. }
  1193. else
  1194. {
  1195. mtCOVERAGE_TEST_MARKER();
  1196. }
  1197. }
  1198. }
  1199. }
  1200. /*-----------------------------------------------------------*/
  1201. BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue, TickType_t xTicksToWait )
  1202. {
  1203. BaseType_t xEntryTimeSet = pdFALSE;
  1204. TimeOut_t xTimeOut;
  1205. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1206. #if( configUSE_MUTEXES == 1 )
  1207. BaseType_t xInheritanceOccurred = pdFALSE;
  1208. #endif
  1209. /* Check the queue pointer is not NULL. */
  1210. configASSERT( ( pxQueue ) );
  1211. /* Check this really is a semaphore, in which case the item size will be
  1212. 0. */
  1213. configASSERT( pxQueue->uxItemSize == 0 );
  1214. /* Cannot block if the scheduler is suspended. */
  1215. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1216. {
  1217. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1218. }
  1219. #endif
  1220. /* This function relaxes the coding standard somewhat to allow return
  1221. statements within the function itself. This is done in the interest
  1222. of execution time efficiency. */
  1223. for( ;; )
  1224. {
  1225. taskENTER_CRITICAL();
  1226. {
  1227. /* Semaphores are queues with an item size of 0, and where the
  1228. number of messages in the queue is the semaphore's count value. */
  1229. const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
  1230. /* Is there data in the queue now? To be running the calling task
  1231. must be the highest priority task wanting to access the queue. */
  1232. if( uxSemaphoreCount > ( UBaseType_t ) 0 )
  1233. {
  1234. traceQUEUE_RECEIVE( pxQueue );
  1235. /* Semaphores are queues with a data size of zero and where the
  1236. messages waiting is the semaphore's count. Reduce the count. */
  1237. pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
  1238. #if ( configUSE_MUTEXES == 1 )
  1239. {
  1240. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1241. {
  1242. /* Record the information required to implement
  1243. priority inheritance should it become necessary. */
  1244. pxQueue->pxMutexHolder = ( int8_t * ) pvTaskIncrementMutexHeldCount(); /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  1245. }
  1246. else
  1247. {
  1248. mtCOVERAGE_TEST_MARKER();
  1249. }
  1250. }
  1251. #endif /* configUSE_MUTEXES */
  1252. /* Check to see if other tasks are blocked waiting to give the
  1253. semaphore, and if so, unblock the highest priority such task. */
  1254. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1255. {
  1256. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1257. {
  1258. queueYIELD_IF_USING_PREEMPTION();
  1259. }
  1260. else
  1261. {
  1262. mtCOVERAGE_TEST_MARKER();
  1263. }
  1264. }
  1265. else
  1266. {
  1267. mtCOVERAGE_TEST_MARKER();
  1268. }
  1269. taskEXIT_CRITICAL();
  1270. return pdPASS;
  1271. }
  1272. else
  1273. {
  1274. if( xTicksToWait == ( TickType_t ) 0 )
  1275. {
  1276. /* For inheritance to have occurred there must have been an
  1277. initial timeout, and an adjusted timeout cannot become 0, as
  1278. if it were 0 the function would have exited. */
  1279. #if( configUSE_MUTEXES == 1 )
  1280. {
  1281. configASSERT( xInheritanceOccurred == pdFALSE );
  1282. }
  1283. #endif /* configUSE_MUTEXES */
  1284. /* The semaphore count was 0 and no block time is specified
  1285. (or the block time has expired) so exit now. */
  1286. taskEXIT_CRITICAL();
  1287. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1288. return errQUEUE_EMPTY;
  1289. }
  1290. else if( xEntryTimeSet == pdFALSE )
  1291. {
  1292. /* The semaphore count was 0 and a block time was specified
  1293. so configure the timeout structure ready to block. */
  1294. vTaskInternalSetTimeOutState( &xTimeOut );
  1295. xEntryTimeSet = pdTRUE;
  1296. }
  1297. else
  1298. {
  1299. /* Entry time was already set. */
  1300. mtCOVERAGE_TEST_MARKER();
  1301. }
  1302. }
  1303. }
  1304. taskEXIT_CRITICAL();
  1305. /* Interrupts and other tasks can give to and take from the semaphore
  1306. now the critical section has been exited. */
  1307. vTaskSuspendAll();
  1308. prvLockQueue( pxQueue );
  1309. /* Update the timeout state to see if it has expired yet. */
  1310. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1311. {
  1312. /* A block time is specified and not expired. If the semaphore
  1313. count is 0 then enter the Blocked state to wait for a semaphore to
  1314. become available. As semaphores are implemented with queues the
  1315. queue being empty is equivalent to the semaphore count being 0. */
  1316. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1317. {
  1318. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  1319. #if ( configUSE_MUTEXES == 1 )
  1320. {
  1321. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1322. {
  1323. taskENTER_CRITICAL();
  1324. {
  1325. xInheritanceOccurred = xTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  1326. }
  1327. taskEXIT_CRITICAL();
  1328. }
  1329. else
  1330. {
  1331. mtCOVERAGE_TEST_MARKER();
  1332. }
  1333. }
  1334. #endif
  1335. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1336. prvUnlockQueue( pxQueue );
  1337. if( xTaskResumeAll() == pdFALSE )
  1338. {
  1339. portYIELD_WITHIN_API();
  1340. }
  1341. else
  1342. {
  1343. mtCOVERAGE_TEST_MARKER();
  1344. }
  1345. }
  1346. else
  1347. {
  1348. /* There was no timeout and the semaphore count was not 0, so
  1349. attempt to take the semaphore again. */
  1350. prvUnlockQueue( pxQueue );
  1351. ( void ) xTaskResumeAll();
  1352. }
  1353. }
  1354. else
  1355. {
  1356. /* Timed out. */
  1357. prvUnlockQueue( pxQueue );
  1358. ( void ) xTaskResumeAll();
  1359. /* If the semaphore count is 0 exit now as the timeout has
  1360. expired. Otherwise return to attempt to take the semaphore that is
  1361. known to be available. As semaphores are implemented by queues the
  1362. queue being empty is equivalent to the semaphore count being 0. */
  1363. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1364. {
  1365. #if ( configUSE_MUTEXES == 1 )
  1366. {
  1367. /* xInheritanceOccurred could only have be set if
  1368. pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
  1369. test the mutex type again to check it is actually a mutex. */
  1370. if( xInheritanceOccurred != pdFALSE )
  1371. {
  1372. taskENTER_CRITICAL();
  1373. {
  1374. UBaseType_t uxHighestWaitingPriority;
  1375. /* This task blocking on the mutex caused another
  1376. task to inherit this task's priority. Now this task
  1377. has timed out the priority should be disinherited
  1378. again, but only as low as the next highest priority
  1379. task that is waiting for the same mutex. */
  1380. uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
  1381. vTaskPriorityDisinheritAfterTimeout( ( void * ) pxQueue->pxMutexHolder, uxHighestWaitingPriority );
  1382. }
  1383. taskEXIT_CRITICAL();
  1384. }
  1385. }
  1386. #endif /* configUSE_MUTEXES */
  1387. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1388. return errQUEUE_EMPTY;
  1389. }
  1390. else
  1391. {
  1392. mtCOVERAGE_TEST_MARKER();
  1393. }
  1394. }
  1395. }
  1396. }
  1397. /*-----------------------------------------------------------*/
  1398. BaseType_t xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )
  1399. {
  1400. BaseType_t xEntryTimeSet = pdFALSE;
  1401. TimeOut_t xTimeOut;
  1402. int8_t *pcOriginalReadPosition;
  1403. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1404. /* Check the pointer is not NULL. */
  1405. configASSERT( ( pxQueue ) );
  1406. /* The buffer into which data is received can only be NULL if the data size
  1407. is zero (so no data is copied into the buffer. */
  1408. configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1409. /* Cannot block if the scheduler is suspended. */
  1410. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1411. {
  1412. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1413. }
  1414. #endif
  1415. /* This function relaxes the coding standard somewhat to allow return
  1416. statements within the function itself. This is done in the interest
  1417. of execution time efficiency. */
  1418. for( ;; )
  1419. {
  1420. taskENTER_CRITICAL();
  1421. {
  1422. const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
  1423. /* Is there data in the queue now? To be running the calling task
  1424. must be the highest priority task wanting to access the queue. */
  1425. if( uxMessagesWaiting > ( UBaseType_t ) 0 )
  1426. {
  1427. /* Remember the read position so it can be reset after the data
  1428. is read from the queue as this function is only peeking the
  1429. data, not removing it. */
  1430. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1431. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1432. traceQUEUE_PEEK( pxQueue );
  1433. /* The data is not being removed, so reset the read pointer. */
  1434. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1435. /* The data is being left in the queue, so see if there are
  1436. any other tasks waiting for the data. */
  1437. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1438. {
  1439. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1440. {
  1441. /* The task waiting has a higher priority than this task. */
  1442. queueYIELD_IF_USING_PREEMPTION();
  1443. }
  1444. else
  1445. {
  1446. mtCOVERAGE_TEST_MARKER();
  1447. }
  1448. }
  1449. else
  1450. {
  1451. mtCOVERAGE_TEST_MARKER();
  1452. }
  1453. taskEXIT_CRITICAL();
  1454. return pdPASS;
  1455. }
  1456. else
  1457. {
  1458. if( xTicksToWait == ( TickType_t ) 0 )
  1459. {
  1460. /* The queue was empty and no block time is specified (or
  1461. the block time has expired) so leave now. */
  1462. taskEXIT_CRITICAL();
  1463. traceQUEUE_PEEK_FAILED( pxQueue );
  1464. return errQUEUE_EMPTY;
  1465. }
  1466. else if( xEntryTimeSet == pdFALSE )
  1467. {
  1468. /* The queue was empty and a block time was specified so
  1469. configure the timeout structure ready to enter the blocked
  1470. state. */
  1471. vTaskInternalSetTimeOutState( &xTimeOut );
  1472. xEntryTimeSet = pdTRUE;
  1473. }
  1474. else
  1475. {
  1476. /* Entry time was already set. */
  1477. mtCOVERAGE_TEST_MARKER();
  1478. }
  1479. }
  1480. }
  1481. taskEXIT_CRITICAL();
  1482. /* Interrupts and other tasks can send to and receive from the queue
  1483. now the critical section has been exited. */
  1484. vTaskSuspendAll();
  1485. prvLockQueue( pxQueue );
  1486. /* Update the timeout state to see if it has expired yet. */
  1487. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1488. {
  1489. /* Timeout has not expired yet, check to see if there is data in the
  1490. queue now, and if not enter the Blocked state to wait for data. */
  1491. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1492. {
  1493. traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
  1494. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1495. prvUnlockQueue( pxQueue );
  1496. if( xTaskResumeAll() == pdFALSE )
  1497. {
  1498. portYIELD_WITHIN_API();
  1499. }
  1500. else
  1501. {
  1502. mtCOVERAGE_TEST_MARKER();
  1503. }
  1504. }
  1505. else
  1506. {
  1507. /* There is data in the queue now, so don't enter the blocked
  1508. state, instead return to try and obtain the data. */
  1509. prvUnlockQueue( pxQueue );
  1510. ( void ) xTaskResumeAll();
  1511. }
  1512. }
  1513. else
  1514. {
  1515. /* The timeout has expired. If there is still no data in the queue
  1516. exit, otherwise go back and try to read the data again. */
  1517. prvUnlockQueue( pxQueue );
  1518. ( void ) xTaskResumeAll();
  1519. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1520. {
  1521. traceQUEUE_PEEK_FAILED( pxQueue );
  1522. return errQUEUE_EMPTY;
  1523. }
  1524. else
  1525. {
  1526. mtCOVERAGE_TEST_MARKER();
  1527. }
  1528. }
  1529. }
  1530. }
  1531. /*-----------------------------------------------------------*/
  1532. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, BaseType_t * const pxHigherPriorityTaskWoken )
  1533. {
  1534. BaseType_t xReturn;
  1535. UBaseType_t uxSavedInterruptStatus;
  1536. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1537. configASSERT( pxQueue );
  1538. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1539. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1540. system call (or maximum API call) interrupt priority. Interrupts that are
  1541. above the maximum system call priority are kept permanently enabled, even
  1542. when the RTOS kernel is in a critical section, but cannot make any calls to
  1543. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1544. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1545. failure if a FreeRTOS API function is called from an interrupt that has been
  1546. assigned a priority above the configured maximum system call priority.
  1547. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1548. that have been assigned a priority at or (logically) below the maximum
  1549. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1550. safe API to ensure interrupt entry is as fast and as simple as possible.
  1551. More information (albeit Cortex-M specific) is provided on the following
  1552. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1553. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1554. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1555. {
  1556. const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
  1557. /* Cannot block in an ISR, so check there is data available. */
  1558. if( uxMessagesWaiting > ( UBaseType_t ) 0 )
  1559. {
  1560. const int8_t cRxLock = pxQueue->cRxLock;
  1561. traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
  1562. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1563. pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
  1564. /* If the queue is locked the event list will not be modified.
  1565. Instead update the lock count so the task that unlocks the queue
  1566. will know that an ISR has removed data while the queue was
  1567. locked. */
  1568. if( cRxLock == queueUNLOCKED )
  1569. {
  1570. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1571. {
  1572. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1573. {
  1574. /* The task waiting has a higher priority than us so
  1575. force a context switch. */
  1576. if( pxHigherPriorityTaskWoken != NULL )
  1577. {
  1578. *pxHigherPriorityTaskWoken = pdTRUE;
  1579. }
  1580. else
  1581. {
  1582. mtCOVERAGE_TEST_MARKER();
  1583. }
  1584. }
  1585. else
  1586. {
  1587. mtCOVERAGE_TEST_MARKER();
  1588. }
  1589. }
  1590. else
  1591. {
  1592. mtCOVERAGE_TEST_MARKER();
  1593. }
  1594. }
  1595. else
  1596. {
  1597. /* Increment the lock count so the task that unlocks the queue
  1598. knows that data was removed while it was locked. */
  1599. pxQueue->cRxLock = ( int8_t ) ( cRxLock + 1 );
  1600. }
  1601. xReturn = pdPASS;
  1602. }
  1603. else
  1604. {
  1605. xReturn = pdFAIL;
  1606. traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
  1607. }
  1608. }
  1609. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1610. return xReturn;
  1611. }
  1612. /*-----------------------------------------------------------*/
  1613. BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void * const pvBuffer )
  1614. {
  1615. BaseType_t xReturn;
  1616. UBaseType_t uxSavedInterruptStatus;
  1617. int8_t *pcOriginalReadPosition;
  1618. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1619. configASSERT( pxQueue );
  1620. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1621. configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
  1622. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1623. system call (or maximum API call) interrupt priority. Interrupts that are
  1624. above the maximum system call priority are kept permanently enabled, even
  1625. when the RTOS kernel is in a critical section, but cannot make any calls to
  1626. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1627. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1628. failure if a FreeRTOS API function is called from an interrupt that has been
  1629. assigned a priority above the configured maximum system call priority.
  1630. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1631. that have been assigned a priority at or (logically) below the maximum
  1632. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1633. safe API to ensure interrupt entry is as fast and as simple as possible.
  1634. More information (albeit Cortex-M specific) is provided on the following
  1635. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1636. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1637. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1638. {
  1639. /* Cannot block in an ISR, so check there is data available. */
  1640. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1641. {
  1642. traceQUEUE_PEEK_FROM_ISR( pxQueue );
  1643. /* Remember the read position so it can be reset as nothing is
  1644. actually being removed from the queue. */
  1645. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1646. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1647. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1648. xReturn = pdPASS;
  1649. }
  1650. else
  1651. {
  1652. xReturn = pdFAIL;
  1653. traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
  1654. }
  1655. }
  1656. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1657. return xReturn;
  1658. }
  1659. /*-----------------------------------------------------------*/
  1660. UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
  1661. {
  1662. UBaseType_t uxReturn;
  1663. configASSERT( xQueue );
  1664. taskENTER_CRITICAL();
  1665. {
  1666. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1667. }
  1668. taskEXIT_CRITICAL();
  1669. return uxReturn;
  1670. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1671. /*-----------------------------------------------------------*/
  1672. UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
  1673. {
  1674. UBaseType_t uxReturn;
  1675. Queue_t *pxQueue;
  1676. pxQueue = ( Queue_t * ) xQueue;
  1677. configASSERT( pxQueue );
  1678. taskENTER_CRITICAL();
  1679. {
  1680. uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
  1681. }
  1682. taskEXIT_CRITICAL();
  1683. return uxReturn;
  1684. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1685. /*-----------------------------------------------------------*/
  1686. UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
  1687. {
  1688. UBaseType_t uxReturn;
  1689. configASSERT( xQueue );
  1690. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1691. return uxReturn;
  1692. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1693. /*-----------------------------------------------------------*/
  1694. void vQueueDelete( QueueHandle_t xQueue )
  1695. {
  1696. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1697. configASSERT( pxQueue );
  1698. traceQUEUE_DELETE( pxQueue );
  1699. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1700. {
  1701. vQueueUnregisterQueue( pxQueue );
  1702. }
  1703. #endif
  1704. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  1705. {
  1706. /* The queue can only have been allocated dynamically - free it
  1707. again. */
  1708. vPortFree( pxQueue );
  1709. }
  1710. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  1711. {
  1712. /* The queue could have been allocated statically or dynamically, so
  1713. check before attempting to free the memory. */
  1714. if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  1715. {
  1716. vPortFree( pxQueue );
  1717. }
  1718. else
  1719. {
  1720. mtCOVERAGE_TEST_MARKER();
  1721. }
  1722. }
  1723. #else
  1724. {
  1725. /* The queue must have been statically allocated, so is not going to be
  1726. deleted. Avoid compiler warnings about the unused parameter. */
  1727. ( void ) pxQueue;
  1728. }
  1729. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  1730. }
  1731. /*-----------------------------------------------------------*/
  1732. #if ( configUSE_TRACE_FACILITY == 1 )
  1733. UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
  1734. {
  1735. return ( ( Queue_t * ) xQueue )->uxQueueNumber;
  1736. }
  1737. #endif /* configUSE_TRACE_FACILITY */
  1738. /*-----------------------------------------------------------*/
  1739. #if ( configUSE_TRACE_FACILITY == 1 )
  1740. void vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber )
  1741. {
  1742. ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
  1743. }
  1744. #endif /* configUSE_TRACE_FACILITY */
  1745. /*-----------------------------------------------------------*/
  1746. #if ( configUSE_TRACE_FACILITY == 1 )
  1747. uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
  1748. {
  1749. return ( ( Queue_t * ) xQueue )->ucQueueType;
  1750. }
  1751. #endif /* configUSE_TRACE_FACILITY */
  1752. /*-----------------------------------------------------------*/
  1753. #if( configUSE_MUTEXES == 1 )
  1754. static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
  1755. {
  1756. UBaseType_t uxHighestPriorityOfWaitingTasks;
  1757. /* If a task waiting for a mutex causes the mutex holder to inherit a
  1758. priority, but the waiting task times out, then the holder should
  1759. disinherit the priority - but only down to the highest priority of any
  1760. other tasks that are waiting for the same mutex. For this purpose,
  1761. return the priority of the highest priority task that is waiting for the
  1762. mutex. */
  1763. if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0 )
  1764. {
  1765. uxHighestPriorityOfWaitingTasks = configMAX_PRIORITIES - listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
  1766. }
  1767. else
  1768. {
  1769. uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
  1770. }
  1771. return uxHighestPriorityOfWaitingTasks;
  1772. }
  1773. #endif /* configUSE_MUTEXES */
  1774. /*-----------------------------------------------------------*/
  1775. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition )
  1776. {
  1777. BaseType_t xReturn = pdFALSE;
  1778. UBaseType_t uxMessagesWaiting;
  1779. /* This function is called from a critical section. */
  1780. uxMessagesWaiting = pxQueue->uxMessagesWaiting;
  1781. if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
  1782. {
  1783. #if ( configUSE_MUTEXES == 1 )
  1784. {
  1785. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1786. {
  1787. /* The mutex is no longer being held. */
  1788. xReturn = xTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
  1789. pxQueue->pxMutexHolder = NULL;
  1790. }
  1791. else
  1792. {
  1793. mtCOVERAGE_TEST_MARKER();
  1794. }
  1795. }
  1796. #endif /* configUSE_MUTEXES */
  1797. }
  1798. else if( xPosition == queueSEND_TO_BACK )
  1799. {
  1800. ( 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. */
  1801. pxQueue->pcWriteTo += pxQueue->uxItemSize;
  1802. if( pxQueue->pcWriteTo >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1803. {
  1804. pxQueue->pcWriteTo = pxQueue->pcHead;
  1805. }
  1806. else
  1807. {
  1808. mtCOVERAGE_TEST_MARKER();
  1809. }
  1810. }
  1811. else
  1812. {
  1813. ( void ) memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1814. pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;
  1815. if( pxQueue->u.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1816. {
  1817. pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
  1818. }
  1819. else
  1820. {
  1821. mtCOVERAGE_TEST_MARKER();
  1822. }
  1823. if( xPosition == queueOVERWRITE )
  1824. {
  1825. if( uxMessagesWaiting > ( UBaseType_t ) 0 )
  1826. {
  1827. /* An item is not being added but overwritten, so subtract
  1828. one from the recorded number of items in the queue so when
  1829. one is added again below the number of recorded items remains
  1830. correct. */
  1831. --uxMessagesWaiting;
  1832. }
  1833. else
  1834. {
  1835. mtCOVERAGE_TEST_MARKER();
  1836. }
  1837. }
  1838. else
  1839. {
  1840. mtCOVERAGE_TEST_MARKER();
  1841. }
  1842. }
  1843. pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
  1844. return xReturn;
  1845. }
  1846. /*-----------------------------------------------------------*/
  1847. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer )
  1848. {
  1849. if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
  1850. {
  1851. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1852. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
  1853. {
  1854. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1855. }
  1856. else
  1857. {
  1858. mtCOVERAGE_TEST_MARKER();
  1859. }
  1860. ( 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. */
  1861. }
  1862. }
  1863. /*-----------------------------------------------------------*/
  1864. static void prvUnlockQueue( Queue_t * const pxQueue )
  1865. {
  1866. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
  1867. /* The lock counts contains the number of extra data items placed or
  1868. removed from the queue while the queue was locked. When a queue is
  1869. locked items can be added or removed, but the event lists cannot be
  1870. updated. */
  1871. taskENTER_CRITICAL();
  1872. {
  1873. int8_t cTxLock = pxQueue->cTxLock;
  1874. /* See if data was added to the queue while it was locked. */
  1875. while( cTxLock > queueLOCKED_UNMODIFIED )
  1876. {
  1877. /* Data was posted while the queue was locked. Are any tasks
  1878. blocked waiting for data to become available? */
  1879. #if ( configUSE_QUEUE_SETS == 1 )
  1880. {
  1881. if( pxQueue->pxQueueSetContainer != NULL )
  1882. {
  1883. if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) != pdFALSE )
  1884. {
  1885. /* The queue is a member of a queue set, and posting to
  1886. the queue set caused a higher priority task to unblock.
  1887. A context switch is required. */
  1888. vTaskMissedYield();
  1889. }
  1890. else
  1891. {
  1892. mtCOVERAGE_TEST_MARKER();
  1893. }
  1894. }
  1895. else
  1896. {
  1897. /* Tasks that are removed from the event list will get
  1898. added to the pending ready list as the scheduler is still
  1899. suspended. */
  1900. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1901. {
  1902. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1903. {
  1904. /* The task waiting has a higher priority so record that a
  1905. context switch is required. */
  1906. vTaskMissedYield();
  1907. }
  1908. else
  1909. {
  1910. mtCOVERAGE_TEST_MARKER();
  1911. }
  1912. }
  1913. else
  1914. {
  1915. break;
  1916. }
  1917. }
  1918. }
  1919. #else /* configUSE_QUEUE_SETS */
  1920. {
  1921. /* Tasks that are removed from the event list will get added to
  1922. the pending ready list as the scheduler is still suspended. */
  1923. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1924. {
  1925. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1926. {
  1927. /* The task waiting has a higher priority so record that
  1928. a context switch is required. */
  1929. vTaskMissedYield();
  1930. }
  1931. else
  1932. {
  1933. mtCOVERAGE_TEST_MARKER();
  1934. }
  1935. }
  1936. else
  1937. {
  1938. break;
  1939. }
  1940. }
  1941. #endif /* configUSE_QUEUE_SETS */
  1942. --cTxLock;
  1943. }
  1944. pxQueue->cTxLock = queueUNLOCKED;
  1945. }
  1946. taskEXIT_CRITICAL();
  1947. /* Do the same for the Rx lock. */
  1948. taskENTER_CRITICAL();
  1949. {
  1950. int8_t cRxLock = pxQueue->cRxLock;
  1951. while( cRxLock > queueLOCKED_UNMODIFIED )
  1952. {
  1953. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1954. {
  1955. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1956. {
  1957. vTaskMissedYield();
  1958. }
  1959. else
  1960. {
  1961. mtCOVERAGE_TEST_MARKER();
  1962. }
  1963. --cRxLock;
  1964. }
  1965. else
  1966. {
  1967. break;
  1968. }
  1969. }
  1970. pxQueue->cRxLock = queueUNLOCKED;
  1971. }
  1972. taskEXIT_CRITICAL();
  1973. }
  1974. /*-----------------------------------------------------------*/
  1975. static BaseType_t prvIsQueueEmpty( const Queue_t *pxQueue )
  1976. {
  1977. BaseType_t xReturn;
  1978. taskENTER_CRITICAL();
  1979. {
  1980. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1981. {
  1982. xReturn = pdTRUE;
  1983. }
  1984. else
  1985. {
  1986. xReturn = pdFALSE;
  1987. }
  1988. }
  1989. taskEXIT_CRITICAL();
  1990. return xReturn;
  1991. }
  1992. /*-----------------------------------------------------------*/
  1993. BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
  1994. {
  1995. BaseType_t xReturn;
  1996. configASSERT( xQueue );
  1997. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1998. {
  1999. xReturn = pdTRUE;
  2000. }
  2001. else
  2002. {
  2003. xReturn = pdFALSE;
  2004. }
  2005. return xReturn;
  2006. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  2007. /*-----------------------------------------------------------*/
  2008. static BaseType_t prvIsQueueFull( const Queue_t *pxQueue )
  2009. {
  2010. BaseType_t xReturn;
  2011. taskENTER_CRITICAL();
  2012. {
  2013. if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
  2014. {
  2015. xReturn = pdTRUE;
  2016. }
  2017. else
  2018. {
  2019. xReturn = pdFALSE;
  2020. }
  2021. }
  2022. taskEXIT_CRITICAL();
  2023. return xReturn;
  2024. }
  2025. /*-----------------------------------------------------------*/
  2026. BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
  2027. {
  2028. BaseType_t xReturn;
  2029. configASSERT( xQueue );
  2030. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( ( Queue_t * ) xQueue )->uxLength )
  2031. {
  2032. xReturn = pdTRUE;
  2033. }
  2034. else
  2035. {
  2036. xReturn = pdFALSE;
  2037. }
  2038. return xReturn;
  2039. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  2040. /*-----------------------------------------------------------*/
  2041. #if ( configUSE_CO_ROUTINES == 1 )
  2042. BaseType_t xQueueCRSend( QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait )
  2043. {
  2044. BaseType_t xReturn;
  2045. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2046. /* If the queue is already full we may have to block. A critical section
  2047. is required to prevent an interrupt removing something from the queue
  2048. between the check to see if the queue is full and blocking on the queue. */
  2049. portDISABLE_INTERRUPTS();
  2050. {
  2051. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  2052. {
  2053. /* The queue is full - do we want to block or just leave without
  2054. posting? */
  2055. if( xTicksToWait > ( TickType_t ) 0 )
  2056. {
  2057. /* As this is called from a coroutine we cannot block directly, but
  2058. return indicating that we need to block. */
  2059. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
  2060. portENABLE_INTERRUPTS();
  2061. return errQUEUE_BLOCKED;
  2062. }
  2063. else
  2064. {
  2065. portENABLE_INTERRUPTS();
  2066. return errQUEUE_FULL;
  2067. }
  2068. }
  2069. }
  2070. portENABLE_INTERRUPTS();
  2071. portDISABLE_INTERRUPTS();
  2072. {
  2073. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  2074. {
  2075. /* There is room in the queue, copy the data into the queue. */
  2076. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  2077. xReturn = pdPASS;
  2078. /* Were any co-routines waiting for data to become available? */
  2079. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  2080. {
  2081. /* In this instance the co-routine could be placed directly
  2082. into the ready list as we are within a critical section.
  2083. Instead the same pending ready list mechanism is used as if
  2084. the event were caused from within an interrupt. */
  2085. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  2086. {
  2087. /* The co-routine waiting has a higher priority so record
  2088. that a yield might be appropriate. */
  2089. xReturn = errQUEUE_YIELD;
  2090. }
  2091. else
  2092. {
  2093. mtCOVERAGE_TEST_MARKER();
  2094. }
  2095. }
  2096. else
  2097. {
  2098. mtCOVERAGE_TEST_MARKER();
  2099. }
  2100. }
  2101. else
  2102. {
  2103. xReturn = errQUEUE_FULL;
  2104. }
  2105. }
  2106. portENABLE_INTERRUPTS();
  2107. return xReturn;
  2108. }
  2109. #endif /* configUSE_CO_ROUTINES */
  2110. /*-----------------------------------------------------------*/
  2111. #if ( configUSE_CO_ROUTINES == 1 )
  2112. BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait )
  2113. {
  2114. BaseType_t xReturn;
  2115. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2116. /* If the queue is already empty we may have to block. A critical section
  2117. is required to prevent an interrupt adding something to the queue
  2118. between the check to see if the queue is empty and blocking on the queue. */
  2119. portDISABLE_INTERRUPTS();
  2120. {
  2121. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  2122. {
  2123. /* There are no messages in the queue, do we want to block or just
  2124. leave with nothing? */
  2125. if( xTicksToWait > ( TickType_t ) 0 )
  2126. {
  2127. /* As this is a co-routine we cannot block directly, but return
  2128. indicating that we need to block. */
  2129. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
  2130. portENABLE_INTERRUPTS();
  2131. return errQUEUE_BLOCKED;
  2132. }
  2133. else
  2134. {
  2135. portENABLE_INTERRUPTS();
  2136. return errQUEUE_FULL;
  2137. }
  2138. }
  2139. else
  2140. {
  2141. mtCOVERAGE_TEST_MARKER();
  2142. }
  2143. }
  2144. portENABLE_INTERRUPTS();
  2145. portDISABLE_INTERRUPTS();
  2146. {
  2147. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  2148. {
  2149. /* Data is available from the queue. */
  2150. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  2151. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  2152. {
  2153. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  2154. }
  2155. else
  2156. {
  2157. mtCOVERAGE_TEST_MARKER();
  2158. }
  2159. --( pxQueue->uxMessagesWaiting );
  2160. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  2161. xReturn = pdPASS;
  2162. /* Were any co-routines waiting for space to become available? */
  2163. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  2164. {
  2165. /* In this instance the co-routine could be placed directly
  2166. into the ready list as we are within a critical section.
  2167. Instead the same pending ready list mechanism is used as if
  2168. the event were caused from within an interrupt. */
  2169. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  2170. {
  2171. xReturn = errQUEUE_YIELD;
  2172. }
  2173. else
  2174. {
  2175. mtCOVERAGE_TEST_MARKER();
  2176. }
  2177. }
  2178. else
  2179. {
  2180. mtCOVERAGE_TEST_MARKER();
  2181. }
  2182. }
  2183. else
  2184. {
  2185. xReturn = pdFAIL;
  2186. }
  2187. }
  2188. portENABLE_INTERRUPTS();
  2189. return xReturn;
  2190. }
  2191. #endif /* configUSE_CO_ROUTINES */
  2192. /*-----------------------------------------------------------*/
  2193. #if ( configUSE_CO_ROUTINES == 1 )
  2194. BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t xCoRoutinePreviouslyWoken )
  2195. {
  2196. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2197. /* Cannot block within an ISR so if there is no space on the queue then
  2198. exit without doing anything. */
  2199. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  2200. {
  2201. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  2202. /* We only want to wake one co-routine per ISR, so check that a
  2203. co-routine has not already been woken. */
  2204. if( xCoRoutinePreviouslyWoken == pdFALSE )
  2205. {
  2206. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  2207. {
  2208. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  2209. {
  2210. return pdTRUE;
  2211. }
  2212. else
  2213. {
  2214. mtCOVERAGE_TEST_MARKER();
  2215. }
  2216. }
  2217. else
  2218. {
  2219. mtCOVERAGE_TEST_MARKER();
  2220. }
  2221. }
  2222. else
  2223. {
  2224. mtCOVERAGE_TEST_MARKER();
  2225. }
  2226. }
  2227. else
  2228. {
  2229. mtCOVERAGE_TEST_MARKER();
  2230. }
  2231. return xCoRoutinePreviouslyWoken;
  2232. }
  2233. #endif /* configUSE_CO_ROUTINES */
  2234. /*-----------------------------------------------------------*/
  2235. #if ( configUSE_CO_ROUTINES == 1 )
  2236. BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxCoRoutineWoken )
  2237. {
  2238. BaseType_t xReturn;
  2239. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2240. /* We cannot block from an ISR, so check there is data available. If
  2241. not then just leave without doing anything. */
  2242. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  2243. {
  2244. /* Copy the data from the queue. */
  2245. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  2246. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  2247. {
  2248. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  2249. }
  2250. else
  2251. {
  2252. mtCOVERAGE_TEST_MARKER();
  2253. }
  2254. --( pxQueue->uxMessagesWaiting );
  2255. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  2256. if( ( *pxCoRoutineWoken ) == pdFALSE )
  2257. {
  2258. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  2259. {
  2260. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  2261. {
  2262. *pxCoRoutineWoken = pdTRUE;
  2263. }
  2264. else
  2265. {
  2266. mtCOVERAGE_TEST_MARKER();
  2267. }
  2268. }
  2269. else
  2270. {
  2271. mtCOVERAGE_TEST_MARKER();
  2272. }
  2273. }
  2274. else
  2275. {
  2276. mtCOVERAGE_TEST_MARKER();
  2277. }
  2278. xReturn = pdPASS;
  2279. }
  2280. else
  2281. {
  2282. xReturn = pdFAIL;
  2283. }
  2284. return xReturn;
  2285. }
  2286. #endif /* configUSE_CO_ROUTINES */
  2287. /*-----------------------------------------------------------*/
  2288. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2289. void vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2290. {
  2291. UBaseType_t ux;
  2292. /* See if there is an empty space in the registry. A NULL name denotes
  2293. a free slot. */
  2294. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2295. {
  2296. if( xQueueRegistry[ ux ].pcQueueName == NULL )
  2297. {
  2298. /* Store the information on this queue. */
  2299. xQueueRegistry[ ux ].pcQueueName = pcQueueName;
  2300. xQueueRegistry[ ux ].xHandle = xQueue;
  2301. traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
  2302. break;
  2303. }
  2304. else
  2305. {
  2306. mtCOVERAGE_TEST_MARKER();
  2307. }
  2308. }
  2309. }
  2310. #endif /* configQUEUE_REGISTRY_SIZE */
  2311. /*-----------------------------------------------------------*/
  2312. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2313. const char *pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2314. {
  2315. UBaseType_t ux;
  2316. const char *pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2317. /* Note there is nothing here to protect against another task adding or
  2318. removing entries from the registry while it is being searched. */
  2319. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2320. {
  2321. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2322. {
  2323. pcReturn = xQueueRegistry[ ux ].pcQueueName;
  2324. break;
  2325. }
  2326. else
  2327. {
  2328. mtCOVERAGE_TEST_MARKER();
  2329. }
  2330. }
  2331. return pcReturn;
  2332. } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
  2333. #endif /* configQUEUE_REGISTRY_SIZE */
  2334. /*-----------------------------------------------------------*/
  2335. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2336. void vQueueUnregisterQueue( QueueHandle_t xQueue )
  2337. {
  2338. UBaseType_t ux;
  2339. /* See if the handle of the queue being unregistered in actually in the
  2340. registry. */
  2341. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2342. {
  2343. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2344. {
  2345. /* Set the name to NULL to show that this slot if free again. */
  2346. xQueueRegistry[ ux ].pcQueueName = NULL;
  2347. /* Set the handle to NULL to ensure the same queue handle cannot
  2348. appear in the registry twice if it is added, removed, then
  2349. added again. */
  2350. xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
  2351. break;
  2352. }
  2353. else
  2354. {
  2355. mtCOVERAGE_TEST_MARKER();
  2356. }
  2357. }
  2358. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  2359. #endif /* configQUEUE_REGISTRY_SIZE */
  2360. /*-----------------------------------------------------------*/
  2361. #if ( configUSE_TIMERS == 1 )
  2362. void vQueueWaitForMessageRestricted( QueueHandle_t xQueue, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely )
  2363. {
  2364. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2365. /* This function should not be called by application code hence the
  2366. 'Restricted' in its name. It is not part of the public API. It is
  2367. designed for use by kernel code, and has special calling requirements.
  2368. It can result in vListInsert() being called on a list that can only
  2369. possibly ever have one item in it, so the list will be fast, but even
  2370. so it should be called with the scheduler locked and not from a critical
  2371. section. */
  2372. /* Only do anything if there are no messages in the queue. This function
  2373. will not actually cause the task to block, just place it on a blocked
  2374. list. It will not block until the scheduler is unlocked - at which
  2375. time a yield will be performed. If an item is added to the queue while
  2376. the queue is locked, and the calling task blocks on the queue, then the
  2377. calling task will be immediately unblocked when the queue is unlocked. */
  2378. prvLockQueue( pxQueue );
  2379. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
  2380. {
  2381. /* There is nothing in the queue, block for the specified period. */
  2382. vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
  2383. }
  2384. else
  2385. {
  2386. mtCOVERAGE_TEST_MARKER();
  2387. }
  2388. prvUnlockQueue( pxQueue );
  2389. }
  2390. #endif /* configUSE_TIMERS */
  2391. /*-----------------------------------------------------------*/
  2392. #if( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  2393. QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
  2394. {
  2395. QueueSetHandle_t pxQueue;
  2396. pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
  2397. return pxQueue;
  2398. }
  2399. #endif /* configUSE_QUEUE_SETS */
  2400. /*-----------------------------------------------------------*/
  2401. #if ( configUSE_QUEUE_SETS == 1 )
  2402. BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2403. {
  2404. BaseType_t xReturn;
  2405. taskENTER_CRITICAL();
  2406. {
  2407. if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
  2408. {
  2409. /* Cannot add a queue/semaphore to more than one queue set. */
  2410. xReturn = pdFAIL;
  2411. }
  2412. else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2413. {
  2414. /* Cannot add a queue/semaphore to a queue set if there are already
  2415. items in the queue/semaphore. */
  2416. xReturn = pdFAIL;
  2417. }
  2418. else
  2419. {
  2420. ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
  2421. xReturn = pdPASS;
  2422. }
  2423. }
  2424. taskEXIT_CRITICAL();
  2425. return xReturn;
  2426. }
  2427. #endif /* configUSE_QUEUE_SETS */
  2428. /*-----------------------------------------------------------*/
  2429. #if ( configUSE_QUEUE_SETS == 1 )
  2430. BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2431. {
  2432. BaseType_t xReturn;
  2433. Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
  2434. if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
  2435. {
  2436. /* The queue was not a member of the set. */
  2437. xReturn = pdFAIL;
  2438. }
  2439. else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2440. {
  2441. /* It is dangerous to remove a queue from a set when the queue is
  2442. not empty because the queue set will still hold pending events for
  2443. the queue. */
  2444. xReturn = pdFAIL;
  2445. }
  2446. else
  2447. {
  2448. taskENTER_CRITICAL();
  2449. {
  2450. /* The queue is no longer contained in the set. */
  2451. pxQueueOrSemaphore->pxQueueSetContainer = NULL;
  2452. }
  2453. taskEXIT_CRITICAL();
  2454. xReturn = pdPASS;
  2455. }
  2456. return xReturn;
  2457. } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
  2458. #endif /* configUSE_QUEUE_SETS */
  2459. /*-----------------------------------------------------------*/
  2460. #if ( configUSE_QUEUE_SETS == 1 )
  2461. QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, TickType_t const xTicksToWait )
  2462. {
  2463. QueueSetMemberHandle_t xReturn = NULL;
  2464. ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2465. return xReturn;
  2466. }
  2467. #endif /* configUSE_QUEUE_SETS */
  2468. /*-----------------------------------------------------------*/
  2469. #if ( configUSE_QUEUE_SETS == 1 )
  2470. QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
  2471. {
  2472. QueueSetMemberHandle_t xReturn = NULL;
  2473. ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2474. return xReturn;
  2475. }
  2476. #endif /* configUSE_QUEUE_SETS */
  2477. /*-----------------------------------------------------------*/
  2478. #if ( configUSE_QUEUE_SETS == 1 )
  2479. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition )
  2480. {
  2481. Queue_t *pxQueueSetContainer = pxQueue->pxQueueSetContainer;
  2482. BaseType_t xReturn = pdFALSE;
  2483. /* This function must be called form a critical section. */
  2484. configASSERT( pxQueueSetContainer );
  2485. configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
  2486. if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
  2487. {
  2488. const int8_t cTxLock = pxQueueSetContainer->cTxLock;
  2489. traceQUEUE_SEND( pxQueueSetContainer );
  2490. /* The data copied is the handle of the queue that contains data. */
  2491. xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
  2492. if( cTxLock == queueUNLOCKED )
  2493. {
  2494. if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
  2495. {
  2496. if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
  2497. {
  2498. /* The task waiting has a higher priority. */
  2499. xReturn = pdTRUE;
  2500. }
  2501. else
  2502. {
  2503. mtCOVERAGE_TEST_MARKER();
  2504. }
  2505. }
  2506. else
  2507. {
  2508. mtCOVERAGE_TEST_MARKER();
  2509. }
  2510. }
  2511. else
  2512. {
  2513. pxQueueSetContainer->cTxLock = ( int8_t ) ( cTxLock + 1 );
  2514. }
  2515. }
  2516. else
  2517. {
  2518. mtCOVERAGE_TEST_MARKER();
  2519. }
  2520. return xReturn;
  2521. }
  2522. #endif /* configUSE_QUEUE_SETS */