dataqueue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-09-30 Bernard first version.
  9. * 2016-10-31 armink fix some resume push and pop thread bugs
  10. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  11. * 2024-01-25 Shell porting to susp_list API
  12. */
  13. #include <rthw.h>
  14. #include <rtdevice.h>
  15. #define DATAQUEUE_MAGIC 0xbead0e0e
  16. struct rt_data_item
  17. {
  18. const void *data_ptr;
  19. rt_size_t data_size;
  20. };
  21. /**
  22. * @brief This function will initialize the data queue. Calling this function will
  23. * initialize the data queue control block and set the notification callback function.
  24. *
  25. * @param queue is a pointer to the data queue object.
  26. *
  27. * @param size is the maximum number of data in the data queue.
  28. *
  29. * @param lwm is low water mark.
  30. * When the number of data in the data queue is less than this value, this function will
  31. * wake up the thread waiting for write data.
  32. *
  33. * @param evt_notify is the notification callback function.
  34. *
  35. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  36. * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
  37. */
  38. rt_err_t
  39. rt_data_queue_init(struct rt_data_queue *queue,
  40. rt_uint16_t size,
  41. rt_uint16_t lwm,
  42. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event))
  43. {
  44. RT_ASSERT(queue != RT_NULL);
  45. RT_ASSERT(size > 0);
  46. queue->evt_notify = evt_notify;
  47. queue->magic = DATAQUEUE_MAGIC;
  48. queue->size = size;
  49. queue->lwm = lwm;
  50. queue->get_index = 0;
  51. queue->put_index = 0;
  52. queue->is_empty = 1;
  53. queue->is_full = 0;
  54. rt_spin_lock_init(&(queue->spinlock));
  55. rt_list_init(&(queue->suspended_push_list));
  56. rt_list_init(&(queue->suspended_pop_list));
  57. queue->queue = (struct rt_data_item *)rt_malloc(sizeof(struct rt_data_item) * size);
  58. if (queue->queue == RT_NULL)
  59. {
  60. return -RT_ENOMEM;
  61. }
  62. return RT_EOK;
  63. }
  64. RTM_EXPORT(rt_data_queue_init);
  65. /**
  66. * @brief This function will write data to the data queue. If the data queue is full,
  67. * the thread will suspend for the specified amount of time.
  68. *
  69. * @param queue is a pointer to the data queue object.
  70. * .
  71. * @param data_ptr is the buffer pointer of the data to be written.
  72. *
  73. * @param size is the size in bytes of the data to be written.
  74. *
  75. * @param timeout is the waiting time.
  76. *
  77. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  78. * When the return value is -RT_ETIMEOUT, it means the specified time out.
  79. */
  80. rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
  81. const void *data_ptr,
  82. rt_size_t data_size,
  83. rt_int32_t timeout)
  84. {
  85. rt_base_t level;
  86. rt_thread_t thread;
  87. rt_err_t result;
  88. RT_ASSERT(queue != RT_NULL);
  89. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  90. /* current context checking */
  91. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  92. result = RT_EOK;
  93. thread = rt_thread_self();
  94. level = rt_spin_lock_irqsave(&(queue->spinlock));
  95. while (queue->is_full)
  96. {
  97. /* queue is full */
  98. if (timeout == 0)
  99. {
  100. result = -RT_ETIMEOUT;
  101. goto __exit;
  102. }
  103. /* reset thread error number */
  104. thread->error = RT_EOK;
  105. /* suspend thread on the push list */
  106. result = rt_thread_suspend_to_list(thread, &queue->suspended_push_list,
  107. RT_IPC_FLAG_FIFO, RT_UNINTERRUPTIBLE);
  108. if (result == RT_EOK)
  109. {
  110. /* start timer */
  111. if (timeout > 0)
  112. {
  113. rt_tick_t timeout_tick = timeout;
  114. /* reset the timeout of thread timer and start it */
  115. rt_timer_control(&(thread->thread_timer),
  116. RT_TIMER_CTRL_SET_TIME,
  117. &timeout_tick);
  118. rt_timer_start(&(thread->thread_timer));
  119. }
  120. /* enable interrupt */
  121. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  122. /* do schedule */
  123. rt_schedule();
  124. /* thread is waked up */
  125. level = rt_spin_lock_irqsave(&(queue->spinlock));
  126. /* error may be modified by waker, so take the lock before accessing it */
  127. result = thread->error;
  128. }
  129. if (result != RT_EOK) goto __exit;
  130. }
  131. queue->queue[queue->put_index].data_ptr = data_ptr;
  132. queue->queue[queue->put_index].data_size = data_size;
  133. queue->put_index += 1;
  134. if (queue->put_index == queue->size)
  135. {
  136. queue->put_index = 0;
  137. }
  138. queue->is_empty = 0;
  139. if (queue->put_index == queue->get_index)
  140. {
  141. queue->is_full = 1;
  142. }
  143. /* there is at least one thread in suspended list */
  144. if (rt_susp_list_dequeue(&queue->suspended_pop_list,
  145. RT_THREAD_RESUME_RES_THR_ERR))
  146. {
  147. /* unlock and perform a schedule */
  148. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  149. /* perform a schedule */
  150. rt_schedule();
  151. return result;
  152. }
  153. __exit:
  154. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  155. if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
  156. {
  157. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
  158. }
  159. return result;
  160. }
  161. RTM_EXPORT(rt_data_queue_push);
  162. /**
  163. * @brief This function will pop data from the data queue. If the data queue is empty,the thread
  164. * will suspend for the specified amount of time.
  165. *
  166. * @note When the number of data in the data queue is less than lwm(low water mark), will
  167. * wake up the thread waiting for write data.
  168. *
  169. * @param queue is a pointer to the data queue object.
  170. *
  171. * @param data_ptr is the buffer pointer of the data to be fetched.
  172. *
  173. * @param size is the size in bytes of the data to be fetched.
  174. *
  175. * @param timeout is the waiting time.
  176. *
  177. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  178. * When the return value is -RT_ETIMEOUT, it means the specified time out.
  179. */
  180. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  181. const void **data_ptr,
  182. rt_size_t *size,
  183. rt_int32_t timeout)
  184. {
  185. rt_base_t level;
  186. rt_thread_t thread;
  187. rt_err_t result;
  188. RT_ASSERT(queue != RT_NULL);
  189. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  190. RT_ASSERT(data_ptr != RT_NULL);
  191. RT_ASSERT(size != RT_NULL);
  192. /* current context checking */
  193. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  194. result = RT_EOK;
  195. thread = rt_thread_self();
  196. level = rt_spin_lock_irqsave(&(queue->spinlock));
  197. while (queue->is_empty)
  198. {
  199. /* queue is empty */
  200. if (timeout == 0)
  201. {
  202. result = -RT_ETIMEOUT;
  203. goto __exit;
  204. }
  205. /* reset thread error number */
  206. thread->error = RT_EOK;
  207. /* suspend thread on the pop list */
  208. result = rt_thread_suspend_to_list(thread, &queue->suspended_pop_list,
  209. RT_IPC_FLAG_FIFO, RT_UNINTERRUPTIBLE);
  210. if (result == RT_EOK)
  211. {
  212. /* start timer */
  213. if (timeout > 0)
  214. {
  215. rt_tick_t timeout_tick = timeout;
  216. /* reset the timeout of thread timer and start it */
  217. rt_timer_control(&(thread->thread_timer),
  218. RT_TIMER_CTRL_SET_TIME,
  219. &timeout_tick);
  220. rt_timer_start(&(thread->thread_timer));
  221. }
  222. /* enable interrupt */
  223. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  224. /* do schedule */
  225. rt_schedule();
  226. /* thread is waked up */
  227. level = rt_spin_lock_irqsave(&(queue->spinlock));
  228. result = thread->error;
  229. if (result != RT_EOK)
  230. goto __exit;
  231. }
  232. }
  233. *data_ptr = queue->queue[queue->get_index].data_ptr;
  234. *size = queue->queue[queue->get_index].data_size;
  235. queue->get_index += 1;
  236. if (queue->get_index == queue->size)
  237. {
  238. queue->get_index = 0;
  239. }
  240. queue->is_full = 0;
  241. if (queue->put_index == queue->get_index)
  242. {
  243. queue->is_empty = 1;
  244. }
  245. if (rt_data_queue_len(queue) <= queue->lwm)
  246. {
  247. /* there is at least one thread in suspended list */
  248. if (rt_susp_list_dequeue(&queue->suspended_push_list,
  249. RT_THREAD_RESUME_RES_THR_ERR))
  250. {
  251. /* unlock and perform a schedule */
  252. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  253. /* perform a schedule */
  254. rt_schedule();
  255. }
  256. else
  257. {
  258. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  259. }
  260. if (queue->evt_notify != RT_NULL)
  261. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);
  262. return result;
  263. }
  264. __exit:
  265. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  266. if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
  267. {
  268. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
  269. }
  270. return result;
  271. }
  272. RTM_EXPORT(rt_data_queue_pop);
  273. /**
  274. * @brief This function will fetch but retaining data in the data queue.
  275. *
  276. * @param queue is a pointer to the data queue object.
  277. *
  278. * @param data_ptr is the buffer pointer of the data to be fetched.
  279. *
  280. * @param size is the size in bytes of the data to be fetched.
  281. *
  282. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  283. * When the return value is -RT_EEMPTY, it means the data queue is empty.
  284. */
  285. rt_err_t rt_data_queue_peek(struct rt_data_queue *queue,
  286. const void **data_ptr,
  287. rt_size_t *size)
  288. {
  289. rt_base_t level;
  290. RT_ASSERT(queue != RT_NULL);
  291. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  292. if (queue->is_empty)
  293. {
  294. return -RT_EEMPTY;
  295. }
  296. level = rt_spin_lock_irqsave(&(queue->spinlock));
  297. *data_ptr = queue->queue[queue->get_index].data_ptr;
  298. *size = queue->queue[queue->get_index].data_size;
  299. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  300. return RT_EOK;
  301. }
  302. RTM_EXPORT(rt_data_queue_peek);
  303. /**
  304. * @brief This function will reset the data queue.
  305. *
  306. * @note Calling this function will wake up all threads on the data queue
  307. * that are hanging and waiting.
  308. *
  309. * @param queue is a pointer to the data queue object.
  310. */
  311. void rt_data_queue_reset(struct rt_data_queue *queue)
  312. {
  313. rt_base_t level;
  314. RT_ASSERT(queue != RT_NULL);
  315. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  316. level = rt_spin_lock_irqsave(&(queue->spinlock));
  317. queue->get_index = 0;
  318. queue->put_index = 0;
  319. queue->is_empty = 1;
  320. queue->is_full = 0;
  321. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  322. rt_enter_critical();
  323. /* wakeup all suspend threads */
  324. /* resume on pop list */
  325. rt_susp_list_resume_all_irq(&queue->suspended_pop_list, RT_ERROR,
  326. &(queue->spinlock));
  327. /* resume on push list */
  328. rt_susp_list_resume_all_irq(&queue->suspended_push_list, RT_ERROR,
  329. &(queue->spinlock));
  330. rt_exit_critical();
  331. rt_schedule();
  332. }
  333. RTM_EXPORT(rt_data_queue_reset);
  334. /**
  335. * @brief This function will deinit the data queue.
  336. *
  337. * @param queue is a pointer to the data queue object.
  338. *
  339. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  340. */
  341. rt_err_t rt_data_queue_deinit(struct rt_data_queue *queue)
  342. {
  343. rt_base_t level;
  344. RT_ASSERT(queue != RT_NULL);
  345. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  346. /* wakeup all suspend threads */
  347. rt_data_queue_reset(queue);
  348. level = rt_spin_lock_irqsave(&(queue->spinlock));
  349. queue->magic = 0;
  350. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  351. rt_free(queue->queue);
  352. return RT_EOK;
  353. }
  354. RTM_EXPORT(rt_data_queue_deinit);
  355. /**
  356. * @brief This function will get the number of data in the data queue.
  357. *
  358. * @param queue is a pointer to the data queue object.
  359. *
  360. * @return Return the number of data in the data queue.
  361. */
  362. rt_uint16_t rt_data_queue_len(struct rt_data_queue *queue)
  363. {
  364. rt_base_t level;
  365. rt_int16_t len;
  366. RT_ASSERT(queue != RT_NULL);
  367. RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
  368. if (queue->is_empty)
  369. {
  370. return 0;
  371. }
  372. level = rt_spin_lock_irqsave(&(queue->spinlock));
  373. if (queue->put_index > queue->get_index)
  374. {
  375. len = queue->put_index - queue->get_index;
  376. }
  377. else
  378. {
  379. len = queue->size + queue->put_index - queue->get_index;
  380. }
  381. rt_spin_unlock_irqrestore(&(queue->spinlock), level);
  382. return len;
  383. }
  384. RTM_EXPORT(rt_data_queue_len);