queue.c 125 KB

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