queue.c 93 KB

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