2017-01-24 20:19:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-12 12:01:29 +00:00
|
|
|
#include "util/types.hpp"
|
2019-07-26 22:34:10 +00:00
|
|
|
#include "util/atomic.hpp"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
// Lightweight condition variable
|
|
|
|
class cond_variable
|
|
|
|
{
|
|
|
|
// Internal waiter counter
|
|
|
|
atomic_t<u32> m_value{0};
|
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
c_waiter_mask = 0x1fff,
|
|
|
|
c_signal_mask = 0xffffffff & ~c_waiter_mask,
|
|
|
|
};
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
protected:
|
2019-09-09 08:09:30 +00:00
|
|
|
// Increment waiter count
|
|
|
|
u32 add_waiter() noexcept
|
|
|
|
{
|
|
|
|
return m_value.atomic_op([](u32& value) -> u32
|
|
|
|
{
|
|
|
|
if ((value & c_signal_mask) == c_signal_mask || (value & c_waiter_mask) == c_waiter_mask)
|
|
|
|
{
|
|
|
|
// Signal or waiter overflow, return immediately
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:40:50 +00:00
|
|
|
// Add waiter (c_waiter_mask)
|
|
|
|
value += 1;
|
2019-09-09 08:09:30 +00:00
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
// Internal waiting function
|
2019-09-09 08:09:30 +00:00
|
|
|
void imp_wait(u32 _old, u64 _timeout) noexcept;
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
// Try to notify up to _count threads
|
|
|
|
void imp_wake(u32 _count) noexcept;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr cond_variable() = default;
|
|
|
|
|
|
|
|
// Intrusive wait algorithm for lockable objects
|
2016-09-06 22:38:52 +00:00
|
|
|
template <typename T>
|
2019-09-09 08:09:30 +00:00
|
|
|
void wait(T& object, u64 usec_timeout = -1) noexcept
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
const u32 _old = add_waiter();
|
|
|
|
|
|
|
|
if (!_old)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:38:52 +00:00
|
|
|
object.unlock();
|
2019-09-09 08:09:30 +00:00
|
|
|
imp_wait(_old, usec_timeout);
|
2016-09-06 22:38:52 +00:00
|
|
|
object.lock();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 14:50:22 +00:00
|
|
|
// Unlock all specified objects but don't lock them again
|
|
|
|
template <typename... Locks>
|
2019-09-09 08:09:30 +00:00
|
|
|
void wait_unlock(u64 usec_timeout, Locks&&... locks)
|
2018-10-02 14:50:22 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
const u32 _old = add_waiter();
|
2018-10-02 14:50:22 +00:00
|
|
|
(..., std::forward<Locks>(locks).unlock());
|
2019-09-09 08:09:30 +00:00
|
|
|
|
|
|
|
if (!_old)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp_wait(_old, usec_timeout);
|
2018-10-02 14:50:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
// Wake one thread
|
|
|
|
void notify_one() noexcept
|
|
|
|
{
|
|
|
|
if (m_value)
|
|
|
|
{
|
|
|
|
imp_wake(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wake all threads
|
|
|
|
void notify_all() noexcept
|
|
|
|
{
|
|
|
|
if (m_value)
|
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
imp_wake(-1);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 19:20:46 +00:00
|
|
|
|
2021-05-22 07:35:15 +00:00
|
|
|
static constexpr u64 max_timeout = u64{umax} / 1000;
|
2017-01-24 20:19:52 +00:00
|
|
|
};
|