FutexQueue.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. bool FutexQueue::should_add_blocker(Thread::Blocker& b, void* data)
  11. {
  12. VERIFY(data != nullptr); // Thread that is requesting to be blocked
  13. VERIFY(m_lock.is_locked());
  14. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  15. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: should block thread {}", this, *static_cast<Thread*>(data));
  16. return true;
  17. }
  18. u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function<FutexQueue*()>& get_target_queue, u32 requeue_count, bool& is_empty, bool& is_empty_target)
  19. {
  20. is_empty_target = false;
  21. ScopedSpinLock lock(m_lock);
  22. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue({}, {})", this, wake_count, requeue_count);
  23. u32 did_wake = 0, did_requeue = 0;
  24. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  25. VERIFY(data);
  26. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  27. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  28. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue unblocking {}", this, *static_cast<Thread*>(data));
  29. VERIFY(did_wake < wake_count);
  30. if (blocker.unblock()) {
  31. if (++did_wake >= wake_count)
  32. stop_iterating = true;
  33. return true;
  34. }
  35. return false;
  36. });
  37. is_empty = is_empty_locked();
  38. if (requeue_count > 0) {
  39. auto blockers_to_requeue = do_take_blockers(requeue_count);
  40. if (!blockers_to_requeue.is_empty()) {
  41. if (auto* target_futex_queue = get_target_queue()) {
  42. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue requeueing {} blockers to {}", this, blockers_to_requeue.size(), target_futex_queue);
  43. // While still holding m_lock, notify each blocker
  44. for (auto& info : blockers_to_requeue) {
  45. VERIFY(info.blocker->blocker_type() == Thread::Blocker::Type::Futex);
  46. auto& blocker = *static_cast<Thread::FutexBlocker*>(info.blocker);
  47. blocker.begin_requeue();
  48. }
  49. lock.unlock();
  50. did_requeue = blockers_to_requeue.size();
  51. ScopedSpinLock target_lock(target_futex_queue->m_lock);
  52. // Now that we have the lock of the target, append the blockers
  53. // and notify them that they completed the move
  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.finish_requeue(*target_futex_queue);
  58. }
  59. target_futex_queue->do_append_blockers(move(blockers_to_requeue));
  60. is_empty_target = target_futex_queue->is_empty_locked();
  61. } else {
  62. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n_requeue could not get target queue to requeue {} blockers", this, blockers_to_requeue.size());
  63. do_append_blockers(move(blockers_to_requeue));
  64. }
  65. }
  66. }
  67. return did_wake + did_requeue;
  68. }
  69. u32 FutexQueue::wake_n(u32 wake_count, const Optional<u32>& bitset, bool& is_empty)
  70. {
  71. if (wake_count == 0)
  72. return 0; // should we assert instead?
  73. ScopedSpinLock lock(m_lock);
  74. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n({})", this, wake_count);
  75. u32 did_wake = 0;
  76. do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
  77. VERIFY(data);
  78. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  79. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  80. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_n unblocking {}", this, *static_cast<Thread*>(data));
  81. VERIFY(did_wake < wake_count);
  82. if (bitset.has_value() ? blocker.unblock_bitset(bitset.value()) : blocker.unblock()) {
  83. if (++did_wake >= wake_count)
  84. stop_iterating = true;
  85. return true;
  86. }
  87. return false;
  88. });
  89. is_empty = is_empty_locked();
  90. return did_wake;
  91. }
  92. u32 FutexQueue::wake_all(bool& is_empty)
  93. {
  94. ScopedSpinLock lock(m_lock);
  95. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_all", this);
  96. u32 did_wake = 0;
  97. do_unblock([&](Thread::Blocker& b, void* data, bool&) {
  98. VERIFY(data);
  99. VERIFY(b.blocker_type() == Thread::Blocker::Type::Futex);
  100. auto& blocker = static_cast<Thread::FutexBlocker&>(b);
  101. dbgln_if(FUTEXQUEUE_DEBUG, "FutexQueue @ {}: wake_all unblocking {}", this, *static_cast<Thread*>(data));
  102. if (blocker.unblock(true)) {
  103. did_wake++;
  104. return true;
  105. }
  106. return false;
  107. });
  108. is_empty = is_empty_locked();
  109. return did_wake;
  110. }
  111. }