FutexQueue.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Debug.h>
  7. #include <Kernel/FutexQueue.h>
  8. #include <Kernel/Thread.h>
  9. namespace Kernel {
  10. FutexQueue::FutexQueue()
  11. {
  12. }
  13. FutexQueue::~FutexQueue()
  14. {
  15. }
  16. bool FutexQueue::should_add_blocker(Thread::Blocker& b, void* data)
  17. {
  18. VERIFY(data != nullptr); // Thread that is requesting to be blocked
  19. VERIFY(m_lock.is_locked());
  20. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  21. VERIFY(m_imminent_waits > 0);
  22. m_imminent_waits--;
  23. if (m_was_removed) {
  24. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: should not block thread {}: was removed", this, *static_cast<Thread*>(data));
  25. return false;
  26. }
  27. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: should block thread {}", this, *static_cast<Thread*>(data));
  28. return true;
  29. }
  30. u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function<FutexQueue*()>& get_target_queue, u32 requeue_count, bool& is_empty, bool& is_empty_target)
  31. {
  32. is_empty_target = false;
  33. SpinlockLocker lock(m_lock);
  34. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue({}, {})", this, wake_count, requeue_count);
  35. u32 did_wake = 0, did_requeue = 0;
  36. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  37. VERIFY(data);
  38. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  39. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  40. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue unblocking {}", this, *static_cast<Thread*>(data));
  41. VERIFY(did_wake < wake_count);
  42. if (blocker.unblock()) {
  43. if (++did_wake >= wake_count)
  44. stop_iterating = true;
  45. return true;
  46. }
  47. return false;
  48. });
  49. is_empty = is_empty_and_no_imminent_waits_locked();
  50. if (requeue_count > 0) {
  51. auto blockers_to_requeue = do_take_blockers(requeue_count);
  52. if (!blockers_to_requeue.is_empty()) {
  53. if (auto* target_futex_queue = get_target_queue()) {
  54. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue requeueing {} blockers to {}", this, blockers_to_requeue.size(), target_futex_queue);
  55. // While still holding m_lock, notify each blocker
  56. for (auto& info : blockers_to_requeue) {
  57. VERIFY(info.blocker->blocker_type() == Thread::Blocker::Type::Futex);
  58. auto& blocker = *static_cast<Thread::FutexBlocker*>(info.blocker);
  59. blocker.begin_requeue();
  60. }
  61. lock.unlock();
  62. did_requeue = blockers_to_requeue.size();
  63. SpinlockLocker target_lock(target_futex_queue->m_lock);
  64. // Now that we have the lock of the target, append the blockers
  65. // and notify them that they completed the move
  66. for (auto& info : blockers_to_requeue) {
  67. VERIFY(info.blocker->blocker_type() == Thread::Blocker::Type::Futex);
  68. auto& blocker = *static_cast<Thread::FutexBlocker*>(info.blocker);
  69. blocker.finish_requeue(*target_futex_queue);
  70. }
  71. target_futex_queue->do_append_blockers(move(blockers_to_requeue));
  72. is_empty_target = target_futex_queue->is_empty_and_no_imminent_waits_locked();
  73. } else {
  74. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue could not get target queue to requeue {} blockers", this, blockers_to_requeue.size());
  75. do_append_blockers(move(blockers_to_requeue));
  76. }
  77. }
  78. }
  79. return did_wake + did_requeue;
  80. }
  81. u32 FutexQueue::wake_n(u32 wake_count, const Optional<u32>& bitset, bool& is_empty)
  82. {
  83. if (wake_count == 0) {
  84. is_empty = false;
  85. return 0; // should we assert instead?
  86. }
  87. SpinlockLocker lock(m_lock);
  88. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n({})", this, wake_count);
  89. u32 did_wake = 0;
  90. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  91. VERIFY(data);
  92. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  93. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  94. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n unblocking {}", this, *static_cast<Thread*>(data));
  95. VERIFY(did_wake < wake_count);
  96. if (bitset.has_value() ? blocker.unblock_bitset(bitset.value()) : blocker.unblock()) {
  97. if (++did_wake >= wake_count)
  98. stop_iterating = true;
  99. return true;
  100. }
  101. return false;
  102. });
  103. is_empty = is_empty_and_no_imminent_waits_locked();
  104. return did_wake;
  105. }
  106. u32 FutexQueue::wake_all(bool& is_empty)
  107. {
  108. SpinlockLocker lock(m_lock);
  109. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_all", this);
  110. u32 did_wake = 0;
  111. do_unblock([&](Thread::Blocker& b, void* data, bool&) {
  112. VERIFY(data);
  113. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  114. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  115. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_all unblocking {}", this, *static_cast<Thread*>(data));
  116. if (blocker.unblock(true)) {
  117. did_wake++;
  118. return true;
  119. }
  120. return false;
  121. });
  122. is_empty = is_empty_and_no_imminent_waits_locked();
  123. return did_wake;
  124. }
  125. bool FutexQueue::is_empty_and_no_imminent_waits_locked()
  126. {
  127. return m_imminent_waits == 0 && is_empty_locked();
  128. }
  129. bool FutexQueue::queue_imminent_wait()
  130. {
  131. SpinlockLocker lock(m_lock);
  132. if (m_was_removed)
  133. return false;
  134. m_imminent_waits++;
  135. return true;
  136. }
  137. bool FutexQueue::try_remove()
  138. {
  139. SpinlockLocker lock(m_lock);
  140. if (m_was_removed)
  141. return false;
  142. if (!is_empty_and_no_imminent_waits_locked())
  143. return false;
  144. m_was_removed = true;
  145. return true;
  146. }
  147. void FutexQueue::did_remove()
  148. {
  149. SpinlockLocker lock(m_lock);
  150. VERIFY(m_was_removed);
  151. VERIFY(is_empty_and_no_imminent_waits_locked());
  152. }
  153. }