include/Cnx/sync/SharedMutex.h file

CnxSharedMutex provides several higher-level reader-writer mutex types similar to those provided in C++'s <shared_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 CnxSharedMutex
CnxSharedMutex is the generic (it's not timed or recursive) reader-writer mutex provided by Cnx. It's directly comparable to C++'s std::shared_mutex and is suitable for any situation where a reader-writer mutex is necessary.
struct CnxSharedTimedMutex
CnxSharedTimedMutex is a higher-level reader-writer mutex type for use when timed backoff of mutex locking is required, and is directly comparable to C++'s std::shared_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 reader-writer synchronization, but blocking for longer than X milliseconds when trying to acquire the lock would be problematic, a CnxSharedTimedMutex would be the appropriate mutex type to use.

Functions

CnxSharedMutex cnx_shared_mutex_new(void)
Creates a new shared mutex.
void cnx_shared_mutex_lock(CnxSharedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex exclusively (aka for writing)
bool cnx_shared_mutex_try_lock(CnxSharedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex exclusively (aka for writing)
void cnx_shared_mutex_lock_shared(CnxSharedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex non-exclusively (shared, aka for reading)
bool cnx_shared_mutex_try_lock_shared(CnxSharedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex non-exclusively (shared, aka for reading)
void cnx_shared_mutex_unlock(CnxSharedMutex*restrict mutex)
Unlocks the exclusively locked mutex pointed to by mutex
void cnx_shared_mutex_unlock_shared(CnxSharedMutex*restrict mutex)
Unlocks the non-exclusively (aka shared) locked mutex pointed to by mutex
void cnx_shared_mutex_free(CnxSharedMutex*restrict mutex)
Destroys the mutex pointed to by mutex
CnxSharedTimedMutex cnx_shared_timed_mutex_new(void)
Creates a new shared timed mutex.
void cnx_shared_timed_mutex_lock(CnxSharedTimedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex exclusively (aka for writing)
bool cnx_shared_timed_mutex_try_lock(CnxSharedTimedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex exclusively (aka for writing)
bool cnx_shared_timed_mutex_try_lock_for(CnxSharedTimedMutex*restrict mutex, CnxDuration duration)
Attempts to lock the mutex pointed to by mutex exclusively (aka for writing)
bool cnx_shared_timed_mutex_try_lock_until(CnxSharedTimedMutex*restrict mutex, CnxTimePoint stop_point)
Attempts to lock the mutex pointed to by mutex exclusively (aka for writing)
void cnx_shared_timed_mutex_lock_shared(CnxSharedTimedMutex*restrict mutex)
Unconditionally locks the mutex pointed to by mutex non-exclusively (shared, aka for reading)
bool cnx_shared_timed_mutex_try_lock_shared(CnxSharedTimedMutex*restrict mutex)
Attempts to lock the mutex pointed to by mutex non-exclusively (shared, aka for reading)
bool cnx_shared_timed_mutex_try_lock_shared_for(CnxSharedTimedMutex*restrict mutex, CnxDuration duration)
Attempts to lock the mutex pointed to by mutex non-exclusively (aka shared, for reading)
bool cnx_shared_timed_mutex_try_lock_shared_until(CnxSharedTimedMutex*restrict mutex, CnxTimePoint stop_point)
Attempts to lock the mutex pointed to by mutex non-exclusively (aka shared, for reading)
void cnx_shared_timed_mutex_unlock(CnxSharedTimedMutex*restrict mutex)
Unlocks the exclusively locked mutex pointed to by mutex
void cnx_shared_timed_mutex_unlock_shared(CnxSharedTimedMutex*restrict mutex)
Unlocks the non-exclusively (aka shared) locked mutex pointed to by mutex
void cnx_shared_timed_mutex_free(CnxSharedTimedMutex*restrict mutex)
Destroys the mutex pointed to by mutex