WaitQueue.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  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. bool WaitQueue::should_add_blocker(Thread::Blocker& b, void* data)
  31. {
  32. ASSERT(data != nullptr); // Thread that is requesting to be blocked
  33. ASSERT(m_lock.is_locked());
  34. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  35. if (m_wake_requested) {
  36. m_wake_requested = false;
  37. #ifdef WAITQUEUE_DEBUG
  38. dbg() << "WaitQueue @ " << this << ": do not block thread " << *static_cast<Thread*>(data) << ", wake was pending";
  39. #endif
  40. return false;
  41. }
  42. #ifdef WAITQUEUE_DEBUG
  43. dbg() << "WaitQueue @ " << this << ": should block thread " << *static_cast<Thread*>(data);
  44. #endif
  45. return true;
  46. }
  47. void WaitQueue::wake_one()
  48. {
  49. ScopedSpinLock lock(m_lock);
  50. #ifdef WAITQUEUE_DEBUG
  51. dbg() << "WaitQueue @ " << this << ": wake_one";
  52. #endif
  53. bool did_unblock_one = do_unblock_some([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  54. ASSERT(data);
  55. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  56. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  57. #ifdef WAITQUEUE_DEBUG
  58. dbg() << "WaitQueue @ " << this << ": wake_one unblocking " << *static_cast<Thread*>(data);
  59. #endif
  60. if (blocker.unblock()) {
  61. stop_iterating = true;
  62. return true;
  63. }
  64. return false;
  65. });
  66. m_wake_requested = !did_unblock_one;
  67. }
  68. void WaitQueue::wake_n(u32 wake_count)
  69. {
  70. if (wake_count == 0)
  71. return; // should we assert instaed?
  72. ScopedSpinLock lock(m_lock);
  73. #ifdef WAITQUEUE_DEBUG
  74. dbg() << "WaitQueue @ " << this << ": wake_n(" << wake_count << ")";
  75. #endif
  76. bool did_unblock_some = do_unblock_some([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  77. ASSERT(data);
  78. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  79. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  80. #ifdef WAITQUEUE_DEBUG
  81. dbg() << "WaitQueue @ " << this << ": wake_n unblocking " << *static_cast<Thread*>(data);
  82. #endif
  83. ASSERT(wake_count > 0);
  84. if (blocker.unblock()) {
  85. if (--wake_count == 0)
  86. stop_iterating = true;
  87. return true;
  88. }
  89. return false;
  90. });
  91. m_wake_requested = !did_unblock_some;
  92. }
  93. void WaitQueue::wake_all()
  94. {
  95. ScopedSpinLock lock(m_lock);
  96. #ifdef WAITQUEUE_DEBUG
  97. dbg() << "WaitQueue @ " << this << ": wake_all";
  98. #endif
  99. bool did_unblock_any = do_unblock_all([&](Thread::Blocker& b, void* data) {
  100. ASSERT(data);
  101. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  102. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  103. #ifdef WAITQUEUE_DEBUG
  104. dbg() << "WaitQueue @ " << this << ": wake_all unblocking " << *static_cast<Thread*>(data);
  105. #endif
  106. return blocker.unblock();
  107. });
  108. m_wake_requested = !did_unblock_any;
  109. }
  110. }