queue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "FreeRTOS.h"
  31. #include "queue.h"
  32. /* Semaphores do not actually store or copy data, so have an item size of
  33. * zero. */
  34. #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
  35. #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
  36. typedef struct QueueDefinition
  37. {
  38. struct rt_ipc_object *rt_ipc;
  39. } xQUEUE;
  40. typedef xQUEUE Queue_t;
  41. static volatile rt_uint8_t mutex_index = 0;
  42. static volatile rt_uint8_t sem_index = 0;
  43. static volatile rt_uint8_t queue_index = 0;
  44. /*-----------------------------------------------------------*/
  45. BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
  46. BaseType_t xNewQueue )
  47. {
  48. Queue_t * const pxQueue = xQueue;
  49. struct rt_ipc_object *pipc;
  50. rt_uint8_t type;
  51. configASSERT( pxQueue );
  52. pipc = pxQueue->rt_ipc;
  53. RT_ASSERT( pipc != RT_NULL );
  54. type = rt_object_get_type( &pipc->parent );
  55. if ( type == RT_Object_Class_Semaphore )
  56. {
  57. rt_sem_control( ( rt_sem_t ) pipc, RT_IPC_CMD_RESET, ( void * ) 0);
  58. }
  59. else if ( type == RT_Object_Class_MessageQueue )
  60. {
  61. rt_mq_control( ( rt_mq_t ) pipc, RT_IPC_CMD_RESET, RT_NULL );
  62. }
  63. return pdPASS;
  64. }
  65. /*-----------------------------------------------------------*/
  66. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  67. QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
  68. const UBaseType_t uxItemSize,
  69. uint8_t * pucQueueStorage,
  70. StaticQueue_t * pxStaticQueue,
  71. const uint8_t ucQueueType )
  72. {
  73. Queue_t * pxNewQueue = NULL;
  74. char name[RT_NAME_MAX] = {0};
  75. /* The StaticQueue_t structure and the queue storage area must be
  76. * supplied. */
  77. configASSERT( pxStaticQueue );
  78. if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
  79. ( pxStaticQueue != NULL ) &&
  80. /* A queue storage area should be provided if the item size is not 0, and
  81. * should not be provided if the item size is 0. */
  82. ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
  83. ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
  84. {
  85. if ( ucQueueType == queueQUEUE_TYPE_RECURSIVE_MUTEX || ucQueueType == queueQUEUE_TYPE_MUTEX )
  86. {
  87. rt_snprintf( name, RT_NAME_MAX, "mutex%02d", mutex_index++ );
  88. rt_mutex_init( ( rt_mutex_t ) &( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.mutex, name, RT_IPC_FLAG_PRIO );
  89. }
  90. else if ( ucQueueType == queueQUEUE_TYPE_BINARY_SEMAPHORE || ucQueueType == queueQUEUE_TYPE_COUNTING_SEMAPHORE )
  91. {
  92. rt_snprintf( name, RT_NAME_MAX, "sem%02d", sem_index++ );
  93. rt_sem_init( ( rt_sem_t ) &( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.semaphore, name, 0, RT_IPC_FLAG_PRIO );
  94. ( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.semaphore.max_value = uxQueueLength;
  95. }
  96. else if ( ucQueueType == queueQUEUE_TYPE_BASE )
  97. {
  98. rt_snprintf( name, RT_NAME_MAX, "queue%02d", queue_index++ );
  99. rt_mq_init( &( pxStaticQueue->ipc_obj ), name, pucQueueStorage, uxItemSize, QUEUE_BUFFER_SIZE( uxQueueLength, uxItemSize ), RT_IPC_FLAG_PRIO );
  100. }
  101. else
  102. {
  103. return pxNewQueue;
  104. }
  105. pxStaticQueue->rt_ipc = ( struct rt_ipc_object * ) &pxStaticQueue->ipc_obj;
  106. pxNewQueue = ( QueueHandle_t ) pxStaticQueue;
  107. }
  108. return pxNewQueue;
  109. }
  110. #endif /* configSUPPORT_STATIC_ALLOCATION */
  111. /*-----------------------------------------------------------*/
  112. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  113. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
  114. const UBaseType_t uxItemSize,
  115. const uint8_t ucQueueType )
  116. {
  117. Queue_t * pxNewQueue = NULL;
  118. char name[RT_NAME_MAX] = {0};
  119. struct rt_ipc_object * pipc = RT_NULL;
  120. if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
  121. /* Check for multiplication overflow. */
  122. ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
  123. /* Check for addition overflow. */
  124. ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
  125. {
  126. pxNewQueue = ( Queue_t * ) RT_KERNEL_MALLOC( sizeof( Queue_t ) );
  127. if ( pxNewQueue == NULL )
  128. {
  129. return ( QueueHandle_t ) pxNewQueue;
  130. }
  131. if ( ucQueueType == queueQUEUE_TYPE_RECURSIVE_MUTEX || ucQueueType == queueQUEUE_TYPE_MUTEX )
  132. {
  133. rt_snprintf( name, RT_NAME_MAX, "mutex%02d", mutex_index++ );
  134. pipc = ( struct rt_ipc_object * ) rt_mutex_create( name, RT_IPC_FLAG_PRIO );
  135. }
  136. else if ( ucQueueType == queueQUEUE_TYPE_BINARY_SEMAPHORE || ucQueueType == queueQUEUE_TYPE_COUNTING_SEMAPHORE )
  137. {
  138. rt_snprintf( name, RT_NAME_MAX, "sem%02d", sem_index++ );
  139. pipc = ( struct rt_ipc_object * ) RT_KERNEL_MALLOC( sizeof( struct rt_semaphore_wrapper ) );
  140. if ( pipc != RT_NULL )
  141. {
  142. rt_sem_init( ( rt_sem_t ) pipc, name, 0, RT_IPC_FLAG_PRIO );
  143. ( ( struct rt_semaphore_wrapper * ) pipc )->max_value = uxQueueLength;
  144. /* Mark as dynamic so we can distinguish in vQueueDelete */
  145. pipc->parent.type &= ~RT_Object_Class_Static;
  146. }
  147. }
  148. else if ( ucQueueType == queueQUEUE_TYPE_BASE )
  149. {
  150. rt_snprintf( name, RT_NAME_MAX, "queue%02d", queue_index++ );
  151. pipc = ( struct rt_ipc_object * ) rt_mq_create( name, uxItemSize, uxQueueLength, RT_IPC_FLAG_PRIO);
  152. }
  153. if ( pipc == RT_NULL )
  154. {
  155. RT_KERNEL_FREE( pxNewQueue );
  156. return NULL;
  157. }
  158. pxNewQueue->rt_ipc = pipc;
  159. }
  160. return ( QueueHandle_t ) pxNewQueue;
  161. }
  162. #endif /* configSUPPORT_STATIC_ALLOCATION */
  163. /*-----------------------------------------------------------*/
  164. #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  165. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  166. {
  167. QueueHandle_t xNewQueue;
  168. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  169. xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
  170. return xNewQueue;
  171. }
  172. #endif /* configUSE_MUTEXES */
  173. /*-----------------------------------------------------------*/
  174. #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  175. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
  176. StaticQueue_t * pxStaticQueue )
  177. {
  178. QueueHandle_t xNewQueue;
  179. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  180. xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
  181. return xNewQueue;
  182. }
  183. #endif /* configUSE_MUTEXES */
  184. /*-----------------------------------------------------------*/
  185. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  186. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  187. {
  188. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  189. configASSERT( pxMutex );
  190. return xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  191. }
  192. #endif /* configUSE_RECURSIVE_MUTEXES */
  193. /*-----------------------------------------------------------*/
  194. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  195. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
  196. TickType_t xTicksToWait )
  197. {
  198. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  199. configASSERT( pxMutex );
  200. return xQueueSemaphoreTake( pxMutex, xTicksToWait );
  201. }
  202. #endif /* configUSE_RECURSIVE_MUTEXES */
  203. /*-----------------------------------------------------------*/
  204. #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  205. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
  206. const UBaseType_t uxInitialCount,
  207. StaticQueue_t * pxStaticQueue )
  208. {
  209. QueueHandle_t xHandle = NULL;
  210. if( ( uxMaxCount != 0 ) &&
  211. ( uxInitialCount <= uxMaxCount ) )
  212. {
  213. xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  214. if( xHandle != NULL )
  215. {
  216. ( ( rt_sem_t ) ( ( Queue_t * ) xHandle )->rt_ipc )->value = uxInitialCount;
  217. }
  218. }
  219. else
  220. {
  221. configASSERT( xHandle );
  222. }
  223. return xHandle;
  224. }
  225. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  226. /*-----------------------------------------------------------*/
  227. #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  228. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
  229. const UBaseType_t uxInitialCount )
  230. {
  231. QueueHandle_t xHandle = NULL;
  232. if( ( uxMaxCount != 0 ) &&
  233. ( uxInitialCount <= uxMaxCount ) )
  234. {
  235. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  236. if( xHandle != NULL )
  237. {
  238. ( ( rt_sem_t ) ( ( Queue_t * ) xHandle )->rt_ipc )->value = uxInitialCount;
  239. }
  240. }
  241. else
  242. {
  243. configASSERT( xHandle );
  244. }
  245. return xHandle;
  246. }
  247. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  248. /*-----------------------------------------------------------*/
  249. BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
  250. const void * const pvItemToQueue,
  251. TickType_t xTicksToWait,
  252. const BaseType_t xCopyPosition )
  253. {
  254. Queue_t * const pxQueue = xQueue;
  255. struct rt_ipc_object *pipc;
  256. rt_uint8_t type;
  257. rt_base_t level;
  258. rt_err_t err = -RT_ERROR;
  259. configASSERT( pxQueue );
  260. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  261. {
  262. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  263. }
  264. #endif
  265. pipc = pxQueue->rt_ipc;
  266. RT_ASSERT( pipc != RT_NULL );
  267. type = rt_object_get_type( &pipc->parent );
  268. if ( type == RT_Object_Class_Mutex )
  269. {
  270. err = rt_mutex_release( ( rt_mutex_t ) pipc );
  271. }
  272. else if ( type == RT_Object_Class_Semaphore )
  273. {
  274. level = rt_hw_interrupt_disable();
  275. if ( ( ( rt_sem_t ) pipc )->value < ( ( struct rt_semaphore_wrapper * ) pipc )->max_value )
  276. {
  277. err = rt_sem_release( ( rt_sem_t ) pipc );
  278. }
  279. rt_hw_interrupt_enable( level );
  280. }
  281. else if ( type == RT_Object_Class_MessageQueue )
  282. {
  283. if ( xCopyPosition == queueSEND_TO_BACK )
  284. {
  285. err = rt_mq_send_wait( ( rt_mq_t ) pipc, pvItemToQueue, ( ( rt_mq_t ) pipc )->msg_size, ( rt_int32_t ) xTicksToWait );
  286. }
  287. else if ( xCopyPosition == queueSEND_TO_FRONT )
  288. {
  289. // TODO: need to implement the timeout for LIFO
  290. err = rt_mq_urgent( ( rt_mq_t ) pipc, pvItemToQueue, ( ( rt_mq_t ) pipc )->msg_size );
  291. }
  292. }
  293. return rt_err_to_freertos( err );
  294. }
  295. /*-----------------------------------------------------------*/
  296. BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
  297. const void * const pvItemToQueue,
  298. BaseType_t * const pxHigherPriorityTaskWoken,
  299. const BaseType_t xCopyPosition )
  300. {
  301. Queue_t * const pxQueue = xQueue;
  302. struct rt_ipc_object *pipc;
  303. rt_uint8_t type;
  304. rt_err_t err = -RT_ERROR;
  305. configASSERT( pxQueue );
  306. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  307. {
  308. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  309. }
  310. #endif
  311. pipc = pxQueue->rt_ipc;
  312. RT_ASSERT( pipc != RT_NULL );
  313. type = rt_object_get_type( &pipc->parent );
  314. if ( type == RT_Object_Class_MessageQueue )
  315. {
  316. if ( xCopyPosition == queueSEND_TO_BACK )
  317. {
  318. err = rt_mq_send( ( rt_mq_t ) pipc, pvItemToQueue, ( ( rt_mq_t ) pipc )->msg_size);
  319. }
  320. else if ( xCopyPosition == queueSEND_TO_FRONT )
  321. {
  322. err = rt_mq_urgent( ( rt_mq_t ) pipc, pvItemToQueue, ( ( rt_mq_t ) pipc )->msg_size );
  323. }
  324. }
  325. return rt_err_to_freertos( err );
  326. }
  327. /*-----------------------------------------------------------*/
  328. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
  329. BaseType_t * const pxHigherPriorityTaskWoken )
  330. {
  331. Queue_t * const pxQueue = xQueue;
  332. struct rt_ipc_object *pipc;
  333. rt_uint8_t type;
  334. rt_base_t level;
  335. rt_err_t err = -RT_ERROR
  336. configASSERT( pxQueue );
  337. pipc = pxQueue->rt_ipc;
  338. RT_ASSERT( pipc != RT_NULL );
  339. type = rt_object_get_type( &pipc->parent );
  340. RT_ASSERT( type != RT_Object_Class_Mutex );
  341. if ( type == RT_Object_Class_Semaphore )
  342. {
  343. level = rt_hw_interrupt_disable();
  344. if ( ( ( rt_sem_t ) pipc )->value < ( ( struct rt_semaphore_wrapper * ) pipc )->max_value )
  345. {
  346. err = rt_sem_release( ( rt_sem_t ) pipc );
  347. }
  348. rt_hw_interrupt_enable( level );
  349. }
  350. if ( pxHigherPriorityTaskWoken != NULL )
  351. {
  352. *pxHigherPriorityTaskWoken = pdFALSE;
  353. }
  354. return rt_err_to_freertos( err );
  355. }
  356. /*-----------------------------------------------------------*/
  357. BaseType_t xQueueReceive( QueueHandle_t xQueue,
  358. void * const pvBuffer,
  359. TickType_t xTicksToWait )
  360. {
  361. Queue_t * const pxQueue = xQueue;
  362. struct rt_ipc_object *pipc;
  363. rt_uint8_t type;
  364. rt_err_t err = -RT_ERROR;
  365. /* Check the queue pointer is not NULL. */
  366. configASSERT( ( pxQueue ) );
  367. /* Cannot block if the scheduler is suspended. */
  368. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  369. {
  370. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  371. }
  372. #endif
  373. pipc = pxQueue->rt_ipc;
  374. RT_ASSERT( pipc != RT_NULL );
  375. type = rt_object_get_type( &pipc->parent );
  376. if ( type == RT_Object_Class_MessageQueue )
  377. {
  378. err = rt_mq_recv( ( rt_mq_t ) pipc, pvBuffer, ( ( rt_mq_t ) pipc )->msg_size, ( rt_int32_t ) xTicksToWait );
  379. }
  380. return rt_err_to_freertos( err );
  381. }
  382. /*-----------------------------------------------------------*/
  383. BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
  384. TickType_t xTicksToWait )
  385. {
  386. Queue_t * const pxQueue = xQueue;
  387. struct rt_ipc_object *pipc;
  388. rt_uint8_t type;
  389. rt_err_t err = -RT_ERROR;
  390. /* Check the queue pointer is not NULL. */
  391. configASSERT( ( pxQueue ) );
  392. /* Cannot block if the scheduler is suspended. */
  393. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  394. {
  395. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  396. }
  397. #endif
  398. pipc = pxQueue->rt_ipc;
  399. RT_ASSERT( pipc != RT_NULL );
  400. type = rt_object_get_type( &pipc->parent );
  401. if ( type == RT_Object_Class_Mutex )
  402. {
  403. err = rt_mutex_take( ( rt_mutex_t ) pipc, ( rt_int32_t ) xTicksToWait );
  404. }
  405. else if ( type == RT_Object_Class_Semaphore )
  406. {
  407. err = rt_sem_take( ( rt_sem_t ) pipc, ( rt_int32_t ) xTicksToWait );
  408. }
  409. return rt_err_to_freertos( err );
  410. }
  411. /*-----------------------------------------------------------*/
  412. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
  413. void * const pvBuffer,
  414. BaseType_t * const pxHigherPriorityTaskWoken )
  415. {
  416. Queue_t * const pxQueue = xQueue;
  417. struct rt_ipc_object *pipc;
  418. rt_uint8_t type;
  419. rt_err_t err = -RT_ERROR
  420. configASSERT( pxQueue );
  421. pipc = pxQueue->rt_ipc;
  422. RT_ASSERT( pipc != RT_NULL );
  423. type = rt_object_get_type( &pipc->parent );
  424. RT_ASSERT( type != RT_Object_Class_Mutex );
  425. if ( type == RT_Object_Class_Semaphore )
  426. {
  427. err = rt_sem_take( ( rt_sem_t ) pipc, RT_WAITING_NO );
  428. }
  429. else if ( type == RT_Object_Class_MessageQueue )
  430. {
  431. err = rt_mq_recv( ( rt_mq_t ) pipc, pvBuffer, ( ( rt_mq_t ) pipc )->msg_size, RT_WAITING_NO );
  432. }
  433. if ( pxHigherPriorityTaskWoken != NULL )
  434. {
  435. *pxHigherPriorityTaskWoken = pdFALSE;
  436. }
  437. return rt_err_to_freertos( err );
  438. }
  439. /*-----------------------------------------------------------*/
  440. void vQueueDelete( QueueHandle_t xQueue )
  441. {
  442. Queue_t * const pxQueue = xQueue;
  443. struct rt_ipc_object *pipc;
  444. rt_uint8_t type;
  445. configASSERT( pxQueue );
  446. pipc = pxQueue->rt_ipc;
  447. RT_ASSERT( pipc != RT_NULL );
  448. type = rt_object_get_type( &pipc->parent );
  449. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  450. if ( rt_object_is_systemobject( ( rt_object_t ) pipc ) )
  451. #endif
  452. {
  453. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  454. if ( type == RT_Object_Class_Mutex )
  455. {
  456. rt_mutex_detach( ( rt_mutex_t ) pipc );
  457. }
  458. else if ( type == RT_Object_Class_Semaphore )
  459. {
  460. rt_sem_detach( ( rt_sem_t ) pipc );
  461. }
  462. else if ( type == RT_Object_Class_MessageQueue )
  463. {
  464. rt_mq_detach( ( rt_mq_t ) pipc );
  465. }
  466. #endif
  467. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  468. }
  469. else
  470. {
  471. #endif
  472. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  473. if ( type == RT_Object_Class_Mutex )
  474. {
  475. rt_mutex_delete( ( rt_mutex_t ) pipc );
  476. }
  477. else if ( type == RT_Object_Class_Semaphore )
  478. {
  479. /* Allocated with rt_sem_init in xQueueGenericCreate */
  480. pipc->parent.type |= RT_Object_Class_Static;
  481. rt_sem_detach( ( rt_sem_t ) pipc );
  482. RT_KERNEL_FREE( pipc );
  483. }
  484. else if ( type == RT_Object_Class_MessageQueue )
  485. {
  486. rt_mq_delete( ( rt_mq_t ) pipc );
  487. }
  488. else
  489. {
  490. return;
  491. }
  492. RT_KERNEL_FREE( pxQueue );
  493. #endif
  494. }
  495. }
  496. /*-----------------------------------------------------------*/