waitqueue.c 5.5 KB

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