WaitQueue.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/Debug.h>
  27. #include <Kernel/Thread.h>
  28. #include <Kernel/WaitQueue.h>
  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 || !m_should_block) {
  36. m_wake_requested = false;
  37. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: do not block thread {}, {}", this, data, m_should_block ? "wake was pending" : "not blocking");
  38. return false;
  39. }
  40. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: should block thread {}", this, data);
  41. return true;
  42. }
  43. u32 WaitQueue::wake_one()
  44. {
  45. u32 did_wake = 0;
  46. ScopedSpinLock lock(m_lock);
  47. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_one", this);
  48. bool did_unblock_one = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  49. ASSERT(data);
  50. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  51. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  52. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_one unblocking {}", this, data);
  53. if (blocker.unblock()) {
  54. stop_iterating = true;
  55. did_wake = 1;
  56. return true;
  57. }
  58. return false;
  59. });
  60. m_wake_requested = !did_unblock_one;
  61. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_one woke {} threads", this, did_wake);
  62. return did_wake;
  63. }
  64. u32 WaitQueue::wake_n(u32 wake_count)
  65. {
  66. if (wake_count == 0)
  67. return 0; // should we assert instead?
  68. ScopedSpinLock lock(m_lock);
  69. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_n({})", this, wake_count);
  70. u32 did_wake = 0;
  71. bool did_unblock_some = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  72. ASSERT(data);
  73. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  74. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  75. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_n unblocking {}", this, data);
  76. ASSERT(did_wake < wake_count);
  77. if (blocker.unblock()) {
  78. if (++did_wake >= wake_count)
  79. stop_iterating = true;
  80. return true;
  81. }
  82. return false;
  83. });
  84. m_wake_requested = !did_unblock_some;
  85. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_n({}) woke {} threads", this, wake_count, did_wake);
  86. return did_wake;
  87. }
  88. u32 WaitQueue::wake_all()
  89. {
  90. ScopedSpinLock lock(m_lock);
  91. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_all", this);
  92. u32 did_wake = 0;
  93. bool did_unblock_any = do_unblock([&](Thread::Blocker& b, void* data, bool&) {
  94. ASSERT(data);
  95. ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
  96. auto& blocker = static_cast<Thread::QueueBlocker&>(b);
  97. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_all unblocking {}", this, data);
  98. if (blocker.unblock()) {
  99. did_wake++;
  100. return true;
  101. }
  102. return false;
  103. });
  104. m_wake_requested = !did_unblock_any;
  105. dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_all woke {} threads", this, did_wake);
  106. return did_wake;
  107. }
  108. }