| 1 |
use crate::runtime::task::{self, unowned, Id, JoinHandle, OwnedTasks, Schedule, Task}; |
| 2 |
use crate::runtime::tests::NoopSchedule; |
| 3 |
|
| 4 |
use std::collections::VecDeque; |
| 5 |
use std::future::Future; |
| 6 |
use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 |
use std::sync::{Arc, Mutex}; |
| 8 |
|
| 9 |
struct AssertDropHandle { |
| 10 |
is_dropped: Arc<AtomicBool>, |
| 11 |
} |
| 12 |
impl AssertDropHandle { |
| 13 |
#[track_caller] |
| 14 |
fn assert_dropped(&self) { |
| 15 |
assert!(self.is_dropped.load(Ordering::SeqCst)); |
| 16 |
} |
| 17 |
|
| 18 |
#[track_caller] |
| 19 |
fn assert_not_dropped(&self) { |
| 20 |
assert!(!self.is_dropped.load(Ordering::SeqCst)); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
struct AssertDrop { |
| 25 |
is_dropped: Arc<AtomicBool>, |
| 26 |
} |
| 27 |
impl AssertDrop { |
| 28 |
fn new() -> (Self, AssertDropHandle) { |
| 29 |
let shared = Arc::new(AtomicBool::new(false)); |
| 30 |
( |
| 31 |
AssertDrop { |
| 32 |
is_dropped: shared.clone(), |
| 33 |
}, |
| 34 |
AssertDropHandle { |
| 35 |
is_dropped: shared.clone(), |
| 36 |
}, |
| 37 |
) |
| 38 |
} |
| 39 |
} |
| 40 |
impl Drop for AssertDrop { |
| 41 |
fn drop(&mut self) { |
| 42 |
self.is_dropped.store(true, Ordering::SeqCst); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// A Notified does not shut down on drop, but it is dropped once the ref-count |
| 47 |
// hits zero. |
| 48 |
#[test] |
| 49 |
fn create_drop1() { |
| 50 |
let (ad, handle) = AssertDrop::new(); |
| 51 |
let (notified, join) = unowned( |
| 52 |
async { |
| 53 |
drop(ad); |
| 54 |
unreachable!() |
| 55 |
}, |
| 56 |
NoopSchedule, |
| 57 |
Id::next(), |
| 58 |
); |
| 59 |
drop(notified); |
| 60 |
handle.assert_not_dropped(); |
| 61 |
drop(join); |
| 62 |
handle.assert_dropped(); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn create_drop2() { |
| 67 |
let (ad, handle) = AssertDrop::new(); |
| 68 |
let (notified, join) = unowned( |
| 69 |
async { |
| 70 |
drop(ad); |
| 71 |
unreachable!() |
| 72 |
}, |
| 73 |
NoopSchedule, |
| 74 |
Id::next(), |
| 75 |
); |
| 76 |
drop(join); |
| 77 |
handle.assert_not_dropped(); |
| 78 |
drop(notified); |
| 79 |
handle.assert_dropped(); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn drop_abort_handle1() { |
| 84 |
let (ad, handle) = AssertDrop::new(); |
| 85 |
let (notified, join) = unowned( |
| 86 |
async { |
| 87 |
drop(ad); |
| 88 |
unreachable!() |
| 89 |
}, |
| 90 |
NoopSchedule, |
| 91 |
Id::next(), |
| 92 |
); |
| 93 |
let abort = join.abort_handle(); |
| 94 |
drop(join); |
| 95 |
handle.assert_not_dropped(); |
| 96 |
drop(notified); |
| 97 |
handle.assert_not_dropped(); |
| 98 |
drop(abort); |
| 99 |
handle.assert_dropped(); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn drop_abort_handle2() { |
| 104 |
let (ad, handle) = AssertDrop::new(); |
| 105 |
let (notified, join) = unowned( |
| 106 |
async { |
| 107 |
drop(ad); |
| 108 |
unreachable!() |
| 109 |
}, |
| 110 |
NoopSchedule, |
| 111 |
Id::next(), |
| 112 |
); |
| 113 |
let abort = join.abort_handle(); |
| 114 |
drop(notified); |
| 115 |
handle.assert_not_dropped(); |
| 116 |
drop(abort); |
| 117 |
handle.assert_not_dropped(); |
| 118 |
drop(join); |
| 119 |
handle.assert_dropped(); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn drop_abort_handle_clone() { |
| 124 |
let (ad, handle) = AssertDrop::new(); |
| 125 |
let (notified, join) = unowned( |
| 126 |
async { |
| 127 |
drop(ad); |
| 128 |
unreachable!() |
| 129 |
}, |
| 130 |
NoopSchedule, |
| 131 |
Id::next(), |
| 132 |
); |
| 133 |
let abort = join.abort_handle(); |
| 134 |
let abort_clone = abort.clone(); |
| 135 |
drop(join); |
| 136 |
handle.assert_not_dropped(); |
| 137 |
drop(notified); |
| 138 |
handle.assert_not_dropped(); |
| 139 |
drop(abort); |
| 140 |
handle.assert_not_dropped(); |
| 141 |
drop(abort_clone); |
| 142 |
handle.assert_dropped(); |
| 143 |
} |
| 144 |
|
| 145 |
// Shutting down through Notified works |
| 146 |
#[test] |
| 147 |
fn create_shutdown1() { |
| 148 |
let (ad, handle) = AssertDrop::new(); |
| 149 |
let (notified, join) = unowned( |
| 150 |
async { |
| 151 |
drop(ad); |
| 152 |
unreachable!() |
| 153 |
}, |
| 154 |
NoopSchedule, |
| 155 |
Id::next(), |
| 156 |
); |
| 157 |
drop(join); |
| 158 |
handle.assert_not_dropped(); |
| 159 |
notified.shutdown(); |
| 160 |
handle.assert_dropped(); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn create_shutdown2() { |
| 165 |
let (ad, handle) = AssertDrop::new(); |
| 166 |
let (notified, join) = unowned( |
| 167 |
async { |
| 168 |
drop(ad); |
| 169 |
unreachable!() |
| 170 |
}, |
| 171 |
NoopSchedule, |
| 172 |
Id::next(), |
| 173 |
); |
| 174 |
handle.assert_not_dropped(); |
| 175 |
notified.shutdown(); |
| 176 |
handle.assert_dropped(); |
| 177 |
drop(join); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn unowned_poll() { |
| 182 |
let (task, _) = unowned(async {}, NoopSchedule, Id::next()); |
| 183 |
task.run(); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn schedule() { |
| 188 |
with(|rt| { |
| 189 |
rt.spawn(async { |
| 190 |
crate::task::yield_now().await; |
| 191 |
}); |
| 192 |
|
| 193 |
assert_eq!(2, rt.tick()); |
| 194 |
rt.shutdown(); |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn shutdown() { |
| 200 |
with(|rt| { |
| 201 |
rt.spawn(async { |
| 202 |
loop { |
| 203 |
crate::task::yield_now().await; |
| 204 |
} |
| 205 |
}); |
| 206 |
|
| 207 |
rt.tick_max(1); |
| 208 |
|
| 209 |
rt.shutdown(); |
| 210 |
}) |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn shutdown_immediately() { |
| 215 |
with(|rt| { |
| 216 |
rt.spawn(async { |
| 217 |
loop { |
| 218 |
crate::task::yield_now().await; |
| 219 |
} |
| 220 |
}); |
| 221 |
|
| 222 |
rt.shutdown(); |
| 223 |
}) |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn spawn_during_shutdown() { |
| 228 |
static DID_SPAWN: AtomicBool = AtomicBool::new(false); |
| 229 |
|
| 230 |
struct SpawnOnDrop(Runtime); |
| 231 |
impl Drop for SpawnOnDrop { |
| 232 |
fn drop(&mut self) { |
| 233 |
DID_SPAWN.store(true, Ordering::SeqCst); |
| 234 |
self.0.spawn(async {}); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
with(|rt| { |
| 239 |
let rt2 = rt.clone(); |
| 240 |
rt.spawn(async move { |
| 241 |
let _spawn_on_drop = SpawnOnDrop(rt2); |
| 242 |
|
| 243 |
loop { |
| 244 |
crate::task::yield_now().await; |
| 245 |
} |
| 246 |
}); |
| 247 |
|
| 248 |
rt.tick_max(1); |
| 249 |
rt.shutdown(); |
| 250 |
}); |
| 251 |
|
| 252 |
assert!(DID_SPAWN.load(Ordering::SeqCst)); |
| 253 |
} |
| 254 |
|
| 255 |
fn with(f: impl FnOnce(Runtime)) { |
| 256 |
struct Reset; |
| 257 |
|
| 258 |
impl Drop for Reset { |
| 259 |
fn drop(&mut self) { |
| 260 |
let _rt = CURRENT.try_lock().unwrap().take(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
let _reset = Reset; |
| 265 |
|
| 266 |
let rt = Runtime(Arc::new(Inner { |
| 267 |
owned: OwnedTasks::new(16), |
| 268 |
core: Mutex::new(Core { |
| 269 |
queue: VecDeque::new(), |
| 270 |
}), |
| 271 |
})); |
| 272 |
|
| 273 |
*CURRENT.try_lock().unwrap() = Some(rt.clone()); |
| 274 |
f(rt) |
| 275 |
} |
| 276 |
|
| 277 |
#[derive(Clone)] |
| 278 |
struct Runtime(Arc<Inner>); |
| 279 |
|
| 280 |
struct Inner { |
| 281 |
core: Mutex<Core>, |
| 282 |
owned: OwnedTasks<Runtime>, |
| 283 |
} |
| 284 |
|
| 285 |
struct Core { |
| 286 |
queue: VecDeque<task::Notified<Runtime>>, |
| 287 |
} |
| 288 |
|
| 289 |
static CURRENT: Mutex<Option<Runtime>> = Mutex::new(None); |
| 290 |
|
| 291 |
impl Runtime { |
| 292 |
fn spawn<T>(&self, future: T) -> JoinHandle<T::Output> |
| 293 |
where |
| 294 |
T: 'static + Send + Future, |
| 295 |
T::Output: 'static + Send, |
| 296 |
{ |
| 297 |
let (handle, notified) = self.0.owned.bind(future, self.clone(), Id::next()); |
| 298 |
|
| 299 |
if let Some(notified) = notified { |
| 300 |
self.schedule(notified); |
| 301 |
} |
| 302 |
|
| 303 |
handle |
| 304 |
} |
| 305 |
|
| 306 |
fn tick(&self) -> usize { |
| 307 |
self.tick_max(usize::MAX) |
| 308 |
} |
| 309 |
|
| 310 |
fn tick_max(&self, max: usize) -> usize { |
| 311 |
let mut n = 0; |
| 312 |
|
| 313 |
while !self.is_empty() && n < max { |
| 314 |
let task = self.next_task(); |
| 315 |
n += 1; |
| 316 |
let task = self.0.owned.assert_owner(task); |
| 317 |
task.run(); |
| 318 |
} |
| 319 |
|
| 320 |
n |
| 321 |
} |
| 322 |
|
| 323 |
fn is_empty(&self) -> bool { |
| 324 |
self.0.core.try_lock().unwrap().queue.is_empty() |
| 325 |
} |
| 326 |
|
| 327 |
fn next_task(&self) -> task::Notified<Runtime> { |
| 328 |
self.0.core.try_lock().unwrap().queue.pop_front().unwrap() |
| 329 |
} |
| 330 |
|
| 331 |
fn shutdown(&self) { |
| 332 |
let mut core = self.0.core.try_lock().unwrap(); |
| 333 |
|
| 334 |
self.0.owned.close_and_shutdown_all(0); |
| 335 |
|
| 336 |
while let Some(task) = core.queue.pop_back() { |
| 337 |
drop(task); |
| 338 |
} |
| 339 |
|
| 340 |
drop(core); |
| 341 |
assert!(self.0.owned.is_empty()); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
impl Schedule for Runtime { |
| 346 |
fn release(&self, task: &Task<Self>) -> Option<Task<Self>> { |
| 347 |
self.0.owned.remove(task) |
| 348 |
} |
| 349 |
|
| 350 |
fn schedule(&self, task: task::Notified<Self>) { |
| 351 |
self.0.core.try_lock().unwrap().queue.push_back(task); |
| 352 |
} |
| 353 |
} |