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"
|
2018-10-02 16:50:22 +02:00
|
|
|
#include <shared_mutex>
|
2018-11-25 17:43:02 +01:00
|
|
|
#include "asm.h"
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
// Lightweight condition variable
|
|
|
|
class cond_variable
|
|
|
|
{
|
|
|
|
// Internal waiter counter
|
|
|
|
atomic_t<u32> m_value{0};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Internal waiting function
|
|
|
|
bool imp_wait(u32 _old, u64 _timeout) noexcept;
|
|
|
|
|
|
|
|
// 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>
|
2018-09-05 15:24:11 +02:00
|
|
|
bool wait(T& object, u64 usec_timeout = -1)
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
const u32 _old = m_value.fetch_add(1); // Increment waiter counter
|
2016-09-07 00:38:52 +02:00
|
|
|
object.unlock();
|
2017-01-24 21:19:52 +01:00
|
|
|
const bool res = imp_wait(_old, usec_timeout);
|
2016-09-07 00:38:52 +02:00
|
|
|
object.lock();
|
2017-01-24 21:19:52 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:50:22 +02:00
|
|
|
// Unlock all specified objects but don't lock them again
|
|
|
|
template <typename... Locks>
|
|
|
|
bool wait_unlock(u64 usec_timeout, Locks&&... locks)
|
|
|
|
{
|
|
|
|
const u32 _old = m_value.fetch_add(1); // Increment waiter counter
|
|
|
|
(..., std::forward<Locks>(locks).unlock());
|
|
|
|
return imp_wait(_old, usec_timeout);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-11-26 16:55:22 +01:00
|
|
|
imp_wake(65535);
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 20:20:46 +01:00
|
|
|
|
|
|
|
static constexpr u64 max_timeout = u64{UINT32_MAX} / 1000 * 1000000;
|
2017-01-24 21:19:52 +01:00
|
|
|
};
|
2018-05-14 22:07:36 +02:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
// Condition variable fused with a pseudo-mutex supporting only reader locks (up to 32 readers).
|
|
|
|
class shared_cond
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
|
|
|
// For information, shouldn't modify
|
2019-06-04 15:37:50 +02:00
|
|
|
enum : u64
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
2019-06-04 15:37:50 +02:00
|
|
|
// Wait bit is aligned for compatibility with 32-bit futex.
|
2018-11-25 17:43:02 +01:00
|
|
|
c_wait = 1,
|
2019-06-04 15:37:50 +02:00
|
|
|
c_sig = 1ull << 32,
|
|
|
|
c_lock = 1ull << 32 | 1,
|
2018-11-25 17:43:02 +01:00
|
|
|
};
|
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
// Split in 32-bit parts for convenient bit combining
|
|
|
|
atomic_t<u64> m_cvx32{0};
|
2018-11-25 17:43:02 +01:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
class shared_lock
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
2019-06-04 15:37:50 +02:00
|
|
|
shared_cond* m_this;
|
2018-11-25 17:43:02 +01:00
|
|
|
u32 m_slot;
|
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
friend class shared_cond;
|
2018-11-25 17:43:02 +01:00
|
|
|
|
|
|
|
public:
|
2019-06-04 15:37:50 +02:00
|
|
|
shared_lock(shared_cond* _this) noexcept
|
2018-11-25 17:43:02 +01:00
|
|
|
: m_this(_this)
|
|
|
|
{
|
|
|
|
// Lock and remember obtained slot index
|
2019-06-04 15:37:50 +02:00
|
|
|
m_slot = m_this->m_cvx32.atomic_op([](u64& cvx32)
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
|
|
|
// Combine used bits and invert to find least significant bit unused
|
2019-06-28 19:43:36 +02:00
|
|
|
const u32 slot = static_cast<u32>(utils::cnttz64(~((cvx32 & 0xffffffff) | (cvx32 >> 32)), true));
|
2018-11-25 17:43:02 +01:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
// Set lock bits (does nothing if all slots are used)
|
|
|
|
const u64 bit = (1ull << slot) & 0xffffffff;
|
|
|
|
cvx32 |= bit | (bit << 32);
|
2018-11-25 17:43:02 +01:00
|
|
|
return slot;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
shared_lock(const shared_lock&) = delete;
|
|
|
|
|
2019-06-06 20:32:35 +02:00
|
|
|
shared_lock(shared_lock&& rhs)
|
|
|
|
: m_this(rhs.m_this)
|
|
|
|
, m_slot(rhs.m_slot)
|
|
|
|
{
|
|
|
|
rhs.m_slot = 32;
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
shared_lock& operator=(const shared_lock&) = delete;
|
2018-11-25 17:43:02 +01:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
~shared_lock()
|
|
|
|
{
|
|
|
|
// Clear the slot (does nothing if all slots are used)
|
|
|
|
const u64 bit = (1ull << m_slot) & 0xffffffff;
|
|
|
|
m_this->m_cvx32 &= ~(bit | (bit << 32));
|
|
|
|
}
|
2018-11-25 17:43:02 +01:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
explicit operator bool() const noexcept
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
2019-06-04 15:37:50 +02:00
|
|
|
// Check success
|
|
|
|
return m_slot < 32;
|
2018-11-25 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wait(u64 usec_timeout = -1) const noexcept
|
|
|
|
{
|
|
|
|
return m_this->wait(*this, usec_timeout);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-26 16:55:22 +01:00
|
|
|
bool imp_wait(u32 slot, u64 _timeout) noexcept;
|
2018-11-25 17:43:02 +01:00
|
|
|
void imp_notify() noexcept;
|
|
|
|
|
|
|
|
public:
|
2019-06-04 15:37:50 +02:00
|
|
|
constexpr shared_cond() = default;
|
|
|
|
|
|
|
|
shared_lock try_shared_lock() noexcept
|
|
|
|
{
|
|
|
|
return shared_lock(this);
|
|
|
|
}
|
2018-11-25 17:43:02 +01:00
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
u32 count() const noexcept
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
2019-06-04 15:37:50 +02:00
|
|
|
const u64 cvx32 = m_cvx32;
|
2019-06-28 19:43:36 +02:00
|
|
|
return utils::popcnt32(static_cast<u32>(cvx32 | (cvx32 >> 32)));
|
2018-11-25 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-04 15:37:50 +02:00
|
|
|
bool wait(shared_lock const& lock, u64 usec_timeout = -1) noexcept
|
2018-11-25 17:43:02 +01:00
|
|
|
{
|
|
|
|
AUDIT(lock.m_this == this);
|
2018-11-26 16:55:22 +01:00
|
|
|
return imp_wait(lock.m_slot, usec_timeout);
|
2018-11-25 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:32:35 +02:00
|
|
|
void wait_all() noexcept;
|
|
|
|
|
|
|
|
bool wait_all(shared_lock& lock) noexcept;
|
|
|
|
|
2018-11-25 17:43:02 +01:00
|
|
|
void notify_all() noexcept
|
|
|
|
{
|
2019-06-04 15:37:50 +02:00
|
|
|
if (LIKELY(!m_cvx32))
|
2018-11-25 17:43:02 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
imp_notify();
|
|
|
|
}
|
2019-06-06 20:32:35 +02:00
|
|
|
|
|
|
|
bool notify_all(shared_lock& lock) noexcept;
|
2018-11-25 17:43:02 +01:00
|
|
|
};
|