dataqueue.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * File : dataqueue.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <rthw.h>
  27. struct rt_data_item
  28. {
  29. const void *data_ptr;
  30. rt_size_t data_size;
  31. };
  32. rt_err_t
  33. rt_data_queue_init(struct rt_data_queue *queue,
  34. rt_uint16_t size,
  35. rt_uint16_t lwm,
  36. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event))
  37. {
  38. RT_ASSERT(queue != RT_NULL);
  39. queue->evt_notify = evt_notify;
  40. queue->size = size;
  41. queue->lwm = lwm;
  42. queue->waiting_lwm = RT_FALSE;
  43. queue->get_index = 0;
  44. queue->put_index = 0;
  45. rt_list_init(&(queue->suspended_push_list));
  46. rt_list_init(&(queue->suspended_pop_list));
  47. queue->queue = (struct rt_data_item *)rt_malloc(sizeof(struct rt_data_item) * size);
  48. if (queue->queue == RT_NULL)
  49. {
  50. return -RT_ENOMEM;
  51. }
  52. return RT_EOK;
  53. }
  54. RTM_EXPORT(rt_data_queue_init);
  55. rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
  56. const void *data_ptr,
  57. rt_size_t data_size,
  58. rt_int32_t timeout)
  59. {
  60. rt_uint16_t mask;
  61. rt_ubase_t level;
  62. rt_thread_t thread;
  63. rt_err_t result;
  64. RT_ASSERT(queue != RT_NULL);
  65. result = RT_EOK;
  66. thread = rt_thread_self();
  67. mask = queue->size - 1;
  68. level = rt_hw_interrupt_disable();
  69. while (queue->put_index - queue->get_index == queue->size)
  70. {
  71. queue->waiting_lwm = RT_TRUE;
  72. /* queue is full */
  73. if (timeout == 0)
  74. {
  75. result = -RT_ETIMEOUT;
  76. goto __exit;
  77. }
  78. /* current context checking */
  79. RT_DEBUG_NOT_IN_INTERRUPT;
  80. /* reset thread error number */
  81. thread->error = RT_EOK;
  82. /* suspend thread on the push list */
  83. rt_thread_suspend(thread);
  84. rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
  85. /* start timer */
  86. if (timeout > 0)
  87. {
  88. /* reset the timeout of thread timer and start it */
  89. rt_timer_control(&(thread->thread_timer),
  90. RT_TIMER_CTRL_SET_TIME,
  91. &timeout);
  92. rt_timer_start(&(thread->thread_timer));
  93. }
  94. /* enable interrupt */
  95. rt_hw_interrupt_enable(level);
  96. /* do schedule */
  97. rt_schedule();
  98. /* thread is waked up */
  99. result = thread->error;
  100. level = rt_hw_interrupt_disable();
  101. if (result != RT_EOK)
  102. {
  103. queue->waiting_lwm = RT_FALSE;
  104. goto __exit;
  105. }
  106. }
  107. queue->queue[queue->put_index & mask].data_ptr = data_ptr;
  108. queue->queue[queue->put_index & mask].data_size = data_size;
  109. queue->put_index += 1;
  110. if (!rt_list_isempty(&(queue->suspended_pop_list)))
  111. {
  112. /* there is at least one thread in suspended list */
  113. /* get thread entry */
  114. thread = rt_list_entry(queue->suspended_pop_list.next,
  115. struct rt_thread,
  116. tlist);
  117. /* resume it */
  118. rt_thread_resume(thread);
  119. rt_hw_interrupt_enable(level);
  120. /* perform a schedule */
  121. rt_schedule();
  122. return result;
  123. }
  124. __exit:
  125. rt_hw_interrupt_enable(level);
  126. if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
  127. {
  128. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
  129. }
  130. return result;
  131. }
  132. RTM_EXPORT(rt_data_queue_push);
  133. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  134. const void** data_ptr,
  135. rt_size_t *size,
  136. rt_int32_t timeout)
  137. {
  138. rt_ubase_t level;
  139. rt_thread_t thread;
  140. rt_err_t result;
  141. rt_uint16_t mask;
  142. RT_ASSERT(queue != RT_NULL);
  143. RT_ASSERT(data_ptr != RT_NULL);
  144. RT_ASSERT(size != RT_NULL);
  145. result = RT_EOK;
  146. thread = rt_thread_self();
  147. mask = queue->size - 1;
  148. level = rt_hw_interrupt_disable();
  149. while (queue->get_index == queue->put_index)
  150. {
  151. /* queue is empty */
  152. if (timeout == 0)
  153. {
  154. result = -RT_ETIMEOUT;
  155. goto __exit;
  156. }
  157. /* current context checking */
  158. RT_DEBUG_NOT_IN_INTERRUPT;
  159. /* reset thread error number */
  160. thread->error = RT_EOK;
  161. /* suspend thread on the pop list */
  162. rt_thread_suspend(thread);
  163. rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
  164. /* start timer */
  165. if (timeout > 0)
  166. {
  167. /* reset the timeout of thread timer and start it */
  168. rt_timer_control(&(thread->thread_timer),
  169. RT_TIMER_CTRL_SET_TIME,
  170. &timeout);
  171. rt_timer_start(&(thread->thread_timer));
  172. }
  173. /* enable interrupt */
  174. rt_hw_interrupt_enable(level);
  175. /* do schedule */
  176. rt_schedule();
  177. /* thread is waked up */
  178. result = thread->error;
  179. level = rt_hw_interrupt_disable();
  180. if (result != RT_EOK)
  181. goto __exit;
  182. }
  183. *data_ptr = queue->queue[queue->get_index & mask].data_ptr;
  184. *size = queue->queue[queue->get_index & mask].data_size;
  185. queue->get_index += 1;
  186. if ((queue->waiting_lwm == RT_TRUE) &&
  187. (queue->put_index - queue->get_index) <= queue->lwm)
  188. {
  189. queue->waiting_lwm = RT_FALSE;
  190. /*
  191. * there is at least one thread in suspended list
  192. * and less than low water mark
  193. */
  194. if (!rt_list_isempty(&(queue->suspended_push_list)))
  195. {
  196. /* get thread entry */
  197. thread = rt_list_entry(queue->suspended_push_list.next,
  198. struct rt_thread,
  199. tlist);
  200. /* resume it */
  201. rt_thread_resume(thread);
  202. rt_hw_interrupt_enable(level);
  203. /* perform a schedule */
  204. rt_schedule();
  205. }
  206. if (queue->evt_notify != RT_NULL)
  207. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);
  208. return result;
  209. }
  210. __exit:
  211. rt_hw_interrupt_enable(level);
  212. if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
  213. {
  214. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
  215. }
  216. return result;
  217. }
  218. RTM_EXPORT(rt_data_queue_pop);
  219. rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
  220. const void** data_ptr,
  221. rt_size_t *size)
  222. {
  223. rt_ubase_t level;
  224. rt_uint16_t mask;
  225. RT_ASSERT(queue != RT_NULL);
  226. mask = queue->size - 1;
  227. level = rt_hw_interrupt_disable();
  228. if (queue->get_index == queue->put_index)
  229. {
  230. rt_hw_interrupt_enable(level);
  231. return -RT_EEMPTY;
  232. }
  233. *data_ptr = queue->queue[queue->get_index & mask].data_ptr;
  234. *size = queue->queue[queue->get_index & mask].data_size;
  235. rt_hw_interrupt_enable(level);
  236. return RT_EOK;
  237. }
  238. RTM_EXPORT(rt_data_queue_peak);
  239. void rt_data_queue_reset(struct rt_data_queue *queue)
  240. {
  241. struct rt_thread *thread;
  242. register rt_ubase_t temp;
  243. rt_enter_critical();
  244. /* wakeup all suspend threads */
  245. /* resume on pop list */
  246. while (!rt_list_isempty(&(queue->suspended_pop_list)))
  247. {
  248. /* disable interrupt */
  249. temp = rt_hw_interrupt_disable();
  250. /* get next suspend thread */
  251. thread = rt_list_entry(queue->suspended_pop_list.next,
  252. struct rt_thread,
  253. tlist);
  254. /* set error code to RT_ERROR */
  255. thread->error = -RT_ERROR;
  256. /*
  257. * resume thread
  258. * In rt_thread_resume function, it will remove current thread from
  259. * suspend list
  260. */
  261. rt_thread_resume(thread);
  262. /* enable interrupt */
  263. rt_hw_interrupt_enable(temp);
  264. }
  265. /* resume on push list */
  266. while (!rt_list_isempty(&(queue->suspended_push_list)))
  267. {
  268. /* disable interrupt */
  269. temp = rt_hw_interrupt_disable();
  270. /* get next suspend thread */
  271. thread = rt_list_entry(queue->suspended_push_list.next,
  272. struct rt_thread,
  273. tlist);
  274. /* set error code to RT_ERROR */
  275. thread->error = -RT_ERROR;
  276. /*
  277. * resume thread
  278. * In rt_thread_resume function, it will remove current thread from
  279. * suspend list
  280. */
  281. rt_thread_resume(thread);
  282. /* enable interrupt */
  283. rt_hw_interrupt_enable(temp);
  284. }
  285. rt_exit_critical();
  286. rt_schedule();
  287. }
  288. RTM_EXPORT(rt_data_queue_reset);