include/Cnx/sync/Mutex.h file

CnxMutex provides several higher-level mutex types similar to those provided in C++'s <mutex>

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Classes

struct CnxMutex
CnxMutex is the generic higher-level mutex type provided by Cnx. It is a simple mutex type suitable for general purpose use (ie, it is not timed, recursive, etc), and is directly comparable to C++'s std::mutex
struct CnxRecursiveMutex
CnxRecursiveMutex is a higher-level mutex type, directly comparable to C++'s std::recursive_mutex`, for use when an algorithm requires that a thread be able to lock the same mutex multiple times in its control flow. While such an algorithm would cause the thread to deadlock itself when used with a normal mutex, this is the intended task for a recursive mutex. For example, if a recursive algorithm requires synchronization using the same mutex at multiple levels of recursion, and one level of the algorithm can't release the lock before calling the next, a CnxRecursiveMutex would make this type of algorithm possible and prevent the thread from deadlocking itself.
struct CnxTimedMutex
CnxTimedMutex is a higher-level mutex type for use when timed backoff of mutex locking is required, and is directly comparable to C++'s std::timed_mutex. It allows for specifying a timeout, which if reached will cause execution to stop attempting to acquire the lock and signal failure to the caller, instead of blocking indefinitely until the lock was successfully acquired like a tradition mutex. For example, if an algorithm needs synchronization, but blocking for longer than X milliseconds when trying to acquire the lock would be problematic, a CnxTimedMutex would be the appropriate mutex type to use.
struct CnxRecursiveTimedMutex
CnxRecursiveTimedMutex is a higher-level mutex type for use when the properties of both a recursive and a timed mutex are necessary for the task at hand. It's directly comparable to C++'s std::recursive_timed_mutex. It allows for both specifying a timeout for lock acquisition as well as recursive lock acquisition on the same thread.
struct cnx_defer_lock_t
cnx_defer_lock_t is a tag type intended for use with scoped lock guards such as CnxUniqueLock and CnxSharedLock, to communicate that the guard should defer locking the given mutex until requested to do so. This is in contrast to the default behavior, where a lock guard will acquire the lock upon its construction.
struct cnx_adopt_lock_t
cnx_adopt_lock_t is a tag type intended for use with scoped lock guards such as CnxUniqueLock and CnxSharedLock, to communicate that the given mutex has already been locked and thus the guard does not need to acquire the lock itself. This is in contrast to the default behavior, where a lock guard will acquire the lock upon its construction.
struct cnx_try_lock_t
cnx_try_lock_t is a tag type intended for use with scoped lock guards such as CnxUniqueLock and CnxSharedLock, to communicate that the guard should only attempt to lock the given mutex (via try_lock), and not block if lock acquisition was unsuccessful. This is in contrast to the default behavior, where a lock guard will unconditionally acquire the lock upon its construction.

Functions

CnxMutex cnx_mutex_new(void)
Creates a new mutex.
void cnx_mutex_lock(CnxMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex
bool cnx_mutex_try_lock(CnxMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex
void cnx_mutex_unlock(CnxMutex*restrict mutex)
Unlocks the locked mutex pointed to by mutex
void cnx_mutex_free(CnxMutex*restrict mutex)
Destroys the mutex pointed to by mutex
CnxRecursiveMutex cnx_recursive_mutex_new(void)
Creates a new recursive mutex.
void cnx_recursive_mutex_lock(CnxRecursiveMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex
bool cnx_recursive_mutex_try_lock(CnxRecursiveMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex
void cnx_recursive_mutex_unlock(CnxRecursiveMutex*restrict mutex)
Unlocks the locked mutex pointed to by mutex
void cnx_recursive_mutex_free(CnxRecursiveMutex*restrict mutex)
Destroys the mutex pointed to by mutex
CnxTimedMutex cnx_timed_mutex_new(void)
Creates a new timed mutex.
void cnx_timed_mutex_lock(CnxTimedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex
bool cnx_timed_mutex_try_lock(CnxTimedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex
bool cnx_timed_mutex_try_lock_for(CnxTimedMutex*restrict mutex, CnxDuration duration)
Attempts to lock the mutex pointed to by mutex
bool cnx_timed_mutex_try_lock_until(CnxTimedMutex*restrict mutex, CnxTimePoint stop_point)
Attempts to lock the mutex pointed to by mutex
void cnx_timed_mutex_unlock(CnxTimedMutex*restrict mutex)
Unlocks the locked mutex pointed to by mutex
void cnx_timed_mutex_free(CnxTimedMutex*restrict mutex)
Destroys the mutex pointed to by mutex
CnxRecursiveTimedMutex cnx_recursive_timed_mutex_new(void)
Creates a new recursive timed mutex.
void cnx_recursive_timed_mutex_lock(CnxRecursiveTimedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex
bool cnx_recursive_timed_mutex_try_lock(CnxRecursiveTimedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex
bool cnx_recursive_timed_mutex_try_lock_for(CnxRecursiveTimedMutex*restrict mutex, CnxDuration duration)
Attempts to lock the mutex pointed to by mutex
bool cnx_recursive_timed_mutex_try_lock_until(CnxRecursiveTimedMutex*restrict mutex, CnxTimePoint stop_point)
Attempts to lock the mutex pointed to by mutex
void cnx_recursive_timed_mutex_unlock(CnxRecursiveTimedMutex*restrict mutex)
Unlocks the locked mutex pointed to by mutex
void cnx_recursive_timed_mutex_free(CnxRecursiveTimedMutex*restrict mutex)
Destroys the mutex pointed to by mutex

Variables

static let cnx_defer_lock
A global instantiation of cnx_defer_lock_t, provided for convenience.
static let cnx_adopt_lock
A global instantiation of cnx_adopt_lock_t, provided for convenience.
static let cnx_try_lock
A global instantiation of cnx_try_lock_t, provided for convenience.