2019-09-05 01:57:56 +03:00
|
|
|
// shared_mutex standard header
|
|
|
|
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
|
|
|
#ifndef _SHARED_MUTEX_
|
|
|
|
#define _SHARED_MUTEX_
|
|
|
|
#include <yvals_core.h>
|
|
|
|
#if _STL_COMPILER_PREPROCESSOR
|
|
|
|
|
|
|
|
#ifdef _M_CEE_PURE
|
|
|
|
#error <shared_mutex> is not supported when compiling with /clr:pure.
|
2023-08-11 02:48:21 +03:00
|
|
|
#endif // defined(_M_CEE_PURE)
|
2019-09-05 01:57:56 +03:00
|
|
|
|
2022-11-08 05:45:18 +03:00
|
|
|
#include <condition_variable>
|
2019-09-05 01:57:56 +03:00
|
|
|
#include <mutex>
|
|
|
|
#include <xthreads.h>
|
|
|
|
|
|
|
|
#pragma pack(push, _CRT_PACKING)
|
|
|
|
#pragma warning(push, _STL_WARNING_LEVEL)
|
|
|
|
#pragma warning(disable : _STL_DISABLED_WARNINGS)
|
|
|
|
_STL_DISABLE_CLANG_WARNINGS
|
|
|
|
#pragma push_macro("new")
|
|
|
|
#undef new
|
|
|
|
|
|
|
|
_STD_BEGIN
|
2022-09-20 00:47:02 +03:00
|
|
|
_EXPORT_STD class shared_mutex { // class for mutual exclusion shared across threads
|
2019-09-05 01:57:56 +03:00
|
|
|
public:
|
|
|
|
using native_handle_type = _Smtx_t*;
|
|
|
|
|
2023-02-03 03:34:51 +03:00
|
|
|
shared_mutex() = default;
|
|
|
|
shared_mutex(const shared_mutex&) = delete;
|
|
|
|
shared_mutex& operator=(const shared_mutex&) = delete;
|
2019-09-05 01:57:56 +03:00
|
|
|
|
|
|
|
void lock() noexcept /* strengthened */ { // lock exclusive
|
|
|
|
_Smtx_lock_exclusive(&_Myhandle);
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock() noexcept /* strengthened */ { // try to lock exclusive
|
2019-09-05 01:57:56 +03:00
|
|
|
return _Smtx_try_lock_exclusive(&_Myhandle) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() noexcept /* strengthened */ { // unlock exclusive
|
|
|
|
_Smtx_unlock_exclusive(&_Myhandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock_shared() noexcept /* strengthened */ { // lock non-exclusive
|
|
|
|
_Smtx_lock_shared(&_Myhandle);
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared() noexcept /* strengthened */ { // try to lock non-exclusive
|
2019-09-05 01:57:56 +03:00
|
|
|
return _Smtx_try_lock_shared(&_Myhandle) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_shared() noexcept /* strengthened */ { // unlock non-exclusive
|
|
|
|
_Smtx_unlock_shared(&_Myhandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
_NODISCARD native_handle_type native_handle() noexcept /* strengthened */ { // get native handle
|
|
|
|
return &_Myhandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-03 03:34:51 +03:00
|
|
|
_Smtx_t _Myhandle = nullptr;
|
2019-09-05 01:57:56 +03:00
|
|
|
};
|
|
|
|
|
2022-09-20 00:47:02 +03:00
|
|
|
_EXPORT_STD class shared_timed_mutex { // class for mutual exclusion shared across threads
|
2022-04-04 23:13:16 +03:00
|
|
|
private:
|
2019-09-05 01:57:56 +03:00
|
|
|
using _Read_cnt_t = unsigned int;
|
|
|
|
|
|
|
|
static constexpr _Read_cnt_t _Max_readers = static_cast<_Read_cnt_t>(-1);
|
|
|
|
|
|
|
|
public:
|
2023-02-14 03:23:21 +03:00
|
|
|
shared_timed_mutex() = default;
|
2019-09-05 01:57:56 +03:00
|
|
|
|
2023-02-03 03:34:51 +03:00
|
|
|
shared_timed_mutex(const shared_timed_mutex&) = delete;
|
|
|
|
shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
|
2019-09-05 01:57:56 +03:00
|
|
|
|
|
|
|
void lock() { // lock exclusive
|
|
|
|
unique_lock<mutex> _Lock(_Mymtx);
|
|
|
|
while (_Writing) {
|
|
|
|
_Write_queue.wait(_Lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
_Writing = true;
|
|
|
|
while (0 < _Readers) {
|
|
|
|
_Read_queue.wait(_Lock); // wait for writing, no readers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock() { // try to lock exclusive
|
2019-09-05 01:57:56 +03:00
|
|
|
lock_guard<mutex> _Lock(_Mymtx);
|
|
|
|
if (_Writing || 0 < _Readers) {
|
|
|
|
return false;
|
|
|
|
} else { // set writing, no readers
|
|
|
|
_Writing = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_for(
|
|
|
|
const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration
|
2020-10-17 09:13:42 +03:00
|
|
|
return try_lock_until(_To_absolute_time(_Rel_time));
|
2019-09-05 01:57:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
|
2021-03-03 00:12:29 +03:00
|
|
|
// try to lock until time point
|
2024-04-19 04:46:44 +03:00
|
|
|
static_assert(chrono::_Is_clock_v<_Clock>, "Clock type required");
|
2019-09-05 01:57:56 +03:00
|
|
|
auto _Not_writing = [this] { return !_Writing; };
|
|
|
|
auto _Zero_readers = [this] { return _Readers == 0; };
|
|
|
|
unique_lock<mutex> _Lock(_Mymtx);
|
|
|
|
|
|
|
|
if (!_Write_queue.wait_until(_Lock, _Abs_time, _Not_writing)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Writing = true;
|
|
|
|
|
|
|
|
if (!_Read_queue.wait_until(_Lock, _Abs_time, _Zero_readers)) { // timeout, leave writing state
|
|
|
|
_Writing = false;
|
|
|
|
_Lock.unlock(); // unlock before notifying, for efficiency
|
|
|
|
_Write_queue.notify_all();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() { // unlock exclusive
|
|
|
|
{ // unlock before notifying, for efficiency
|
|
|
|
lock_guard<mutex> _Lock(_Mymtx);
|
|
|
|
|
|
|
|
_Writing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Write_queue.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock_shared() { // lock non-exclusive
|
|
|
|
unique_lock<mutex> _Lock(_Mymtx);
|
|
|
|
while (_Writing || _Readers == _Max_readers) {
|
|
|
|
_Write_queue.wait(_Lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
++_Readers;
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared() { // try to lock non-exclusive
|
2019-09-05 01:57:56 +03:00
|
|
|
lock_guard<mutex> _Lock(_Mymtx);
|
|
|
|
if (_Writing || _Readers == _Max_readers) {
|
|
|
|
return false;
|
|
|
|
} else { // count another reader
|
|
|
|
++_Readers;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& _Rel_time) {
|
2021-03-03 00:12:29 +03:00
|
|
|
// try to lock non-exclusive for relative time
|
2020-10-17 09:13:42 +03:00
|
|
|
return try_lock_shared_until(_To_absolute_time(_Rel_time));
|
2019-09-05 01:57:56 +03:00
|
|
|
}
|
|
|
|
|
2023-06-15 11:55:06 +03:00
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
|
|
|
|
// try to lock non-exclusive until absolute time
|
2024-04-19 04:46:44 +03:00
|
|
|
static_assert(chrono::_Is_clock_v<_Clock>, "Clock type required");
|
2019-09-05 01:57:56 +03:00
|
|
|
const auto _Can_acquire = [this] { return !_Writing && _Readers < _Max_readers; };
|
|
|
|
|
|
|
|
unique_lock<mutex> _Lock(_Mymtx);
|
|
|
|
|
|
|
|
if (!_Write_queue.wait_until(_Lock, _Abs_time, _Can_acquire)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++_Readers;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_shared() { // unlock non-exclusive
|
|
|
|
_Read_cnt_t _Local_readers;
|
|
|
|
bool _Local_writing;
|
|
|
|
|
|
|
|
{ // unlock before notifying, for efficiency
|
|
|
|
lock_guard<mutex> _Lock(_Mymtx);
|
|
|
|
--_Readers;
|
|
|
|
_Local_readers = _Readers;
|
|
|
|
_Local_writing = _Writing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_Local_writing && _Local_readers == 0) {
|
|
|
|
_Read_queue.notify_one();
|
|
|
|
} else if (!_Local_writing && _Local_readers == _Max_readers - 1) {
|
|
|
|
_Write_queue.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-03 03:34:51 +03:00
|
|
|
mutex _Mymtx{};
|
|
|
|
condition_variable _Read_queue{};
|
|
|
|
condition_variable _Write_queue{};
|
|
|
|
_Read_cnt_t _Readers = 0;
|
|
|
|
bool _Writing = false;
|
2019-09-05 01:57:56 +03:00
|
|
|
};
|
|
|
|
|
2022-09-20 00:47:02 +03:00
|
|
|
_EXPORT_STD template <class _Mutex>
|
2020-12-04 02:59:35 +03:00
|
|
|
class shared_lock { // shareable lock
|
2019-09-05 01:57:56 +03:00
|
|
|
public:
|
|
|
|
using mutex_type = _Mutex;
|
|
|
|
|
2023-02-03 03:34:51 +03:00
|
|
|
shared_lock() noexcept = default;
|
2019-09-05 01:57:56 +03:00
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_CTOR_LOCK explicit shared_lock(mutex_type& _Mtx)
|
2019-09-05 01:57:56 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) { // construct with mutex and lock shared
|
|
|
|
_Mtx.lock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_lock(mutex_type& _Mtx, defer_lock_t) noexcept
|
2020-05-15 01:58:54 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) {} // construct with unlocked mutex
|
2019-09-05 01:57:56 +03:00
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_CTOR_LOCK shared_lock(mutex_type& _Mtx, try_to_lock_t)
|
2020-05-15 01:58:54 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared()) {} // construct with mutex and try to lock shared
|
2019-09-05 01:57:56 +03:00
|
|
|
|
2023-02-14 03:23:21 +03:00
|
|
|
_NODISCARD_CTOR_LOCK shared_lock(mutex_type& _Mtx, adopt_lock_t) noexcept // strengthened
|
2020-05-15 01:58:54 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) {} // construct with mutex and adopt ownership
|
2019-09-05 01:57:56 +03:00
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_CTOR_LOCK shared_lock(mutex_type& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
|
2020-05-15 01:58:54 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared_for(_Rel_time)) {
|
|
|
|
// construct with mutex and try to lock for relative time
|
2019-09-05 01:57:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_CTOR_LOCK shared_lock(mutex_type& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
|
2020-05-15 01:58:54 +03:00
|
|
|
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared_until(_Abs_time)) {
|
|
|
|
// construct with mutex and try to lock until absolute time
|
2024-04-19 04:46:44 +03:00
|
|
|
static_assert(chrono::_Is_clock_v<_Clock>, "Clock type required");
|
2019-09-05 01:57:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~shared_lock() noexcept {
|
|
|
|
if (_Owns) {
|
|
|
|
_Pmtx->unlock_shared();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_CTOR_LOCK shared_lock(shared_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
|
2019-09-05 01:57:56 +03:00
|
|
|
_Other._Pmtx = nullptr;
|
|
|
|
_Other._Owns = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_lock& operator=(shared_lock&& _Right) noexcept {
|
|
|
|
if (_Owns) {
|
|
|
|
_Pmtx->unlock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
_Pmtx = _Right._Pmtx;
|
|
|
|
_Owns = _Right._Owns;
|
|
|
|
_Right._Pmtx = nullptr;
|
|
|
|
_Right._Owns = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-07-15 01:03:09 +03:00
|
|
|
shared_lock(const shared_lock&) = delete;
|
2019-09-05 01:57:56 +03:00
|
|
|
shared_lock& operator=(const shared_lock&) = delete;
|
|
|
|
|
|
|
|
void lock() { // lock the mutex
|
|
|
|
_Validate();
|
|
|
|
_Pmtx->lock_shared();
|
|
|
|
_Owns = true;
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock() { // try to lock the mutex
|
2019-09-05 01:57:56 +03:00
|
|
|
_Validate();
|
|
|
|
_Owns = _Pmtx->try_lock_shared();
|
|
|
|
return _Owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) {
|
2021-03-03 00:12:29 +03:00
|
|
|
// try to lock the mutex for _Rel_time
|
2019-09-05 01:57:56 +03:00
|
|
|
_Validate();
|
|
|
|
_Owns = _Pmtx->try_lock_shared_for(_Rel_time);
|
|
|
|
return _Owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
2022-09-01 02:06:54 +03:00
|
|
|
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
|
2021-03-03 00:12:29 +03:00
|
|
|
// try to lock the mutex until _Abs_time
|
2024-04-19 04:46:44 +03:00
|
|
|
static_assert(chrono::_Is_clock_v<_Clock>, "Clock type required");
|
2019-09-05 01:57:56 +03:00
|
|
|
_Validate();
|
|
|
|
_Owns = _Pmtx->try_lock_shared_until(_Abs_time);
|
|
|
|
return _Owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() { // try to unlock the mutex
|
|
|
|
if (!_Pmtx || !_Owns) {
|
|
|
|
_Throw_system_error(errc::operation_not_permitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
_Pmtx->unlock_shared();
|
|
|
|
_Owns = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(shared_lock& _Right) noexcept {
|
|
|
|
_STD swap(_Pmtx, _Right._Pmtx);
|
|
|
|
_STD swap(_Owns, _Right._Owns);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_type* release() noexcept {
|
|
|
|
_Mutex* _Res = _Pmtx;
|
|
|
|
_Pmtx = nullptr;
|
|
|
|
_Owns = false;
|
|
|
|
return _Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
_NODISCARD bool owns_lock() const noexcept {
|
|
|
|
return _Owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const noexcept {
|
|
|
|
return _Owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
_NODISCARD mutex_type* mutex() const noexcept {
|
|
|
|
return _Pmtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-03 03:34:51 +03:00
|
|
|
_Mutex* _Pmtx = nullptr;
|
|
|
|
bool _Owns = false;
|
2019-09-05 01:57:56 +03:00
|
|
|
|
|
|
|
void _Validate() const { // check if the mutex can be locked
|
|
|
|
if (!_Pmtx) {
|
|
|
|
_Throw_system_error(errc::operation_not_permitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_Owns) {
|
|
|
|
_Throw_system_error(errc::resource_deadlock_would_occur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-20 00:47:02 +03:00
|
|
|
_EXPORT_STD template <class _Mutex>
|
2019-09-05 01:57:56 +03:00
|
|
|
void swap(shared_lock<_Mutex>& _Left, shared_lock<_Mutex>& _Right) noexcept {
|
|
|
|
_Left.swap(_Right);
|
|
|
|
}
|
|
|
|
_STD_END
|
|
|
|
#pragma pop_macro("new")
|
|
|
|
_STL_RESTORE_CLANG_WARNINGS
|
|
|
|
#pragma warning(pop)
|
|
|
|
#pragma pack(pop)
|
|
|
|
#endif // _STL_COMPILER_PREPROCESSOR
|
|
|
|
#endif // _SHARED_MUTEX_
|