| |
| |
| |
| |
| |
|
|
| #ifndef MAMBA_UTIL_SYNCHRONIZED_VALUE_HPP |
| #define MAMBA_UTIL_SYNCHRONIZED_VALUE_HPP |
|
|
| #include <concepts> |
| #include <functional> |
| #include <mutex> |
| #include <shared_mutex> |
| #include <tuple> |
| #include <utility> |
|
|
| namespace mamba::util |
| { |
| |
| |
| |
|
|
|
|
| template <class L, template <class...> class U> |
| struct is_type_instance_of : std::false_type |
| { |
| }; |
|
|
| template <template <class...> class L, template <class...> class U, class... T> |
| requires std::same_as<L<T...>, U<T...>> |
| struct is_type_instance_of<L<T...>, U> : std::true_type |
| { |
| }; |
|
|
| |
| |
| template <class T, template <class...> class U> |
| constexpr bool is_type_instance_of_v = is_type_instance_of<T, U>::value; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| template <class T, class U> |
| concept weakly_equality_comparable_with = requires( |
| const std::remove_reference_t<T>& t, |
| const std::remove_reference_t<U>& u |
| ) { |
| { t == u } -> std::convertible_to<bool>; |
| { t != u } -> std::convertible_to<bool>; |
| { u == t } -> std::convertible_to<bool>; |
| { u != t } -> std::convertible_to<bool>; |
| }; |
|
|
| template <class T, class U> |
| concept weakly_assignable_from = requires(T t, U&& u) { t = std::forward<U>(u); }; |
|
|
|
|
| |
|
|
|
|
| |
| template <class T> |
| concept BasicLockable = requires(T& x) { |
| x.lock(); |
| x.unlock(); |
| }; |
| |
|
|
| |
| template <class T> |
| concept Lockable = BasicLockable<T> and requires(T& x) { |
| { x.try_lock() } -> std::convertible_to<bool>; |
| }; |
|
|
| |
| template <class T> |
| concept Mutex = Lockable<T> and std::default_initializable<T> and std::destructible<T> |
| and (not std::movable<T>) and (not std::copyable<T>); |
|
|
| |
| template <class T> |
| concept SharedMutex = Mutex<T> and requires(T& x) { |
| x.lock_shared(); |
| { x.try_lock_shared() } -> std::convertible_to<bool>; |
| x.unlock_shared(); |
| }; |
|
|
| |
| |
| |
| template <Mutex M> |
| [[nodiscard]] |
| auto lock_as_readonly(M& mutex) |
| { |
| return std::unique_lock{ mutex }; |
| } |
|
|
| template <SharedMutex M> |
| [[nodiscard]] |
| auto lock_as_readonly(M& mutex) |
| { |
| return std::shared_lock{ mutex }; |
| } |
|
|
| |
| |
| |
| |
| |
| template <Mutex... M> |
| requires(sizeof...(M) > 1) and (SharedMutex<M> or ...) |
| [[nodiscard]] auto lock_as_readonly(M&... mutex) |
| { |
| return std::make_tuple(lock_as_readonly(mutex)...); |
| } |
|
|
| |
| |
| |
| |
| template <Mutex... M> |
| requires(sizeof...(M) > 1) and ((not SharedMutex<M>) and ...) |
| [[nodiscard]] auto lock_as_readonly(M&... mutex) |
| { |
| return std::scoped_lock{ mutex... }; |
| } |
|
|
| |
| |
| |
| template <Mutex M> |
| [[nodiscard]] |
| auto lock_as_exclusive(M& mutex) |
| { |
| return std::unique_lock{ mutex }; |
| } |
|
|
| |
| |
| |
| template <Mutex... M> |
| requires(sizeof...(M) > 1) |
| [[nodiscard]] |
| auto lock_as_exclusive(M&... mutex) |
| { |
| return std::scoped_lock{ mutex... }; |
| } |
|
|
| namespace details |
| { |
| template <typename T> |
| T& ref_of(); |
| } |
|
|
| |
| |
| |
| template <Mutex M, bool readonly> |
| using lock_type = std::conditional_t< |
| readonly, |
| decltype(lock_as_readonly(details::ref_of<M>())), |
| decltype(lock_as_exclusive(details::ref_of<M>()))>; |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <std::default_initializable T, Mutex M, bool readonly> |
| class [[nodiscard]] scoped_locked_ptr |
| { |
| std::conditional_t<readonly, const T*, T*> m_value; |
| lock_type<M, readonly> m_lock; |
|
|
| public: |
|
|
| static constexpr bool is_readonly = readonly; |
|
|
| |
| |
| |
| scoped_locked_ptr(T& value, M& mutex) |
| requires(not readonly) |
| : m_value(&value) |
| , m_lock(mutex) |
| { |
| } |
|
|
| |
| |
| |
| scoped_locked_ptr(const T& value, M& mutex) |
| requires(readonly) |
| : m_value(&value) |
| , m_lock(mutex) |
| { |
| } |
|
|
| scoped_locked_ptr(scoped_locked_ptr&& other) noexcept |
| |
| : m_value(std::move(other.m_value)) |
| , m_lock(std::move(other.m_lock)) |
| { |
| other.m_value = nullptr; |
| } |
|
|
| scoped_locked_ptr& operator=(scoped_locked_ptr&& other) noexcept |
| { |
| |
| m_value = std::move(other.m_value); |
| other.m_value = nullptr; |
| return *this; |
| } |
|
|
| [[nodiscard]] auto operator*() -> T& requires(not readonly) { return *m_value; } |
| [[nodiscard]] auto operator*() const -> const T& |
| { |
| return *m_value; |
| } |
|
|
| [[nodiscard]] auto |
| operator->() -> T* requires(not readonly) { return m_value; } |
| [[nodiscard]] auto operator->() const -> const T* |
| { |
| return m_value; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <std::default_initializable T, Mutex M = std::mutex> |
| class synchronized_value |
| { |
| public: |
|
|
| using value_type = T; |
| using mutex_type = M; |
| using this_type = synchronized_value<T, M>; |
|
|
| synchronized_value() noexcept(std::is_nothrow_default_constructible_v<T>); |
|
|
| |
| template <typename V> |
| requires(not std::same_as<T, std::decay_t<V>>) |
| and (not std::same_as<this_type, std::decay_t<V>>) |
| and (not is_type_instance_of_v<std::decay_t<V>, synchronized_value>) |
| and weakly_assignable_from<T&, V> |
| synchronized_value(V&& value) noexcept |
| : m_value(std::forward<V>(value)) |
| { |
| |
| |
| |
| |
| } |
|
|
| |
| |
| |
| synchronized_value(T value) noexcept; |
|
|
| |
| template <typename V> |
| requires std::constructible_from<T, std::initializer_list<V>> |
| synchronized_value(std::initializer_list<V> values); |
|
|
| |
| |
| |
| |
| |
| |
| synchronized_value(const synchronized_value& other); |
|
|
| |
| |
| |
| |
| |
| synchronized_value(synchronized_value&& other) noexcept; |
|
|
| |
| |
| |
| |
| |
| |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and std::constructible_from<T, U> |
| synchronized_value(const synchronized_value<U, OtherMutex>& other); |
|
|
| |
| |
| |
| |
| |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and std::constructible_from<T, U&&> |
| synchronized_value(synchronized_value<U, OtherMutex>&& other) noexcept; |
|
|
| |
| |
| |
| |
| |
| |
| auto operator=(const synchronized_value& other) -> synchronized_value&; |
|
|
| |
| |
| |
| |
| |
| |
| auto operator=(synchronized_value&& other) noexcept -> synchronized_value&; |
|
|
| |
| |
| |
| |
| |
| |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and weakly_assignable_from<T&, U> |
| auto operator=(const synchronized_value<U, OtherMutex>& other) -> synchronized_value&; |
|
|
|
|
| |
| |
| |
| |
| |
| |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and weakly_assignable_from<T&, U&&> |
| auto operator=(synchronized_value<U, OtherMutex>&& other) noexcept -> synchronized_value&; |
|
|
| |
| |
| |
| template <typename V> |
| requires(not std::same_as<T, std::decay_t<V>>) |
| and (not std::same_as<this_type, std::decay_t<V>>) |
| and (not is_type_instance_of_v<std::decay_t<V>, synchronized_value>) |
| and weakly_assignable_from<T&, V> |
| auto operator=(V&& value) noexcept -> synchronized_value& |
| { |
| |
| |
| |
| |
| auto _ = lock_as_exclusive(m_mutex); |
| m_value = std::forward<V>(value); |
| return *this; |
| } |
|
|
| |
| |
| |
| |
| |
| auto operator=(const T& value) noexcept -> synchronized_value&; |
|
|
| |
| |
| |
| |
| [[nodiscard]] |
| auto value() const -> T; |
|
|
| |
| |
| |
| |
| [[nodiscard]] |
| explicit operator T() const |
| { |
| return value(); |
| } |
|
|
| |
| |
| |
| [[nodiscard]] |
| auto unsafe_get() const -> const T& |
| { |
| return m_value; |
| } |
|
|
| |
| |
| |
| [[nodiscard]] |
| auto unsafe_get() -> T& |
| { |
| return m_value; |
| } |
|
|
| using locked_ptr = scoped_locked_ptr<T, M, false>; |
| using const_locked_ptr = scoped_locked_ptr<T, M, true>; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] |
| auto operator->() -> locked_ptr; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] |
| auto operator->() const -> const_locked_ptr; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] |
| auto synchronize() -> locked_ptr; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] |
| auto synchronize() const -> const_locked_ptr; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto apply(Func&& func, Args&&... args); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto apply(Func&& func, Args&&... args) const; |
|
|
| |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto operator()(Func&& func, Args&&... args) |
| { |
| return apply(std::forward<Func>(func), std::forward<Args>(args)...); |
| } |
|
|
| |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto operator()(Func&& func, Args&&... args) const |
| { |
| return apply(std::forward<Func>(func), std::forward<Args>(args)...); |
| } |
|
|
| |
| |
| |
| |
| auto operator==(const weakly_equality_comparable_with<T> auto& other_value) const -> bool; |
|
|
| |
| |
| |
| template <weakly_equality_comparable_with<T> U, Mutex OtherMutex> |
| auto operator==(const synchronized_value<U, OtherMutex>& other_value) const -> bool; |
|
|
| auto swap(synchronized_value& other) -> void; |
| auto swap(T& value) -> void; |
|
|
| private: |
|
|
| T m_value; |
| mutable M m_mutex; |
|
|
| template <std::default_initializable, Mutex> |
| friend class synchronized_value; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename... SynchronizedValues> |
| requires(is_type_instance_of_v<std::remove_cvref_t<SynchronizedValues>, synchronized_value> and ...) |
| auto synchronize(SynchronizedValues&&... sync_values); |
|
|
| |
| |
|
|
| template <std::default_initializable T, Mutex M> |
| synchronized_value<T, M>::synchronized_value() noexcept(std::is_nothrow_default_constructible_v<T>) = default; |
|
|
| template <std::default_initializable T, Mutex M> |
| synchronized_value<T, M>::synchronized_value(T value) noexcept |
| : m_value(std::move(value)) |
| { |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| synchronized_value<T, M>::synchronized_value(const synchronized_value& other) |
| { |
| auto _ = lock_as_readonly(other.m_mutex); |
| m_value = other.m_value; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| synchronized_value<T, M>::synchronized_value(synchronized_value&& other) noexcept |
| { |
| auto _ = lock_as_exclusive(other.m_mutex); |
| m_value = std::move(other.m_value); |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and std::constructible_from<T, U> |
| synchronized_value<T, M>::synchronized_value(const synchronized_value<U, OtherMutex>& other) |
| { |
| auto _ = lock_as_readonly(other.m_mutex); |
| m_value = other.m_value; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and std::constructible_from<T, U&&> |
| synchronized_value<T, M>::synchronized_value(synchronized_value<U, OtherMutex>&& other) noexcept |
| { |
| auto _ = lock_as_exclusive(other.m_mutex); |
| m_value = std::move(other.m_value); |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::operator=(const synchronized_value& other) -> synchronized_value& |
| { |
| auto this_lock [[maybe_unused]] = lock_as_exclusive(m_mutex); |
| auto other_lock [[maybe_unused]] = lock_as_readonly(other.m_mutex); |
| m_value = other.m_value; |
| return *this; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::operator=(synchronized_value&& other) noexcept |
| -> synchronized_value& |
| { |
| auto _ = lock_as_exclusive(other.m_mutex, m_mutex); |
| m_value = std::move(other.m_value); |
| return *this; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and weakly_assignable_from<T&, U> |
| auto synchronized_value<T, M>::operator=(const synchronized_value<U, OtherMutex>& other) |
| -> synchronized_value<T, M>& |
| { |
| auto this_lock [[maybe_unused]] = lock_as_exclusive(m_mutex); |
| auto other_lock [[maybe_unused]] = lock_as_readonly(other.m_mutex); |
| m_value = other.m_value; |
| return *this; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <std::default_initializable U, Mutex OtherMutex> |
| requires(not std::same_as<synchronized_value<T, M>, synchronized_value<U, OtherMutex>>) |
| and weakly_assignable_from<T&, U&&> |
| auto synchronized_value<T, M>::operator=(synchronized_value<U, OtherMutex>&& other) noexcept |
| -> synchronized_value<T, M>& |
| { |
| auto _ = lock_as_exclusive(other.m_mutex, m_mutex); |
| m_value = std::move(other.m_value); |
| return *this; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::operator=(const T& value) noexcept -> synchronized_value& |
| { |
| auto _ = lock_as_exclusive(m_mutex); |
| m_value = value; |
| return *this; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <typename V> |
| requires std::constructible_from<T, std::initializer_list<V>> |
| synchronized_value<T, M>::synchronized_value(std::initializer_list<V> values) |
| : m_value(std::move(values)) |
| { |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::value() const -> T |
| { |
| auto _ = lock_as_readonly(m_mutex); |
| return m_value; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::operator->() -> locked_ptr |
| { |
| return locked_ptr{ m_value, m_mutex }; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::operator->() const -> const_locked_ptr |
| { |
| return const_locked_ptr{ m_value, m_mutex }; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::synchronize() -> locked_ptr |
| { |
| return locked_ptr{ m_value, m_mutex }; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::synchronize() const -> const_locked_ptr |
| { |
| return const_locked_ptr{ m_value, m_mutex }; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto synchronized_value<T, M>::apply(Func&& func, Args&&... args) |
| { |
| auto _ = lock_as_exclusive(m_mutex); |
| return std::invoke(std::forward<Func>(func), m_value, std::forward<Args>(args)...); |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <typename Func, typename... Args> |
| requires std::invocable<Func, T&, Args...> |
| auto synchronized_value<T, M>::apply(Func&& func, Args&&... args) const |
| { |
| auto _ = lock_as_readonly(m_mutex); |
| return std::invoke(std::forward<Func>(func), std::as_const(m_value), std::forward<Args>(args)...); |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto |
| synchronized_value<T, M>::operator==(const weakly_equality_comparable_with<T> auto& other_value |
| ) const -> bool |
| { |
| auto _ = lock_as_readonly(m_mutex); |
| return m_value == other_value; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| template <weakly_equality_comparable_with<T> U, Mutex OtherMutex> |
| auto |
| synchronized_value<T, M>::operator==(const synchronized_value<U, OtherMutex>& other_value) const |
| -> bool |
| { |
| auto _ = lock_as_readonly(m_mutex, other_value.m_mutex); |
| return m_value == other_value.m_value; |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::swap(synchronized_value& other) -> void |
| { |
| auto _ = lock_as_exclusive(m_mutex, other.m_mutex); |
| using std::swap; |
| swap(m_value, other.m_value); |
| } |
|
|
| template <std::default_initializable T, Mutex M> |
| auto synchronized_value<T, M>::swap(T& value) -> void |
| { |
| auto _ = lock_as_exclusive(m_mutex); |
| using std::swap; |
| swap(m_value, value); |
| } |
|
|
| template <typename... SynchronizedValues> |
| requires(is_type_instance_of_v<std::remove_cvref_t<SynchronizedValues>, synchronized_value> and ...) |
| auto synchronize(SynchronizedValues&&... sync_values) |
| { |
| return std::make_tuple(std::forward<SynchronizedValues>(sync_values).synchronize()...); |
| } |
| } |
|
|
| #endif |
|
|