solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Typedefs | Functions
lock.h File Reference

Cross-platform mutex and condition variable wrapper. More...

#include <pthread.h>
Include dependency graph for lock.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef pthread_mutex_t Lock
 
typedef pthread_cond_t Condition
 

Functions

int lock_init (Lock *lock)
 
int lock_acquire (Lock *lock)
 
int lock_release (Lock *lock)
 
int lock_free (Lock *lock)
 
int lock_try_acquire (Lock *lock)
 
int lock_wait (Lock *lock, Condition *condition, int timeout_ms)
 
int cond_init (Condition *condition)
 
int cond_signal (Condition *condition)
 
int cond_broadcast (Condition *condition)
 
int cond_wait (Condition *condition, Lock *lock)
 
int cond_wait_timeout (Condition *condition, Lock *lock, int timeout_ms)
 
int cond_free (Condition *condition)
 

Detailed Description

Cross-platform mutex and condition variable wrapper.

Provides a unified interface for synchronization primitives across Windows (Critical Sections/Condition Variables) and POSIX systems (pthread mutexes/condition variables).

All functions return integer error codes instead of terminating the program, allowing for proper error handling and recovery.

Note
This library is thread-safe where documented, but initialization and destruction functions are NOT thread-safe.

Definition in file lock.h.

Typedef Documentation

◆ Condition

typedef pthread_cond_t Condition

Platform-specific condition variable type (POSIX condition variable).

Definition at line 34 of file lock.h.

◆ Lock

typedef pthread_mutex_t Lock

Platform-specific lock type (POSIX mutex).

Definition at line 32 of file lock.h.

Function Documentation

◆ cond_broadcast()

int cond_broadcast ( Condition condition)

Signals all threads waiting on the condition variable.

Parameters
conditionPointer to an initialized condition variable. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Thread-safe. Wakes up all waiting threads.

Definition at line 248 of file lock.c.

References cond_broadcast().

Referenced by cond_broadcast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cond_free()

int cond_free ( Condition condition)

Destroys a condition variable and releases its resources.

Parameters
conditionPointer to the condition variable to destroy. NULL is allowed (no-op).
Returns
0 on success, -1 on failure.
Note
Not thread-safe. Caller must ensure no other threads are using the condition variable.

Definition at line 316 of file lock.c.

Referenced by threadpool_destroy().

Here is the caller graph for this function:

◆ cond_init()

int cond_init ( Condition condition)

Initializes a condition variable.

Parameters
conditionPointer to the condition variable to initialize. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Not thread-safe. Caller must ensure exclusive access during initialization.

Definition at line 220 of file lock.c.

References cond_init().

Referenced by cond_init(), and threadpool_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cond_signal()

int cond_signal ( Condition condition)

Signals one thread waiting on the condition variable.

Parameters
conditionPointer to an initialized condition variable. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Thread-safe. Wakes up at most one waiting thread.

Definition at line 234 of file lock.c.

References cond_signal().

Referenced by cond_signal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cond_wait()

int cond_wait ( Condition condition,
Lock lock 
)

Waits indefinitely on a condition variable while holding a lock.

Parameters
conditionPointer to an initialized condition variable. Must not be NULL.
lockPointer to an initialized lock currently held by this thread. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Thread-safe, but lock must be held by calling thread. Lock is atomically released during wait and re-acquired before returning.

Definition at line 262 of file lock.c.

References cond_wait().

Referenced by cond_wait(), cond_wait_timeout(), and threadpool_wait().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cond_wait_timeout()

int cond_wait_timeout ( Condition condition,
Lock lock,
int  timeout_ms 
)

Waits on a condition variable with a timeout while holding a lock.

Parameters
conditionPointer to an initialized condition variable. Must not be NULL.
lockPointer to an initialized lock currently held by this thread. Must not be NULL.
timeout_msTimeout in milliseconds. Negative values mean wait indefinitely.
Returns
0 on success, -1 on timeout, other error codes on failure.
Note
Thread-safe, but lock must be held by calling thread. Lock is atomically released during wait and re-acquired before returning.

Definition at line 275 of file lock.c.

References cond_wait(), and cond_wait_timeout().

Referenced by cond_wait_timeout(), lock_wait(), and threadpool_destroy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_acquire()

int lock_acquire ( Lock lock)

Acquires a lock, blocking until available.

Parameters
lockPointer to an initialized lock. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Thread-safe. This function will block until the lock is acquired.

Definition at line 145 of file lock.c.

References lock_acquire().

Referenced by lock_acquire(), threadpool_destroy(), and threadpool_wait().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_free()

int lock_free ( Lock lock)

Destroys a lock and releases its resources.

Parameters
lockPointer to the lock to destroy. NULL is allowed (no-op).
Returns
0 on success, -1 on failure.
Note
Not thread-safe. Caller must ensure no other threads are using the lock.

Definition at line 182 of file lock.c.

References lock_free().

Referenced by lock_free(), and threadpool_destroy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_init()

int lock_init ( Lock lock)

Initializes a lock.

Parameters
lockPointer to the lock to initialize. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Not thread-safe. Caller must ensure exclusive access during initialization.

Definition at line 132 of file lock.c.

References lock_init().

Referenced by lock_init(), and threadpool_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_release()

int lock_release ( Lock lock)

Releases a previously acquired lock.

Parameters
lockPointer to an initialized lock currently held by this thread. Must not be NULL.
Returns
0 on success, -1 on failure.
Note
Thread-safe, but only the thread that acquired the lock should release it.

Definition at line 168 of file lock.c.

References lock_release().

Referenced by lock_release(), threadpool_destroy(), and threadpool_wait().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_try_acquire()

int lock_try_acquire ( Lock lock)

Attempts to acquire a lock without blocking.

Parameters
lockPointer to an initialized lock. Must not be NULL.
Returns
0 if acquired, LOCK_ERROR_TRYLOCK if busy, other error codes on failure.
Note
Thread-safe. Returns immediately whether the lock was acquired or not.

Definition at line 196 of file lock.c.

References lock_try_acquire().

Referenced by lock_try_acquire().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_wait()

int lock_wait ( Lock lock,
Condition condition,
int  timeout_ms 
)

Waits on a condition variable with a timeout while holding a lock. This is a convenience function equivalent to cond_wait_timeout().

Parameters
lockPointer to an initialized lock currently held by this thread. Must not be NULL.
conditionPointer to an initialized condition variable. Must not be NULL.
timeout_msTimeout in milliseconds. Negative values mean wait indefinitely.
Returns
0 on success, -1 on timeout, other error codes on failure.
Note
Thread-safe, but lock must be held by calling thread.

Definition at line 216 of file lock.c.

References cond_wait_timeout(), and lock_wait().

Referenced by lock_wait().

Here is the call graph for this function:
Here is the caller graph for this function: