The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
wait_queue.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * wait queue: a list of processes blocked until some event occurs.
5 *
6 * a process calls wait_queue_block() to give up the CPU until another
7 * context wakes it with wait_queue_wake_one() or wait_queue_wake_all().
8 * this replaces the busy-wait (hlt-in-a-loop) blocking that stalled the
9 * whole system while one process waited on a pipe.
10 *
11 * used by: pipe read/write (wait for data / space), and any future
12 * blocking operation (sleep, disk I/O completion).
13 *
14 * all operations must run with the caller in kernel context. blocking and
15 * waking are done with interrupts masked, since a wake can come from an
16 * interrupt handler.
17 *
18 * a sleep is either uninterruptible (wait_queue_block, used by mutexes:
19 * the process wakes only when the event happens) or interruptible (the
20 * _interruptible variants): a signal for the process also wakes it, and
21 * the caller must then check signal_pending() and give up its wait.
22 */
23
25
26// forward declaration: we only store a process pointer here, so including
27// process.h would create a cycle (process.h -> fd_table.h -> wait_queue.h).
28struct process;
29
30/**
31 * a queue of blocked processes, linked through process_t::wait_next.
32 * zero-initialized (head == NULL) means empty.
33 */
34typedef struct
35{
36 struct process *head; // first waiter, or NULL if none
38
39/**
40 * initialize a wait queue to empty.
41 * @param wq the queue to initialize.
42 */
44
45/**
46 * block the current process on this queue and yield the CPU.
47 * the process is marked BLOCKED, removed from the scheduler, and appended
48 * to the queue. returns only once another context has woken it, at which
49 * point it is runnable again.
50 * @param wq the queue to block on.
51 */
53
54/**
55 * block on this queue while holding a spinlock, releasing the lock for as
56 * long as the process sleeps and holding it again on return.
57 *
58 * this is the check-then-sleep pattern: take the lock, test a condition,
59 * and if it is not met call this. the lock is held from the test until
60 * the process is on the queue, so a waker, which takes the lock to change
61 * the condition, cannot slip in between and be lost. callers re-test the
62 * condition in a loop after waking.
63 * @param wq the queue to block on.
64 * @param lock a lock taken with spin_lock_irqsave() by the caller.
65 */
67
68/**
69 * wake the process at the head of the queue, if any.
70 * the woken process is marked READY and handed back to the scheduler.
71 * @param wq the queue to wake from.
72 */
74
75/**
76 * wake every process on the queue.
77 * @param wq the queue to drain.
78 */
80
81/**
82 * like wait_queue_block, but a signal for the process also wakes it. the
83 * caller re-tests its condition and signal_pending() after the return.
84 * @param wq the queue to block on.
85 */
87
88/**
89 * like wait_queue_block_locked, but a signal for the process also wakes
90 * it. the caller re-tests its condition and signal_pending() after the
91 * return.
92 * @param wq the queue to block on.
93 * @param lock a lock taken with spin_lock_irqsave() by the caller.
94 */
96
97/**
98 * sleep on a queue while a counter has the value the caller saw, until a
99 * wake or a signal. a waker changes the counter, then wakes the queue: a
100 * change before the sleep is seen, a later one finds the process asleep.
101 * for a condition that no single lock guards (waitpid).
102 * @param wq the queue to sleep on.
103 * @param word the counter.
104 * @param seen the value the caller read before it tested its condition.
105 */
106void wait_queue_wait_word(wait_queue_t *wq, const volatile uint32_t *word, uint32_t seen);
107
108/**
109 * sleep on a queue: the common part of the block functions, for code that
110 * builds its own (the timed queues). call it with sched_lock held and
111 * interrupts masked. an interruptible sleep with a signal pending returns
112 * at once. it returns with sched_lock held.
113 * @param wq the queue to sleep on.
114 * @param interruptible true if a signal wakes the process.
115 */
117
118/**
119 * wait_queue_wake_all() for a caller that holds sched_lock.
120 * @param wq the queue to drain.
121 */
123
124/**
125 * take a process out of the queue it sleeps on, if it sleeps
126 * interruptibly, and make it runnable (a signal arrived). it takes
127 * sched_lock.
128 * @param proc the process.
129 * @return true if it was asleep and is now woken.
130 */
131bool wait_queue_interrupt(struct process *proc);
unsigned int uint32_t
Definition fb.h:17
process control block.
Definition process.h:69
bool interruptible
Definition process.h:116
kernel locking rules:
Definition spinlock.h:67
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35
struct process * head
Definition wait_queue.h:36
void wait_queue_block_locked(wait_queue_t *wq, spinlock_t *lock)
block on this queue while holding a spinlock, releasing the lock for as long as the process sleeps an...
Definition wait_queue.c:142
void wait_queue_wake_one(wait_queue_t *wq)
wake the process at the head of the queue, if any.
Definition wait_queue.c:179
void wait_queue_init(wait_queue_t *wq)
initialize a wait queue to empty.
Definition wait_queue.c:37
void wait_queue_block(wait_queue_t *wq)
block the current process on this queue and yield the CPU.
Definition wait_queue.c:117
void wait_queue_block_interruptible(wait_queue_t *wq)
like wait_queue_block, but a signal for the process also wakes it.
Definition wait_queue.c:122
void wait_queue_block_locked_interruptible(wait_queue_t *wq, spinlock_t *lock)
like wait_queue_block_locked, but a signal for the process also wakes it.
Definition wait_queue.c:147
bool wait_queue_interrupt(process_t *proc)
take a process out of the queue it sleeps on, if it sleeps interruptibly, and make it runnable (a sig...
Definition wait_queue.c:152
void wait_queue_sleep_locked(wait_queue_t *wq, bool interruptible)
sleep on a queue: the common part of the block functions, for code that builds its own (the timed que...
Definition wait_queue.c:42
void wait_queue_wait_word(wait_queue_t *wq, const volatile uint32_t *word, uint32_t seen)
sleep on a queue while a counter has the value the caller saw, until a wake or a signal.
Definition wait_queue.c:127
void wait_queue_wake_all(wait_queue_t *wq)
wake every process on the queue.
Definition wait_queue.c:211
void wait_queue_wake_all_locked(wait_queue_t *wq)
wait_queue_wake_all() for a caller that holds sched_lock.
Definition wait_queue.c:195