-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstate.rs
More file actions
295 lines (280 loc) · 10.2 KB
/
state.rs
File metadata and controls
295 lines (280 loc) · 10.2 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::common::constants::{CoroutineState, SyscallName, SyscallState};
use crate::common::now;
use crate::coroutine::listener::Listener;
use crate::coroutine::Coroutine;
use crate::{error, info};
use std::fmt::Debug;
use std::io::Error;
impl<Param, Yield, Return> Coroutine<'_, Param, Yield, Return>
where
Yield: Debug + Copy + Eq,
Return: Debug + Copy + Eq,
{
/// Returns the previous state of this `StateCoroutine`.
/// Note: user should not use this method.
fn change_state(
&self,
new_state: CoroutineState<Yield, Return>,
) -> CoroutineState<Yield, Return> {
let old_state = self.state.replace(new_state);
//对→Running的转换:先记录日志再通知MonitorListener。
//on_state_changed(Running)会通过MonitorListener设置10ms的NOTIFY_NODE定时器,
//如果先通知再记录日志,在QEMU等慢平台上info!()可能耗时>10ms,
//导致定时器在日志记录期间过期→SIGURG→抢占活锁。
//先记录日志确保NOTIFY_NODE定时器在日志I/O完成后才启动。
// For →Running transitions: log BEFORE notifying MonitorListener.
// on_state_changed(Running) sets a 10ms NOTIFY_NODE timer via MonitorListener.
// If notified first, info!() can take >10ms on slow platforms (QEMU),
// causing the timer to expire during logging → SIGURG → preemption live-lock.
// Logging first ensures the NOTIFY_NODE timer starts after slow I/O completes.
if matches!(new_state, CoroutineState::Running) {
info!("{} {:?}->{:?}", self.name(), old_state, new_state);
self.on_state_changed(self, old_state, new_state);
} else {
self.on_state_changed(self, old_state, new_state);
if let CoroutineState::Error(_) = new_state {
error!("{} {:?}->{:?}", self.name(), old_state, new_state);
} else {
info!("{} {:?}->{:?}", self.name(), old_state, new_state);
}
}
old_state
}
/// suspend -> ready
///
/// # Errors
/// if change state fails.
pub(crate) fn ready(&self) -> std::io::Result<()> {
let current = self.state();
match current {
CoroutineState::Ready => return Ok(()),
CoroutineState::Suspend(_, timestamp) if timestamp <= now() => {
let new_state = CoroutineState::Ready;
let old_state = self.change_state(new_state);
self.on_ready(self, old_state);
return Ok(());
}
_ => {}
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Ready
)))
}
/// ready -> running
/// syscall -> running
///
/// below just for test
/// suspend -> running
///
/// # Errors
/// if change state fails.
pub fn running(&self) -> std::io::Result<()> {
let current = self.state();
match current {
CoroutineState::Running => return Ok(()),
CoroutineState::Ready | CoroutineState::Syscall(_, _, SyscallState::Executing) => {
let new_state = CoroutineState::Running;
let old_state = self.change_state(new_state);
self.on_running(self, old_state);
return Ok(());
}
// #[cfg(test)] preemptive.rs use this
CoroutineState::Suspend(_, timestamp) if timestamp <= now() => {
let new_state = CoroutineState::Running;
let old_state = self.change_state(new_state);
self.on_running(self, old_state);
return Ok(());
}
CoroutineState::Syscall(_, _, SyscallState::Callback | SyscallState::Timeout) => {
return Ok(());
}
_ => {}
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Running
)))
}
/// running -> suspend
///
/// # Errors
/// if change state fails.
pub(super) fn suspend(&self, val: Yield, timestamp: u64) -> std::io::Result<()> {
let current = self.state();
if CoroutineState::Running == current {
let new_state = CoroutineState::Suspend(val, timestamp);
let old_state = self.change_state(new_state);
self.on_suspend(self, old_state);
return Ok(());
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Suspend(val, timestamp)
)))
}
/// running -> syscall
/// inner: syscall -> syscall
///
/// # Errors
/// if change state fails.
pub fn syscall(
&self,
val: Yield,
syscall: SyscallName,
syscall_state: SyscallState,
) -> std::io::Result<()> {
let current = self.state();
match current {
CoroutineState::Running => {
let new_state = CoroutineState::Syscall(val, syscall, syscall_state);
let old_state = self.change_state(new_state);
self.on_syscall(self, old_state);
return Ok(());
}
CoroutineState::Syscall(_, original_syscall, _) if original_syscall == syscall => {
let new_state = CoroutineState::Syscall(val, syscall, syscall_state);
let old_state = self.change_state(new_state);
self.on_syscall(self, old_state);
return Ok(());
}
_ => {}
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Syscall(val, syscall, syscall_state)
)))
}
/// running -> cancel
///
/// # Errors
/// if change state fails.
pub(super) fn cancel(&self) -> std::io::Result<()> {
let current = self.state();
if CoroutineState::Running == current {
let new_state = CoroutineState::Cancelled;
let old_state = self.change_state(new_state);
self.on_cancel(self, old_state);
return Ok(());
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Cancelled
)))
}
/// running -> complete
///
/// # Errors
/// if change state fails.
pub(super) fn complete(&self, val: Return) -> std::io::Result<()> {
let current = self.state();
if CoroutineState::Running == current {
let new_state = CoroutineState::Complete(val);
let old_state = self.change_state(new_state);
self.on_complete(self, old_state, val);
return Ok(());
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Complete(val)
)))
}
/// running -> error
///
/// # Errors
/// if change state fails.
pub(super) fn error(&self, msg: &'static str) -> std::io::Result<()> {
let current = self.state();
if CoroutineState::Running == current {
let new_state = CoroutineState::Error(msg);
let old_state = self.change_state(new_state);
self.on_error(self, old_state, msg);
return Ok(());
}
Err(Error::other(format!(
"{} unexpected {current}->{:?}",
self.name(),
CoroutineState::<Yield, Return>::Error(msg)
)))
}
}
#[cfg(all(test, not(feature = "preemptive")))]
mod tests {
use super::*;
use crate::coroutine::suspender::Suspender;
#[test]
fn test_ready() -> std::io::Result<()> {
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.ready()?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.suspend((), u64::MAX)?;
assert_eq!(CoroutineState::Suspend((), u64::MAX), co.state());
assert!(co.ready().is_err());
Ok(())
}
#[test]
fn test_running() -> std::io::Result<()> {
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.running()?;
co.complete(())?;
assert_eq!(CoroutineState::Complete(()), co.state());
assert!(co.running().is_err());
Ok(())
}
#[test]
fn test_suspend() -> std::io::Result<()> {
let mut co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.suspend((), u64::MAX)?;
assert_eq!(CoroutineState::Suspend((), u64::MAX), co.state());
assert!(co.resume().is_err());
assert!(co.suspend((), u64::MAX).is_err());
Ok(())
}
#[test]
fn test_syscall() -> std::io::Result<()> {
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.syscall((), SyscallName::nanosleep, SyscallState::Executing)?;
assert_eq!(
CoroutineState::Syscall((), SyscallName::nanosleep, SyscallState::Executing),
co.state()
);
assert!(co
.syscall((), SyscallName::sleep, SyscallState::Executing)
.is_err());
Ok(())
}
#[test]
fn test_complete() -> std::io::Result<()> {
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.complete(())?;
assert_eq!(CoroutineState::Complete(()), co.state());
assert!(co.complete(()).is_err());
Ok(())
}
#[test]
fn test_error() -> std::io::Result<()> {
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
assert_eq!(CoroutineState::Ready, co.state());
co.running()?;
co.error("test error, ignore it")?;
assert_eq!(CoroutineState::Error("test error, ignore it"), co.state());
assert!(co.error("abc").is_err());
Ok(())
}
}