-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-wait.hh
149 lines (128 loc) · 3.45 KB
/
k-wait.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef CHICKADEE_K_WAIT_HH
#define CHICKADEE_K_WAIT_HH
#include "kernel.hh"
#include "k-waitstruct.hh"
// k-wait.hh
// Defines `waiter` and `wait_queue` member functions.
// `k-waitstruct.hh` defines the `waiter` and `wait_queue` types.
// (Separating the structures and functions into different header files
// avoids problems with circular dependencies.)
inline waiter::waiter() {
}
inline waiter::~waiter() {
// optional error-checking code
}
inline void waiter::prepare(wait_queue& wq) {
auto irqs = wq.lock_.lock();
p_ = current();
assert(p_->pstate_ != proc::ps_blocked);
p_->pstate_ = proc::ps_blocked;
wq_ = &wq;
wq_->q_.push_back(this);
wq_->lock_.unlock(irqs);
}
inline void waiter::block() {
assert(p_ == current());
if (p_->pstate_ == proc::ps_blocked) {
p_->yield();
} else {
}
assert(p_->pstate_ == proc::ps_runnable);
clear();
// your code here
}
inline void waiter::clear() {
assert(p_);
auto irqs = wq_->lock_.lock();
wake();
if (links_.is_linked()) links_.erase();
wq_->lock_.unlock(irqs);
}
inline void waiter::wake() {
p_->wake();
}
// waiter::block_until(wq, predicate)
// Block on `wq` until `predicate()` returns true.
template <typename F>
inline void waiter::block_until(wait_queue& wq, F predicate) {
while (true) {
prepare(wq);
if (predicate()) {
break;
}
block();
}
clear();
}
// waiter::block_until(wq, predicate, lock, irqs)
// Block on `wq` until `predicate()` returns true. The `lock`
// must be locked; it is unlocked before blocking (if blocking
// is necessary). All calls to `predicate` have `lock` locked,
// and `lock` is locked on return.
template <typename F>
inline void waiter::block_until(wait_queue& wq, F predicate,
spinlock& lock, irqstate& irqs) {
while (true) {
prepare(wq);
if (predicate()) {
break;
}
lock.unlock(irqs);
block();
irqs = lock.lock();
}
clear();
}
// waiter::block_until(wq, predicate, guard)
// Block on `wq` until `predicate()` returns true. The `guard`
// must be locked on entry; it is unlocked before blocking (if
// blocking is necessary) and locked on return.
template <typename F>
inline void waiter::block_until(wait_queue& wq, F predicate,
spinlock_guard& guard) {
block_until(wq, predicate, guard.lock_, guard.irqs_);
}
// wait_queue::wake_all()
// Lock the wait queue, then clear it by waking all waiters.
inline void wait_queue::wake_all() {
spinlock_guard guard(lock_);
while (auto w = q_.pop_front()) {
w->wake();
}
}
inline void wait_queue::wake_all(spinlock_guard& guard) {
while (auto w = q_.pop_front()) {
w->wake();
}
}
inline void wait_queue::wake_n(size_t n) {
spinlock_guard guard(lock_);
while (auto w = q_.pop_front()) {
w->wake();
n--;
if (n == 0) {
break;
}
}
}
inline void wait_queue::wake_one() {
spinlock_guard guard(lock_);
auto w = q_.pop_front();
if (w) {
w->wake();
}
}
inline void wait_queue::wake_one(spinlock_guard& guard) {
auto w = q_.pop_front();
if (w) {
w->wake();
}
}
inline void waiter::block_until_woken(wait_queue& wq, spinlock_guard& guard) {
prepare(wq);
guard.unlock();
block();
guard.lock();
clear();
}
#endif