The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
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
"
14
#include "
kernel/sync/wait_queue.h
"
15
16
/**
17
* a mutex. zero-initialized (or MUTEX_INIT) means unlocked.
18
*/
19
typedef
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
*/
33
void
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
*/
39
void
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
*/
45
void
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
*/
52
bool
mutex_held
(
const
mutex_t
*mutex);
stdint.h
mutex_lock
void mutex_lock(mutex_t *mutex)
acquire a mutex, sleeping until it is free.
Definition
mutex.c:15
mutex_unlock
void mutex_unlock(mutex_t *mutex)
release a mutex held by the current process and wake one waiter.
Definition
mutex.c:39
mutex_held
bool mutex_held(const mutex_t *mutex)
check whether the current process holds a mutex (for assertions).
Definition
mutex.c:53
mutex_init
void mutex_init(mutex_t *mutex)
initialize a mutex to the unlocked state.
Definition
mutex.c:7
mutex_t
sleeping mutex for long critical sections in process context (filesystem and disk I/O).
Definition
mutex.h:20
mutex_t::waiters
wait_queue_t waiters
Definition
mutex.h:23
mutex_t::lock
spinlock_t lock
Definition
mutex.h:24
mutex_t::owner
struct process * owner
Definition
mutex.h:22
mutex_t::locked
volatile bool locked
Definition
mutex.h:21
process
process control block.
Definition
process.h:69
spinlock_t
kernel locking rules:
Definition
spinlock.h:67
wait_queue_t
a queue of blocked processes, linked through process_t::wait_next.
Definition
wait_queue.h:35
wait_queue.h
src
kernel
sync
mutex.h
Generated by
1.18.0