queue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. /*-----------------------------------------------------------*/
  44. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  45. QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
  46. const UBaseType_t uxItemSize,
  47. uint8_t * pucQueueStorage,
  48. StaticQueue_t * pxStaticQueue,
  49. const uint8_t ucQueueType )
  50. {
  51. Queue_t * pxNewQueue = NULL;
  52. char name[RT_NAME_MAX] = {0};
  53. /* The StaticQueue_t structure and the queue storage area must be
  54. * supplied. */
  55. configASSERT( pxStaticQueue );
  56. if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
  57. ( pxStaticQueue != NULL ) &&
  58. /* A queue storage area should be provided if the item size is not 0, and
  59. * should not be provided if the item size is 0. */
  60. ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
  61. ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
  62. {
  63. if ( ucQueueType == queueQUEUE_TYPE_RECURSIVE_MUTEX || ucQueueType == queueQUEUE_TYPE_MUTEX )
  64. {
  65. rt_snprintf( name, RT_NAME_MAX - 1, "mutex%02d", mutex_index++ );
  66. rt_mutex_init( ( rt_mutex_t ) &( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.mutex, name, RT_IPC_FLAG_PRIO );
  67. }
  68. else if ( ucQueueType == queueQUEUE_TYPE_BINARY_SEMAPHORE || ucQueueType == queueQUEUE_TYPE_COUNTING_SEMAPHORE )
  69. {
  70. rt_snprintf( name, RT_NAME_MAX - 1, "sem%02d", sem_index++ );
  71. rt_sem_init( ( rt_sem_t ) &( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.semaphore, name, 0, RT_IPC_FLAG_PRIO );
  72. ( ( StaticSemaphore_t * ) pxStaticQueue )->ipc_obj.semaphore.max_value = uxQueueLength;
  73. }
  74. else
  75. {
  76. return pxNewQueue;
  77. }
  78. pxStaticQueue->rt_ipc = ( struct rt_ipc_object * ) &pxStaticQueue->ipc_obj;
  79. pxNewQueue = ( QueueHandle_t ) pxStaticQueue;
  80. }
  81. return pxNewQueue;
  82. }
  83. #endif /* configSUPPORT_STATIC_ALLOCATION */
  84. /*-----------------------------------------------------------*/
  85. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  86. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
  87. const UBaseType_t uxItemSize,
  88. const uint8_t ucQueueType )
  89. {
  90. Queue_t * pxNewQueue = NULL;
  91. char name[RT_NAME_MAX] = {0};
  92. struct rt_ipc_object * pipc = RT_NULL;
  93. if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
  94. /* Check for multiplication overflow. */
  95. ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
  96. /* Check for addition overflow. */
  97. ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
  98. {
  99. pxNewQueue = ( Queue_t * ) RT_KERNEL_MALLOC( sizeof( Queue_t ) );
  100. if ( pxNewQueue == NULL )
  101. {
  102. return ( QueueHandle_t ) pxNewQueue;
  103. }
  104. if ( ucQueueType == queueQUEUE_TYPE_RECURSIVE_MUTEX || ucQueueType == queueQUEUE_TYPE_MUTEX )
  105. {
  106. rt_snprintf( name, RT_NAME_MAX - 1, "mutex%02d", mutex_index++ );
  107. pipc = ( struct rt_ipc_object * ) rt_mutex_create( name, RT_IPC_FLAG_PRIO );
  108. }
  109. else if ( ucQueueType == queueQUEUE_TYPE_BINARY_SEMAPHORE || ucQueueType == queueQUEUE_TYPE_COUNTING_SEMAPHORE )
  110. {
  111. rt_snprintf( name, RT_NAME_MAX - 1, "sem%02d", sem_index++ );
  112. pipc = ( struct rt_ipc_object * ) RT_KERNEL_MALLOC( sizeof( struct rt_semaphore_wrapper ) );
  113. if ( pipc != RT_NULL )
  114. {
  115. rt_sem_init( ( rt_sem_t ) pipc, name, 0, RT_IPC_FLAG_PRIO );
  116. ( ( struct rt_semaphore_wrapper * ) pipc )->max_value = uxQueueLength;
  117. /* Mark as static so we can distinguish in vQueueDelete */
  118. pipc->parent.type &= ~RT_Object_Class_Static;
  119. }
  120. }
  121. if ( pipc == RT_NULL )
  122. {
  123. RT_KERNEL_FREE( pxNewQueue );
  124. return NULL;
  125. }
  126. pxNewQueue->rt_ipc = pipc;
  127. }
  128. return ( QueueHandle_t ) pxNewQueue;
  129. }
  130. #endif /* configSUPPORT_STATIC_ALLOCATION */
  131. /*-----------------------------------------------------------*/
  132. #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  133. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  134. {
  135. QueueHandle_t xNewQueue;
  136. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  137. xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
  138. return xNewQueue;
  139. }
  140. #endif /* configUSE_MUTEXES */
  141. /*-----------------------------------------------------------*/
  142. #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  143. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
  144. StaticQueue_t * pxStaticQueue )
  145. {
  146. QueueHandle_t xNewQueue;
  147. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  148. xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
  149. return xNewQueue;
  150. }
  151. #endif /* configUSE_MUTEXES */
  152. /*-----------------------------------------------------------*/
  153. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  154. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  155. {
  156. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  157. configASSERT( pxMutex );
  158. return xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  159. }
  160. #endif /* configUSE_RECURSIVE_MUTEXES */
  161. /*-----------------------------------------------------------*/
  162. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  163. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
  164. TickType_t xTicksToWait )
  165. {
  166. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  167. configASSERT( pxMutex );
  168. return xQueueSemaphoreTake( pxMutex, xTicksToWait );
  169. }
  170. #endif /* configUSE_RECURSIVE_MUTEXES */
  171. /*-----------------------------------------------------------*/
  172. #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  173. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
  174. const UBaseType_t uxInitialCount,
  175. StaticQueue_t * pxStaticQueue )
  176. {
  177. QueueHandle_t xHandle = NULL;
  178. if( ( uxMaxCount != 0 ) &&
  179. ( uxInitialCount <= uxMaxCount ) )
  180. {
  181. xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  182. if( xHandle != NULL )
  183. {
  184. ( ( rt_sem_t ) ( ( Queue_t * ) xHandle )->rt_ipc )->value = uxInitialCount;
  185. }
  186. }
  187. else
  188. {
  189. configASSERT( xHandle );
  190. }
  191. return xHandle;
  192. }
  193. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  194. /*-----------------------------------------------------------*/
  195. #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  196. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
  197. const UBaseType_t uxInitialCount )
  198. {
  199. QueueHandle_t xHandle = NULL;
  200. if( ( uxMaxCount != 0 ) &&
  201. ( uxInitialCount <= uxMaxCount ) )
  202. {
  203. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  204. if( xHandle != NULL )
  205. {
  206. ( ( rt_sem_t ) ( ( Queue_t * ) xHandle )->rt_ipc )->value = uxInitialCount;
  207. }
  208. }
  209. else
  210. {
  211. configASSERT( xHandle );
  212. }
  213. return xHandle;
  214. }
  215. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  216. /*-----------------------------------------------------------*/
  217. BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
  218. const void * const pvItemToQueue,
  219. TickType_t xTicksToWait,
  220. const BaseType_t xCopyPosition )
  221. {
  222. Queue_t * const pxQueue = xQueue;
  223. struct rt_ipc_object *pipc;
  224. rt_uint8_t type;
  225. rt_base_t level;
  226. rt_err_t err = -RT_ERROR;
  227. configASSERT( pxQueue );
  228. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  229. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  230. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  231. {
  232. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  233. }
  234. #endif
  235. pipc = pxQueue->rt_ipc;
  236. RT_ASSERT( pipc != RT_NULL );
  237. type = rt_object_get_type( &pipc->parent );
  238. if ( type == RT_Object_Class_Mutex )
  239. {
  240. err = rt_mutex_release( ( rt_mutex_t ) pipc );
  241. }
  242. else if ( type == RT_Object_Class_Semaphore )
  243. {
  244. level = rt_hw_interrupt_disable();
  245. if ( ( ( rt_sem_t ) pipc )->value < ( ( struct rt_semaphore_wrapper * ) pipc )->max_value )
  246. {
  247. err = rt_sem_release( ( rt_sem_t ) pipc );
  248. }
  249. rt_hw_interrupt_enable( level );
  250. }
  251. return rt_err_to_freertos( err );
  252. }
  253. /*-----------------------------------------------------------*/
  254. BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
  255. TickType_t xTicksToWait )
  256. {
  257. Queue_t * const pxQueue = xQueue;
  258. struct rt_ipc_object *pipc;
  259. rt_uint8_t type;
  260. rt_err_t err = -RT_ERROR;
  261. /* Check the queue pointer is not NULL. */
  262. configASSERT( ( pxQueue ) );
  263. /* Cannot block if the scheduler is suspended. */
  264. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  265. {
  266. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  267. }
  268. #endif
  269. pipc = pxQueue->rt_ipc;
  270. RT_ASSERT( pipc != RT_NULL );
  271. type = rt_object_get_type( &pipc->parent );
  272. if ( type == RT_Object_Class_Mutex )
  273. {
  274. err = rt_mutex_take( ( rt_mutex_t ) pipc, ( rt_int32_t ) xTicksToWait );
  275. }
  276. else if ( type == RT_Object_Class_Semaphore )
  277. {
  278. err = rt_sem_take( ( rt_sem_t ) pipc, ( rt_int32_t ) xTicksToWait );
  279. }
  280. return rt_err_to_freertos( err );
  281. }
  282. /*-----------------------------------------------------------*/
  283. void vQueueDelete( QueueHandle_t xQueue )
  284. {
  285. Queue_t * const pxQueue = xQueue;
  286. struct rt_ipc_object *pipc;
  287. rt_uint8_t type;
  288. configASSERT( pxQueue );
  289. pipc = pxQueue->rt_ipc;
  290. RT_ASSERT( pipc != RT_NULL );
  291. type = rt_object_get_type( &pipc->parent );
  292. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  293. if ( rt_object_is_systemobject( ( rt_object_t ) pipc ) )
  294. #endif
  295. {
  296. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  297. if ( type == RT_Object_Class_Mutex )
  298. {
  299. rt_mutex_detach( ( rt_mutex_t ) pipc );
  300. }
  301. else if ( type == RT_Object_Class_Semaphore )
  302. {
  303. rt_sem_detach( ( rt_sem_t ) pipc );
  304. }
  305. else if ( type == RT_Object_Class_MailBox )
  306. {
  307. rt_mb_detach( ( rt_mailbox_t ) pipc );
  308. }
  309. #endif
  310. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  311. }
  312. else
  313. {
  314. #endif
  315. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  316. if ( type == RT_Object_Class_Mutex )
  317. {
  318. rt_mutex_delete( ( rt_mutex_t ) pipc );
  319. }
  320. else if ( type == RT_Object_Class_Semaphore )
  321. {
  322. /* Allocated with rt_sem_init in xQueueGenericCreate */
  323. pipc->parent.type |= RT_Object_Class_Static;
  324. rt_sem_detach( ( rt_sem_t ) pipc );
  325. RT_KERNEL_FREE( pipc );
  326. }
  327. else if ( type == RT_Object_Class_MailBox )
  328. {
  329. rt_mb_delete( ( rt_mailbox_t ) pipc );
  330. }
  331. else
  332. {
  333. return;
  334. }
  335. RT_KERNEL_FREE( pxQueue );
  336. #endif
  337. }
  338. }
  339. /*-----------------------------------------------------------*/