FutexQueue.cpp 5.7 KB

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