queue.c 130 KB

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