queue.c 123 KB

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