The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * sleeping mutex for long critical sections in process context (filesystem
5 * and disk I/O). a process that finds the mutex held blocks on its wait queue
6 * instead of spinning or masking interrupts, so interrupts and other
7 * processes keep running while the holder works.
8 *
9 * never take a mutex from an interrupt handler, and never block on one while
10 * holding a spinlock. use a spinlock for short sections shared with IRQs.
11 */
12
13#include "common/stdint.h"
15
16/**
17 * a mutex. zero-initialized (or MUTEX_INIT) means unlocked.
18 */
19typedef struct
20{
21 volatile bool locked; // true while held
22 struct process *owner; // holder, for assertions and debugging
23 wait_queue_t waiters; // processes blocked in mutex_lock
24 spinlock_t lock; // guards locked and owner
25} mutex_t;
26
27#define MUTEX_INIT {false, 0, {0}, {0, 0, 0}}
28
29/**
30 * initialize a mutex to the unlocked state.
31 * @param mutex the mutex to initialize.
32 */
33void mutex_init(mutex_t *mutex);
34
35/**
36 * acquire a mutex, sleeping until it is free. not recursive.
37 * @param mutex the mutex to acquire.
38 */
39void mutex_lock(mutex_t *mutex);
40
41/**
42 * release a mutex held by the current process and wake one waiter.
43 * @param mutex the mutex to release.
44 */
45void mutex_unlock(mutex_t *mutex);
46
47/**
48 * check whether the current process holds a mutex (for assertions).
49 * @param mutex the mutex to check.
50 * @return true if held by the current process.
51 */
52bool mutex_held(const mutex_t *mutex);
void mutex_lock(mutex_t *mutex)
acquire a mutex, sleeping until it is free.
Definition mutex.c:15
void mutex_unlock(mutex_t *mutex)
release a mutex held by the current process and wake one waiter.
Definition mutex.c:39
bool mutex_held(const mutex_t *mutex)
check whether the current process holds a mutex (for assertions).
Definition mutex.c:53
void mutex_init(mutex_t *mutex)
initialize a mutex to the unlocked state.
Definition mutex.c:7
sleeping mutex for long critical sections in process context (filesystem and disk I/O).
Definition mutex.h:20
wait_queue_t waiters
Definition mutex.h:23
spinlock_t lock
Definition mutex.h:24
struct process * owner
Definition mutex.h:22
volatile bool locked
Definition mutex.h:21
process control block.
Definition process.h:69
kernel locking rules:
Definition spinlock.h:67
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35