waitqueue.c 5.1 KB

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