WaitQueue.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Thread.h>
  27. #include <Kernel/WaitQueue.h>
  28. //#define WAITQUEUE_DEBUG
  29. namespace Kernel {
  30. WaitQueue::WaitQueue()
  31. {
  32. }
  33. WaitQueue::~WaitQueue()
  34. {
  35. }
  36. bool WaitQueue::enqueue(Thread& thread)
  37. {
  38. ScopedSpinLock queue_lock(m_lock);
  39. if (m_wake_requested) {
  40. // wake_* was called when no threads were in the queue
  41. // we shouldn't wait at all
  42. m_wake_requested = false;
  43. #ifdef WAITQUEUE_DEBUG
  44. dbg() << "WaitQueue " << VirtualAddress(this) << ": enqueue: wake_all pending";
  45. #endif
  46. return false;
  47. }
  48. m_threads.append(thread);
  49. return true;
  50. }
  51. bool WaitQueue::dequeue(Thread& thread)
  52. {
  53. ScopedSpinLock queue_lock(m_lock);
  54. if (m_threads.contains(thread)) {
  55. m_threads.remove(thread);
  56. return true;
  57. }
  58. return false;
  59. }
  60. void WaitQueue::wake_one(Atomic<bool>* lock)
  61. {
  62. ScopedSpinLock queue_lock(m_lock);
  63. if (lock)
  64. *lock = false;
  65. if (m_threads.is_empty()) {
  66. // Save the fact that a wake was requested
  67. m_wake_requested = true;
  68. #ifdef WAITQUEUE_DEBUG
  69. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one: nobody to wake, mark as pending";
  70. #endif
  71. return;
  72. }
  73. #ifdef WAITQUEUE_DEBUG
  74. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one:";
  75. #endif
  76. auto* thread = m_threads.take_first();
  77. #ifdef WAITQUEUE_DEBUG
  78. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one: wake thread " << *thread;
  79. #endif
  80. thread->wake_from_queue();
  81. m_wake_requested = false;
  82. Scheduler::yield();
  83. }
  84. void WaitQueue::wake_n(u32 wake_count)
  85. {
  86. ScopedSpinLock queue_lock(m_lock);
  87. if (m_threads.is_empty()) {
  88. // Save the fact that a wake was requested
  89. m_wake_requested = true;
  90. #ifdef WAITQUEUE_DEBUG
  91. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: nobody to wake, mark as pending";
  92. #endif
  93. return;
  94. }
  95. #ifdef WAITQUEUE_DEBUG
  96. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: " << wake_count;
  97. #endif
  98. for (u32 i = 0; i < wake_count; ++i) {
  99. Thread* thread = m_threads.take_first();
  100. if (!thread)
  101. break;
  102. #ifdef WAITQUEUE_DEBUG
  103. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: wake thread " << *thread;
  104. #endif
  105. thread->wake_from_queue();
  106. }
  107. m_wake_requested = false;
  108. Scheduler::yield();
  109. }
  110. void WaitQueue::wake_all()
  111. {
  112. ScopedSpinLock queue_lock(m_lock);
  113. if (m_threads.is_empty()) {
  114. // Save the fact that a wake was requested
  115. m_wake_requested = true;
  116. #ifdef WAITQUEUE_DEBUG
  117. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: nobody to wake, mark as pending";
  118. #endif
  119. return;
  120. }
  121. #ifdef WAITQUEUE_DEBUG
  122. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: ";
  123. #endif
  124. while (!m_threads.is_empty()) {
  125. Thread* thread = m_threads.take_first();
  126. #ifdef WAITQUEUE_DEBUG
  127. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: wake thread " << *thread;
  128. #endif
  129. thread->wake_from_queue();
  130. }
  131. m_wake_requested = false;
  132. Scheduler::yield();
  133. }
  134. void WaitQueue::clear()
  135. {
  136. ScopedSpinLock queue_lock(m_lock);
  137. #ifdef WAITQUEUE_DEBUG
  138. dbg() << "WaitQueue " << VirtualAddress(this) << ": clear";
  139. #endif
  140. m_threads.clear();
  141. m_wake_requested = false;
  142. }
  143. }