Rewrite Utilitis/sema.cpp

This commit is contained in:
Eladash 2023-09-11 12:52:10 +03:00 committed by Elad Ashkenazi
parent 37ba19776a
commit 7a4ee286be
3 changed files with 79 additions and 48 deletions

View file

@ -8,72 +8,84 @@ void semaphore_base::imp_wait()
{ {
busy_wait(); busy_wait();
const s32 value = m_value.load(); const u32 value = m_value.load();
if (value > 0 && m_value.compare_and_swap_test(value, value - 1)) if (value & c_value_mask && m_value.compare_and_swap_test(value, value - c_value))
{ {
return; return;
} }
} }
bool waits = false;
while (true) while (true)
{ {
// Try hard way // Try hard way
const s32 value = m_value.atomic_op([](s32& value) const u32 value = m_value.fetch_op([&](u32& value)
{ {
// Use sign bit to acknowledge waiter presence ensure(value != c_waiter_mask); // "semaphore_base: overflow"
if (value && value > smin)
{
value--;
if (value < 0) if (value & c_value_mask)
{
// Obtain signal
value -= c_value;
if (waits)
{ {
// Remove sign bit // Remove waiter
value -= s32{smin}; value -= c_waiter;
} }
} }
else else if (!waits)
{ {
// Set sign bit // Add waiter
value = smin; value += c_waiter;
} }
return value;
}); });
if (value >= 0) if (value & c_value_mask)
{ {
// Signal other waiter to wake up or to restore sign bit break;
m_value.notify_one();
return;
} }
m_value.wait(value); m_value.wait(value + (waits ? 0 : c_waiter));
waits = true;
} }
} }
void semaphore_base::imp_post(s32 _old) void semaphore_base::imp_post(u32 _old)
{ {
ensure(_old < 0); // "semaphore_base: overflow" ensure(~_old & c_value_mask); // "semaphore_base: overflow"
m_value.notify_one(); if ((_old & c_waiter_mask) / c_waiter > (_old & c_value_mask) / c_value)
{
m_value.notify_one();
}
} }
bool semaphore_base::try_post(s32 _max) bool semaphore_base::try_post(u32 _max)
{ {
// Conditional increment // Conditional increment
const s32 value = m_value.fetch_op([&](s32& value) const auto [value, ok] = m_value.fetch_op([&](u32& value)
{ {
if (value < _max) if ((value & c_value_mask) <= _max)
{ {
value += 1; value += c_value;
return true;
} }
return false;
}); });
if (value < 0) if (!ok)
{
return false;
}
if (value & c_waiter_mask)
{ {
imp_post(value); imp_post(value);
} }
return value < _max; return true;
} }

View file

@ -7,14 +7,22 @@
class semaphore_base class semaphore_base
{ {
// Semaphore value // Semaphore value
atomic_t<s32> m_value; atomic_t<u32> m_value;
enum : u32
{
c_value = 1u << 0,
c_value_mask = +c_value * 0xffff,
c_waiter = 1u << 16,
c_waiter_mask = +c_waiter * 0xffff,
};
void imp_wait(); void imp_wait();
void imp_post(s32 _old); void imp_post(u32 _old);
protected: protected:
explicit constexpr semaphore_base(s32 value) explicit constexpr semaphore_base(u32 value) noexcept
: m_value{value} : m_value{value}
{ {
} }
@ -22,10 +30,10 @@ protected:
void wait() void wait()
{ {
// Load value // Load value
const s32 value = m_value.load(); const u32 value = m_value.load();
// Conditional decrement // Conditional decrement
if (value <= 0 || !m_value.compare_and_swap_test(value, value - 1)) [[unlikely]] if ((value & c_value_mask) == 0 || !m_value.compare_and_swap_test(value, value - c_value)) [[unlikely]]
{ {
imp_wait(); imp_wait();
} }
@ -33,36 +41,47 @@ protected:
bool try_wait() bool try_wait()
{ {
return m_value.try_dec(0); return m_value.fetch_op([](u32& value)
{
if (value & c_value_mask)
{
value -= c_value;
return true;
}
return false;
}).second;
} }
void post(s32 _max) void post(u32 _max)
{ {
// Unconditional increment // Unconditional increment
const s32 value = m_value.fetch_add(1); const u32 value = m_value.fetch_add(c_value);
if (value < 0 || value >= _max) [[unlikely]] if (value & c_waiter_mask || (value & c_value_mask) >= std::min<u32>(c_value_mask, _max)) [[unlikely]]
{ {
imp_post(value); imp_post(value);
} }
} }
bool try_post(s32 _max); bool try_post(u32 _max);
public: public:
// Get current semaphore value // Get current semaphore value
s32 get() const s32 get() const
{ {
// Load value // Load value
const s32 value = m_value; const u32 raw_value = m_value;
const u32 waiters = (raw_value & c_waiter_mask) / c_waiter;
const u32 value = (raw_value & c_value_mask) / c_value;
// Return only positive value // Return only positive value
return value < 0 ? 0 : value; return static_cast<s32>(waiters >= value ? 0 : value - waiters);
} }
}; };
// Lightweight semaphore template (default arguments define binary semaphore and Def == Max) // Lightweight semaphore template (default arguments define binary semaphore and Def == Max)
template <s32 Max = 1, s32 Def = Max> template <s16 Max = 1, s16 Def = Max>
class semaphore final : public semaphore_base class semaphore final : public semaphore_base
{ {
static_assert(Max >= 0, "semaphore<>: Max is out of bounds"); static_assert(Max >= 0, "semaphore<>: Max is out of bounds");
@ -73,13 +92,13 @@ class semaphore final : public semaphore_base
public: public:
// Default constructor (recommended) // Default constructor (recommended)
constexpr semaphore() constexpr semaphore() noexcept
: base(Def) : base(Def)
{ {
} }
// Explicit value constructor (not recommended) // Explicit value constructor (not recommended)
explicit constexpr semaphore(s32 value) explicit constexpr semaphore(s16 value) noexcept
: base(value) : base(value)
{ {
} }

View file

@ -3420,14 +3420,14 @@ extern bool ppu_stdcx(ppu_thread& ppu, u32 addr, u64 reg_value)
struct jit_core_allocator struct jit_core_allocator
{ {
const s32 thread_count = g_cfg.core.llvm_threads ? std::min<s32>(g_cfg.core.llvm_threads, limit()) : limit(); const s16 thread_count = g_cfg.core.llvm_threads ? std::min<s32>(g_cfg.core.llvm_threads, limit()) : limit();
// Initialize global semaphore with the max number of threads // Initialize global semaphore with the max number of threads
::semaphore<0x7fffffff> sem{std::max<s32>(thread_count, 1)}; ::semaphore<0x7fff> sem{std::max<s16>(thread_count, 1)};
static s32 limit() static s16 limit()
{ {
return static_cast<s32>(utils::get_thread_count()); return static_cast<s16>(std::min<s32>(0x7fff, utils::get_thread_count()));
} }
}; };