queue.c 89 KB

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