queue.c 122 KB

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