Skip to content
modkitv0.2

thread.h

#include <modkit/thread.h>25 functions · 1 structs · 5 typedefs · 3 macros

Cross-platform threading primitives.

Provides thread-safe primitives for multi-threaded applications:ThreadsMutexes (mutual exclusion locks)Semaphores (signaling between threads)Atomic integers (lock-free operations) Platform support:Desktop (Windows/Linux/macOS): Full supportWeb/Emscripten: Not supported (single-threaded only)Mobile: Supported with platform-specific constraints Usage:

Functions

mk_atomic_addfunction

int mk_atomic_add(mk_atomic_int_t *atomic, int value)

Add to an atomic integer.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer
valueintValue to add

Returns Previous value (before addition)

mk_atomic_casfunction

bool mk_atomic_cas(mk_atomic_int_t *atomic, int expected, int desired)

Compare and swap an atomic integer.

If current value equals expected, sets to desired and returns true.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer
expectedintExpected current value
desiredintNew value if expected matches

Returns true if swap occurred, false otherwise

mk_atomic_decfunction

int mk_atomic_dec(mk_atomic_int_t *atomic)

Decrement an atomic integer.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer

Returns Previous value (before decrement)

mk_atomic_getfunction

int mk_atomic_get(mk_atomic_int_t *atomic)

Get the value of an atomic integer.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer to read

Returns Current value

mk_atomic_incfunction

int mk_atomic_inc(mk_atomic_int_t *atomic)

Increment an atomic integer.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer

Returns Previous value (before increment)

mk_atomic_initfunction

void mk_atomic_init(mk_atomic_int_t *atomic, int value)

Initialize an atomic integer.

Must be called before any other atomic operations.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer to initialize
valueintInitial value

mk_atomic_setfunction

int mk_atomic_set(mk_atomic_int_t *atomic, int value)

Set the value of an atomic integer.

ParameterTypeDescription
atomicmk_atomic_int_t *Atomic integer to set
valueintNew value

Returns Previous value

mk_mutex_createfunction

mk_mutex_t mk_mutex_create(void)

Create a mutex.

Returns Mutex handle or MK_MUTEX_INVALID on failure

mk_mutex_destroyfunction

void mk_mutex_destroy(mk_mutex_t mutex)

Destroy a mutex.

The mutex must not be locked when destroyed.

ParameterTypeDescription
mutexmk_mutex_tMutex to destroy

mk_mutex_lockfunction

void mk_mutex_lock(mk_mutex_t mutex)

Lock a mutex.

Blocks if the mutex is already locked by another thread.

ParameterTypeDescription
mutexmk_mutex_tMutex to lock

mk_mutex_try_lockfunction

bool mk_mutex_try_lock(mk_mutex_t mutex)

Try to lock a mutex without blocking.

ParameterTypeDescription
mutexmk_mutex_tMutex to try to lock

Returns true if lock was acquired, false if already locked

mk_mutex_unlockfunction

void mk_mutex_unlock(mk_mutex_t mutex)

Unlock a mutex.

Must be called from the same thread that locked it.

ParameterTypeDescription
mutexmk_mutex_tMutex to unlock

mk_semaphore_createfunction

mk_semaphore_t mk_semaphore_create(int initial_value)

Create a semaphore.

ParameterTypeDescription
initial_valueintInitial semaphore count (typically 0)

Returns Semaphore handle or MK_SEMAPHORE_INVALID on failure

mk_semaphore_destroyfunction

void mk_semaphore_destroy(mk_semaphore_t sem)

Destroy a semaphore.

ParameterTypeDescription
semmk_semaphore_tSemaphore to destroy

mk_semaphore_signalfunction

void mk_semaphore_signal(mk_semaphore_t sem)

Signal a semaphore (increment).

Wakes up one waiting thread if any.

ParameterTypeDescription
semmk_semaphore_tSemaphore to signal

mk_semaphore_try_waitfunction

bool mk_semaphore_try_wait(mk_semaphore_t sem)

Try to wait on a semaphore without blocking.

ParameterTypeDescription
semmk_semaphore_tSemaphore to try to wait on

Returns true if semaphore was decremented, false if count was zero

mk_semaphore_waitfunction

void mk_semaphore_wait(mk_semaphore_t sem)

Wait on a semaphore (decrement).

Blocks if the count is zero until another thread signals.

ParameterTypeDescription
semmk_semaphore_tSemaphore to wait on

mk_semaphore_wait_timeoutfunction

bool mk_semaphore_wait_timeout(mk_semaphore_t sem, uint32_t timeout_ms)

Wait on a semaphore with timeout.

ParameterTypeDescription
semmk_semaphore_tSemaphore to wait on
timeout_msuint32_tMaximum time to wait in milliseconds

Returns true if semaphore was decremented, false on timeout

mk_thread_createfunction

mk_thread_t mk_thread_create(mk_thread_func_t func, const char *name, void *user_data)

Create and start a new thread.

ParameterTypeDescription
funcmk_thread_func_tThread function to execute
nameconst char *Thread name (for debugging, can be NULL)
user_datavoid *User data passed to thread function

Returns Thread handle or MK_THREAD_INVALID on failure

mk_thread_current_idfunction

uint64_t mk_thread_current_id(void)

Get the current thread's ID.

Useful for debugging and identifying threads.

Returns Current thread ID

mk_thread_destroyfunction

void mk_thread_destroy(mk_thread_t thread)

Destroy a thread handle.

The thread must have been joined first (or detached).

ParameterTypeDescription
threadmk_thread_tThread to destroy

mk_thread_joinfunction

int mk_thread_join(mk_thread_t thread)

Wait for a thread to finish execution.

Blocks until the thread exits.

ParameterTypeDescription
threadmk_thread_tThread to wait for

Returns Thread exit code, or -1 if thread is invalid

mk_thread_sleepfunction

void mk_thread_sleep(uint32_t milliseconds)

Sleep the current thread.

ParameterTypeDescription
millisecondsuint32_tTime to sleep in milliseconds

mk_thread_supportedfunction

bool mk_thread_supported(void)

Check if threading is supported on this platform.

Returns false on Web/Emscripten where only single-threaded operation is possible.

Returns true if threading primitives are available

mk_thread_yieldfunction

void mk_thread_yield(void)

Yield execution to other threads.

Hints to the scheduler that this thread can give up its time slice.

Structs

mk_atomic_intstruct

Atomic integer for lock-free operations.

Initialize with mk_atomic_init() before use.

FieldTypeDescription
_opaquevoid *Platform-specific storage.

Typedefs

mk_atomic_int_ttypedef

typedef struct mk_atomic_int mk_atomic_int_t

Atomic integer for lock-free operations.

Initialize with mk_atomic_init() before use.

mk_mutex_ttypedef

typedef struct mk_mutex* mk_mutex_t

Mutex handle (mutual exclusion lock).

mk_semaphore_ttypedef

typedef struct mk_semaphore* mk_semaphore_t

Semaphore handle (signaling primitive).

mk_thread_func_ttypedef

typedef int(*) mk_thread_func_t(void *user_data)

Thread function signature.

Returns Exit code (0 for success)

mk_thread_ttypedef

typedef struct mk_thread* mk_thread_t

Thread handle.

Macros

MK_MUTEX_INVALIDdefine

MK_MUTEX_INVALID

Invalid sentinel for mutex.

MK_SEMAPHORE_INVALIDdefine

MK_SEMAPHORE_INVALID

Invalid sentinel for semaphore.

MK_THREAD_INVALIDdefine

MK_THREAD_INVALID

Invalid sentinel for thread.