2015-09-18 00:41:14 +02:00
|
|
|
#pragma once
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
#include "types.h"
|
2016-02-01 22:55:43 +01:00
|
|
|
#include "Atomic.h"
|
2016-04-25 12:49:12 +02:00
|
|
|
#include "Platform.h"
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2015-11-30 16:10:17 +01:00
|
|
|
//! An attempt to create effective implementation of "shared mutex", lock-free in optimistic case.
|
2016-04-25 12:49:12 +02:00
|
|
|
//! All locking and unlocking may be done by a single LOCK XADD or LOCK CMPXCHG instruction.
|
2015-11-30 16:10:17 +01:00
|
|
|
//! MSVC implementation of std::shared_timed_mutex seems suboptimal.
|
|
|
|
//! std::shared_mutex is not available until C++17.
|
|
|
|
class shared_mutex final
|
2015-09-18 00:41:14 +02:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
enum : u32
|
2015-09-18 00:41:14 +02:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
SM_WRITER_LOCK = 1u << 31, // Exclusive lock flag, must be MSB
|
|
|
|
SM_WAITERS_BIT = 1u << 30, // Flag set if m_wq_size or m_rq_size is non-zero
|
|
|
|
SM_INVALID_BIT = 1u << 29, // Unreachable reader count bit (may be set by incorrect unlock_shared() call)
|
2015-11-30 16:10:17 +01:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
SM_READER_MASK = SM_WAITERS_BIT - 1, // Valid reader count bit mask
|
|
|
|
SM_READER_MAX = 1u << 24, // Max reader count
|
2015-09-18 00:41:14 +02:00
|
|
|
};
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
atomic_t<u32> m_ctrl{}; // Control variable: reader count | SM_* flags
|
2015-11-30 16:10:17 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
struct internal;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
atomic_t<internal*> m_data{}; // Internal data
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
void lock_shared_hard();
|
|
|
|
void unlock_shared_notify();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
void lock_hard();
|
|
|
|
void unlock_notify();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
public:
|
|
|
|
constexpr shared_mutex() = default;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Initialize internal data
|
|
|
|
void initialize_once();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
~shared_mutex();
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
bool try_lock_shared()
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
auto ctrl = m_ctrl.load();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
if (UNLIKELY(ctrl >= SM_READER_MAX))
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
ctrl = 0;
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Weak attempt
|
|
|
|
return LIKELY(m_ctrl.compare_and_swap_test(ctrl, ctrl + 1));
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void lock_shared()
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
// Optimization: unconditional increment, compensated later
|
|
|
|
if (UNLIKELY(m_ctrl++ >= SM_READER_MAX))
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
lock_shared_hard();
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-18 00:41:14 +02:00
|
|
|
|
2015-11-30 16:10:17 +01:00
|
|
|
void unlock_shared()
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
if (UNLIKELY(m_ctrl-- >= SM_READER_MAX))
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
unlock_shared_notify();
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-18 00:41:14 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
bool try_lock()
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
return LIKELY(m_ctrl.compare_and_swap_test(0, SM_WRITER_LOCK));
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
2015-09-18 00:41:14 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
void lock()
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
if (UNLIKELY(!try_lock()))
|
|
|
|
{
|
|
|
|
lock_hard();
|
|
|
|
}
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
2015-09-18 00:41:14 +02:00
|
|
|
|
2015-11-30 16:10:17 +01:00
|
|
|
void unlock()
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
if (UNLIKELY(m_ctrl.fetch_sub(SM_WRITER_LOCK) != SM_WRITER_LOCK))
|
2015-11-30 16:10:17 +01:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
unlock_notify();
|
2015-11-30 16:10:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
//! Simplified shared (reader) lock implementation.
|
2015-11-30 16:10:17 +01:00
|
|
|
//! std::shared_lock may be used instead if necessary.
|
|
|
|
class reader_lock final
|
|
|
|
{
|
|
|
|
shared_mutex& m_mutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
reader_lock(const reader_lock&) = delete;
|
|
|
|
|
|
|
|
reader_lock(shared_mutex& mutex)
|
|
|
|
: m_mutex(mutex)
|
|
|
|
{
|
|
|
|
m_mutex.lock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
~reader_lock()
|
|
|
|
{
|
|
|
|
m_mutex.unlock_shared();
|
|
|
|
}
|
2015-09-18 00:41:14 +02:00
|
|
|
};
|
2016-04-25 12:49:12 +02:00
|
|
|
|
|
|
|
//! Simplified exclusive (writer) lock implementation.
|
|
|
|
//! std::lock_guard may or std::unique_lock be used instead if necessary.
|
|
|
|
class writer_lock final
|
|
|
|
{
|
|
|
|
shared_mutex& m_mutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
writer_lock(const writer_lock&) = delete;
|
|
|
|
|
|
|
|
writer_lock(shared_mutex& mutex)
|
|
|
|
: m_mutex(mutex)
|
|
|
|
{
|
|
|
|
m_mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~writer_lock()
|
|
|
|
{
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
};
|