queue.c 136 KB

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