2017-01-24 20:19:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-12 12:01:29 +00:00
|
|
|
#include "util/types.hpp"
|
2019-07-26 22:34:10 +00:00
|
|
|
#include "util/atomic.hpp"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
// Lightweight semaphore helper class
|
|
|
|
class semaphore_base
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
// Semaphore value
|
2023-09-11 09:52:10 +00:00
|
|
|
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,
|
|
|
|
};
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2017-02-15 15:07:42 +00:00
|
|
|
void imp_wait();
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2023-09-11 09:52:10 +00:00
|
|
|
void imp_post(u32 _old);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
protected:
|
2023-09-11 09:52:10 +00:00
|
|
|
explicit constexpr semaphore_base(u32 value) noexcept
|
2017-01-24 20:19:52 +00:00
|
|
|
: m_value{value}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait()
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
// Load value
|
2023-09-11 09:52:10 +00:00
|
|
|
const u32 value = m_value.load();
|
2017-02-15 15:07:42 +00:00
|
|
|
|
|
|
|
// Conditional decrement
|
2023-09-11 09:52:10 +00:00
|
|
|
if ((value & c_value_mask) == 0 || !m_value.compare_and_swap_test(value, value - c_value)) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
imp_wait();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:28:37 +00:00
|
|
|
bool try_wait()
|
|
|
|
{
|
2023-09-11 09:52:10 +00:00
|
|
|
return m_value.fetch_op([](u32& value)
|
|
|
|
{
|
|
|
|
if (value & c_value_mask)
|
|
|
|
{
|
|
|
|
value -= c_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}).second;
|
2018-09-05 19:28:37 +00:00
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2023-09-11 09:52:10 +00:00
|
|
|
void post(u32 _max)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
// Unconditional increment
|
2023-09-11 09:52:10 +00:00
|
|
|
const u32 value = m_value.fetch_add(c_value);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2023-09-11 09:52:10 +00:00
|
|
|
if (value & c_waiter_mask || (value & c_value_mask) >= std::min<u32>(c_value_mask, _max)) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
imp_post(value);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 09:52:10 +00:00
|
|
|
bool try_post(u32 _max);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Get current semaphore value
|
|
|
|
s32 get() const
|
|
|
|
{
|
|
|
|
// Load value
|
2023-09-11 09:52:10 +00:00
|
|
|
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;
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
// Return only positive value
|
2023-09-11 09:52:10 +00:00
|
|
|
return static_cast<s32>(waiters >= value ? 0 : value - waiters);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lightweight semaphore template (default arguments define binary semaphore and Def == Max)
|
2023-09-11 09:52:10 +00:00
|
|
|
template <s16 Max = 1, s16 Def = Max>
|
2017-01-24 20:19:52 +00:00
|
|
|
class semaphore final : public semaphore_base
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
static_assert(Max >= 0, "semaphore<>: Max is out of bounds");
|
|
|
|
static_assert(Def >= 0, "semaphore<>: Def is out of bounds");
|
2017-01-24 20:19:52 +00:00
|
|
|
static_assert(Def <= Max, "semaphore<>: Def is too big");
|
|
|
|
|
|
|
|
using base = semaphore_base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Default constructor (recommended)
|
2023-09-11 09:52:10 +00:00
|
|
|
constexpr semaphore() noexcept
|
2018-09-03 19:28:33 +00:00
|
|
|
: base(Def)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit value constructor (not recommended)
|
2023-09-11 09:52:10 +00:00
|
|
|
explicit constexpr semaphore(s16 value) noexcept
|
2018-09-03 19:28:33 +00:00
|
|
|
: base(value)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain a semaphore
|
2018-09-03 19:28:33 +00:00
|
|
|
void lock()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
return base::wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to obtain a semaphore
|
2018-09-03 19:28:33 +00:00
|
|
|
bool try_lock()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
return base::try_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a semaphore
|
2018-09-03 19:28:33 +00:00
|
|
|
void unlock()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
return base::post(Max);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to return a semaphore
|
2018-09-03 19:28:33 +00:00
|
|
|
bool try_unlock()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
return base::try_post(Max);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get max semaphore value
|
|
|
|
static constexpr s32 size()
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
};
|