| @@ -67,13 +67,8 @@ pub(crate) enum Resume { |
| 67 | 67 | |
| 68 | 68 | pub(crate) enum Request { |
| 69 | 69 | /// An operation for the handler with this marker; the payload is the |
| 70 | | − /// operation's index, then its arguments. `multi`: a `multi ctl` |
| 71 | | − /// operation. |
| 72 | | − Op { |
| 73 | | − marker: u64, |
| 74 | | − payload: Value, |
| 75 | | − multi: bool, |
| 76 | | − }, |
| 70 | + /// operation's index, then its arguments. |
| 71 | + Op { marker: u64, payload: Value }, |
| 77 | 72 | /// The actor's reduction budget is spent: yield to the scheduler. |
| 78 | 73 | Preempt, |
| 79 | 74 | } |
| @@ -81,21 +76,15 @@ pub(crate) enum Request { |
| 81 | 76 | pub(crate) type HandlerYielder = Yielder<Resume, Request>; |
| 82 | 77 | type HandlerFiber = Coroutine<Resume, Request, Value>; |
| 83 | 78 | |
| 84 | | −/// The handler fiber running now, per actor: its yielder, and its frame. |
| 85 | | −/// Both null on the actor's own stack. |
| 86 | | −#[derive(Clone, Copy)] |
| 79 | +/// A handler fiber's yielder and frame. It lives at the bottom of the |
| 80 | +/// fiber's own stack; each actor points at the one of the fiber running |
| 81 | +/// now (null on the actor's own stack). One pointer, so that saving it in |
| 82 | +/// `drive` costs one word. |
| 87 | 83 | pub(crate) struct Running { |
| 88 | 84 | yielder: *const HandlerYielder, |
| 89 | 85 | frame: *mut Frame, |
| 90 | 86 | } |
| 91 | 87 | |
| 92 | | −impl Running { |
| 93 | | − pub(crate) const NONE: Running = Running { |
| 94 | | − yielder: std::ptr::null(), |
| 95 | | − frame: std::ptr::null_mut(), |
| 96 | | − }; |
| 97 | | −} |
| 98 | | − |
| 99 | 88 | struct Frame { |
| 100 | 89 | marker: u64, |
| 101 | 90 | fiber: Option<HandlerFiber>, |
| @@ -118,6 +107,8 @@ struct Frame { |
| 118 | 107 | |
| 119 | 108 | /// The captures of a multi-shot handler. |
| 120 | 109 | struct Multi { |
| 110 | + /// Bit `i`: operation `i` is `multi ctl` (the others are `ctl`). |
| 111 | + ops: u64, |
| 121 | 112 | captures: Vec<Capture>, |
| 122 | 113 | /// The capture the fiber's stack holds, suspended at its operation. |
| 123 | 114 | occupant: Option<usize>, |
| @@ -180,7 +171,7 @@ pub(crate) unsafe fn free_frame(b: *mut Block, pending: &mut Vec<*mut Block>) { |
| 180 | 171 | |
| 181 | 172 | /// # Safety |
| 182 | 173 | /// `ctx` must be the running actor's context. |
| 183 | | −unsafe fn current<'a>(ctx: *mut Ctx) -> &'a mut Running { |
| 174 | +unsafe fn current<'a>(ctx: *mut Ctx) -> &'a mut *const Running { |
| 184 | 175 | // SAFETY: forwarded caller guarantee. |
| 185 | 176 | unsafe { crate::sched::current_handler(ctx) } |
| 186 | 177 | } |
| @@ -201,10 +192,11 @@ pub unsafe extern "C" fn tl_handle_ctl( |
| 201 | 192 | marker: u64, |
| 202 | 193 | ) -> Value { |
| 203 | 194 | // SAFETY: forwarded caller guarantee. |
| 204 | | − unsafe { handle(ctx, body, cap, ret, marker, false) } |
| 195 | + unsafe { handle(ctx, body, cap, ret, marker, None) } |
| 205 | 196 | } |
| 206 | 197 | |
| 207 | | −/// [`tl_handle_ctl`] for an effect with `multi ctl` operations. |
| 198 | +/// [`tl_handle_ctl`] for an effect with `multi ctl` operations: bit `i` |
| 199 | +/// of `multi_ops` is set if operation `i` is one. |
| 208 | 200 | /// |
| 209 | 201 | /// # Safety |
| 210 | 202 | /// As for [`tl_handle_ctl`]. |
| @@ -215,9 +207,10 @@ pub unsafe extern "C" fn tl_handle_multi( |
| 215 | 207 | cap: Value, |
| 216 | 208 | ret: Value, |
| 217 | 209 | marker: u64, |
| 210 | + multi_ops: u64, |
| 218 | 211 | ) -> Value { |
| 219 | 212 | // SAFETY: forwarded caller guarantee. |
| 220 | | − unsafe { handle(ctx, body, cap, ret, marker, true) } |
| 213 | + unsafe { handle(ctx, body, cap, ret, marker, Some(multi_ops)) } |
| 221 | 214 | } |
| 222 | 215 | |
| 223 | 216 | /// # Safety |
| @@ -228,7 +221,7 @@ unsafe fn handle( |
| 228 | 221 | cap: Value, |
| 229 | 222 | ret: Value, |
| 230 | 223 | marker: u64, |
| 231 | | − multi: bool, |
| 224 | + multi_ops: Option<u64>, |
| 232 | 225 | ) -> Value { |
| 233 | 226 | let block = tl_alloc(ctx, TAG_FRAME, 0, WORD) as Value; |
| 234 | 227 | let body_cap = dup(cap); |
| @@ -241,7 +234,8 @@ unsafe fn handle( |
| 241 | 234 | waiting: false, |
| 242 | 235 | cancelled: false, |
| 243 | 236 | nested: 0, |
| 244 | | − multi: multi.then(|| Multi { |
| 237 | + multi: multi_ops.map(|ops| Multi { |
| 238 | + ops, |
| 245 | 239 | captures: Vec::new(), |
| 246 | 240 | occupant: None, |
| 247 | 241 | in_use: true, |
| @@ -253,10 +247,11 @@ unsafe fn handle( |
| 253 | 247 | let ctx = ctx_addr as *mut Ctx; |
| 254 | 248 | // SAFETY: the fiber runs on the actor that created it. |
| 255 | 249 | unsafe { |
| 256 | | − *current(ctx) = Running { |
| 250 | + let running = Running { |
| 257 | 251 | yielder, |
| 258 | 252 | frame: frame_addr as *mut Frame, |
| 259 | 253 | }; |
| 254 | + *current(ctx) = &running; |
| 260 | 255 | call_closure(ctx, body, &[body_cap]) |
| 261 | 256 | } |
| 262 | 257 | }); |
| @@ -267,7 +262,12 @@ unsafe fn handle( |
| 267 | 262 | Block::fields_ptr(block as *mut Block) |
| 268 | 263 | .cast::<*mut Frame>() |
| 269 | 264 | .write(f); |
| 270 | | − let parent = current(ctx).frame; |
| 265 | + let running = *current(ctx); |
| 266 | + let parent = if running.is_null() { |
| 267 | + std::ptr::null_mut() |
| 268 | + } else { |
| 269 | + (*running).frame |
| 270 | + }; |
| 271 | 271 | if !parent.is_null() { |
| 272 | 272 | (*parent).nested += 1; |
| 273 | 273 | } |
| @@ -293,36 +293,12 @@ unsafe fn handle( |
| 293 | 293 | /// `ctx` must be the running actor's context; `cap` a handler capability. |
| 294 | 294 | #[unsafe(no_mangle)] |
| 295 | 295 | pub unsafe extern "C" fn tl_perform_ctl(ctx: *mut Ctx, cap: Value, payload: Value) -> Value { |
| 296 | | − // SAFETY: forwarded caller guarantee. |
| 297 | | − unsafe { perform(ctx, cap, payload, false) } |
| 298 | | −} |
| 299 | | − |
| 300 | | −/// [`tl_perform_ctl`] for a `multi ctl` operation. |
| 301 | | −/// |
| 302 | | −/// # Safety |
| 303 | | −/// As for [`tl_perform_ctl`]. |
| 304 | | −#[unsafe(no_mangle)] |
| 305 | | −pub unsafe extern "C" fn tl_perform_multi(ctx: *mut Ctx, cap: Value, payload: Value) -> Value { |
| 306 | | − // SAFETY: forwarded caller guarantee. |
| 307 | | − unsafe { perform(ctx, cap, payload, true) } |
| 308 | | −} |
| 309 | | − |
| 310 | | −/// # Safety |
| 311 | | −/// As for [`tl_perform_ctl`]. |
| 312 | | −unsafe fn perform(ctx: *mut Ctx, cap: Value, payload: Value, multi: bool) -> Value { |
| 313 | 296 | // SAFETY: caller guarantees a capability block: clauses, then marker. |
| 314 | 297 | unsafe { |
| 315 | 298 | let b = cap as *mut Block; |
| 316 | 299 | let n = (*b).header.scan_fsize as usize; |
| 317 | 300 | let marker = int_get(*Block::fields_ptr(b).add(n - 1)) as u64; |
| 318 | | − suspend( |
| 319 | | − ctx, |
| 320 | | − Request::Op { |
| 321 | | − marker, |
| 322 | | − payload, |
| 323 | | − multi, |
| 324 | | − }, |
| 325 | | − ) |
| 301 | + suspend(ctx, Request::Op { marker, payload }) |
| 326 | 302 | } |
| 327 | 303 | } |
| 328 | 304 | |
| @@ -336,7 +312,7 @@ unsafe fn suspend(ctx: *mut Ctx, request: Request) -> Value { |
| 336 | 312 | // fiber executing this code. |
| 337 | 313 | unsafe { |
| 338 | 314 | let y = *current(ctx); |
| 339 | | − if y.yielder.is_null() { |
| 315 | + if y.is_null() { |
| 340 | 316 | if let Request::Op { payload, .. } = request { |
| 341 | 317 | drop_value(ctx, payload); |
| 342 | 318 | } |
| @@ -345,7 +321,7 @@ unsafe fn suspend(ctx: *mut Ctx, request: Request) -> Value { |
| 345 | 321 | c"an operation reached the actor's root without a handler".as_ptr(), |
| 346 | 322 | ); |
| 347 | 323 | } |
| 348 | | − let answer = (*y.yielder).suspend(request); |
| 324 | + let answer = (*(*y).yielder).suspend(request); |
| 349 | 325 | *current(ctx) = y; |
| 350 | 326 | let mode = match answer { |
| 351 | 327 | Resume::Value(v) => return v, |
| @@ -366,7 +342,7 @@ unsafe fn suspend(ctx: *mut Ctx, request: Request) -> Value { |
| 366 | 342 | pub(crate) unsafe fn preempt_in_fiber(ctx: *mut Ctx) -> bool { |
| 367 | 343 | // SAFETY: forwarded caller guarantee. |
| 368 | 344 | unsafe { |
| 369 | | − if current(ctx).yielder.is_null() { |
| 345 | + if current(ctx).is_null() { |
| 370 | 346 | return false; |
| 371 | 347 | } |
| 372 | 348 | let v = suspend(ctx, Request::Preempt); |
| @@ -378,6 +354,11 @@ pub(crate) unsafe fn preempt_in_fiber(ctx: *mut Ctx) -> bool { |
| 378 | 354 | /// Run the frame's fiber with `input` until it finishes or performs an |
| 379 | 355 | /// operation of this handler; returns the handler's result. |
| 380 | 356 | /// |
| 357 | +/// A clause that calls `resume` and then does more work (`x + resume(())`) |
| 358 | +/// nests `resume_code`, `drive` and `call_clause` once per operation: each |
| 359 | +/// ends with a tail call, so that the nesting costs as little stack as |
| 360 | +/// possible. |
| 361 | +/// |
| 381 | 362 | /// # Safety |
| 382 | 363 | /// `ctx` must be the running actor's context; `block` a live frame. |
| 383 | 364 | unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| @@ -429,46 +410,19 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 429 | 410 | } |
| 430 | 411 | input = Resume::Value(UNIT); |
| 431 | 412 | } |
| 432 | | − Request::Op { |
| 433 | | − marker, |
| 434 | | − payload, |
| 435 | | − multi, |
| 436 | | − } if marker == f.marker => { |
| 437 | | − let resume = match &mut f.multi { |
| 438 | | − Some(m) => { |
| 439 | | − m.captures.push(Capture { |
| 440 | | − held: Held::Live, |
| 441 | | − multi, |
| 442 | | − resumed: 0, |
| 443 | | − holds_fiber: f.nested > 0, |
| 444 | | − }); |
| 445 | | − let index = m.captures.len() - 1; |
| 446 | | − m.occupant = Some(index); |
| 447 | | − m.in_use = false; |
| 448 | | − resume_closure(ctx, block, index as i64, multi_resume_code) |
| 449 | | − } |
| 450 | | − None => { |
| 451 | | − f.suspensions += 1; |
| 452 | | − f.waiting = true; |
| 453 | | − resume_closure(ctx, block, f.suspensions, resume_code) |
| 454 | | − } |
| 413 | + Request::Op { marker, payload } if marker == f.marker => { |
| 414 | + let resume = if f.multi.is_some() { |
| 415 | + capture(ctx, block, payload) |
| 416 | + } else { |
| 417 | + f.suspensions += 1; |
| 418 | + f.waiting = true; |
| 419 | + resume_closure(ctx, block, f.suspensions, resume_code) |
| 455 | 420 | }; |
| 456 | 421 | return call_clause(ctx, f.cap, payload, Some(resume)); |
| 457 | 422 | } |
| 458 | | − Request::Op { |
| 459 | | − marker, |
| 460 | | − payload, |
| 461 | | − multi, |
| 462 | | − } => { |
| 423 | + Request::Op { marker, payload } => { |
| 463 | 424 | // Not ours: ask the handlers around this one. |
| 464 | | − let answer = suspend( |
| 465 | | − ctx, |
| 466 | | − Request::Op { |
| 467 | | − marker, |
| 468 | | − payload, |
| 469 | | − multi, |
| 470 | | − }, |
| 471 | | − ); |
| 425 | + let answer = suspend(ctx, Request::Op { marker, payload }); |
| 472 | 426 | input = if (*ctx).yielding == YIELD_CANCEL { |
| 473 | 427 | Resume::Cancel |
| 474 | 428 | } else { |
| @@ -492,6 +446,37 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 492 | 446 | } |
| 493 | 447 | } |
| 494 | 448 | |
| 449 | +/// A multi-shot handler's fiber suspended at one of its operations (in |
| 450 | +/// `payload`, borrowed): record the capture; returns its `resume` |
| 451 | +/// closure. Out of line, so that `drive` |
| 452 | +/// keeps the stack frame it had for one-shot handlers (nested `resume`s |
| 453 | +/// recurse through it). |
| 454 | +/// |
| 455 | +/// # Safety |
| 456 | +/// `ctx` must be the running actor's context; `block` a live multi frame. |
| 457 | +#[inline(never)] |
| 458 | +unsafe fn capture(ctx: *mut Ctx, block: Value, payload: Value) -> Value { |
| 459 | + // SAFETY: forwarded caller guarantee; the payload starts with the |
| 460 | + // operation's index. |
| 461 | + unsafe { |
| 462 | + let f = frame(block); |
| 463 | + let holds_fiber = f.nested > 0; |
| 464 | + let m = f.multi.as_mut().expect("multi"); |
| 465 | + let op = int_get(*Block::fields_ptr(payload as *mut Block)) as u32; |
| 466 | + let multi = op < 64 && m.ops & (1 << op) != 0; |
| 467 | + m.captures.push(Capture { |
| 468 | + held: Held::Live, |
| 469 | + multi, |
| 470 | + resumed: 0, |
| 471 | + holds_fiber, |
| 472 | + }); |
| 473 | + let index = m.captures.len() - 1; |
| 474 | + m.occupant = Some(index); |
| 475 | + m.in_use = false; |
| 476 | + resume_closure(ctx, block, index as i64, multi_resume_code) |
| 477 | + } |
| 478 | +} |
| 479 | + |
| 495 | 480 | /// The handler is done: cancel the suspended body (one-shot), or release |
| 496 | 481 | /// every capture left (multi-shot; see the module documentation). Returns |
| 497 | 482 | /// whether the released code started an unwinding of its own, which then |
| @@ -499,6 +484,7 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 499 | 484 | /// |
| 500 | 485 | /// # Safety |
| 501 | 486 | /// `ctx` must be the running actor's context; `block` a live frame. |
| 487 | +#[inline(never)] |
| 502 | 488 | unsafe fn finish(ctx: *mut Ctx, block: Value) -> bool { |
| 503 | 489 | // SAFETY: caller guarantees a live frame on this actor. |
| 504 | 490 | unsafe { |
| @@ -558,6 +544,7 @@ unsafe fn finish(ctx: *mut Ctx, block: Value) -> bool { |
| 558 | 544 | /// |
| 559 | 545 | /// # Safety |
| 560 | 546 | /// `ctx` must be the running actor's context; `block` a live frame. |
| 547 | +#[inline(never)] |
| 561 | 548 | unsafe fn run_out(ctx: *mut Ctx, block: Value, mode: Resume) -> bool { |
| 562 | 549 | // SAFETY: caller guarantees a live frame on this actor. |
| 563 | 550 | unsafe { |
| @@ -620,6 +607,7 @@ unsafe fn run_out(ctx: *mut Ctx, block: Value, mode: Resume) -> bool { |
| 620 | 607 | /// occupant first. Returns a copy of what the stack now holds, if it had to |
| 621 | 608 | /// be written back (the saved capture), or an error message on targets |
| 622 | 609 | /// without snapshots. |
| 610 | +#[inline(never)] |
| 623 | 611 | fn materialize(f: &mut Frame, index: usize) -> Result<Option<Snapshot>, &'static std::ffi::CStr> { |
| 624 | 612 | let unsupported = c"multi-shot resumptions are not supported on this target yet"; |
| 625 | 613 | let fiber = f |
| @@ -655,10 +643,21 @@ unsafe fn call_clause(ctx: *mut Ctx, cap: Value, payload: Value, resume: Option< |
| 655 | 643 | let fields = Block::fields_ptr(p); |
| 656 | 644 | let op = int_get(*fields) as usize; |
| 657 | 645 | let clause = dup(*Block::fields_ptr(cap as *mut Block).add(op)); |
| 658 | | − let mut args: Vec<Value> = (1..n).map(|i| dup(*fields.add(i))).collect(); |
| 646 | + // The generated-code convention, without a vector: the call is the |
| 647 | + // last thing done, so it can reuse this frame (a clause calling |
| 648 | + // `resume` nests this code once per operation). |
| 649 | + let mut regs = [UNIT; 5]; |
| 650 | + let args = (1..n).map(|i| dup(*fields.add(i))).chain(resume); |
| 651 | + for (i, a) in args.enumerate() { |
| 652 | + if i < 5 { |
| 653 | + regs[i] = a; |
| 654 | + } else { |
| 655 | + (*ctx).xargs[i - 5] = a; |
| 656 | + } |
| 657 | + } |
| 659 | 658 | drop_value(ctx, payload); |
| 660 | | − args.extend(resume); |
| 661 | | − call_closure(ctx, clause, &args) |
| 659 | + let code: ResumeCode = std::mem::transmute(crate::values::closure_code(clause)); |
| 660 | + code(ctx, clause, regs[0], regs[1], regs[2], regs[3], regs[4]) |
| 662 | 661 | } |
| 663 | 662 | } |
| 664 | 663 | |
| @@ -698,28 +697,24 @@ unsafe extern "C" fn resume_code( |
| 698 | 697 | // this call. |
| 699 | 698 | unsafe { |
| 700 | 699 | let env = Block::fields_ptr(this as *mut Block); |
| 701 | | − let block = dup(*env); |
| 700 | + let block = *env; |
| 702 | 701 | let seq = int_get(*env.add(1)); |
| 703 | | − drop_value(ctx, this); |
| 704 | 702 | let f = frame(block); |
| 705 | | − let result = if f.cancelled { |
| 706 | | − drop_value(ctx, x); |
| 707 | | − crate::effects::tl_panic_str( |
| 708 | | − ctx, |
| 709 | | − c"`resume` called after its handler finished".as_ptr(), |
| 710 | | − ) |
| 703 | + let message = if f.cancelled { |
| 704 | + c"`resume` called after its handler finished" |
| 711 | 705 | } else if !f.waiting || f.suspensions != seq { |
| 712 | | − drop_value(ctx, x); |
| 713 | | − crate::effects::tl_panic_str( |
| 714 | | − ctx, |
| 715 | | − c"`resume` called twice (continuations are one-shot)".as_ptr(), |
| 716 | | − ) |
| 706 | + c"`resume` called twice (continuations are one-shot)" |
| 717 | 707 | } else { |
| 708 | + // The fiber waits, so the handler has not finished: its |
| 709 | + // `tl_handle_ctl` keeps the frame alive while `drive` runs, and |
| 710 | + // this closure can go first, leaving `drive` the last call. |
| 718 | 711 | f.waiting = false; |
| 719 | | − drive(ctx, block, Resume::Value(x)) |
| 712 | + drop_value(ctx, this); |
| 713 | + return drive(ctx, block, Resume::Value(x)); |
| 720 | 714 | }; |
| 721 | | − drop_value(ctx, block); |
| 722 | | − result |
| 715 | + drop_value(ctx, this); |
| 716 | + drop_value(ctx, x); |
| 717 | + crate::effects::tl_panic_str(ctx, message.as_ptr()) |
| 723 | 718 | } |
| 724 | 719 | } |
| 725 | 720 | |
| @@ -738,19 +733,20 @@ unsafe extern "C" fn multi_resume_code( |
| 738 | 733 | // this call; its frame is live while it holds it. |
| 739 | 734 | unsafe { |
| 740 | 735 | let env = Block::fields_ptr(this as *mut Block); |
| 741 | | − let block = dup(*env); |
| 736 | + let block = *env; |
| 742 | 737 | let index = int_get(*env.add(1)) as usize; |
| 743 | 738 | let last_use = (*(this as *mut Block)).header.rc == 1; |
| 739 | + let prepared = prepare(ctx, block, index, last_use); |
| 740 | + // As in `resume_code`: once prepared, the handler is running, and |
| 741 | + // its `tl_handle_multi` keeps the frame alive. |
| 744 | 742 | drop_value(ctx, this); |
| 745 | | − let result = match prepare(ctx, block, index, last_use) { |
| 743 | + match prepared { |
| 746 | 744 | Ok(()) => drive(ctx, block, Resume::Value(x)), |
| 747 | 745 | Err(message) => { |
| 748 | 746 | drop_value(ctx, x); |
| 749 | 747 | crate::effects::tl_panic_str(ctx, message.as_ptr()) |
| 750 | 748 | } |
| 751 | | − }; |
| 752 | | − drop_value(ctx, block); |
| 753 | | − result |
| 749 | + } |
| 754 | 750 | } |
| 755 | 751 | } |
| 756 | 752 | |
| @@ -760,6 +756,7 @@ unsafe extern "C" fn multi_resume_code( |
| 760 | 756 | /// |
| 761 | 757 | /// # Safety |
| 762 | 758 | /// `ctx` must be the running actor's context; `block` a live multi frame. |
| 759 | +#[inline(never)] |
| 763 | 760 | unsafe fn prepare( |
| 764 | 761 | ctx: *mut Ctx, |
| 765 | 762 | block: Value, |
| @@ -814,6 +811,7 @@ unsafe fn prepare( |
| 814 | 811 | /// # Safety |
| 815 | 812 | /// `ctx` must be the running actor's context; `fiber` suspended at a |
| 816 | 813 | /// multi-shot capture without other handler fibers in it. |
| 814 | +#[inline(never)] |
| 817 | 815 | unsafe fn copying_pass(ctx: *mut Ctx, fiber: &mut HandlerFiber) { |
| 818 | 816 | // SAFETY: forwarded caller guarantee. |
| 819 | 817 | unsafe { |