2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-01 10:57:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-12 17:46:41 +00:00
|
|
|
#include <AK/Atomic.h>
|
2020-07-04 22:00:57 +00:00
|
|
|
#include <Kernel/SpinLock.h>
|
2019-12-22 11:23:44 +00:00
|
|
|
#include <Kernel/Thread.h>
|
2019-12-01 10:57:20 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
class WaitQueue : public Thread::BlockCondition {
|
2019-12-01 10:57:20 +00:00
|
|
|
public:
|
2021-01-24 05:30:10 +00:00
|
|
|
u32 wake_one();
|
2020-12-22 06:21:58 +00:00
|
|
|
u32 wake_n(u32 wake_count);
|
|
|
|
u32 wake_all();
|
2020-12-08 04:29:41 +00:00
|
|
|
|
2021-01-24 05:30:10 +00:00
|
|
|
void should_block(bool block)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_should_block = block;
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
template<class... Args>
|
|
|
|
Thread::BlockResult wait_on(const Thread::BlockTimeout& timeout, Args&&... args)
|
|
|
|
{
|
|
|
|
return Thread::current()->block<Thread::QueueBlocker>(timeout, *this, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-02-14 23:02:14 +00:00
|
|
|
template<class... Args>
|
|
|
|
void wait_forever(Args&&... args)
|
|
|
|
{
|
|
|
|
(void)Thread::current()->block<Thread::QueueBlocker>({}, *this, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
protected:
|
|
|
|
virtual bool should_add_blocker(Thread::Blocker& b, void* data) override;
|
2019-12-01 10:57:20 +00:00
|
|
|
|
|
|
|
private:
|
2020-07-05 21:46:51 +00:00
|
|
|
bool m_wake_requested { false };
|
2021-01-24 05:30:10 +00:00
|
|
|
bool m_should_block { true };
|
2019-12-01 10:57:20 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|