13#include <shared_mutex>
15#include <condition_variable>
17#if defined(__cpp_lib_barrier)
21#if defined(__cpp_lib_latch)
25#if defined(__cpp_lib_semaphore)
29#if __has_include(<stop_token>)
271 using super = std::unique_lock<Mutex>;
292 using super = std::shared_lock<Mutex>;
323 if(!
mLock.owns_lock()) {
330 if(!
mLock.owns_lock()) {
336#if defined(__cpp_lib_semaphore)
381#if defined(__cpp_lib_latch)
393 using Latch = std::latch;
396#if defined(__cpp_lib_barrier)
449#if __has_include(<stop_token>)
502 template<
typename T,
typename Mutex>
class LockedObject;
538 template<
typename... Args>
544 template<
typename OtherMutex>
558 with([&other](
T& value)
mutable { value->mValue = std::move(other); });
572 with([&other](
T& value)
mutable { value.mValue = std::move(other); });
588 template<
typename Func>
decltype(
auto)
with(
Func&& func) {
594 template<
typename Func>
decltype(
auto)
operator|(
Func&& func) {
return with(std::forward<Func>(func)); }
597 template<
typename Func>
decltype(
auto)
operator()(
Func&& func) {
return with(std::forward<Func>(func)); }
637 template<
typename Func>
decltype(
auto)
with(
Func&& func)
const {
643 template<
typename Func>
decltype(
auto)
operator|(
Func&& func)
const {
return with(std::forward<Func>(func)); }
646 template<
typename Func>
decltype(
auto)
operator()(
Func&& func)
const {
return with(std::forward<Func>(func)); }
663 return with([](
const T& value) {
return T(value); });
669 return func(
objects.mValue...);
674 return object.with(std::forward<Func>(func));
679 return object.with(std::forward<Func>(func));
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
The class Lock is a mutex wrapper that provides a convenient RAII-style mechanism for owning one or m...
Definition Threading.hpp:246
std::scoped_lock< Mutexes... > super
Definition Threading.hpp:247
A helper class that acquires a temporary lock and overloads operator-> to allow access to the object.
Definition Threading.hpp:601
U * operator->() const noexcept
Access the LockedObject value from a temporary lock.
Definition Threading.hpp:613
U & operator*() const noexcept
Access the LockedObject value from a temporary lock.
Definition Threading.hpp:616
UniqueLock< Mutex > mLock
The temporary lock object.
Definition Threading.hpp:606
TemporaryLock(LockedObject< T, Mutex > &owner) noexcept
Creates a new temporary lock object for owner.
Definition Threading.hpp:610
LockedObject< T, Mutex > & mOwner
The temporary lock owner,.
Definition Threading.hpp:603
A helper class that wraps and protects a value from non-synchronized access.
Definition Threading.hpp:516
LockedObject & operator=(LockedObject< T, OtherMutex > &&other) noexcept
Assigns the LockedObject by moving the value of another.
Definition Threading.hpp:564
T ValueType
The type of value used by the LockedObject instance.
Definition Threading.hpp:521
auto lock()
Access the LockedObject contents by acquiring a temporary lock.
Definition Threading.hpp:623
Mutex mMutex
The mutex guarding the value.
Definition Threading.hpp:531
auto operator->()
Executes a function Func within a synchronized block by locking all objects.
Definition Threading.hpp:626
decltype(auto) with(Func &&func) const
Executes a function Func within a synchronized block.
Definition Threading.hpp:637
LockedObject(const LockedObject< T, OtherMutex > &other)
Creates a new LockedObject by copying the value of another.
Definition Threading.hpp:545
auto operator->() const
Executes a function Func within a synchronized block by locking all objects.
Definition Threading.hpp:654
Mutex MutexType
The type of mutex used by the LockedObject instance.
Definition Threading.hpp:524
decltype(auto) with(Func &&func)
Executes a function Func within a synchronized block.
Definition Threading.hpp:588
auto lock() const
Access the LockedObject contents by acquiring a temporary lock.
Definition Threading.hpp:649
friend decltype(auto) lock(Func &&func, Ts &... objects)
Executes a function Func within a synchronized block by locking all objects.
Definition Threading.hpp:667
LockedObject & operator=(const T &other)
Assigns the LockedObject by copying a value.
Definition Threading.hpp:557
auto operator*() const
Executes a function Func within a synchronized block by locking all objects.
Definition Threading.hpp:657
LockedObject & operator=(T &&other) noexcept
Assigns the LockedObject by copying a value.
Definition Threading.hpp:571
auto operator*()
Executes a function Func within a synchronized block by locking all objects.
Definition Threading.hpp:629
T mValue
The value that is locked and guarded by the mutex.
Definition Threading.hpp:528
LockedObject & operator=(const LockedObject< T, OtherMutex > &other)
Assigns the LockedObject by copying the value of another.
Definition Threading.hpp:550
T copy() const
Returns a copy of the locked object.
Definition Threading.hpp:662
The class SharedLock is a general-purpose shared mutex ownership wrapper allowing deferred locking,...
Definition Threading.hpp:291
std::shared_lock< Mutex > super
Definition Threading.hpp:292
The class UniqueLock is a general-purpose mutex ownership wrapper allowing deferred locking,...
Definition Threading.hpp:270
std::unique_lock< Mutex > super
Definition Threading.hpp:271
A concept that checks if the type T is a LockedObject.
Definition Threading.hpp:507
Definition Application.hpp:19
std::recursive_mutex RecursiveMutex
The RecursiveMutex class is a synchronization primitive that can be used to protect shared data from ...
Definition Threading.hpp:109
std::thread Thread
The class thread represents a single thread of execution.
Definition Threading.hpp:52
decltype(auto) lock(Func &&func, Ts &... objects)
Definition Threading.hpp:667
std::shared_mutex SharedMutex
The SharedMutex class is a synchronization primitive that can be used to protect shared data from bei...
Definition Threading.hpp:153
std::timed_mutex TimedMutex
The TimedMutex class is a synchronization primitive that can be used to protect shared data from bein...
Definition Threading.hpp:86
std::mutex Mutex
The Mutex class is a synchronization primitive that can be used to protect shared data from being sim...
Definition Threading.hpp:73
std::recursive_timed_mutex RecursiveTimedMutex
The RecursiveTimedMutex class is a synchronization primitive that can be used to protect shared data ...
Definition Threading.hpp:123
std::condition_variable ConditionVariable
The condition_variable class is a synchronization primitive that can be used to block a thread,...
Definition Threading.hpp:228
std::shared_timed_mutex SharedTimedMutex
The SharedTimedMutex class is a synchronization primitive that can be used to protect shared data fro...
Definition Threading.hpp:174
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
A fake lockable type that performs no locking.
Definition Threading.hpp:178
constexpr void unlock() noexcept
Definition Threading.hpp:181
constexpr void lock() noexcept
Definition Threading.hpp:179
constexpr bool try_lock_for(Duration) noexcept
Definition Threading.hpp:182
constexpr bool try_lock() noexcept
Definition Threading.hpp:180
constexpr bool try_lock_until(TimeLimit) noexcept
Definition Threading.hpp:183
An object that temporarily unlocks a lock.
Definition Threading.hpp:299
Relocker & operator=(Relocker &&other) noexcept=delete
Relocker(const Relocker &)=delete
Copy constructor is deleted.
~Relocker()
Destroy the relocker.
Definition Threading.hpp:322
void lock()
Lock the underlying lock.
Definition Threading.hpp:329
Relocker(Relocker &&other) noexcept=delete
Relocker & operator=(const Relocker &)=delete
Copy assignment is deleted.
Lock & mLock
The underlying lock.
Definition Threading.hpp:301
Relocker(Lock &lock)
Construct a relocker.
Definition Threading.hpp:309