PatchworkOS
Loading...
Searching...
No Matches

Recursive Mutex. More...

Data Structures

struct  mutex_t
 Mutex structure. More...
 

Macros

#define MUTEX_SCOPE(mutex)
 Acquires a mutex for the reminder of the current scope.
 
#define MUTEX_CREATE   {.waitQueue = WAIT_QUEUE_CREATE, .owner = NULL, .depth = 0, .lock = LOCK_CREATE}
 Create a mutex initializer.
 

Functions

void mutex_init (mutex_t *mtx)
 Initializes a mutex.
 
void mutex_deinit (mutex_t *mtx)
 Deinitializes a mutex.
 
void mutex_acquire (mutex_t *mtx)
 Acquires a mutex, blocking until it is available.
 
bool mutex_acquire_timeout (mutex_t *mtx, clock_t timeout)
 Acquires a mutex, blocking until it is available or the timeout is reached.
 
void mutex_release (mutex_t *mtx)
 Releases a mutex.
 
static void mutex_cleanup (mutex_t **mtx)
 

Detailed Description

Recursive Mutex.

Macro Definition Documentation

◆ MUTEX_CREATE

#define MUTEX_CREATE   {.waitQueue = WAIT_QUEUE_CREATE, .owner = NULL, .depth = 0, .lock = LOCK_CREATE}

Create a mutex initializer.

Returns
A mutex initializer.

Definition at line 32 of file mutex.h.

◆ MUTEX_SCOPE

#define MUTEX_SCOPE (   mutex)
Value:
__attribute__((cleanup(mutex_cleanup))) mutex_t* CONCAT(m, __COUNTER__) = (mutex); \
mutex_acquire((mutex))
#define CONCAT(a, b)
Concatenates two tokens.
Definition defs.h:76
static void mutex_cleanup(mutex_t **mtx)
Definition mutex.h:89
static mtx_t mutex
Definition heap.c:35
Mutex structure.
Definition mutex.h:39

Acquires a mutex for the reminder of the current scope.

Parameters
mutexPointer to the mutex to acquire.

Definition at line 23 of file mutex.h.

Function Documentation

◆ mutex_acquire()

void mutex_acquire ( mutex_t mtx)

Acquires a mutex, blocking until it is available.

If the mutex is already owned by the current thread, this function will return immediately.

Parameters
mtxPointer to the mutex to acquire.

Definition at line 26 of file mutex.c.

References assert, CLOCKS_NEVER, and mutex_acquire_timeout().

Referenced by dentry_free(), vfs_link(), and vfs_remove().

◆ mutex_acquire_timeout()

bool mutex_acquire_timeout ( mutex_t mtx,
clock_t  timeout 
)

Acquires a mutex, blocking until it is available or the timeout is reached.

If the mutex is already owned by the current thread, this function will return immediately.

Parameters
mtxPointer to the mutex to acquire.
timeoutTimeout in clock ticks or CLOCKS_NEVER to wait indefinitely.
Returns
true if the mutex was acquired, false if the timeout was reached.

Definition at line 33 of file mutex.c.

References assert, CLOCKS_NEVER, CONFIG_MUTEX_MAX_SLOW_SPIN, mutex_t::depth, ERR, mutex_t::lock, lock_acquire(), lock_release(), NULL, mutex_t::owner, sched_thread(), timer_uptime(), WAIT_BLOCK_LOCK, WAIT_BLOCK_LOCK_TIMEOUT, and mutex_t::waitQueue.

Referenced by mutex_acquire().

◆ mutex_cleanup()

static void mutex_cleanup ( mutex_t **  mtx)
inlinestatic

Definition at line 89 of file mutex.h.

References mutex_release().

◆ mutex_deinit()

void mutex_deinit ( mutex_t mtx)

Deinitializes a mutex.

Parameters
mtxPointer to the mutex to deinitialize.

Definition at line 20 of file mutex.c.

References assert, NULL, mutex_t::owner, wait_queue_deinit(), and mutex_t::waitQueue.

◆ mutex_init()

void mutex_init ( mutex_t mtx)

Initializes a mutex.

Parameters
mtxPointer to the mutex to initialize.

Definition at line 12 of file mutex.c.

References mutex_t::depth, mutex_t::lock, lock_init(), NULL, mutex_t::owner, wait_queue_init(), and mutex_t::waitQueue.

Referenced by aml_init(), dentry_new(), and inode_new().

◆ mutex_release()

void mutex_release ( mutex_t mtx)

Releases a mutex.

The mutex must be owned by the current thread.

Parameters
mtxPointer to the mutex to release.

Definition at line 103 of file mutex.c.

References assert, mutex_t::depth, EOK, mutex_t::lock, LOCK_SCOPE, NULL, mutex_t::owner, sched_thread_unsafe(), wait_unblock(), and mutex_t::waitQueue.

Referenced by dentry_free(), mutex_cleanup(), vfs_link(), and vfs_remove().