queue.c 118 KB

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