picofuse

Macros | Typedefs

Mutex primitives. More...

Collaboration diagram for Mutex:

Macros

#define SYS_MUTEX_CAPACITY   32
 Maximum number of mutexes available in static-pool implementations.
 

Typedefs

typedef struct sys_mutex_t sys_mutex_t
 Mutex.
 

Lifecycle

sys_mutex_tsys_mutex_init (void)
 Initialize a new mutex. More...
 
void sys_mutex_deinit (sys_mutex_t *mutex)
 Release a mutex. More...
 

Methods

bool sys_mutex_lock (sys_mutex_t *mutex)
 Lock a mutex, by blocking. More...
 
bool sys_mutex_trylock (sys_mutex_t *mutex)
 Try to lock a mutex. More...
 
bool sys_mutex_unlock (sys_mutex_t *mutex)
 Unlock a mutex. More...
 

Detailed Description

Mutex primitives.

Function Documentation

◆ sys_mutex_deinit()

void sys_mutex_deinit ( sys_mutex_t mutex)

Release a mutex.

Parameters
mutexPointer to the mutex to release

Releases all resources associated with the mutex and renders it unusable. The mutex should not be locked when this function is called.

◆ sys_mutex_init()

sys_mutex_t* sys_mutex_init ( void  )

Initialize a new mutex.

Returns
Initialized mutex structure

Creates and initializes a new mutex for thread synchronization. The mutex is initially unlocked and ready for use. The returned mutex can be released with sys_mutex_deinit(). Implementations that use a static pool may return NULL after SYS_MUTEX_CAPACITY mutexes have been allocated.

◆ sys_mutex_lock()

bool sys_mutex_lock ( sys_mutex_t mutex)

Lock a mutex, by blocking.

Parameters
mutexPointer to the mutex to lock
Returns
true if the mutex was successfully locked, false on error

Attempts to lock the specified mutex. If the mutex is already locked by another thread, this function will block until the mutex becomes available. Every successful lock must be paired with sys_mutex_unlock() Attempting to lock an already owned mutex may result in deadlock

◆ sys_mutex_trylock()

bool sys_mutex_trylock ( sys_mutex_t mutex)

Try to lock a mutex.

Parameters
mutexPointer to the mutex to try locking
Returns
true if the mutex was successfully locked, false if already locked or on error

Attempts to lock the specified mutex without blocking. If the mutex is already locked, this function returns immediately with false. Every successful trylock must be paired with sys_mutex_unlock()

◆ sys_mutex_unlock()

bool sys_mutex_unlock ( sys_mutex_t mutex)

Unlock a mutex.

Parameters
mutexPointer to the mutex to unlock
Returns
true if the mutex was successfully unlocked, false on error

Releases a previously acquired mutex lock, allowing other threads to acquire the mutex. Only the thread that locked the mutex should unlock it.