2020-12-21 23:21:58 -07:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-12-21 23:21:58 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-21 23:21:58 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Atomic.h>
|
|
|
|
#include <AK/RefCounted.h>
|
2021-08-22 01:37:17 +02:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/VMObject.h>
|
2020-12-21 23:21:58 -07:00
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-22 15:30:14 +02:00
|
|
|
class FutexQueue final
|
2021-08-16 23:29:25 +02:00
|
|
|
: public RefCounted<FutexQueue>
|
2021-08-22 15:59:47 +02:00
|
|
|
, public Thread::BlockerSet {
|
2020-12-21 23:21:58 -07:00
|
|
|
public:
|
2021-08-16 23:29:25 +02:00
|
|
|
FutexQueue();
|
2020-12-21 23:21:58 -07:00
|
|
|
virtual ~FutexQueue();
|
|
|
|
|
2022-07-13 09:29:51 +03:00
|
|
|
ErrorOr<u32> wake_n_requeue(u32, Function<ErrorOr<FutexQueue*>()> const&, u32, bool&, bool&);
|
2022-04-01 20:58:27 +03:00
|
|
|
u32 wake_n(u32, Optional<u32> const&, bool&);
|
2020-12-21 23:21:58 -07:00
|
|
|
u32 wake_all(bool&);
|
|
|
|
|
|
|
|
template<class... Args>
|
2022-04-01 20:58:27 +03:00
|
|
|
Thread::BlockResult wait_on(Thread::BlockTimeout const& timeout, Args&&... args)
|
2020-12-21 23:21:58 -07:00
|
|
|
{
|
|
|
|
return Thread::current()->block<Thread::FutexBlocker>(timeout, *this, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-07-06 12:48:48 -06:00
|
|
|
bool queue_imminent_wait();
|
|
|
|
bool try_remove();
|
|
|
|
|
|
|
|
bool is_empty_and_no_imminent_waits()
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-06 12:48:48 -06:00
|
|
|
return is_empty_and_no_imminent_waits_locked();
|
|
|
|
}
|
|
|
|
bool is_empty_and_no_imminent_waits_locked();
|
|
|
|
|
2020-12-21 23:21:58 -07:00
|
|
|
protected:
|
2021-08-24 01:13:53 +02:00
|
|
|
virtual bool should_add_blocker(Thread::Blocker& b, void*) override;
|
2020-12-21 23:21:58 -07:00
|
|
|
|
|
|
|
private:
|
2021-07-06 12:48:48 -06:00
|
|
|
size_t m_imminent_waits { 1 }; // We only create this object if we're going to be waiting, so start out with 1
|
|
|
|
bool m_was_removed { false };
|
2020-12-21 23:21:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|