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