2017-01-24 21:19:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
#include <mutex>
|
2017-01-24 21:19:52 +01:00
|
|
|
#include "types.h"
|
|
|
|
#include "Atomic.h"
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
// Shared mutex with small size (u32).
|
2017-01-24 21:19:52 +01:00
|
|
|
class shared_mutex final
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
enum : u32
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
2018-11-09 13:44:45 +01:00
|
|
|
c_one = 1u << 14, // Fixed-point 1.0 value (one writer, max_readers = c_one - 1)
|
2018-09-21 19:17:16 +02:00
|
|
|
c_sig = 1u << 30,
|
|
|
|
c_err = 1u << 31,
|
2017-01-24 21:19:52 +01:00
|
|
|
};
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
atomic_t<u32> m_value{};
|
2017-01-24 21:19:52 +01:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
void imp_lock_shared(u32 val);
|
|
|
|
void imp_unlock_shared(u32 old);
|
|
|
|
void imp_wait();
|
2018-10-01 19:03:40 +02:00
|
|
|
void imp_signal();
|
2018-09-21 19:17:16 +02:00
|
|
|
void imp_lock(u32 val);
|
|
|
|
void imp_unlock(u32 old);
|
2017-01-24 21:19:52 +01:00
|
|
|
void imp_lock_upgrade();
|
2018-10-01 19:03:40 +02:00
|
|
|
void imp_lock_unlock();
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr shared_mutex() = default;
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
bool try_lock_shared()
|
|
|
|
{
|
|
|
|
const u32 value = m_value.load();
|
|
|
|
|
|
|
|
// Conditional increment
|
|
|
|
return value < c_one - 1 && m_value.compare_and_swap_test(value, value + 1);
|
|
|
|
}
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
void lock_shared()
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
const u32 value = m_value.load();
|
2017-01-24 21:19:52 +01:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
if (UNLIKELY(value >= c_one - 1 || !m_value.compare_and_swap_test(value, value + 1)))
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
imp_lock_shared(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_shared()
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
// Unconditional decrement (can result in broken state)
|
|
|
|
const u32 value = m_value.fetch_sub(1);
|
2017-01-24 21:19:52 +01:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
if (UNLIKELY(value >= c_one))
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
imp_unlock_shared(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
bool try_lock()
|
|
|
|
{
|
2018-10-01 19:03:40 +02:00
|
|
|
return m_value.compare_and_swap_test(0, c_one);
|
2018-09-21 19:17:16 +02:00
|
|
|
}
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
const u32 value = m_value.compare_and_swap(0, c_one);
|
2017-01-24 21:19:52 +01:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
if (UNLIKELY(value))
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
imp_lock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
// Unconditional decrement (can result in broken state)
|
|
|
|
const u32 value = m_value.fetch_sub(c_one);
|
2017-01-24 21:19:52 +01:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
if (UNLIKELY(value != c_one))
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
imp_unlock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
bool try_lock_upgrade()
|
|
|
|
{
|
|
|
|
const u32 value = m_value.load();
|
|
|
|
|
|
|
|
// Conditional increment, try to convert a single reader into a writer, ignoring other writers
|
|
|
|
return (value + c_one - 1) % c_one == 0 && m_value.compare_and_swap_test(value, value + c_one - 1);
|
|
|
|
}
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
void lock_upgrade()
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
if (UNLIKELY(!try_lock_upgrade()))
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
|
|
|
imp_lock_upgrade();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
void lock_downgrade()
|
2017-01-24 21:19:52 +01:00
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
// Convert to reader lock (can result in broken state)
|
|
|
|
m_value -= c_one - 1;
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
2017-10-05 14:09:08 +02:00
|
|
|
|
2018-10-01 19:03:40 +02:00
|
|
|
// Optimized wait for lockability without locking, relaxed
|
|
|
|
void lock_unlock()
|
|
|
|
{
|
|
|
|
if (UNLIKELY(m_value != 0))
|
|
|
|
{
|
|
|
|
imp_lock_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
// Check whether can immediately obtain an exclusive (writer) lock
|
|
|
|
bool is_free() const
|
2017-10-05 14:09:08 +02:00
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
return m_value.load() == 0;
|
2017-10-05 14:09:08 +02:00
|
|
|
}
|
2018-04-03 21:42:47 +02:00
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
// Check whether can immediately obtain a shared (reader) lock
|
2018-04-03 21:42:47 +02:00
|
|
|
bool is_lockable() const
|
|
|
|
{
|
2018-09-21 19:17:16 +02:00
|
|
|
return m_value.load() < c_one - 1;
|
2018-04-03 21:42:47 +02:00
|
|
|
}
|
2017-01-24 21:19:52 +01:00
|
|
|
};
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Simplified shared (reader) lock implementation.
|
2017-01-24 21:19:52 +01:00
|
|
|
class reader_lock final
|
|
|
|
{
|
|
|
|
shared_mutex& m_mutex;
|
2017-02-15 16:07:42 +01:00
|
|
|
bool m_upgraded = false;
|
2017-01-24 21:19:52 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
reader_lock(const reader_lock&) = delete;
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
reader_lock& operator=(const reader_lock&) = delete;
|
|
|
|
|
2017-01-24 21:19:52 +01:00
|
|
|
explicit reader_lock(shared_mutex& mutex)
|
|
|
|
: m_mutex(mutex)
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
m_mutex.lock_shared();
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
// One-way lock upgrade; note that the observed state could have been changed
|
2017-02-15 16:07:42 +01:00
|
|
|
void upgrade()
|
|
|
|
{
|
|
|
|
if (!m_upgraded)
|
|
|
|
{
|
|
|
|
m_mutex.lock_upgrade();
|
|
|
|
m_upgraded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:17:16 +02:00
|
|
|
// Try to upgrade; if it succeeds, the observed state has NOT been changed
|
|
|
|
bool try_upgrade()
|
|
|
|
{
|
|
|
|
return m_upgraded || (m_upgraded = m_mutex.try_lock_upgrade());
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:19:52 +01:00
|
|
|
~reader_lock()
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
m_upgraded ? m_mutex.unlock() : m_mutex.unlock_shared();
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|
|
|
|
};
|