
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! The scheduler's glue: its counters (`running`, `pending`, |
| 2 | //! `io_waiting`), idle and deadlock detection, and the reactor's wake-ups. |
| 3 | //! The hand-offs it is built on have loom models (`handoff`, `timers`, |
| 4 | //! `otp`, `inbox`); these run whole programs of test actors, on one thread |
| 5 | //! and on several, and check what the scheduler decides. |
| 6 | //! |
| 7 | //! Test actors talk through statics: each test runs alone ([`test_turn`]). |
| 8 | //! An assertion that fails in an actor aborts the test binary (a panic |
| 9 | //! cannot unwind out of its `extern "C"` entry), as in the crate's other |
| 10 | //! actor tests; a wake-up that never comes fails [`yield_until`]. |
| 11 | |
| 12 | mod dist; |
| 13 | #[cfg(feature = "tls")] |
| 14 | mod transport; |
| 15 | |
| 16 | use std::io::{ErrorKind, Read, Write}; |
| 17 | use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; |
| 18 | use std::sync::{Condvar, Mutex}; |
| 19 | use std::thread::JoinHandle; |
| 20 | use std::time::{Duration, Instant}; |
| 21 | |
| 22 | use mio::net::UnixStream; |
| 23 | use mio::{Interest, Token}; |
| 24 | |
| 25 | use crate::abi::{Block, Ctx, UNIT, Value, YIELD_KILL, YIELD_NONE, int, int_get}; |
| 26 | use crate::fiber::Stack; |
| 27 | use crate::heap::drop_value; |
| 28 | use crate::otp::{Received, Tags, Timeout, Wait}; |
| 29 | |
| 30 | use super::{ |
| 31 | Body, EntryFn, Exit, HANDLER_STACK_POOL, HandlerStacks, STACK_SIZE, Shared, Tie, |
| 32 | WARM_HANDLER_STACKS, park, preempt_root, root_stack_size, run, spawn_actor, test_turn, this, |
| 33 | }; |
| 34 | |
| 35 | /// Thread counts each program runs with: one (the interpreter's order), |
| 36 | /// and several (stealing, one idle thread for all). |
| 37 | const THREADS: [usize; 2] = [1, 4]; |
| 38 | |
| 39 | #[test] |
| 40 | fn actor_roots_accept_fs_and_reject_old_runtime_mismatches() { |
| 41 | use crate::process::RootCap; |
| 42 | assert_eq!( |
| 43 | super::parse_actor_caps(&[0, 1, 2, 3]), |
| 44 | Some(vec![ |
| 45 | RootCap::Proc, |
| 46 | RootCap::Clock, |
| 47 | RootCap::Random, |
| 48 | RootCap::Fs |
| 49 | ]) |
| 50 | ); |
| 51 | assert_eq!(super::parse_actor_caps(&[0, 1, 2, 4]), None); |
| 52 | assert_eq!(super::parse_actor_caps(&[0, 1, 2, 3, 0]), None, "over four"); |
| 53 | } |
| 54 | |
| 55 | /// Run `entry` as the root actor on `threads` threads, as `tl_main` does, |
| 56 | /// then check that the scheduler is at rest: nothing runs, is queued or |
| 57 | /// waits on a socket, and every actor is gone. Returns the root's exit. |
| 58 | fn run_root(threads: usize, entry: EntryFn) -> Exit { |
| 59 | run_root_clock(threads, false, entry) |
| 60 | } |
| 61 | |
| 62 | /// `run_root` with the `LOUISE_VIRTUAL_TIME` opt-in set explicitly, so |
| 63 | /// the tests do not depend on the environment (ADR 0055). |
| 64 | fn run_root_clock(threads: usize, virtual_time: bool, entry: EntryFn) -> Exit { |
| 65 | let _turn = test_turn(); |
| 66 | let mut sched = Shared::new(threads); |
| 67 | sched.virtual_time = virtual_time; |
| 68 | let (root, _) = spawn_actor( |
| 69 | &sched, |
| 70 | Body::Entry(entry), |
| 71 | root_stack_size(), |
| 72 | None, |
| 73 | Tie::None, |
| 74 | ); |
| 75 | let exit = run(&sched, root, threads); |
| 76 | crate::console::flush(); |
| 77 | assert_eq!(sched.running.load(Ordering::SeqCst), 0, "running"); |
| 78 | assert_eq!(sched.pending.load(Ordering::SeqCst), 0, "pending"); |
| 79 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0, "io_waiting"); |
| 80 | assert!(sched.control().io_waiters.is_empty(), "io_waiters"); |
| 81 | assert!(sched.actors.all().is_empty(), "actors left"); |
| 82 | exit |
| 83 | } |
| 84 | |
| 85 | /// Spawn an actor running `entry`, untied to the running actor. |
| 86 | /// |
| 87 | /// # Safety |
| 88 | /// `ctx` must be the running actor's context. |
| 89 | unsafe fn spawn(ctx: *mut Ctx, entry: EntryFn) -> i64 { |
| 90 | // SAFETY: forwarded caller guarantee. |
| 91 | let (a, sched) = unsafe { this(ctx) }; |
| 92 | spawn_actor( |
| 93 | sched, |
| 94 | Body::Entry(entry), |
| 95 | STACK_SIZE, |
| 96 | Some(a.pid), |
| 97 | Tie::None, |
| 98 | ) |
| 99 | .0 |
| 100 | } |
| 101 | |
| 102 | /// How long [`yield_until`] waits before it fails: far more than any |
| 103 | /// condition here takes (microseconds), so a lost wake-up fails the test |
| 104 | /// instead of hanging it. |
| 105 | const PATIENCE: Duration = Duration::from_secs(10); |
| 106 | |
| 107 | /// Yield until `done` holds (`what`, for the failure); the other actors |
| 108 | /// run meanwhile. |
| 109 | /// |
| 110 | /// # Safety |
| 111 | /// `ctx` must be the running actor's context, on its own stack. |
| 112 | unsafe fn yield_until(ctx: *mut Ctx, what: &str, done: impl Fn(&Shared) -> bool) { |
| 113 | let limit = Instant::now() + PATIENCE; |
| 114 | // SAFETY: forwarded caller guarantee; the scheduler outlives its actors. |
| 115 | unsafe { |
| 116 | let (_, sched) = this(ctx); |
| 117 | while !done(sched) { |
| 118 | assert!(Instant::now() < limit, "still waiting: {what}"); |
| 119 | preempt_root(ctx); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Wait for a message nobody sends; returns once the actor is stopped. |
| 125 | /// |
| 126 | /// # Safety |
| 127 | /// `ctx` must be the running actor's context. |
| 128 | unsafe fn wait_forever(ctx: *mut Ctx) { |
| 129 | // SAFETY: forwarded caller guarantee. |
| 130 | unsafe { |
| 131 | let (a, sched) = this(ctx); |
| 132 | let tag = sched.tag(); |
| 133 | let received = a.with_proc(|p| p.receive(a.inbox(), Tags::One(tag), false, Timeout::Never)); |
| 134 | assert!(matches!(received, Received::Parked(_)), "no message"); |
| 135 | let woken = park(ctx); |
| 136 | drop_value(ctx, woken); |
| 137 | assert_ne!((*ctx).yielding, YIELD_NONE, "only an exit ends the wait"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// A connected pair of sockets; the first registered with the running |
| 142 | /// actor's reactor, for reading only (so its only events are the second |
| 143 | /// writing to it), with its token. |
| 144 | /// |
| 145 | /// # Safety |
| 146 | /// `ctx` must be the running actor's context. |
| 147 | unsafe fn socket_pair(ctx: *mut Ctx) -> (UnixStream, UnixStream, Token) { |
| 148 | // SAFETY: forwarded caller guarantee. |
| 149 | let (_, sched) = unsafe { this(ctx) }; |
| 150 | let (mut reader, writer) = UnixStream::pair().expect("a socket pair"); |
| 151 | let token = sched.io_token(); |
| 152 | sched |
| 153 | .with_registry(|r| r.register(&mut reader, token, Interest::READABLE)) |
| 154 | .expect("registered"); |
| 155 | (reader, writer, token) |
| 156 | } |
| 157 | |
| 158 | /// Park on socket `token`, as `net` does, unless an event came since the |
| 159 | /// count `seen`; false if the actor was stopped instead. |
| 160 | /// |
| 161 | /// # Safety |
| 162 | /// `ctx` must be the running actor's context. |
| 163 | unsafe fn wait_on_socket(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 164 | // SAFETY: forwarded caller guarantee. |
| 165 | unsafe { crate::net::wait_for(ctx, token, seen) } |
| 166 | } |
| 167 | |
| 168 | static COUNTED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 169 | |
| 170 | /// Yields a few times, then finishes. |
| 171 | unsafe extern "C" fn yields_then_ends(ctx: *mut Ctx) -> Value { |
| 172 | for _ in 0..3 { |
| 173 | // SAFETY: an actor's entry, on its own stack. |
| 174 | unsafe { preempt_root(ctx) }; |
| 175 | } |
| 176 | UNIT |
| 177 | } |
| 178 | |
| 179 | /// Waits on the socket in `COUNTED_TOKEN` until woken, then finishes. |
| 180 | unsafe extern "C" fn waits_on_the_socket(ctx: *mut Ctx) -> Value { |
| 181 | let token = Token(COUNTED_TOKEN.load(Ordering::SeqCst)); |
| 182 | // SAFETY: an actor's entry. |
| 183 | unsafe { |
| 184 | let seen = this(ctx).1.io_events(); |
| 185 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 186 | } |
| 187 | UNIT |
| 188 | } |
| 189 | |
| 190 | /// Spawns actors that yield and finish, and actors that wait on a socket |
| 191 | /// until the root writes to it; checks the counters while they wait. |
| 192 | unsafe extern "C" fn counts_actors(ctx: *mut Ctx) -> Value { |
| 193 | // SAFETY: the root actor's entry. |
| 194 | unsafe { |
| 195 | let (reader, mut writer, token) = socket_pair(ctx); |
| 196 | COUNTED_TOKEN.store(token.0, Ordering::SeqCst); |
| 197 | for _ in 0..4 { |
| 198 | spawn(ctx, yields_then_ends); |
| 199 | spawn(ctx, waits_on_the_socket); |
| 200 | } |
| 201 | // Only the root runs, nothing is queued, four wait on the socket. |
| 202 | yield_until(ctx, "the waiters to park", |s| { |
| 203 | s.io_waiting.load(Ordering::SeqCst) == 4 |
| 204 | && s.running.load(Ordering::SeqCst) == 1 |
| 205 | && s.pending.load(Ordering::SeqCst) == 0 |
| 206 | && s.actors.all().len() == 5 |
| 207 | }); |
| 208 | let (_, sched) = this(ctx); |
| 209 | assert_eq!(sched.control().io_waiters[&token].len(), 4); |
| 210 | writer.write_all(b"x").expect("written"); |
| 211 | yield_until(ctx, "the waiters to finish", |s| s.actors.all().len() == 1); |
| 212 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 213 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 214 | drop(reader); |
| 215 | } |
| 216 | UNIT |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn counters_rest_once_actors_ran_waited_on_a_socket_and_finished() { |
| 221 | for threads in THREADS { |
| 222 | assert_eq!( |
| 223 | run_root(threads, counts_actors), |
| 224 | Exit::Normal, |
| 225 | "{threads} threads" |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// Waits for a message nobody sends. |
| 231 | unsafe extern "C" fn waits_forever(ctx: *mut Ctx) -> Value { |
| 232 | // SAFETY: an actor's entry. |
| 233 | unsafe { wait_forever(ctx) }; |
| 234 | UNIT |
| 235 | } |
| 236 | |
| 237 | /// Spawns an actor waiting forever, then waits forever too. |
| 238 | unsafe extern "C" fn all_wait_forever(ctx: *mut Ctx) -> Value { |
| 239 | // SAFETY: the root actor's entry. |
| 240 | unsafe { |
| 241 | spawn(ctx, waits_forever); |
| 242 | wait_forever(ctx); |
| 243 | } |
| 244 | UNIT |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn actors_all_waiting_for_messages_are_a_deadlock() { |
| 249 | for threads in THREADS { |
| 250 | assert_eq!( |
| 251 | run_root(threads, all_wait_forever), |
| 252 | Exit::Crashed("deadlock: every actor is waiting for a message".into()), |
| 253 | "{threads} threads" |
| 254 | ); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static BUSY_STARTED: AtomicUsize = AtomicUsize::new(0); |
| 259 | static BUSY_DONE: AtomicUsize = AtomicUsize::new(0); |
| 260 | |
| 261 | /// Counts itself started, then holds its thread without yielding until |
| 262 | /// the other busy actor started too: it can only start on another thread. |
| 263 | unsafe extern "C" fn busy_until_both_run(_ctx: *mut Ctx) -> Value { |
| 264 | BUSY_STARTED.fetch_add(1, Ordering::SeqCst); |
| 265 | let limit = Instant::now() + PATIENCE; |
| 266 | while BUSY_STARTED.load(Ordering::SeqCst) < 2 { |
| 267 | assert!(Instant::now() < limit, "the other busy actor never ran"); |
| 268 | std::hint::spin_loop(); |
| 269 | } |
| 270 | BUSY_DONE.fetch_add(1, Ordering::SeqCst); |
| 271 | UNIT |
| 272 | } |
| 273 | |
| 274 | /// Once the other threads parked, spawns two busy actors in one slice, |
| 275 | /// then waits for both. |
| 276 | unsafe extern "C" fn spawns_two_busy(ctx: *mut Ctx) -> Value { |
| 277 | // SAFETY: the root actor's entry. |
| 278 | unsafe { |
| 279 | yield_until(ctx, "the other threads parked", |sched| { |
| 280 | sched.parking.parked() == 3 |
| 281 | }); |
| 282 | spawn(ctx, busy_until_both_run); |
| 283 | spawn(ctx, busy_until_both_run); |
| 284 | yield_until(ctx, "both busy actors done", |_| { |
| 285 | BUSY_DONE.load(Ordering::SeqCst) == 2 |
| 286 | }); |
| 287 | } |
| 288 | UNIT |
| 289 | } |
| 290 | |
| 291 | /// The second of two actors spawned in one slice waits in the spawner's |
| 292 | /// queue (the first is in its next slot) while every other thread sleeps: |
| 293 | /// one is woken to take it, so the two run in parallel (ADR 0004, |
| 294 | /// Revision 8). |
| 295 | #[test] |
| 296 | fn actors_spawned_together_run_in_parallel() { |
| 297 | BUSY_STARTED.store(0, Ordering::SeqCst); |
| 298 | BUSY_DONE.store(0, Ordering::SeqCst); |
| 299 | assert_eq!(run_root(4, spawns_two_busy), Exit::Normal); |
| 300 | } |
| 301 | |
| 302 | /// Sleeps (in virtual time) while another actor waits forever. |
| 303 | unsafe extern "C" fn sleeps_while_another_waits(ctx: *mut Ctx) -> Value { |
| 304 | // SAFETY: the root actor's entry. |
| 305 | unsafe { |
| 306 | spawn(ctx, waits_forever); |
| 307 | let (a, sched) = this(ctx); |
| 308 | let deadline = sched.clock() + 60_000; |
| 309 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 310 | sched.add_timer(deadline, a.pid, wait); |
| 311 | let woken = park(ctx); |
| 312 | assert_eq!(woken, UNIT); |
| 313 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 314 | // Virtual time: the clock jumped to the deadline. |
| 315 | assert_eq!(sched.clock(), deadline); |
| 316 | } |
| 317 | UNIT |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn an_actor_waiting_on_a_timer_is_no_deadlock() { |
| 322 | for threads in THREADS { |
| 323 | assert_eq!( |
| 324 | run_root(threads, sleeps_while_another_waits), |
| 325 | Exit::Normal, |
| 326 | "{threads} threads" |
| 327 | ); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static VTIME_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 332 | |
| 333 | /// Waits on the socket in `VTIME_TOKEN` until it is stopped: while it |
| 334 | /// waits, the root's timer must fire by a clock jump (LOUISE_VIRTUAL_TIME). |
| 335 | unsafe extern "C" fn waits_on_the_vtime_socket(ctx: *mut Ctx) -> Value { |
| 336 | let token = Token(VTIME_TOKEN.load(Ordering::SeqCst)); |
| 337 | // SAFETY: an actor's entry. |
| 338 | unsafe { |
| 339 | let seen = this(ctx).1.io_events(); |
| 340 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 341 | } |
| 342 | UNIT |
| 343 | } |
| 344 | |
| 345 | /// With LOUISE_VIRTUAL_TIME, opening a socket does not switch the clock |
| 346 | /// to real time, and a timer fires by a clock jump while another actor is |
| 347 | /// parked on a socket that never becomes ready (ADR 0055). |
| 348 | unsafe extern "C" fn sleeps_while_another_waits_on_a_socket(ctx: *mut Ctx) -> Value { |
| 349 | // SAFETY: the root actor's entry. |
| 350 | unsafe { |
| 351 | let (_reader, _writer, token) = socket_pair(ctx); |
| 352 | VTIME_TOKEN.store(token.0, Ordering::SeqCst); |
| 353 | let waiter = spawn(ctx, waits_on_the_vtime_socket); |
| 354 | yield_until(ctx, "the waiter to park", |s| { |
| 355 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 356 | }); |
| 357 | let (a, sched) = this(ctx); |
| 358 | assert!(!sched.real_time(), "a socket did not switch to real time"); |
| 359 | let deadline = sched.clock() + 60_000; |
| 360 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 361 | sched.add_timer(deadline, a.pid, wait); |
| 362 | let woken = park(ctx); |
| 363 | assert_eq!(woken, UNIT); |
| 364 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 365 | // Virtual time: the clock jumped to the deadline. |
| 366 | assert_eq!(sched.clock(), deadline); |
| 367 | assert!(!sched.real_time(), "the clock stayed virtual"); |
| 368 | super::exit_actor(sched, waiter, Exit::Killed); |
| 369 | yield_until(ctx, "the waiter to finish", |s| s.actors.all().len() == 1); |
| 370 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 371 | } |
| 372 | UNIT |
| 373 | } |
| 374 | |
| 375 | #[test] |
| 376 | fn virtual_time_jumps_while_an_actor_waits_on_a_socket() { |
| 377 | for threads in THREADS { |
| 378 | assert_eq!( |
| 379 | run_root_clock(threads, true, sleeps_while_another_waits_on_a_socket), |
| 380 | Exit::Normal, |
| 381 | "{threads} threads" |
| 382 | ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /// Without LOUISE_VIRTUAL_TIME, the first socket switches the clock to |
| 387 | /// real time (ADR 0004, Revision 3). |
| 388 | unsafe extern "C" fn opens_a_socket(ctx: *mut Ctx) -> Value { |
| 389 | // SAFETY: the root actor's entry. |
| 390 | unsafe { |
| 391 | let (_reader, _writer, _token) = socket_pair(ctx); |
| 392 | assert!( |
| 393 | this(ctx).1.real_time(), |
| 394 | "the first socket switches to real time" |
| 395 | ); |
| 396 | } |
| 397 | UNIT |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn a_socket_switches_to_real_time_by_default() { |
| 402 | for threads in THREADS { |
| 403 | assert_eq!( |
| 404 | run_root(threads, opens_a_socket), |
| 405 | Exit::Normal, |
| 406 | "{threads} threads" |
| 407 | ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /// Write a byte to `writer` from a thread outside the scheduler, once |
| 412 | /// every actor waits, one of them on a socket, and a scheduler thread |
| 413 | /// idles: that thread must wait on the reactor, not report a deadlock. |
| 414 | /// |
| 415 | /// # Safety |
| 416 | /// `sched` must outlive the thread: join it before the actor that |
| 417 | /// started it returns. |
| 418 | unsafe fn write_when_idle(sched: &Shared, mut writer: UnixStream) -> JoinHandle<()> { |
| 419 | let address = std::ptr::from_ref(sched) as usize; |
| 420 | std::thread::spawn(move || { |
| 421 | // SAFETY: caller guarantee. |
| 422 | let sched = unsafe { &*(address as *const Shared) }; |
| 423 | while !(sched.idling.load(Ordering::SeqCst) && sched.io_waiting.load(Ordering::SeqCst) == 1) |
| 424 | { |
| 425 | std::thread::sleep(Duration::from_micros(100)); |
| 426 | } |
| 427 | writer.write_all(b"x").expect("written"); |
| 428 | }) |
| 429 | } |
| 430 | |
| 431 | /// Waits on a socket while another actor waits forever, until a thread |
| 432 | /// outside the scheduler writes to it. |
| 433 | unsafe extern "C" fn reads_while_another_waits(ctx: *mut Ctx) -> Value { |
| 434 | // SAFETY: the root actor's entry; it joins the writer before it |
| 435 | // returns. |
| 436 | unsafe { |
| 437 | spawn(ctx, waits_forever); |
| 438 | let (mut reader, writer, token) = socket_pair(ctx); |
| 439 | let (_, sched) = this(ctx); |
| 440 | let outside = write_when_idle(sched, writer); |
| 441 | let mut byte = [0]; |
| 442 | loop { |
| 443 | let seen = sched.io_events(); |
| 444 | match reader.read(&mut byte) { |
| 445 | Ok(n) => { |
| 446 | assert_eq!(n, 1); |
| 447 | break; |
| 448 | } |
| 449 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 450 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 451 | } |
| 452 | Err(e) => panic!("read: {e}"), |
| 453 | } |
| 454 | } |
| 455 | outside.join().expect("the writer"); |
| 456 | } |
| 457 | UNIT |
| 458 | } |
| 459 | |
| 460 | #[test] |
| 461 | fn an_actor_waiting_on_a_socket_is_no_deadlock() { |
| 462 | for threads in THREADS { |
| 463 | assert_eq!( |
| 464 | run_root(threads, reads_while_another_waits), |
| 465 | Exit::Normal, |
| 466 | "{threads} threads" |
| 467 | ); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static SHARED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 472 | static WOKEN: AtomicUsize = AtomicUsize::new(0); |
| 473 | |
| 474 | /// Waits on the socket in `SHARED_TOKEN`, then counts itself woken. |
| 475 | unsafe extern "C" fn waits_on_the_shared_socket(ctx: *mut Ctx) -> Value { |
| 476 | let token = Token(SHARED_TOKEN.load(Ordering::SeqCst)); |
| 477 | // SAFETY: an actor's entry. |
| 478 | unsafe { |
| 479 | let seen = this(ctx).1.io_events(); |
| 480 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 481 | } |
| 482 | WOKEN.fetch_add(1, Ordering::SeqCst); |
| 483 | UNIT |
| 484 | } |
| 485 | |
| 486 | /// Three actors wait on one socket; one event wakes them all. |
| 487 | unsafe extern "C" fn wakes_every_waiter(ctx: *mut Ctx) -> Value { |
| 488 | // SAFETY: the root actor's entry. |
| 489 | unsafe { |
| 490 | WOKEN.store(0, Ordering::SeqCst); |
| 491 | let (reader, mut writer, token) = socket_pair(ctx); |
| 492 | SHARED_TOKEN.store(token.0, Ordering::SeqCst); |
| 493 | let mut pids: Vec<i64> = (0..3) |
| 494 | .map(|_| spawn(ctx, waits_on_the_shared_socket)) |
| 495 | .collect(); |
| 496 | yield_until(ctx, "three waiters", |s| { |
| 497 | s.io_waiting.load(Ordering::SeqCst) == 3 |
| 498 | }); |
| 499 | let (_, sched) = this(ctx); |
| 500 | let mut waiting = sched.control().io_waiters[&token].clone(); |
| 501 | waiting.sort_unstable(); |
| 502 | pids.sort_unstable(); |
| 503 | assert_eq!(waiting, pids, "every waiter is kept"); |
| 504 | assert_eq!(WOKEN.load(Ordering::SeqCst), 0); |
| 505 | writer.write_all(b"x").expect("written"); |
| 506 | yield_until(ctx, "three woken", |_| WOKEN.load(Ordering::SeqCst) == 3); |
| 507 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 508 | drop(reader); |
| 509 | } |
| 510 | UNIT |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn every_actor_waiting_on_a_socket_is_woken_by_its_event() { |
| 515 | for threads in THREADS { |
| 516 | assert_eq!( |
| 517 | run_root(threads, wakes_every_waiter), |
| 518 | Exit::Normal, |
| 519 | "{threads} threads" |
| 520 | ); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static KEEPER_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 525 | |
| 526 | /// Waits on the socket in `KEEPER_TOKEN` until it is stopped: while it |
| 527 | /// waits, scheduler threads poll the reactor between slices. |
| 528 | unsafe extern "C" fn waits_until_stopped(ctx: *mut Ctx) -> Value { |
| 529 | let token = Token(KEEPER_TOKEN.load(Ordering::SeqCst)); |
| 530 | // SAFETY: an actor's entry. |
| 531 | unsafe { |
| 532 | let seen = this(ctx).1.io_events(); |
| 533 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 534 | } |
| 535 | UNIT |
| 536 | } |
| 537 | |
| 538 | /// An event that comes between an operation's `WouldBlock` and its |
| 539 | /// parking: the parking is refused, and the retry finds the data. |
| 540 | unsafe extern "C" fn retries_after_a_missed_event(ctx: *mut Ctx) -> Value { |
| 541 | // SAFETY: the root actor's entry. |
| 542 | unsafe { |
| 543 | let (a, sched) = this(ctx); |
| 544 | let (_keeper_reader, _keeper_writer, keeper_token) = socket_pair(ctx); |
| 545 | KEEPER_TOKEN.store(keeper_token.0, Ordering::SeqCst); |
| 546 | let keeper = spawn(ctx, waits_until_stopped); |
| 547 | yield_until(ctx, "the keeper to park", |s| { |
| 548 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 549 | }); |
| 550 | |
| 551 | let (mut reader, mut writer, token) = socket_pair(ctx); |
| 552 | let mut byte = [0]; |
| 553 | let seen = sched.io_events(); |
| 554 | let blocked = reader.read(&mut byte).expect_err("nothing to read yet"); |
| 555 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 556 | // The event comes now, and another thread's poll takes it: nobody |
| 557 | // waits on this socket, but the count moves. |
| 558 | writer.write_all(b"x").expect("written"); |
| 559 | yield_until(ctx, "the event to be counted", |s| s.io_events() != seen); |
| 560 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 561 | assert!(!sched.io_wait(token, a.pid, seen), "parking refused"); |
| 562 | a.with_proc(|p| p.wait = Wait::Ready); |
| 563 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 564 | assert_eq!( |
| 565 | sched.io_waiting.load(Ordering::SeqCst), |
| 566 | 1, |
| 567 | "the keeper only" |
| 568 | ); |
| 569 | assert_eq!(reader.read(&mut byte).expect("the retry reads"), 1); |
| 570 | |
| 571 | // With no event since, the same parking is accepted. |
| 572 | let seen = sched.io_events(); |
| 573 | let blocked = reader.read(&mut byte).expect_err("nothing to read"); |
| 574 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 575 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 576 | assert!(sched.io_wait(token, a.pid, seen), "parked"); |
| 577 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 2); |
| 578 | sched.io_unwait(token, a.pid); |
| 579 | a.with_proc(|p| p.wait = Wait::Ready); |
| 580 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 1); |
| 581 | |
| 582 | // A stopped waiter's wait is forgotten. |
| 583 | super::exit_actor(sched, keeper, Exit::Killed); |
| 584 | yield_until(ctx, "the keeper to finish", |s| s.actors.all().len() == 1); |
| 585 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 586 | assert!(sched.control().io_waiters.is_empty()); |
| 587 | } |
| 588 | UNIT |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn an_event_before_parking_makes_the_operation_retry() { |
| 593 | for threads in THREADS { |
| 594 | assert_eq!( |
| 595 | run_root(threads, retries_after_a_missed_event), |
| 596 | Exit::Normal, |
| 597 | "{threads} threads" |
| 598 | ); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | const TEST_HANDLER_STACK: usize = 64 << 10; |
| 603 | |
| 604 | /// Write a marker near the unused top so warm retention and `release` are |
| 605 | /// observable. `top - 16` is aligned and above the guard page. |
| 606 | fn mark_stack(stack: &Stack, marker: usize) -> usize { |
| 607 | let address = stack.top() - 16; |
| 608 | // SAFETY: an unused stack owns this aligned, mapped word. |
| 609 | unsafe { (address as *mut usize).write_volatile(marker) }; |
| 610 | address |
| 611 | } |
| 612 | |
| 613 | fn stack_marker(address: usize) -> usize { |
| 614 | // SAFETY: the caller keeps the marked stack alive. |
| 615 | unsafe { (address as *const usize).read_volatile() } |
| 616 | } |
| 617 | |
| 618 | #[test] |
| 619 | fn handler_stack_pool_miss_hit_and_size() { |
| 620 | let _turn = test_turn(); |
| 621 | let pool = HandlerStacks::new(2, 4); |
| 622 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "empty miss"); |
| 623 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 624 | assert_eq!(stack.size(), TEST_HANDLER_STACK); |
| 625 | let top = stack.top(); |
| 626 | pool.pool(stack, TEST_HANDLER_STACK); |
| 627 | let reused = pool.take(TEST_HANDLER_STACK).expect("warm hit"); |
| 628 | assert_eq!(reused.top(), top, "the same mapping was reused"); |
| 629 | assert_eq!(reused.size(), TEST_HANDLER_STACK); |
| 630 | pool.pool(reused, TEST_HANDLER_STACK); |
| 631 | // Ordinary pool drop unmaps the retained stack. |
| 632 | } |
| 633 | |
| 634 | #[test] |
| 635 | fn handler_stack_pool_releases_only_past_warm_limit() { |
| 636 | let _turn = test_turn(); |
| 637 | let warm = HandlerStacks::new(1, 2); |
| 638 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 639 | let marked = mark_stack(&stack, 0x5a17); |
| 640 | warm.pool(stack, TEST_HANDLER_STACK); |
| 641 | let stack = warm.take(TEST_HANDLER_STACK).expect("retained"); |
| 642 | assert_eq!(stack_marker(marked), 0x5a17, "warm pages retained"); |
| 643 | |
| 644 | // Keep one warm entry, then pool another: the latter is cold-released |
| 645 | // and reads as zero when taken again, without any large mapping stress. |
| 646 | warm.pool(stack, TEST_HANDLER_STACK); |
| 647 | let cold = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 648 | let cleared = mark_stack(&cold, 0x5a17); |
| 649 | warm.pool(cold, TEST_HANDLER_STACK); |
| 650 | let cold = warm |
| 651 | .take(TEST_HANDLER_STACK) |
| 652 | .expect("cold retained mapping"); |
| 653 | assert_eq!( |
| 654 | cold.size(), |
| 655 | TEST_HANDLER_STACK, |
| 656 | "cold mapping keeps its size" |
| 657 | ); |
| 658 | assert_eq!(stack_marker(cleared), 0, "cold pages released"); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn handler_stack_pool_releases_over_capacity_without_double_insertion() { |
| 663 | let _turn = test_turn(); |
| 664 | let pool = HandlerStacks::new(0, 2); |
| 665 | let first = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 666 | let second = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 667 | let surplus = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 668 | let first_top = first.top(); |
| 669 | let second_top = second.top(); |
| 670 | pool.pool(first, TEST_HANDLER_STACK); |
| 671 | pool.pool(second, TEST_HANDLER_STACK); |
| 672 | // Over capacity: dropped/unmapped here, outside the pool mutex. |
| 673 | pool.pool(surplus, TEST_HANDLER_STACK); |
| 674 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), second_top); |
| 675 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), first_top); |
| 676 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "no duplicate"); |
| 677 | let stats = pool.stats(); |
| 678 | assert_eq!((stats.hits, stats.misses), (2, 1)); |
| 679 | assert_eq!(stats.unmaps, 1); |
| 680 | } |
| 681 | |
| 682 | #[test] |
| 683 | fn handler_stack_pool_production_cap_is_stable_across_refills() { |
| 684 | let _turn = test_turn(); |
| 685 | // Small mappings exercise the real 16-warm/64-cap policy without making |
| 686 | // this unit test reserve the production 72 × 18.131 MiB extent. |
| 687 | let pool = HandlerStacks::new(WARM_HANDLER_STACKS, HANDLER_STACK_POOL); |
| 688 | let mut live = Vec::new(); |
| 689 | for round in 0..4 { |
| 690 | while let Some(stack) = pool.take(TEST_HANDLER_STACK) { |
| 691 | live.push(stack); |
| 692 | } |
| 693 | while live.len() < 72 { |
| 694 | live.push(Stack::new(TEST_HANDLER_STACK).unwrap()); |
| 695 | } |
| 696 | for stack in live.drain(..) { |
| 697 | pool.pool(stack, TEST_HANDLER_STACK); |
| 698 | } |
| 699 | assert_eq!(pool.stacks().len(), HANDLER_STACK_POOL); |
| 700 | let stats = pool.stats(); |
| 701 | assert_eq!(stats.warm_inserts, WARM_HANDLER_STACKS * (round + 1)); |
| 702 | assert_eq!( |
| 703 | stats.cold_releases, |
| 704 | (HANDLER_STACK_POOL - WARM_HANDLER_STACKS) * (round + 1) |
| 705 | ); |
| 706 | assert_eq!(stats.unmaps, (72 - HANDLER_STACK_POOL) * (round + 1)); |
| 707 | } |
| 708 | let stats = pool.stats(); |
| 709 | assert_eq!(stats.hits, HANDLER_STACK_POOL * 3); |
| 710 | assert_eq!(stats.misses, 4); |
| 711 | } |
| 712 | |
| 713 | #[test] |
| 714 | fn ordinary_handler_pool_drop_unmaps_retained_stack() { |
| 715 | // Shared's ordinary (non-PROGRAM_MODE) teardown drops this same vector. |
| 716 | // Probe by address, without dereferencing the dropped stack: an exclusive |
| 717 | // no-replace mapping can succeed only if the old extent was unmapped. |
| 718 | let _turn = test_turn(); |
| 719 | let pool = HandlerStacks::new(1, 1); |
| 720 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 721 | let (base, len) = stack.map_extent(); |
| 722 | pool.pool(stack, TEST_HANDLER_STACK); |
| 723 | drop(pool); |
| 724 | // SAFETY: `base..base+len` was just released; NOREPLACE refuses rather |
| 725 | // than replacing anything another thread may have mapped there. The new |
| 726 | // PROT_NONE mapping is this test's own and is unmapped exactly once. |
| 727 | unsafe { |
| 728 | let replacement = libc::mmap( |
| 729 | base.cast(), |
| 730 | len, |
| 731 | libc::PROT_NONE, |
| 732 | libc::MAP_PRIVATE |
| 733 | | libc::MAP_ANONYMOUS |
| 734 | | libc::MAP_NORESERVE |
| 735 | | libc::MAP_FIXED_NOREPLACE, |
| 736 | -1, |
| 737 | 0, |
| 738 | ); |
| 739 | assert_ne!( |
| 740 | replacement, |
| 741 | libc::MAP_FAILED, |
| 742 | "retained extent is still mapped: {}", |
| 743 | std::io::Error::last_os_error() |
| 744 | ); |
| 745 | assert_eq!(libc::munmap(replacement, len), 0); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // ---- fs owned adapters: a deterministic kill while their actor parks ------ |
| 750 | |
| 751 | /// One scenario's synchronization and lifecycle evidence. Each run owns a |
| 752 | /// fresh value, whose address is kept by the caller until scheduler and pool |
| 753 | /// work finish; there is no test-global state or real file operation. |
| 754 | #[derive(Default)] |
| 755 | struct FsAdapterControl { |
| 756 | worker_started: AtomicBool, |
| 757 | release_worker: Mutex<bool>, |
| 758 | release: Condvar, |
| 759 | killed_while_blocked: AtomicBool, |
| 760 | killer_done: AtomicBool, |
| 761 | worker_done: AtomicBool, |
| 762 | worker_on_io_pool: AtomicBool, |
| 763 | argument_finalizers: AtomicUsize, |
| 764 | result_finalizers: AtomicUsize, |
| 765 | unwound: AtomicUsize, |
| 766 | } |
| 767 | |
| 768 | unsafe extern "C" fn fs_argument_finalizer(ptr: *mut std::ffi::c_void) { |
| 769 | // SAFETY: every test resource carries the still-live scenario address. |
| 770 | let control = unsafe { &*(ptr as *const FsAdapterControl) }; |
| 771 | control.argument_finalizers.fetch_add(1, Ordering::SeqCst); |
| 772 | } |
| 773 | |
| 774 | unsafe extern "C" fn fs_result_finalizer(ptr: *mut std::ffi::c_void) { |
| 775 | // SAFETY: as `fs_argument_finalizer`. |
| 776 | let control = unsafe { &*(ptr as *const FsAdapterControl) }; |
| 777 | control.result_finalizers.fetch_add(1, Ordering::SeqCst); |
| 778 | } |
| 779 | |
| 780 | /// A closure with at most two immediate fields, for the test root and killer. |
| 781 | /// Its parameters are `(self, proc root, unit, ...)` because test scheduler |
| 782 | /// programs use the default one-root actor layout. |
| 783 | unsafe fn fs_test_closure(ctx: *mut Ctx, code: crate::process::Code, env: &[Value]) -> Value { |
| 784 | assert!(env.len() <= 2); |
| 785 | // SAFETY: both entries have the runtime closure code-pointer layout; |
| 786 | // `immortal_cap` uses the same type erasure for runtime root clauses. |
| 787 | let closure = unsafe { |
| 788 | crate::values::tl_closure_alloc( |
| 789 | ctx, |
| 790 | std::mem::transmute::<crate::process::Code, crate::values::ClosureFn>(code), |
| 791 | std::mem::transmute::<crate::process::Code, crate::values::ClosureFn>( |
| 792 | crate::values::tl_borrowed0, |
| 793 | ), |
| 794 | env.len() as u8, |
| 795 | ) |
| 796 | }; |
| 797 | // SAFETY: the closure was allocated with exactly `env.len()` fields. |
| 798 | unsafe { |
| 799 | Block::fields_ptr(closure as *mut Block).copy_from_nonoverlapping(env.as_ptr(), env.len()); |
| 800 | } |
| 801 | closure |
| 802 | } |
| 803 | |
| 804 | /// A controlled I/O-pool callback. The observed arguments are resources with |
| 805 | /// finalizers, so freeing either an input or the result twice is visible. |
| 806 | unsafe extern "C" fn controlled_fs_thunk(ctx: *mut Ctx, args: *const Value) -> Value { |
| 807 | // SAFETY: the adapter passes one/two live borrowed test resources. |
| 808 | let ptr = unsafe { crate::resource::tl_resource_get(args.read()) }; |
| 809 | let control = ptr as *mut FsAdapterControl; |
| 810 | // SAFETY: the scenario owns `control` until this worker is complete. |
| 811 | let control = unsafe { &*control }; |
| 812 | control.worker_on_io_pool.store( |
| 813 | std::thread::current() |
| 814 | .name() |
| 815 | .is_some_and(|name| name.starts_with("louise-blocking-io-")), |
| 816 | Ordering::SeqCst, |
| 817 | ); |
| 818 | assert!(!control.worker_started.swap(true, Ordering::SeqCst)); |
| 819 | |
| 820 | // Hold the actor in Wait::Blocking until the killer has completed its |
| 821 | // logical exit. No operation, disk access or unbounded work runs here. |
| 822 | let mut released = control.release_worker.lock().expect("release lock"); |
| 823 | while !*released { |
| 824 | released = control.release.wait(released).expect("release wait"); |
| 825 | } |
| 826 | drop(released); |
| 827 | |
| 828 | // Return a fresh owned result even though the actor is already dead. |
| 829 | // Scheduler wake-up/park must dispose of it once while unwinding. |
| 830 | let result = crate::resource::tl_resource_new( |
| 831 | ctx, |
| 832 | std::ptr::from_ref(control) as *mut std::ffi::c_void, |
| 833 | Some(fs_result_finalizer), |
| 834 | true, |
| 835 | ); |
| 836 | control.worker_done.store(true, Ordering::SeqCst); |
| 837 | result |
| 838 | } |
| 839 | |
| 840 | /// The second actor observes the controlled worker start, kills its parent |
| 841 | /// and only then releases the worker. On either one or four scheduler threads |
| 842 | /// the parent cannot resume before both checkpoints have happened. |
| 843 | unsafe extern "C" fn fs_adapter_killer( |
| 844 | ctx: *mut Ctx, |
| 845 | this_: Value, |
| 846 | proc: Value, |
| 847 | _unit: Value, |
| 848 | _: Value, |
| 849 | _: Value, |
| 850 | _: Value, |
| 851 | ) -> Value { |
| 852 | // SAFETY: closure of `fs_test_closure` and the program actor convention. |
| 853 | unsafe { |
| 854 | let fields = Block::fields_ptr(this_ as *mut Block); |
| 855 | let control = int_get(fields.read()) as usize as *const FsAdapterControl; |
| 856 | let target = int_get(fields.add(1).read()); |
| 857 | drop_value(ctx, this_); |
| 858 | drop_value(ctx, proc); |
| 859 | let limit = Instant::now() + PATIENCE; |
| 860 | while !(*control).worker_started.load(Ordering::SeqCst) { |
| 861 | assert!(Instant::now() < limit, "the fs worker did not start"); |
| 862 | preempt_root(ctx); |
| 863 | } |
| 864 | let (_, sched) = this(ctx); |
| 865 | super::exit_actor(sched, target, Exit::Killed); |
| 866 | (*control) |
| 867 | .killed_while_blocked |
| 868 | .store(true, Ordering::SeqCst); |
| 869 | (*control).killer_done.store(true, Ordering::SeqCst); |
| 870 | { |
| 871 | let mut released = (*control).release_worker.lock().expect("release lock"); |
| 872 | *released = true; |
| 873 | (*control).release.notify_one(); |
| 874 | } |
| 875 | UNIT |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /// Parent/blocked actor. It never reads the inputs after handing them to the |
| 880 | /// adapter, nor reads the worker's result after park disposes of it. |
| 881 | unsafe extern "C" fn killed_in_fs_adapter( |
| 882 | ctx: *mut Ctx, |
| 883 | this_: Value, |
| 884 | proc: Value, |
| 885 | _unit: Value, |
| 886 | _: Value, |
| 887 | _: Value, |
| 888 | _: Value, |
| 889 | ) -> Value { |
| 890 | // SAFETY: as `fs_adapter_killer`. |
| 891 | unsafe { |
| 892 | let fields = Block::fields_ptr(this_ as *mut Block); |
| 893 | let control = int_get(fields.read()) as usize as *mut FsAdapterControl; |
| 894 | let arity = int_get(fields.add(1).read()); |
| 895 | drop_value(ctx, this_); |
| 896 | drop_value(ctx, proc); |
| 897 | assert!(arity == 1 || arity == 2); |
| 898 | let mut args = [UNIT; 2]; |
| 899 | for arg in args.iter_mut().take(arity as usize) { |
| 900 | *arg = crate::resource::tl_resource_new( |
| 901 | ctx, |
| 902 | control as *mut std::ffi::c_void, |
| 903 | Some(fs_argument_finalizer), |
| 904 | true, |
| 905 | ); |
| 906 | } |
| 907 | let (me, _) = this(ctx); |
| 908 | let killer = fs_test_closure( |
| 909 | ctx, |
| 910 | fs_adapter_killer, |
| 911 | &[int(control as usize as i64), int(me.pid)], |
| 912 | ); |
| 913 | crate::sched::spawn_program(ctx, killer, Tie::None); |
| 914 | let out = if arity == 1 { |
| 915 | crate::fs::test_blocking1(ctx, args[0], controlled_fs_thunk) |
| 916 | } else { |
| 917 | crate::fs::test_blocking2(ctx, args[0], args[1], controlled_fs_thunk) |
| 918 | }; |
| 919 | assert_eq!(out, UNIT, "a killed adapter returns unit"); |
| 920 | assert_eq!( |
| 921 | (*ctx).yielding, |
| 922 | YIELD_KILL, |
| 923 | "the completed worker resumes one unwinding" |
| 924 | ); |
| 925 | (*control).unwound.fetch_add(1, Ordering::SeqCst); |
| 926 | assert_eq!( |
| 927 | (*control).argument_finalizers.load(Ordering::SeqCst), |
| 928 | arity as usize |
| 929 | ); |
| 930 | assert_eq!((*control).result_finalizers.load(Ordering::SeqCst), 1); |
| 931 | assert!((*control).killed_while_blocked.load(Ordering::SeqCst)); |
| 932 | assert!((*control).worker_done.load(Ordering::SeqCst)); |
| 933 | assert!((*control).worker_on_io_pool.load(Ordering::SeqCst)); |
| 934 | UNIT |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /// Run one fresh scenario with its control address in the root closure. |
| 939 | fn run_fs_adapter_kill(threads: usize, arity: i64) { |
| 940 | let _turn = test_turn(); |
| 941 | let control = FsAdapterControl::default(); |
| 942 | // The root actor owns a closure holding only this address and arity. |
| 943 | // `run` does not return until all finalizers and the one unwind finished. |
| 944 | // SAFETY: `control` stays allocated at this stack address until `run` |
| 945 | // returns; it is not moved while the root/killer closures, pool thunk and |
| 946 | // argument/result finalizers can hold its borrowed address. No use remains |
| 947 | // after that synchronous join. `fs_test_closure` is given the matching |
| 948 | // owned entry and exactly two immediate fields, both initialized here. |
| 949 | let body = unsafe { |
| 950 | fs_test_closure( |
| 951 | std::ptr::null_mut(), |
| 952 | killed_in_fs_adapter, |
| 953 | &[ |
| 954 | int(std::ptr::from_ref(&control) as usize as i64), |
| 955 | int(arity), |
| 956 | ], |
| 957 | ) |
| 958 | }; |
| 959 | let sched = Shared::new(threads); |
| 960 | let (root, _) = spawn_actor( |
| 961 | &sched, |
| 962 | Body::Program(body), |
| 963 | root_stack_size(), |
| 964 | None, |
| 965 | Tie::None, |
| 966 | ); |
| 967 | assert_eq!( |
| 968 | run(&sched, root, threads), |
| 969 | Exit::Killed, |
| 970 | "{threads} threads" |
| 971 | ); |
| 972 | assert!(control.killer_done.load(Ordering::SeqCst)); |
| 973 | crate::console::flush(); |
| 974 | assert_eq!(control.unwound.load(Ordering::SeqCst), 1); |
| 975 | assert!(sched.control().io_waiters.is_empty()); |
| 976 | assert!(sched.actors.all().is_empty(), "actors left"); |
| 977 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 978 | } |
| 979 | |
| 980 | #[test] |
| 981 | fn fs_owned_adapters_destroy_args_and_result_when_killed_while_blocked() { |
| 982 | for threads in THREADS { |
| 983 | // Both exact helper bodies: one resource input through blocking1 and |
| 984 | // two independently owned inputs through blocking2. Four tiny |
| 985 | // scenarios use no real filesystem objects or large allocation. |
| 986 | run_fs_adapter_kill(threads, 1); |
| 987 | run_fs_adapter_kill(threads, 2); |
| 988 | } |
| 989 | } |