mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-04-28 13:28:01 +03:00
Replace notifier::lock_shared() with try_lock_shared()
Also add notify_one(), try_lock() and unlock() Move some code in cond.cpp
This commit is contained in:
parent
8d5bbfb850
commit
a33f297315
3 changed files with 134 additions and 64 deletions
100
Utilities/cond.h
100
Utilities/cond.h
|
@ -59,87 +59,61 @@ class notifier
|
|||
atomic_t<u32> m_counter{0};
|
||||
cond_variable m_cond;
|
||||
|
||||
bool imp_try_lock(u32 count);
|
||||
|
||||
void imp_unlock(u32 count);
|
||||
|
||||
u32 imp_notify(u32 count);
|
||||
|
||||
public:
|
||||
constexpr notifier() = default;
|
||||
|
||||
void lock_shared()
|
||||
bool try_lock()
|
||||
{
|
||||
m_counter++;
|
||||
return imp_try_lock(max_readers);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
imp_unlock(max_readers);
|
||||
}
|
||||
|
||||
bool try_lock_shared()
|
||||
{
|
||||
return imp_try_lock(1);
|
||||
}
|
||||
|
||||
void unlock_shared()
|
||||
{
|
||||
const u32 counter = --m_counter;
|
||||
|
||||
if (counter & 0x7f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (counter >= 0x80)
|
||||
{
|
||||
const u32 _old = m_counter.atomic_op([](u32& value) -> u32
|
||||
{
|
||||
if (value & 0x7f)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return std::exchange(value, 0) >> 7;
|
||||
});
|
||||
|
||||
if (_old && m_cond.m_value)
|
||||
{
|
||||
m_cond.imp_wake(_old);
|
||||
}
|
||||
}
|
||||
imp_unlock(1);
|
||||
}
|
||||
|
||||
explicit_bool_t wait(u64 usec_timeout = -1)
|
||||
{
|
||||
const u32 _old = m_cond.m_value.fetch_add(1);
|
||||
|
||||
if (0x80 <= m_counter.fetch_op([](u32& value)
|
||||
{
|
||||
value--;
|
||||
|
||||
if (value >= 0x80)
|
||||
{
|
||||
value -= 0x80;
|
||||
}
|
||||
}))
|
||||
{
|
||||
// Return without waiting
|
||||
m_cond.imp_wait(_old, 0);
|
||||
m_counter++;
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool res = m_cond.imp_wait(_old, usec_timeout);
|
||||
m_counter++;
|
||||
return res;
|
||||
}
|
||||
explicit_bool_t wait(u64 usec_timeout = -1);
|
||||
|
||||
void notify_all()
|
||||
{
|
||||
if (m_counter)
|
||||
{
|
||||
m_counter.atomic_op([](u32& value)
|
||||
{
|
||||
if (const u32 add = value & 0x7f)
|
||||
{
|
||||
// Mutex is locked in shared mode
|
||||
value += add << 7;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Mutex is unlocked
|
||||
value = 0;
|
||||
}
|
||||
});
|
||||
imp_notify(-1);
|
||||
}
|
||||
|
||||
// Notify after imaginary "exclusive" lock+unlock
|
||||
m_cond.notify_all();
|
||||
}
|
||||
|
||||
void notify_one()
|
||||
{
|
||||
// TODO
|
||||
if (m_counter)
|
||||
{
|
||||
if (imp_notify(1))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_cond.notify_one();
|
||||
}
|
||||
|
||||
static constexpr u32 max_readers = 0x7f;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue