dataqueue.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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) goto __exit;
  102. }
  103. queue->queue[queue->put_index & mask].data_ptr = data_ptr;
  104. queue->queue[queue->put_index & mask].data_size = data_size;
  105. queue->put_index += 1;
  106. if (!rt_list_isempty(&(queue->suspended_pop_list)))
  107. {
  108. /* there is at least one thread in suspended list */
  109. /* get thread entry */
  110. thread = rt_list_entry(queue->suspended_pop_list.next,
  111. struct rt_thread,
  112. tlist);
  113. /* resume it */
  114. rt_thread_resume(thread);
  115. rt_hw_interrupt_enable(level);
  116. /* perform a schedule */
  117. rt_schedule();
  118. return result;
  119. }
  120. __exit:
  121. rt_hw_interrupt_enable(level);
  122. if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
  123. {
  124. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
  125. }
  126. return result;
  127. }
  128. RTM_EXPORT(rt_data_queue_push);
  129. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  130. const void** data_ptr,
  131. rt_size_t *size,
  132. rt_int32_t timeout)
  133. {
  134. rt_ubase_t level;
  135. rt_thread_t thread;
  136. rt_err_t result;
  137. rt_uint16_t mask;
  138. RT_ASSERT(queue != RT_NULL);
  139. RT_ASSERT(data_ptr != RT_NULL);
  140. RT_ASSERT(size != RT_NULL);
  141. result = RT_EOK;
  142. thread = rt_thread_self();
  143. mask = queue->size - 1;
  144. level = rt_hw_interrupt_disable();
  145. while (queue->get_index == queue->put_index)
  146. {
  147. /* queue is empty */
  148. if (timeout == 0)
  149. {
  150. result = -RT_ETIMEOUT;
  151. goto __exit;
  152. }
  153. /* current context checking */
  154. RT_DEBUG_NOT_IN_INTERRUPT;
  155. /* reset thread error number */
  156. thread->error = RT_EOK;
  157. /* suspend thread on the pop list */
  158. rt_thread_suspend(thread);
  159. rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
  160. /* start timer */
  161. if (timeout > 0)
  162. {
  163. /* reset the timeout of thread timer and start it */
  164. rt_timer_control(&(thread->thread_timer),
  165. RT_TIMER_CTRL_SET_TIME,
  166. &timeout);
  167. rt_timer_start(&(thread->thread_timer));
  168. }
  169. /* enable interrupt */
  170. rt_hw_interrupt_enable(level);
  171. /* do schedule */
  172. rt_schedule();
  173. /* thread is waked up */
  174. result = thread->error;
  175. level = rt_hw_interrupt_disable();
  176. if (result != RT_EOK)
  177. goto __exit;
  178. }
  179. *data_ptr = queue->queue[queue->get_index & mask].data_ptr;
  180. *size = queue->queue[queue->get_index & mask].data_size;
  181. queue->get_index += 1;
  182. if ((queue->waiting_lwm == RT_TRUE) &&
  183. (queue->put_index - queue->get_index) <= queue->lwm)
  184. {
  185. queue->waiting_lwm = RT_FALSE;
  186. /*
  187. * there is at least one thread in suspended list
  188. * and less than low water mark
  189. */
  190. if (!rt_list_isempty(&(queue->suspended_push_list)))
  191. {
  192. /* get thread entry */
  193. thread = rt_list_entry(queue->suspended_push_list.next,
  194. struct rt_thread,
  195. tlist);
  196. /* resume it */
  197. rt_thread_resume(thread);
  198. rt_hw_interrupt_enable(level);
  199. /* perform a schedule */
  200. rt_schedule();
  201. }
  202. if (queue->evt_notify != RT_NULL)
  203. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);
  204. return result;
  205. }
  206. __exit:
  207. rt_hw_interrupt_enable(level);
  208. if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
  209. {
  210. queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
  211. }
  212. return result;
  213. }
  214. RTM_EXPORT(rt_data_queue_pop);
  215. rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
  216. const void** data_ptr,
  217. rt_size_t *size)
  218. {
  219. rt_ubase_t level;
  220. rt_uint16_t mask;
  221. RT_ASSERT(queue != RT_NULL);
  222. mask = queue->size - 1;
  223. level = rt_hw_interrupt_disable();
  224. if (queue->get_index == queue->put_index)
  225. {
  226. rt_hw_interrupt_enable(level);
  227. return -RT_EEMPTY;
  228. }
  229. *data_ptr = queue->queue[queue->get_index & mask].data_ptr;
  230. *size = queue->queue[queue->get_index & mask].data_size;
  231. rt_hw_interrupt_enable(level);
  232. return RT_EOK;
  233. }
  234. RTM_EXPORT(rt_data_queue_peak);
  235. void rt_data_queue_reset(struct rt_data_queue *queue)
  236. {
  237. struct rt_thread *thread;
  238. register rt_ubase_t temp;
  239. rt_enter_critical();
  240. /* wakeup all suspend threads */
  241. /* resume on pop list */
  242. while (!rt_list_isempty(&(queue->suspended_pop_list)))
  243. {
  244. /* disable interrupt */
  245. temp = rt_hw_interrupt_disable();
  246. /* get next suspend thread */
  247. thread = rt_list_entry(queue->suspended_pop_list.next,
  248. struct rt_thread,
  249. tlist);
  250. /* set error code to RT_ERROR */
  251. thread->error = -RT_ERROR;
  252. /*
  253. * resume thread
  254. * In rt_thread_resume function, it will remove current thread from
  255. * suspend list
  256. */
  257. rt_thread_resume(thread);
  258. /* enable interrupt */
  259. rt_hw_interrupt_enable(temp);
  260. }
  261. /* resume on push list */
  262. while (!rt_list_isempty(&(queue->suspended_push_list)))
  263. {
  264. /* disable interrupt */
  265. temp = rt_hw_interrupt_disable();
  266. /* get next suspend thread */
  267. thread = rt_list_entry(queue->suspended_push_list.next,
  268. struct rt_thread,
  269. tlist);
  270. /* set error code to RT_ERROR */
  271. thread->error = -RT_ERROR;
  272. /*
  273. * resume thread
  274. * In rt_thread_resume function, it will remove current thread from
  275. * suspend list
  276. */
  277. rt_thread_resume(thread);
  278. /* enable interrupt */
  279. rt_hw_interrupt_enable(temp);
  280. }
  281. rt_exit_critical();
  282. rt_schedule();
  283. }
  284. RTM_EXPORT(rt_data_queue_reset);