FutexQueue.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Atomic.h>
  8. #include <AK/RefCounted.h>
  9. #include <Kernel/Locking/Spinlock.h>
  10. #include <Kernel/Memory/VMObject.h>
  11. #include <Kernel/Thread.h>
  12. namespace Kernel {
  13. class FutexQueue final
  14. : public RefCounted<FutexQueue>
  15. , public Thread::BlockerSet {
  16. public:
  17. FutexQueue();
  18. virtual ~FutexQueue();
  19. u32 wake_n_requeue(u32, const Function<FutexQueue*()>&, u32, bool&, bool&);
  20. u32 wake_n(u32, const Optional<u32>&, bool&);
  21. u32 wake_all(bool&);
  22. template<class... Args>
  23. Thread::BlockResult wait_on(const Thread::BlockTimeout& timeout, Args&&... args)
  24. {
  25. return Thread::current()->block<Thread::FutexBlocker>(timeout, *this, forward<Args>(args)...);
  26. }
  27. bool queue_imminent_wait();
  28. bool try_remove();
  29. bool is_empty_and_no_imminent_waits()
  30. {
  31. SpinlockLocker lock(m_lock);
  32. return is_empty_and_no_imminent_waits_locked();
  33. }
  34. bool is_empty_and_no_imminent_waits_locked();
  35. protected:
  36. virtual bool should_add_blocker(Thread::Blocker& b, void*) override;
  37. private:
  38. size_t m_imminent_waits { 1 }; // We only create this object if we're going to be waiting, so start out with 1
  39. bool m_was_removed { false };
  40. };
  41. }