WaitQueue.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. void WaitQueue::wake_one(Atomic<bool>* lock)
  52. {
  53. ScopedSpinLock queue_lock(m_lock);
  54. if (lock)
  55. *lock = false;
  56. if (m_threads.is_empty()) {
  57. // Save the fact that a wake was requested
  58. m_wake_requested = true;
  59. #ifdef WAITQUEUE_DEBUG
  60. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one: nobody to wake, mark as pending";
  61. #endif
  62. return;
  63. }
  64. #ifdef WAITQUEUE_DEBUG
  65. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one:";
  66. #endif
  67. auto* thread = m_threads.take_first();
  68. #ifdef WAITQUEUE_DEBUG
  69. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_one: wake thread " << *thread;
  70. #endif
  71. thread->wake_from_queue();
  72. m_wake_requested = false;
  73. Scheduler::yield();
  74. }
  75. void WaitQueue::wake_n(u32 wake_count)
  76. {
  77. ScopedSpinLock queue_lock(m_lock);
  78. if (m_threads.is_empty()) {
  79. // Save the fact that a wake was requested
  80. m_wake_requested = true;
  81. #ifdef WAITQUEUE_DEBUG
  82. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: nobody to wake, mark as pending";
  83. #endif
  84. return;
  85. }
  86. #ifdef WAITQUEUE_DEBUG
  87. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: " << wake_count;
  88. #endif
  89. for (u32 i = 0; i < wake_count; ++i) {
  90. Thread* thread = m_threads.take_first();
  91. if (!thread)
  92. break;
  93. #ifdef WAITQUEUE_DEBUG
  94. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_n: wake thread " << *thread;
  95. #endif
  96. thread->wake_from_queue();
  97. }
  98. m_wake_requested = false;
  99. Scheduler::yield();
  100. }
  101. void WaitQueue::wake_all()
  102. {
  103. ScopedSpinLock queue_lock(m_lock);
  104. if (m_threads.is_empty()) {
  105. // Save the fact that a wake was requested
  106. m_wake_requested = true;
  107. #ifdef WAITQUEUE_DEBUG
  108. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: nobody to wake, mark as pending";
  109. #endif
  110. return;
  111. }
  112. #ifdef WAITQUEUE_DEBUG
  113. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: ";
  114. #endif
  115. while (!m_threads.is_empty()) {
  116. Thread* thread = m_threads.take_first();
  117. #ifdef WAITQUEUE_DEBUG
  118. dbg() << "WaitQueue " << VirtualAddress(this) << ": wake_all: wake thread " << *thread;
  119. #endif
  120. thread->wake_from_queue();
  121. }
  122. m_wake_requested = false;
  123. Scheduler::yield();
  124. }
  125. void WaitQueue::clear()
  126. {
  127. ScopedSpinLock queue_lock(m_lock);
  128. #ifdef WAITQUEUE_DEBUG
  129. dbg() << "WaitQueue " << VirtualAddress(this) << ": clear";
  130. #endif
  131. m_threads.clear();
  132. m_wake_requested = false;
  133. }
  134. }