| @@ -0,0 +1,136 @@ |
| 1 | +//! Callbacks from C into tilcayo (ADR 0015). |
| 2 | +//! |
| 3 | +//! An extern that takes a tilcayo function hands C a trampoline and a |
| 4 | +//! [`Callback`] as its `void *` environment. The trampoline converts C |
| 5 | +//! arguments to values and calls [`tl_callback_call`], which runs the |
| 6 | +//! closure **in the actor that called into C, on its stack and its |
| 7 | +//! thread**, while that extern call is in progress: |
| 8 | +//! |
| 9 | +//! - **No suspension.** C frames sit on the actor's fiber, and C code may |
| 10 | +//! rely on its thread (thread-locals, locks), so the fiber must neither |
| 11 | +//! park nor move. While a callback is possible, preemption only refills |
| 12 | +//! the budget (`sched::tl_preempt`), exit signals wait for the extern to |
| 13 | +//! return, and `blocking` externs run inline (`blocking`). The type |
| 14 | +//! checker limits callbacks to `<ffi>`, so they cannot reach `proc` |
| 15 | +//! operations or sockets; `sched::park` aborts if one ever does. |
| 16 | +//! - **Panics stay on the tilcayo side.** A panic in the closure unwinds |
| 17 | +//! to the trampoline (yield-bubbling never crosses a C frame), and stays |
| 18 | +//! pending in `ctx->yielding`: further calls return unit without running |
| 19 | +//! anything, C runs to completion, [`tl_callback_end`] discards the |
| 20 | +//! extern's result, and the generated code unwinds after the extern |
| 21 | +//! returns, as after any call that may unwind. |
| 22 | +//! - **Ownership.** The closure is the extern's argument, borrowed for the |
| 23 | +//! call; each call duplicates it (closure calls consume `self`). |
| 24 | +//! Arguments are consumed; the result is owned by the trampoline. |
| 25 | + |
| 26 | +use crate::abi::{Callback, Ctx, UNIT, Value, YIELD_NONE}; |
| 27 | +use crate::heap::drop_value; |
| 28 | +use crate::prims::{call_closure, dup}; |
| 29 | + |
| 30 | +/// Arguments a callback can take: five in registers after the closure, |
| 31 | +/// the rest in `ctx->xargs`. |
| 32 | +const MAX_ARGS: usize = 5 + crate::abi::XARGS; |
| 33 | + |
| 34 | +thread_local! { |
| 35 | + /// Set on threads that must never run tilcayo code (the blocking pool). |
| 36 | + static FOREIGN_THREAD: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 37 | + /// Its address identifies the current thread, cheaply. |
| 38 | + static THREAD_TOKEN: u8 = const { 0 }; |
| 39 | +} |
| 40 | + |
| 41 | +fn thread_token() -> usize { |
| 42 | + THREAD_TOKEN.with(|t| t as *const u8 as usize) |
| 43 | +} |
| 44 | + |
| 45 | +/// Mark the current thread as one that never runs tilcayo code. |
| 46 | +pub(crate) fn mark_foreign_thread() { |
| 47 | + FOREIGN_THREAD.with(|f| f.set(true)); |
| 48 | +} |
| 49 | + |
| 50 | +fn abort(message: &str) -> ! { |
| 51 | + eprintln!("tilrt: {message}"); |
| 52 | + std::process::abort() |
| 53 | +} |
| 54 | + |
| 55 | +/// Prepare `cb` for passing `fun` (a closure, borrowed for the call) to |
| 56 | +/// C, from the extern call running in `ctx`'s actor. |
| 57 | +/// |
| 58 | +/// # Safety |
| 59 | +/// `ctx` must be the running actor's context, `cb` writable, `fun` a live |
| 60 | +/// closure that outlives the matching [`tl_callback_end`]. |
| 61 | +#[unsafe(no_mangle)] |
| 62 | +pub unsafe extern "C" fn tl_callback_begin(ctx: *mut Ctx, cb: *mut Callback, fun: Value) { |
| 63 | + if FOREIGN_THREAD.with(std::cell::Cell::get) { |
| 64 | + abort( |
| 65 | + "a callback was set up on the blocking pool (`blocking` externs cannot take callbacks)", |
| 66 | + ); |
| 67 | + } |
| 68 | + // SAFETY: caller guarantees a running actor and a writable `cb`. |
| 69 | + unsafe { |
| 70 | + crate::sched::enter_callbacks(ctx); |
| 71 | + cb.write(Callback { |
| 72 | + ctx, |
| 73 | + fun, |
| 74 | + thread: thread_token(), |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Call the callback's closure with `argc` arguments (consumed). Returns |
| 80 | +/// its result (owned), or unit without calling it if the actor is |
| 81 | +/// unwinding (a previous call panicked). |
| 82 | +/// |
| 83 | +/// # Safety |
| 84 | +/// `cb` must have been set up by [`tl_callback_begin`]; `args` valid for |
| 85 | +/// `argc` owned values; the closure must take exactly `argc` arguments. |
| 86 | +#[unsafe(no_mangle)] |
| 87 | +pub unsafe extern "C" fn tl_callback_call( |
| 88 | + cb: *const Callback, |
| 89 | + argc: usize, |
| 90 | + args: *const Value, |
| 91 | +) -> Value { |
| 92 | + // SAFETY: caller guarantees a set-up callback and `argc` arguments. |
| 93 | + unsafe { |
| 94 | + let cb = &*cb; |
| 95 | + if cb.thread != thread_token() { |
| 96 | + abort( |
| 97 | + "a callback was called on another thread than its extern call, or after that call returned", |
| 98 | + ); |
| 99 | + } |
| 100 | + if argc > MAX_ARGS { |
| 101 | + abort("a callback takes at most 31 arguments"); |
| 102 | + } |
| 103 | + let args = std::slice::from_raw_parts(args, argc); |
| 104 | + if (*cb.ctx).yielding != YIELD_NONE { |
| 105 | + for a in args { |
| 106 | + drop_value(cb.ctx, *a); |
| 107 | + } |
| 108 | + return UNIT; |
| 109 | + } |
| 110 | + call_closure(cb.ctx, dup(cb.fun), args) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// The extern call that set up `cb` is returning `result` (consumed): |
| 115 | +/// returns it, or drops it and returns unit if a callback panicked (the |
| 116 | +/// caller unwinds and never reads it). |
| 117 | +/// |
| 118 | +/// # Safety |
| 119 | +/// `cb` must have been set up by [`tl_callback_begin`] in this call; |
| 120 | +/// `result` must be owned. |
| 121 | +#[unsafe(no_mangle)] |
| 122 | +pub unsafe extern "C" fn tl_callback_end(cb: *mut Callback, result: Value) -> Value { |
| 123 | + // SAFETY: caller guarantees a set-up callback. |
| 124 | + unsafe { |
| 125 | + let ctx = (*cb).ctx; |
| 126 | + // A late call through a dangling environment aborts if the memory |
| 127 | + // is still intact (best effort). |
| 128 | + (*cb).thread = 0; |
| 129 | + crate::sched::leave_callbacks(ctx); |
| 130 | + if (*ctx).yielding != YIELD_NONE { |
| 131 | + drop_value(ctx, result); |
| 132 | + return UNIT; |
| 133 | + } |
| 134 | + result |
| 135 | + } |
| 136 | +} |