-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk-wait.hh
152 lines (135 loc) · 3.98 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
150
151
152
#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
}
// prepare(wq)
// set waiter process to blocked and add it to the waitqueue
inline void waiter::prepare(wait_queue& wq) {
spinlock_guard g(wq.lock_);
p_ = current();
p_->pstate_ = proc::ps_blocked;
wq_ = &wq;
// add this waiter (i.e., the process) to wait queue
wq.q_.push_front(this);
}
// block()
// yield if the current process is blocked and wake it otherwise.
inline void waiter::block() {
assert(p_ == current());
if(p_->pstate_ == proc::ps_blocked) {
p_->yield();
} // else {
/* p_->pstate_ was set to ps_runnable by a call to waitq.wakeall()
* that snuck in before a call to w.block()
*/
// }
clear();
}
// clear()
// remove waiter from wait queue and wake its corresponding process
inline void waiter::clear() {
spinlock_guard g(wq_->lock_);
if(links_.is_linked()) {
wq_->q_.erase(this);
}
// wake process after removing it from wait queue
wake();
}
// wake()
// set process to runnable and schedule it to run
inline void waiter::wake() {
p_->wake();
}
// waiter::block_until(wq, predicate)
// Block on `wq` until `predicate()` returns true or process group is exiting.
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();
}
}
// wait_queue::wake_proc(p)
// look for waiter with process 'p' and wake it found
inline void wait_queue::wake_proc(proc* p) {
spinlock_guard guard(lock_);
waiter* w = q_.front();
while(w) {
if(w->p_ == p) {
q_.erase(w);
w->wake();
return;
}
w = q_.next(w);
}
}
// wait_queue::wake_some(int count)
// Lock the wait queue, then wakes 'count' processes or
// all processes in the waitqueue, whichever is smaller.
// Returns number of processes awoken.
inline int wait_queue::wake_some(int count) {
spinlock_guard guard(lock_);
int awaken = 0;
log_printf("count: %d\n", count);
while (count > 0) {
if(auto w = q_.pop_front()) {
w->wake();
--count;
++awaken;
} else {
break;
}
}
return awaken;
}
#endif