1
0

waitqueue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/06/26 Bernard Fix the wait queue issue when wakeup a soon
  9. * to blocked thread.
  10. * 2022-01-24 THEWON let rt_wqueue_wait return thread->error when using signal
  11. */
  12. #include <stdint.h>
  13. #include <rthw.h>
  14. #include <rtdevice.h>
  15. #include <rtservice.h>
  16. /**
  17. * @brief This function will insert a node to the wait queue.
  18. *
  19. * @param queue is a pointer to the wait queue.
  20. *
  21. * @param node is a pointer to the node to be inserted.
  22. */
  23. void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node)
  24. {
  25. rt_base_t level;
  26. level = rt_hw_interrupt_disable();
  27. rt_list_insert_before(&(queue->waiting_list), &(node->list));
  28. rt_hw_interrupt_enable(level);
  29. }
  30. /**
  31. * @brief This function will remove a node from the wait queue.
  32. *
  33. * @param node is a pointer to the node to be removed.
  34. */
  35. void rt_wqueue_remove(struct rt_wqueue_node *node)
  36. {
  37. rt_base_t level;
  38. level = rt_hw_interrupt_disable();
  39. rt_list_remove(&(node->list));
  40. rt_hw_interrupt_enable(level);
  41. }
  42. /**
  43. * @brief This function is the default wakeup function, but it doesn't do anything in actual.
  44. * It always return 0, user should define their own wakeup function.
  45. *
  46. * @param wait is a pointer to the wait queue.
  47. *
  48. * @param key is the wakeup condition.
  49. *
  50. * @return always return 0.
  51. */
  52. int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key)
  53. {
  54. return 0;
  55. }
  56. /**
  57. * @brief This function will wake up a pending thread on the specified waiting queue that meets the conditions.
  58. *
  59. * @param queue is a pointer to the wait queue.
  60. *
  61. * @param key is the wakeup conditions, but it is not effective now, because
  62. * default wakeup function always return 0.
  63. * If user wants to use it, user should define their own wakeup function.
  64. */
  65. void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key)
  66. {
  67. rt_base_t level;
  68. int need_schedule = 0;
  69. rt_list_t *queue_list;
  70. struct rt_list_node *node;
  71. struct rt_wqueue_node *entry;
  72. queue_list = &(queue->waiting_list);
  73. level = rt_hw_interrupt_disable();
  74. /* set wakeup flag in the queue */
  75. queue->flag = RT_WQ_FLAG_WAKEUP;
  76. if (!(rt_list_isempty(queue_list)))
  77. {
  78. for (node = queue_list->next; node != queue_list; node = node->next)
  79. {
  80. entry = rt_list_entry(node, struct rt_wqueue_node, list);
  81. if (entry->wakeup(entry, key) == 0)
  82. {
  83. rt_thread_resume(entry->polling_thread);
  84. need_schedule = 1;
  85. rt_wqueue_remove(entry);
  86. break;
  87. }
  88. }
  89. }
  90. rt_hw_interrupt_enable(level);
  91. if (need_schedule)
  92. rt_schedule();
  93. }
  94. /**
  95. * @brief This function will join a thread to the specified waiting queue, the thread will holds a wait or
  96. * timeout return on the specified wait queue.
  97. *
  98. * @param queue is a pointer to the wait queue.
  99. *
  100. * @param condition is parameters compatible with POSIX standard interface (currently meaningless, just pass in 0).
  101. *
  102. * @param msec is the timeout value, unit is millisecond.
  103. *
  104. * @return Return 0 if the thread is woken up.
  105. */
  106. static int _rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec, int suspend_flag)
  107. {
  108. int tick;
  109. rt_thread_t tid = rt_thread_self();
  110. rt_timer_t tmr = &(tid->thread_timer);
  111. struct rt_wqueue_node __wait;
  112. rt_base_t level;
  113. rt_err_t ret;
  114. /* current context checking */
  115. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  116. tick = rt_tick_from_millisecond(msec);
  117. if ((condition) || (tick == 0))
  118. return 0;
  119. __wait.polling_thread = rt_thread_self();
  120. __wait.key = 0;
  121. __wait.wakeup = __wqueue_default_wake;
  122. rt_list_init(&__wait.list);
  123. level = rt_hw_interrupt_disable();
  124. /* reset thread error */
  125. tid->error = RT_EOK;
  126. if (queue->flag == RT_WQ_FLAG_WAKEUP)
  127. {
  128. /* already wakeup */
  129. goto __exit_wakeup;
  130. }
  131. ret = rt_thread_suspend_with_flag(tid, suspend_flag);
  132. if (ret != RT_EOK)
  133. {
  134. rt_hw_interrupt_enable(level);
  135. /* suspend failed */
  136. return -RT_EINTR;
  137. }
  138. rt_wqueue_add(queue, &__wait);
  139. /* start timer */
  140. if (tick != RT_WAITING_FOREVER)
  141. {
  142. rt_timer_control(tmr,
  143. RT_TIMER_CTRL_SET_TIME,
  144. &tick);
  145. rt_timer_start(tmr);
  146. }
  147. rt_hw_interrupt_enable(level);
  148. rt_schedule();
  149. level = rt_hw_interrupt_disable();
  150. __exit_wakeup:
  151. queue->flag = RT_WQ_FLAG_CLEAN;
  152. rt_hw_interrupt_enable(level);
  153. rt_wqueue_remove(&__wait);
  154. return tid->error;
  155. }
  156. int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
  157. {
  158. return _rt_wqueue_wait(queue, condition, msec, RT_UNINTERRUPTIBLE);
  159. }
  160. int rt_wqueue_wait_killable(rt_wqueue_t *queue, int condition, int msec)
  161. {
  162. return _rt_wqueue_wait(queue, condition, msec, RT_KILLABLE);
  163. }
  164. int rt_wqueue_wait_interruptible(rt_wqueue_t *queue, int condition, int msec)
  165. {
  166. return _rt_wqueue_wait(queue, condition, msec, RT_INTERRUPTIBLE);
  167. }