queue.c 93 KB

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