waitqueue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
  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. /* 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. rt_wqueue_add(queue, &__wait);
  131. rt_thread_suspend(tid);
  132. /* start timer */
  133. if (tick != RT_WAITING_FOREVER)
  134. {
  135. rt_timer_control(tmr,
  136. RT_TIMER_CTRL_SET_TIME,
  137. &tick);
  138. rt_timer_start(tmr);
  139. }
  140. rt_hw_interrupt_enable(level);
  141. rt_schedule();
  142. level = rt_hw_interrupt_disable();
  143. __exit_wakeup:
  144. queue->flag = RT_WQ_FLAG_CLEAN;
  145. rt_hw_interrupt_enable(level);
  146. rt_wqueue_remove(&__wait);
  147. return tid->error;
  148. }