2017-01-24 20:19:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "Atomic.h"
|
|
|
|
|
|
|
|
// Lightweight semaphore helper class
|
|
|
|
class semaphore_base
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
// Semaphore value
|
2017-01-24 20:19:52 +00:00
|
|
|
atomic_t<s32> m_value;
|
|
|
|
|
2017-02-15 15:07:42 +00:00
|
|
|
void imp_wait();
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
void imp_post(s32 _old);
|
|
|
|
|
2016-09-06 22:38:52 +00:00
|
|
|
friend class semaphore_lock;
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
protected:
|
|
|
|
explicit constexpr semaphore_base(s32 value)
|
|
|
|
: m_value{value}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait()
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
// Load value
|
|
|
|
const s32 value = m_value.load();
|
|
|
|
|
|
|
|
// Conditional decrement
|
|
|
|
if (UNLIKELY(value <= 0 || !m_value.compare_and_swap_test(value, value - 1)))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_wait();
|
|
|
|
|
|
|
|
void post(s32 _max)
|
|
|
|
{
|
|
|
|
// Unconditional increment
|
2017-02-15 15:07:42 +00:00
|
|
|
const s32 value = m_value.fetch_add(1);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
if (UNLIKELY(value < 0 || value >= _max))
|
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
imp_post(value);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_post(s32 _max);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Get current semaphore value
|
|
|
|
s32 get() const
|
|
|
|
{
|
|
|
|
// Load value
|
|
|
|
const s32 value = m_value;
|
|
|
|
|
|
|
|
// Return only positive value
|
2017-02-15 15:07:42 +00:00
|
|
|
return value < 0 ? 0 : value;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lightweight semaphore template (default arguments define binary semaphore and Def == Max)
|
|
|
|
template <s32 Max = 1, s32 Def = Max>
|
|
|
|
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)
|
|
|
|
constexpr semaphore()
|
2017-02-15 15:07:42 +00:00
|
|
|
: base{Def}
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit value constructor (not recommended)
|
|
|
|
explicit constexpr semaphore(s32 value)
|
2017-02-15 15:07:42 +00:00
|
|
|
: base{value}
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain a semaphore
|
|
|
|
void wait()
|
|
|
|
{
|
|
|
|
return base::wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to obtain a semaphore
|
|
|
|
explicit_bool_t try_wait()
|
|
|
|
{
|
|
|
|
return base::try_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a semaphore
|
|
|
|
void post()
|
|
|
|
{
|
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
|
|
|
|
explicit_bool_t try_post()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
};
|
2016-09-06 22:38:52 +00:00
|
|
|
|
|
|
|
class semaphore_lock
|
|
|
|
{
|
|
|
|
semaphore_base& m_base;
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
m_base.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
m_base.post(INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class cond_variable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit semaphore_lock(const semaphore_lock&) = delete;
|
|
|
|
|
|
|
|
semaphore_lock(semaphore_base& sema)
|
|
|
|
: m_base(sema)
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~semaphore_lock()
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
};
|