waitqueue.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. */
  11. #include <stdint.h>
  12. #include <rthw.h>
  13. #include <rtdevice.h>
  14. #include <rtservice.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 queue is a pointer to 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. level = rt_hw_interrupt_disable();
  40. rt_list_remove(&(node->list));
  41. rt_hw_interrupt_enable(level);
  42. }
  43. /**
  44. * @brief This function is the default wakeup function, but it doesn't do anything in actual.
  45. * It always return 0, user should define their own wakeup function.
  46. *
  47. * @param queue is a pointer to the wait queue.
  48. *
  49. * @param key is the wakeup condition.
  50. *
  51. * @return always return 0.
  52. */
  53. int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key)
  54. {
  55. return 0;
  56. }
  57. /**
  58. * @brief This function will wake up a pending thread on the specified waiting queue that meets the conditions.
  59. *
  60. * @param queue is a pointer to the wait queue.
  61. *
  62. * @param key is the wakeup conditions, but it is not effective now, because
  63. * default wakeup function always return 0.
  64. * If user wants to use it, user should define their own wakeup function.
  65. */
  66. void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key)
  67. {
  68. rt_base_t level;
  69. register int need_schedule = 0;
  70. rt_list_t *queue_list;
  71. struct rt_list_node *node;
  72. struct rt_wqueue_node *entry;
  73. queue_list = &(queue->waiting_list);
  74. level = rt_hw_interrupt_disable();
  75. /* set wakeup flag in the queue */
  76. queue->flag = RT_WQ_FLAG_WAKEUP;
  77. if (!(rt_list_isempty(queue_list)))
  78. {
  79. for (node = queue_list->next; node != queue_list; node = node->next)
  80. {
  81. entry = rt_list_entry(node, struct rt_wqueue_node, list);
  82. if (entry->wakeup(entry, key) == 0)
  83. {
  84. rt_thread_resume(entry->polling_thread);
  85. need_schedule = 1;
  86. rt_wqueue_remove(entry);
  87. break;
  88. }
  89. }
  90. }
  91. rt_hw_interrupt_enable(level);
  92. if (need_schedule)
  93. rt_schedule();
  94. }
  95. /**
  96. * @brief This function will join a thread to the specified waiting queue, the thread will holds a wait or
  97. * timeout return on the specified wait queue.
  98. *
  99. * @param queue is a pointer to the wait queue.
  100. *
  101. * @param condition is parameters compatible with POSIX standard interface (currently meaningless, just pass in 0).
  102. *
  103. * @param msec is the timeout value, unit is millisecond.
  104. *
  105. * @return Return 0 if the thread is woken up.
  106. */
  107. int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
  108. {
  109. int tick;
  110. rt_thread_t tid = rt_thread_self();
  111. rt_timer_t tmr = &(tid->thread_timer);
  112. struct rt_wqueue_node __wait;
  113. rt_base_t level;
  114. /* current context checking */
  115. RT_DEBUG_NOT_IN_INTERRUPT;
  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. if (queue->flag == RT_WQ_FLAG_WAKEUP)
  125. {
  126. /* already wakeup */
  127. goto __exit_wakeup;
  128. }
  129. rt_wqueue_add(queue, &__wait);
  130. rt_thread_suspend(tid);
  131. /* start timer */
  132. if (tick != RT_WAITING_FOREVER)
  133. {
  134. rt_timer_control(tmr,
  135. RT_TIMER_CTRL_SET_TIME,
  136. &tick);
  137. rt_timer_start(tmr);
  138. }
  139. rt_hw_interrupt_enable(level);
  140. rt_schedule();
  141. level = rt_hw_interrupt_disable();
  142. __exit_wakeup:
  143. queue->flag = RT_WQ_FLAG_CLEAN;
  144. rt_hw_interrupt_enable(level);
  145. rt_wqueue_remove(&__wait);
  146. return 0;
  147. }