mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-02 21:11:25 +12:00
Rename cond_x16 to shared_cond
Extend capacity from 16 to 32. Remove redundant m_total counter.
This commit is contained in:
parent
447029a700
commit
9dc0368079
5 changed files with 92 additions and 65 deletions
|
@ -176,61 +176,60 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
// Packed version of cond_one, supports up to 16 readers.
|
||||
class cond_x16
|
||||
// Condition variable fused with a pseudo-mutex supporting only reader locks (up to 32 readers).
|
||||
class shared_cond
|
||||
{
|
||||
// For information, shouldn't modify
|
||||
enum : u32
|
||||
enum : u64
|
||||
{
|
||||
// Wait bit is aligned for compatibility with 32-bit futex.
|
||||
c_wait = 1,
|
||||
c_lock = 1 << 16,
|
||||
c_sig = 1 << 16 | 1,
|
||||
c_sig = 1ull << 32,
|
||||
c_lock = 1ull << 32 | 1,
|
||||
};
|
||||
|
||||
// Split in 16-bit parts for convenient bit combining
|
||||
atomic_t<u32> m_cvx16{0};
|
||||
// Split in 32-bit parts for convenient bit combining
|
||||
atomic_t<u64> m_cvx32{0};
|
||||
|
||||
// Effectively unused, only counts readers
|
||||
atomic_t<u32> m_total{0};
|
||||
|
||||
class lock_x16
|
||||
class shared_lock
|
||||
{
|
||||
cond_x16* m_this;
|
||||
shared_cond* m_this;
|
||||
u32 m_slot;
|
||||
|
||||
friend class cond_x16;
|
||||
friend class shared_cond;
|
||||
|
||||
public:
|
||||
lock_x16(cond_x16* _this) noexcept
|
||||
shared_lock(shared_cond* _this) noexcept
|
||||
: m_this(_this)
|
||||
{
|
||||
// Spin if the number of readers exceeds 16
|
||||
while (UNLIKELY(m_this->m_total++ >= 16))
|
||||
m_this->m_total--;
|
||||
|
||||
// Lock and remember obtained slot index
|
||||
m_slot = m_this->m_cvx16.atomic_op([](u32& cvx16)
|
||||
m_slot = m_this->m_cvx32.atomic_op([](u64& cvx32)
|
||||
{
|
||||
// Combine used bits and invert to find least significant bit unused
|
||||
const u32 slot = utils::cnttz32(~((cvx16 & 0xffff) | (cvx16 >> 16)), true);
|
||||
const u32 slot = utils::cnttz32(~((cvx32 & 0xffffffff) | (cvx32 >> 32)), true);
|
||||
|
||||
// Set lock bit
|
||||
cvx16 |= c_lock << slot;
|
||||
|
||||
AUDIT(slot < 16);
|
||||
// Set lock bits (does nothing if all slots are used)
|
||||
const u64 bit = (1ull << slot) & 0xffffffff;
|
||||
cvx32 |= bit | (bit << 32);
|
||||
return slot;
|
||||
});
|
||||
}
|
||||
|
||||
lock_x16(const lock_x16&) = delete;
|
||||
shared_lock(const shared_lock&) = delete;
|
||||
|
||||
lock_x16& operator=(const lock_x16&) = delete;
|
||||
shared_lock& operator=(const shared_lock&) = delete;
|
||||
|
||||
~lock_x16()
|
||||
~shared_lock()
|
||||
{
|
||||
// Clear the slot
|
||||
m_this->m_cvx16 &= ~((c_wait | c_lock) << m_slot);
|
||||
m_this->m_total -= 1;
|
||||
// Clear the slot (does nothing if all slots are used)
|
||||
const u64 bit = (1ull << m_slot) & 0xffffffff;
|
||||
m_this->m_cvx32 &= ~(bit | (bit << 32));
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept
|
||||
{
|
||||
// Check success
|
||||
return m_slot < 32;
|
||||
}
|
||||
|
||||
bool wait(u64 usec_timeout = -1) const noexcept
|
||||
|
@ -243,14 +242,20 @@ class cond_x16
|
|||
void imp_notify() noexcept;
|
||||
|
||||
public:
|
||||
constexpr cond_x16() = default;
|
||||
constexpr shared_cond() = default;
|
||||
|
||||
lock_x16 lock_one() noexcept
|
||||
shared_lock try_shared_lock() noexcept
|
||||
{
|
||||
return lock_x16(this);
|
||||
return shared_lock(this);
|
||||
}
|
||||
|
||||
bool wait(lock_x16 const& lock, u64 usec_timeout = -1) noexcept
|
||||
u32 count() const noexcept
|
||||
{
|
||||
const u64 cvx32 = m_cvx32;
|
||||
return utils::popcnt32(cvx32 | (cvx32 >> 32));
|
||||
}
|
||||
|
||||
bool wait(shared_lock const& lock, u64 usec_timeout = -1) noexcept
|
||||
{
|
||||
AUDIT(lock.m_this == this);
|
||||
return imp_wait(lock.m_slot, usec_timeout);
|
||||
|
@ -258,7 +263,7 @@ public:
|
|||
|
||||
void notify_all() noexcept
|
||||
{
|
||||
if (LIKELY(!m_cvx16))
|
||||
if (LIKELY(!m_cvx32))
|
||||
return;
|
||||
|
||||
imp_notify();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue