queue.c 139 KB

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