Synchronization » CnxMutex module

CnxMutex provides several high-level mutex types similar to those provided in C++'s <mutex>, to make working with multi-threaded code simpler and easier.

Example:

// MyThing.h
#include <Cnx/sync/Mutex.h>
static MyType my_very_important_thing;
static CnxMutex* my_thing_mutex;

void init_my_thing(void);
void update_my_thing(u64 value);
u64 get_value_from_my_thing(void);

// MyThing.c
#include <Cnx/Allocators.h>
#include "MyThing.h"
void init_my_thing(void) {
    if(my_thing_mutex == nullptr) {
        my_thing_mutex = cnx_allocator_allocate_t(CnxMutex, DEFAULT_ALLOCATOR);
        *my_thing_mutex = cnx_mutex_new();

        cnx_mutex_lock(my_thing_mutex);
        my_very_important_thing = {
        // important intialization
        };
        cnx_mutex_unlock(my_thing_mutex);
    }
}

void update_my_thing(u64 value) {
    cnx_mutex_lock(my_thing_mutex);
    my_very_important_thing.value = value;
    cnx_mutex_unlock(my_thing_mutex);
}

u64 get_value_from_my_thing(void) {
    cnx_mutex_lock(my_thing_mutex);
    let val = my_very_important_thing.value;
    cnx_mutex_unlock(my_thing_mutex);
    return val;
}


// do some compute intensive task...
// update the value
update_my_thing(new_value);

my_val = get_value_from_my_thing();
// do something with my_val

let_mut newval = get_value_from_my_thing();
while(newval == my_val) {
    cnx_this_thread_sleep_for(cnx_milliseconds(100));
    newval = get_value_from_my_thing();
}

my_val = newval;
// do something with new value

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.
struct CnxMutexInterface

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.

Function documentation

CnxMutex cnx_mutex_new(void)

Creates a new mutex.

Returns A CnxMutex

void cnx_mutex_lock(CnxMutex*restrict mutex)

Unconditionally locks the mutex pointed to by mutex

Parameters
mutex - The mutex to lock

bool cnx_mutex_try_lock(CnxMutex*restrict mutex)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
Returns true if successful

If locking the mutex is successful, return true.

void cnx_mutex_unlock(CnxMutex*restrict mutex)

Unlocks the locked mutex pointed to by mutex

Parameters
mutex - The mutex to unlock

void cnx_mutex_free(CnxMutex*restrict mutex)

Destroys the mutex pointed to by mutex

Parameters
mutex - The mutex to free

CnxRecursiveMutex cnx_recursive_mutex_new(void)

Creates a new recursive mutex.

Returns A CnxRecursiveMutex

void cnx_recursive_mutex_lock(CnxRecursiveMutex*restrict mutex)

Unconditionally locks the mutex pointed to by mutex

Parameters
mutex - The mutex to lock

bool cnx_recursive_mutex_try_lock(CnxRecursiveMutex*restrict mutex)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
Returns true if successful

If locking the mutex is successful, return true.

void cnx_recursive_mutex_unlock(CnxRecursiveMutex*restrict mutex)

Unlocks the locked mutex pointed to by mutex

Parameters
mutex - The mutex to unlock

void cnx_recursive_mutex_free(CnxRecursiveMutex*restrict mutex)

Destroys the mutex pointed to by mutex

Parameters
mutex - The mutex to free

CnxTimedMutex cnx_timed_mutex_new(void)

Creates a new timed mutex.

Returns A CnxTimedMutex

void cnx_timed_mutex_lock(CnxTimedMutex*restrict mutex)

Unconditionally locks the mutex pointed to by mutex

Parameters
mutex - The mutex to lock

bool cnx_timed_mutex_try_lock(CnxTimedMutex*restrict mutex)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
Returns true if successful

If locking the mutex is successful, return true.

bool cnx_timed_mutex_try_lock_for(CnxTimedMutex*restrict mutex, CnxDuration duration)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
duration - The amount to time spend trying to lock the mutex
Returns true if successful

Tries to lock the mutex until the amount of time specified by duration has passed. If locking is successful before duration has elapsed, returns true. Otherwise stops trying to acquire the lock and returns false.

bool cnx_timed_mutex_try_lock_until(CnxTimedMutex*restrict mutex, CnxTimePoint stop_point)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
stop_point - The point in time at which to quit trying to lock the mutex
Returns true if successful

Tries to lock the mutex until the the point in time specified by stop_point. If locking is successful before stop_point has occurred, returns true. Otherwise stops trying to acquire the lock and returns false.

void cnx_timed_mutex_unlock(CnxTimedMutex*restrict mutex)

Unlocks the locked mutex pointed to by mutex

Parameters
mutex - The mutex to unlock

void cnx_timed_mutex_free(CnxTimedMutex*restrict mutex)

Destroys the mutex pointed to by mutex

Parameters
mutex - The mutex to free

CnxRecursiveTimedMutex cnx_recursive_timed_mutex_new(void)

Creates a new recursive timed mutex.

Returns A CnxRecursiveTimedMutex

void cnx_recursive_timed_mutex_lock(CnxRecursiveTimedMutex*restrict mutex)

Unconditionally locks the mutex pointed to by mutex

Parameters
mutex - The mutex to lock

bool cnx_recursive_timed_mutex_try_lock(CnxRecursiveTimedMutex*restrict mutex)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
Returns true if successful

If locking the mutex is successful, return true.

bool cnx_recursive_timed_mutex_try_lock_for(CnxRecursiveTimedMutex*restrict mutex, CnxDuration duration)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
duration - The amount to time spend trying to lock the mutex
Returns true if successful

Tries to lock the mutex until the amount of time specified by duration has passed. If locking is successful before duration has elapsed, returns true. Otherwise stops trying to acquire the lock and returns false.

bool cnx_recursive_timed_mutex_try_lock_until(CnxRecursiveTimedMutex*restrict mutex, CnxTimePoint stop_point)

Attempts to lock the mutex pointed to by mutex

Parameters
mutex - The mutex to lock
stop_point - The point in time at which to quit trying to lock the mutex
Returns true if successful

Tries to lock the mutex until the the point in time specified by stop_point. If locking is successful before stop_point has occurred, returns true. Otherwise stops trying to acquire the lock and returns false.

void cnx_recursive_timed_mutex_unlock(CnxRecursiveTimedMutex*restrict mutex)

Unlocks the locked mutex pointed to by mutex

Parameters
mutex - The mutex to unlock

void cnx_recursive_timed_mutex_free(CnxRecursiveTimedMutex*restrict mutex)

Destroys the mutex pointed to by mutex

Parameters
mutex - The mutex to free

Variable documentation

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.