2017-01-24 21:19:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
2019-07-27 00:34:10 +02:00
|
|
|
#include "util/atomic.hpp"
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
// Lightweight condition variable
|
|
|
|
class cond_variable
|
|
|
|
{
|
|
|
|
// Internal waiter counter
|
|
|
|
atomic_t<u32> m_value{0};
|
|
|
|
|
2019-09-09 10:09:30 +02:00
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
c_waiter_mask = 0x1fff,
|
|
|
|
c_signal_mask = 0xffffffff & ~c_waiter_mask,
|
|
|
|
};
|
|
|
|
|
2017-01-24 21:19:52 +01:00
|
|
|
protected:
|
2019-09-09 10:09:30 +02: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 14:40:50 +02:00
|
|
|
// Add waiter (c_waiter_mask)
|
|
|
|
value += 1;
|
2019-09-09 10:09:30 +02:00
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:19:52 +01:00
|
|
|
// Internal waiting function
|
2019-09-09 10:09:30 +02:00
|
|
|
void imp_wait(u32 _old, u64 _timeout) noexcept;
|
2017-01-24 21:19:52 +01: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-07 00:38:52 +02:00
|
|
|
template <typename T>
|
2019-09-09 10:09:30 +02:00
|
|
|
void wait(T& object, u64 usec_timeout = -1) noexcept
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
2019-09-09 10:09:30 +02:00
|
|
|
const u32 _old = add_waiter();
|
|
|
|
|
|
|
|
if (!_old)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
object.unlock();
|
2019-09-09 10:09:30 +02:00
|
|
|
imp_wait(_old, usec_timeout);
|
2016-09-07 00:38:52 +02:00
|
|
|
object.lock();
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
|
|
|
|
2018-10-02 16:50:22 +02:00
|
|
|
// Unlock all specified objects but don't lock them again
|
|
|
|
template <typename... Locks>
|
2019-09-09 10:09:30 +02:00
|
|
|
void wait_unlock(u64 usec_timeout, Locks&&... locks)
|
2018-10-02 16:50:22 +02:00
|
|
|
{
|
2019-09-09 10:09:30 +02:00
|
|
|
const u32 _old = add_waiter();
|
2018-10-02 16:50:22 +02:00
|
|
|
(..., std::forward<Locks>(locks).unlock());
|
2019-09-09 10:09:30 +02:00
|
|
|
|
|
|
|
if (!_old)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp_wait(_old, usec_timeout);
|
2018-10-02 16:50:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:19:52 +01: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 10:09:30 +02:00
|
|
|
imp_wake(-1);
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 20:20:46 +01:00
|
|
|
|
2019-09-09 10:09:30 +02:00
|
|
|
static constexpr u64 max_timeout = UINT64_MAX / 1000;
|
2017-01-24 21:19:52 +01:00
|
|
|
};
|