FutexQueue.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/FutexQueue.h>
  28. #include <Kernel/Thread.h>
  29. namespace Kernel {
  30. bool FutexQueue::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::Futex);
  35. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: should block thread {}", this, *static_cast<Thread*>(data));
  36. return true;
  37. }
  38. u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function<FutexQueue*()>& get_target_queue, u32 requeue_count, bool& is_empty, bool& is_empty_target)
  39. {
  40. is_empty_target = false;
  41. ScopedSpinLock lock(m_lock);
  42. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n_requeue({}, {})", this, wake_count, requeue_count);
  43. u32 did_wake = 0, did_requeue = 0;
  44. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  45. ASSERT(data);
  46. ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex);
  47. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  48. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n_requeue unblocking {}", this, *static_cast<Thread*>(data));
  49. ASSERT(did_wake < wake_count);
  50. if (blocker.unblock()) {
  51. if (++did_wake >= wake_count)
  52. stop_iterating = true;
  53. return true;
  54. }
  55. return false;
  56. });
  57. is_empty = is_empty_locked();
  58. if (requeue_count > 0) {
  59. auto blockers_to_requeue = do_take_blockers(requeue_count);
  60. if (!blockers_to_requeue.is_empty()) {
  61. if (auto* target_futex_queue = get_target_queue()) {
  62. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n_requeue requeueing {} blockers to {}", this, blockers_to_requeue.size(), target_futex_queue);
  63. // While still holding m_lock, notify each blocker
  64. for (auto& info : blockers_to_requeue) {
  65. ASSERT(info.blocker->blocker_type() == Thread::Blocker::Type::Futex);
  66. auto& blocker = *static_cast<Thread::FutexBlocker*>(info.blocker);
  67. blocker.begin_requeue();
  68. }
  69. lock.unlock();
  70. did_requeue = blockers_to_requeue.size();
  71. ScopedSpinLock target_lock(target_futex_queue->m_lock);
  72. // Now that we have the lock of the target, append the blockers
  73. // and notify them that they completed the move
  74. for (auto& info : blockers_to_requeue) {
  75. ASSERT(info.blocker->blocker_type() == Thread::Blocker::Type::Futex);
  76. auto& blocker = *static_cast<Thread::FutexBlocker*>(info.blocker);
  77. blocker.finish_requeue(*target_futex_queue);
  78. }
  79. target_futex_queue->do_append_blockers(move(blockers_to_requeue));
  80. is_empty_target = target_futex_queue->is_empty_locked();
  81. } else {
  82. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n_requeue could not get target queue to requeue {} blockers", this, blockers_to_requeue.size());
  83. do_append_blockers(move(blockers_to_requeue));
  84. }
  85. }
  86. }
  87. return did_wake + did_requeue;
  88. }
  89. u32 FutexQueue::wake_n(u32 wake_count, const Optional<u32>& bitset, bool& is_empty)
  90. {
  91. if (wake_count == 0)
  92. return 0; // should we assert instead?
  93. ScopedSpinLock lock(m_lock);
  94. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n({})", this, wake_count);
  95. u32 did_wake = 0;
  96. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  97. ASSERT(data);
  98. ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex);
  99. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  100. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_n unblocking {}", this, *static_cast<Thread*>(data));
  101. ASSERT(did_wake < wake_count);
  102. if (bitset.has_value() ? blocker.unblock_bitset(bitset.value()) : blocker.unblock()) {
  103. if (++did_wake >= wake_count)
  104. stop_iterating = true;
  105. return true;
  106. }
  107. return false;
  108. });
  109. is_empty = is_empty_locked();
  110. return did_wake;
  111. }
  112. u32 FutexQueue::wake_all(bool& is_empty)
  113. {
  114. ScopedSpinLock lock(m_lock);
  115. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_all", this);
  116. u32 did_wake = 0;
  117. do_unblock([&](Thread::Blocker& b, void* data, bool&) {
  118. ASSERT(data);
  119. ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex);
  120. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  121. dbgln<FUTEXQUEUE_DEBUG>("FutexQueue @ {}: wake_all unblocking {}", this, *static_cast<Thread*>(data));
  122. if (blocker.unblock(true)) {
  123. did_wake++;
  124. return true;
  125. }
  126. return false;
  127. });
  128. is_empty = is_empty_locked();
  129. return did_wake;
  130. }
  131. }