FutexQueue.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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
  14. : public RefCounted<FutexQueue>
  15. , public Thread::BlockCondition {
  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. void did_remove();
  29. bool try_remove();
  30. bool is_empty_and_no_imminent_waits()
  31. {
  32. SpinlockLocker lock(m_lock);
  33. return is_empty_and_no_imminent_waits_locked();
  34. }
  35. bool is_empty_and_no_imminent_waits_locked();
  36. protected:
  37. virtual bool should_add_blocker(Thread::Blocker& b, void* data) override;
  38. private:
  39. size_t m_imminent_waits { 1 }; // We only create this object if we're going to be waiting, so start out with 1
  40. bool m_was_removed { false };
  41. };
  42. }