2017-01-24 20:19:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-03 19:28:33 +00:00
|
|
|
#include <mutex>
|
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
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// Shared mutex with small size (u32).
|
2017-01-24 20:19:52 +00:00
|
|
|
class shared_mutex final
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
enum : u32
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2018-11-09 12:44:45 +00:00
|
|
|
c_one = 1u << 14, // Fixed-point 1.0 value (one writer, max_readers = c_one - 1)
|
2018-09-21 17:17:16 +00:00
|
|
|
c_sig = 1u << 30,
|
|
|
|
c_err = 1u << 31,
|
2017-01-24 20:19:52 +00:00
|
|
|
};
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
atomic_t<u32> m_value{};
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void imp_lock_shared(u32 val);
|
|
|
|
void imp_unlock_shared(u32 old);
|
|
|
|
void imp_wait();
|
2018-10-01 17:03:40 +00:00
|
|
|
void imp_signal();
|
2018-09-21 17:17:16 +00:00
|
|
|
void imp_lock(u32 val);
|
|
|
|
void imp_unlock(u32 old);
|
2017-01-24 20:19:52 +00:00
|
|
|
void imp_lock_upgrade();
|
2018-10-01 17:03:40 +00:00
|
|
|
void imp_lock_unlock();
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr shared_mutex() = default;
|
|
|
|
|
2018-09-21 17:17:16 +00: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 20:19:52 +00:00
|
|
|
|
|
|
|
void lock_shared()
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
const u32 value = m_value.load();
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value >= c_one - 1 || !m_value.compare_and_swap_test(value, value + 1)) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
imp_lock_shared(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:29:31 +00:00
|
|
|
void lock_shared_hle()
|
|
|
|
{
|
|
|
|
const u32 value = m_value.load();
|
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value < c_one - 1) [[likely]]
|
2019-01-29 00:29:31 +00:00
|
|
|
{
|
|
|
|
u32 old = value;
|
2020-02-05 07:00:08 +00:00
|
|
|
if (atomic_storage<u32>::compare_exchange_hle_acq(m_value.raw(), old, value + 1)) [[likely]]
|
2019-01-29 00:29:31 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imp_lock_shared(value);
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
void unlock_shared()
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
// Unconditional decrement (can result in broken state)
|
|
|
|
const u32 value = m_value.fetch_sub(1);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value >= c_one) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
imp_unlock_shared(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:29:31 +00:00
|
|
|
void unlock_shared_hle()
|
|
|
|
{
|
|
|
|
const u32 value = atomic_storage<u32>::fetch_add_hle_rel(m_value.raw(), -1);
|
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value >= c_one) [[unlikely]]
|
2019-01-29 00:29:31 +00:00
|
|
|
{
|
|
|
|
imp_unlock_shared(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
bool try_lock()
|
|
|
|
{
|
2018-10-01 17:03:40 +00:00
|
|
|
return m_value.compare_and_swap_test(0, c_one);
|
2018-09-21 17:17:16 +00:00
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
const u32 value = m_value.compare_and_swap(0, c_one);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
imp_lock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:29:31 +00:00
|
|
|
void lock_hle()
|
|
|
|
{
|
|
|
|
u32 value = 0;
|
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (!atomic_storage<u32>::compare_exchange_hle_acq(m_value.raw(), value, c_one)) [[unlikely]]
|
2019-01-29 00:29:31 +00:00
|
|
|
{
|
|
|
|
imp_lock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
void unlock()
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
// Unconditional decrement (can result in broken state)
|
|
|
|
const u32 value = m_value.fetch_sub(c_one);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value != c_one) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
imp_unlock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:29:31 +00:00
|
|
|
void unlock_hle()
|
|
|
|
{
|
2019-04-13 17:36:44 +00:00
|
|
|
const u32 value = atomic_storage<u32>::fetch_add_hle_rel(m_value.raw(), 0u - c_one);
|
2019-01-29 00:29:31 +00:00
|
|
|
|
2020-02-05 07:00:08 +00:00
|
|
|
if (value != c_one) [[unlikely]]
|
2019-01-29 00:29:31 +00:00
|
|
|
{
|
|
|
|
imp_unlock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00: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 20:19:52 +00:00
|
|
|
|
|
|
|
void lock_upgrade()
|
|
|
|
{
|
2020-02-05 07:00:08 +00:00
|
|
|
if (!try_lock_upgrade()) [[unlikely]]
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
imp_lock_upgrade();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void lock_downgrade()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
// Convert to reader lock (can result in broken state)
|
|
|
|
m_value -= c_one - 1;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2017-10-05 12:09:08 +00:00
|
|
|
|
2018-10-01 17:03:40 +00:00
|
|
|
// Optimized wait for lockability without locking, relaxed
|
|
|
|
void lock_unlock()
|
|
|
|
{
|
2020-02-05 07:00:08 +00:00
|
|
|
if (m_value != 0) [[unlikely]]
|
2018-10-01 17:03:40 +00:00
|
|
|
{
|
|
|
|
imp_lock_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// Check whether can immediately obtain an exclusive (writer) lock
|
|
|
|
bool is_free() const
|
2017-10-05 12:09:08 +00:00
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
return m_value.load() == 0;
|
2017-10-05 12:09:08 +00:00
|
|
|
}
|
2018-04-03 19:42:47 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// Check whether can immediately obtain a shared (reader) lock
|
2018-04-03 19:42:47 +00:00
|
|
|
bool is_lockable() const
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
return m_value.load() < c_one - 1;
|
2018-04-03 19:42:47 +00:00
|
|
|
}
|
2022-08-07 14:48:14 +00:00
|
|
|
|
|
|
|
bool has_waiters() const
|
|
|
|
{
|
|
|
|
return m_value.load() > c_one;
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 17:33:12 +00:00
|
|
|
// Simplified shared (reader) lock implementation.
|
2017-01-24 20:19:52 +00:00
|
|
|
class reader_lock final
|
|
|
|
{
|
|
|
|
shared_mutex& m_mutex;
|
2017-02-15 15:07:42 +00:00
|
|
|
bool m_upgraded = false;
|
2017-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
reader_lock(const reader_lock&) = delete;
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
reader_lock& operator=(const reader_lock&) = delete;
|
|
|
|
|
2017-01-24 20:19:52 +00:00
|
|
|
explicit reader_lock(shared_mutex& mutex)
|
|
|
|
: m_mutex(mutex)
|
|
|
|
{
|
2018-09-03 19:28:33 +00:00
|
|
|
m_mutex.lock_shared();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// One-way lock upgrade; note that the observed state could have been changed
|
2017-02-15 15:07:42 +00:00
|
|
|
void upgrade()
|
|
|
|
{
|
|
|
|
if (!m_upgraded)
|
|
|
|
{
|
|
|
|
m_mutex.lock_upgrade();
|
|
|
|
m_upgraded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00: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 20:19:52 +00:00
|
|
|
~reader_lock()
|
|
|
|
{
|
2018-09-03 19:28:33 +00:00
|
|
|
m_upgraded ? m_mutex.unlock() : m_mutex.unlock_shared();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
};
|