queue.c 119 KB

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