diff --git a/ffi/README.md b/ffi/README.md index 5e64bdc89..c951b9a45 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -93,7 +93,7 @@ public path names, or an error taken by value, is held by value as an opaque han | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | | any other type that is `Copy` and `Send`, with no padding under any order of its fields, such as moq's `Timestamp` or `stats::Traffic` | an opaque value: a struct of its bytes, copied across, which only Rust makes, reads, or changes; its `pub` fields are a getter and a setter, and a `&mut self` method writes back the caller's copy alone, one returning a future or a `Result` refused, since a language raising the error would lose the change; one no export returns is left out, since C could never make one | -| any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free`; one that is `Copy`, such as std's `Instant`, whose padding a `rustc` may place anywhere, is copied out of its handle where Rust takes it by value, and a closure neither takes nor returns one by value | +| any other type | an object: an opaque handle to an `Arc`, freed with `_free`; one that is `Copy`, such as std's `Instant`, whose padding a `rustc` may place anywhere, is copied out of its handle where Rust takes it by value, and a closure neither takes nor returns one by value; one that is not `Send`, such as kio's `clock::Entered`, stays on the thread that made it, as [the C ABI](#the-c-abi)'s threads say | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | | a `pub` field of an object or opaque value | a getter returning a copy, and a setter assigning in place, as `x.field = value` does | @@ -292,6 +292,28 @@ where it was while its file is unchanged. while a getter's copy is C's own: a nested field is got, set, and set back. A type whose C++ copies share one value has `_borrowed`, true while a call or future borrows it, so moving it out would fail. +- **Threads**: a value that is not `Send`, an object or a future, stays on the thread that + made it, which alone borrows, moves, polls, or drops it. A future is such a value if it + holds one, as a poll function's holds its receiver and parameters, or makes one, as its + output, which its last poll makes on its thread. A call or poll on another + fails with `_STATUS_NOT_SEND` before any Rust code runs, so such a future's poll + takes a trailing `status` too. A `_free` or `_drop` elsewhere drops nothing there: it + queues the drop to the value's thread, which runs it at its next check of one of its + values or as it exits. One freed once its thread exited leaks, printing why, since + nothing may drop it. A function that makes such a value, taking none or only in an + `Option`, has a twin `_homed` of the same signature, as do its future's start, + `_start_homed`, and `Default`'s, `_default_homed`: with no such value passed, + it runs on a home, a thread Rust owns, which the value then stays on, and each later + call on it, from any thread, runs there while the caller waits, as does a call on a + value that is not `Send` it returns; with one passed, it runs as the function does. The first present of + a call's receiver and its parameters that are handles of such values, bare or in an + `Option`, sends it to that value's home, if it lives on one; a value made on a thread + the caller owns is not sent, so a call on it from another thread fails its check. Values from different homes, or one + nested in a list or a record, are not sent: the call runs on the first's home, where + any other fails its check with `_STATUS_NOT_SEND`. A home waiting on a call into + Dart, or on another home, runs meanwhile what that call asks of its own values. A + binding whose threads move calls the twins: Go and Dart for every such value, Kotlin + and Swift for such a future made from none, which a pool's thread then polls. - **Opaque values** are `typedef struct { uintN_t opaque[n]; } ;`, the bytes of a `Copy` type with private fields, in units of its alignment up to 8 bytes: C copies one as any struct, and passes `&self` as a `const *` and `&mut self`, a field's setter @@ -447,7 +469,9 @@ packages place it in. No export keeps a view or reference past its call, so none carries `[[clang::lifetime_capture_by]]`; the first that does adds it. - **Checks**: a call passes no `status`, so a borrow or move Rust's rules refuse aborts - with its message, naming the function, as C's does with a NULL one. + with its message, naming the function, as C's does with a NULL one: so does a use or + poll of a value that is not `Send` on another thread than the one that made it, and + one destroyed on another drops on its own thread at its next call or as it exits. - **Sharing** stands in for `clone` where no call can tell the two apart but by counting an `Arc`'s or `Rc`'s clones, which the exporter decides at compile time. A type's copies share when it is `Clone`; `Sync`, since one that is not is borrowed on one thread at a time, which copies on two threads @@ -552,7 +576,12 @@ the rest cross encoded. once the GC collects it, on a daemon thread a phantom reference queue feeds. A `self` method, or passing the object by value, moves its value out and closes it once the call returns. `Clone` is `clone()`, `PartialEq` is `equals`, and `Hash` is `hashCode`, Rust's - hash folded as `Long.hashCode` folds it, or 0 without one. + hash folded as `Long.hashCode` folds it, or 0 without one. A value that is not `Send` + throws an `IllegalStateException` when used on another thread than the one that made + it, and closed or collected on another, it drops on its own at its next call or as it + exits. A future that is not `Send` made from none lives on a home, so a coroutine a + dispatcher resumes on any thread polls it; one a value's method makes stays on that + value's thread. A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. @@ -698,7 +727,10 @@ caller imports it with no ignores. since setting an object moves it in and closes it, which an assignment would hide. - **Checks**: a borrow or move Rust's rules refuse raises, naming the function and the rule, before any Rust code runs, so the call changed nothing: `ValueError` for a value moved out, or a lent object that outlived the one it - borrows from, as for a closed object, and `RuntimeError` for any other. + borrows from, as for a closed object, and `RuntimeError` for any other, as for a value + that is not `Send` used on another thread than the one that made it. Freed on another, + as the collector or the last reference there frees it, such a value drops on its own + thread at its next call or as it exits. - **Records** are slotted dataclasses, compared by their fields, a float by IEEE `==` even against itself, as Rust does, and **enums** an `Enum` of upper-case members, or for one with data a class of a dataclass per variant, @@ -810,7 +842,14 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u holds until it returns, and moving its value out closes it, after which a call throws. A field is a computed property, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in, which an assignment would hide. - An object is `Sendable`, since Rust checks each borrow at run time. + An object is `Sendable` where Rust lets it cross threads, checking each borrow at run + time: a class if its type is `Send` and `Sync`, since its holders share it, and a + noncopyable struct if `Send`, since it only moves. A record, enum, or tuple struct is + `Sendable` only if each handle it holds is, so Swift's compiler keeps the others on one + task, and a use on another thread anyway, as after a suspension that resumed elsewhere, + throws a `CheckError`. A type thrown as an error is `Sendable` unchecked, since + `Swift.Error` requires it. A future that is not `Send` made from none lives on a home, so + any executor awaits it. - **Checks**: a borrow or move Rust's rules refuse at run time, such as a borrow of a value that is not `Sync` on a second thread, throws a `CheckError`, holding the function and the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a @@ -954,6 +993,9 @@ reads a go.mod. and returns it beside the value, so the call allocates nothing for it: cgo moves to the heap any Go local whose address C gets, since C may call back into Go, which can move the goroutine's stack. +- **Threads**: a goroutine moves between threads, so a value that is not `Send` is made on + a home, a thread Rust owns, which runs each call on it, from any goroutine, while the + goroutine's thread waits, and drops it there once closed or collected. - **Records** are structs, and **plain enums** an `int32` type with a constant per variant (`ShapeSquare`). An enum with data is an interface each variant's struct implements (`EventMoved{V0: p}`), sealed so only the package's do; its methods are the @@ -1177,7 +1219,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass future's poll and `cancel()` included, runs on a thread of its own, acting as the caller's for the handles it borrows, while the isolate's thread runs what Rust calls into Dart meanwhile. Any other call runs on the isolate's thread, since nothing it waits - on can call into Dart. + on can call into Dart. An isolate may move between threads, so a value that is not + `Send` is made on a home, a thread Rust owns, which runs each call on it. - **The contract** is checked at the first call, before any other: a Dart file and a library from different builds throw a `StateError`. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 7e5798785..ac43a06c8 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -1063,6 +1063,27 @@ fn main() { "farther hop {}", api::farther_hop(Pair { left: near, right: far }).get() ); + // A value that is not `Send`, all on this thread: a handle to a local's count, and a + // local made from none. + let local = api::Local::new(); + let shared = api::local_or_new(Some(&local)); + let bumped = local.bump(); + println!("local {bumped} {}", shared.bump()); + println!("local fresh {}", api::local_or_new(None).bump()); + println!("local either {}", api::bump_either(None, &shared)); + println!("local later {}", finish(api::local_or_new_later(Some(&local))).bump()); + println!("local later fresh {}", finish(api::local_or_new_later(None)).bump()); +} + +/// `future`'s output, polled on this thread until it is ready. +fn finish(future: F) -> F::Output { + let mut future = pin!(future); + let mut cx = Context::from_waker(Waker::noop()); + loop { + if let Poll::Ready(output) = future.as_mut().poll(&mut cx) { + return output; + } + } } /// A path no Rust string spells: on Unix, bytes that are not UTF-8; on Windows, a lone diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index eb1215991..a8ff74935 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1914,6 +1914,180 @@ impl Tallies { } } +impl Tally { + /// Counts once its future, which holds an `Rc` across a yield and so is not `Send`, is + /// polled a second time: what a binding polling on a pool runs on a home. + pub async fn bump_later(&self) -> u32 { + let held = std::rc::Rc::new(()); + yielded(0).await; + drop(held); + self.bump() + } +} + +/// A count its handles share through an `Rc`, so not `Send`: it stays on the thread that +/// made it, which alone calls, polls, and drops it, as each method checks. +pub struct Local { + count: std::rc::Rc>, + thread: std::thread::ThreadId, +} + +impl Default for Local { + fn default() -> Self { + Self::new() + } +} + +impl Local { + pub fn new() -> Self { + Self { + count: std::rc::Rc::new(std::cell::Cell::new(0)), + thread: std::thread::current().id(), + } + } + + /// Counts once, on its own thread. + pub fn bump(&self) -> u32 { + self.here(); + self.count.set(self.count.get() + 1); + self.count.get() + } + + /// Another handle to the same count. + pub fn share(&self) -> Local { + self.here(); + Local { + count: self.count.clone(), + thread: self.thread, + } + } + + /// Counts once its future, which is not `Send`, is polled a third time. + pub async fn later(&self) -> u32 { + yielded(0).await; + self.bump() + } + + /// What `f` returns, called on this local's thread, where it may use the local again. + pub fn visit(&self, f: impl Fn() -> u32) -> u32 { + self.here(); + f() + } + + /// Counts once, as a poll: its future holds the local, so is not `Send` either. + pub fn poll_count(&self, cx: &mut std::task::Context<'_>) -> std::task::Poll { + let _ = cx; + std::task::Poll::Ready(self.bump()) + } + + /// Which thread it lives on, numbered from 1 in the order each first asked. + pub fn thread_number(&self) -> u64 { + self.here(); + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + thread_local!(static NUMBER: u64 = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)); + NUMBER.with(|number| *number) + } + + /// Whether this runs on its own thread, which every use of it must. + pub fn on_own_thread(&self) -> bool { + std::thread::current().id() == self.thread + } + + fn here(&self) { + assert!(self.on_own_thread(), "a Local is used off its own thread"); + } +} + +/// Equal to another handle to the same count. +impl PartialEq for Local { + fn eq(&self, other: &Self) -> bool { + self.here(); + std::rc::Rc::ptr_eq(&self.count, &other.count) + } +} + +impl Eq for Local {} + +/// Hashed by the count it shares, as its equality compares. +impl std::hash::Hash for Local { + fn hash(&self, state: &mut H) { + self.here(); + std::rc::Rc::as_ptr(&self.count).hash(state); + } +} + +impl Drop for Local { + fn drop(&mut self) { + // A drop elsewhere would panic, which aborts through the C ABI. + self.here(); + LOCALS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } +} + +static LOCALS: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + +/// How many locals have dropped, each on its own thread. +pub fn locals_dropped() -> u32 { + LOCALS.load(std::sync::atomic::Ordering::Relaxed) +} + +/// A handle to `local`'s count if there is one, else a new local: made where `local` +/// stays, or, with none, wherever a local made from nothing would be. +pub fn local_or_new(local: Option<&Local>) -> Local { + local.map_or_else(Local::new, Local::share) +} + +/// `local_or_new` after a yield: a future that is not `Send`, made where `local` stays, +/// or, with none, wherever a local made from nothing would be. +pub async fn local_or_new_later(local: Option<&Local>) -> Local { + yielded(0).await; + local_or_new(local) +} + +/// The first local's count bumped if there is one, else the second's. +pub fn bump_either(first: Option<&Local>, second: &Local) -> u32 { + first.unwrap_or(second).bump() +} + +/// A new local, made after a yield: the future is `Send`, but its output is not, so it is +/// made where the future is polled. +pub async fn local_after() -> Local { + yielded(0).await; + Local::new() +} + +/// `n` doubled after a yield, through an `Rc` it holds across it: a future that is not +/// `Send`, though nothing it takes is not. +pub async fn local_later(n: u32) -> u32 { + let n = std::rc::Rc::new(n); + yielded(0).await; + *n * 2 +} + +/// What a poll registers that is not `Send`, as `Waiter` is but through an `Rc`: a future +/// polling with one keeps it between polls, so is not `Send` either. +pub struct LocalWaiter(std::rc::Rc); + +impl LocalWaiter { + pub fn new(waker: Waker) -> Self { + Self(std::rc::Rc::new(waker)) + } + + /// `n` at once: a poll of the waiter it is a method of, which exports as a future + /// making and keeping it. + pub fn poll_ready(&self, n: u32) -> Poll { + let _ = &self.0; + Poll::Ready(n) + } +} + +/// `n` tripled at once: a poll through a waiter that is not `Send`, which exports as a +/// future making and keeping it. +pub fn poll_tripled(waiter: &LocalWaiter, n: u32) -> Poll { + let _ = &waiter.0; + Poll::Ready(n * 3) +} + static DROPPED: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); /// A level its clones share through an `Arc`, with no `&mut self` method: a clone no call diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 7bf929899..3b0db43b9 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -1,174 +1,175 @@ -fixtures/reject/src/lib.rs:1000:1: rust2-ffi: cannot export fn `poll_risky`: Poll with no Context or waiter parameter `std::task::Poll<()>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1016:1: rust2-ffi: cannot export fn `poll_loose`: Poll with no Context or waiter parameter `std::task::Poll<()>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1023:1: rust2-ffi: cannot export fn `among`: 3 optional borrowed objects, past the 2 Swift takes, which overloads each with and without it since its optional holds an object only by consuming it; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1029:1: rust2-ffi: cannot export fn `wrapped`: error holding an object Swift cannot copy, as `Swift.Error` requires; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1034:1: rust2-ffi: cannot export fn `sum_pair`: array of an element other than u8 `[u16; 2]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1039:1: rust2-ffi: cannot export fn `empty`: empty array `[u8; 0]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1045:1: rust2-ffi: cannot export enum `Wide`: discriminant outside `i32` `Large = 1099511627776`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1047:2: note: has this discriminant -fixtures/reject/src/lib.rs:1052:1: rust2-ffi: cannot export enum `Huge`: discriminant outside `i32` `Large = 18446744073709551616`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1054:2: note: has this discriminant -fixtures/reject/src/lib.rs:1059:1: rust2-ffi: cannot export enum `Top`: discriminant outside `i32` `Large = 340282366920938463463374607431768211455`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1061:2: note: has this discriminant -fixtures/reject/src/lib.rs:1065:1: rust2-ffi: cannot export fn `raw`: unsafe fn; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1017:2: rust2-ffi: cannot export method `Risky::new`: unsafe fn; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1023:1: rust2-ffi: cannot export fn `poll_risky`: Poll with no Context or waiter parameter `std::task::Poll<()>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1039:1: rust2-ffi: cannot export fn `poll_loose`: Poll with no Context or waiter parameter `std::task::Poll<()>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1046:1: rust2-ffi: cannot export fn `among`: 3 optional borrowed objects, past the 2 Swift takes, which overloads each with and without it since its optional holds an object only by consuming it; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1052:1: rust2-ffi: cannot export fn `wrapped`: error holding an object Swift cannot copy, as `Swift.Error` requires; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1057:1: rust2-ffi: cannot export fn `sum_pair`: array of an element other than u8 `[u16; 2]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1062:1: rust2-ffi: cannot export fn `empty`: empty array `[u8; 0]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1068:1: rust2-ffi: cannot export enum `Wide`: discriminant outside `i32` `Large = 1099511627776`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1070:2: note: has this discriminant fixtures/reject/src/lib.rs:106:1: rust2-ffi: cannot export fn `forever_mut`: 'static borrow `&'static mut [u8]`; &mut of a slice `[u8]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1070:1: rust2-ffi: cannot export fn `sum`: C-variadic function; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1076:1: rust2-ffi: cannot export fn `custom`: uncallable ABI `"custom"`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1090:2: rust2-ffi: cannot export method `Raw::peek`: unsafe fn; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1096:1: rust2-ffi: cannot export trait `Trusted`: unsafe trait; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1101:1: rust2-ffi: cannot export trait `Hazard`: unsafe fn `risk`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1106:1: rust2-ffi: cannot export fn `trust`: trait object `dyn Trusted`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1096:1: note: C cannot implement `Trusted`: unsafe trait -fixtures/reject/src/lib.rs:1111:1: rust2-ffi: cannot export fn `hazard`: trait object `dyn Hazard`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1101:1: note: C cannot implement `Hazard`: unsafe fn `risk` -fixtures/reject/src/lib.rs:1116:1: rust2-ffi: cannot export fn `wide_non_zero`: NonZero `u128`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1075:1: rust2-ffi: cannot export enum `Huge`: discriminant outside `i32` `Large = 18446744073709551616`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1077:2: note: has this discriminant +fixtures/reject/src/lib.rs:1082:1: rust2-ffi: cannot export enum `Top`: discriminant outside `i32` `Large = 340282366920938463463374607431768211455`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1084:2: note: has this discriminant +fixtures/reject/src/lib.rs:1088:1: rust2-ffi: cannot export fn `raw`: unsafe fn; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1093:1: rust2-ffi: cannot export fn `sum`: C-variadic function; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1099:1: rust2-ffi: cannot export fn `custom`: uncallable ABI `"custom"`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1113:2: rust2-ffi: cannot export method `Raw::peek`: unsafe fn; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1119:1: rust2-ffi: cannot export trait `Trusted`: unsafe trait; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1124:1: rust2-ffi: cannot export trait `Hazard`: unsafe fn `risk`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1129:1: rust2-ffi: cannot export fn `trust`: trait object `dyn Trusted`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1119:1: note: C cannot implement `Trusted`: unsafe trait fixtures/reject/src/lib.rs:112:1: rust2-ffi: cannot export fn `boxed`: reference inside a parameter `std::boxed::Box<&str>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1138:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1143:1: rust2-ffi: cannot export fn `hidden_wrap`: returned object that borrows `impl std::clone::Clone + '_`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1159:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1164:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1169:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1174:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1179:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1184:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1189:1: rust2-ffi: cannot export fn `count_later`: iterator of borrows in a future or callback; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1194:1: rust2-ffi: cannot export fn `count_opaques`: generic parameter `std::iter::Iterator`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1199:1: rust2-ffi: cannot export fn `unpin_resolver`: trait object `dyn tls::held::Resolver + std::marker::Unpin`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1134:1: rust2-ffi: cannot export fn `hazard`: trait object `dyn Hazard`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1124:1: note: C cannot implement `Hazard`: unsafe fn `risk` +fixtures/reject/src/lib.rs:1139:1: rust2-ffi: cannot export fn `wide_non_zero`: NonZero `u128`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1161:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1166:1: rust2-ffi: cannot export fn `hidden_wrap`: returned object that borrows `impl std::clone::Clone + '_`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1182:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1187:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1192:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1197:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:11:1: rust2-ffi: cannot export fn `map`: foreign type `std::collections::HashMap`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1204:1: rust2-ffi: cannot export fn `borrowed_resolver`: returned object that borrows `std::sync::Arc<(dyn tls::held::Resolver + 'a)>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1209:1: rust2-ffi: cannot export fn `peek_resolver`: reference to a handle inside a value `std::option::Option>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1214:1: rust2-ffi: cannot export fn `peek_codec`: trait object `dyn tls::held::Codec`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1229:1: rust2-ffi: cannot export fn `pump_pipe`: generic parameter `tls::held::Stream`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1234:1: rust2-ffi: cannot export fn `pump_sink`: generic parameter `tls::held::Sink`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1239:1: rust2-ffi: cannot export fn `widen`: generic parameter `tls::held::Width`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1255:2: rust2-ffi: cannot export method `Counter::try_bump`: `&mut self` of a value returning a Result; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1260:2: rust2-ffi: cannot export method `Counter::bump_later`: `&mut self` of a value in a future; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1265:2: rust2-ffi: cannot export method `Counter::bump_ref`: returned borrow of a value written back `&u8`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1272:1: rust2-ffi: cannot export fn `object_alone`: JObject with no &mut jni::Env; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1277:1: rust2-ffi: cannot export fn `two_envs`: several JNI envs; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1282:1: rust2-ffi: cannot export fn `env_later`: JNI value lent to a future; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1287:1: rust2-ffi: cannot export fn `env_counted`: &mut of a value beside a JNI value; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1202:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1207:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1212:1: rust2-ffi: cannot export fn `count_later`: iterator of borrows in a future or callback; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1217:1: rust2-ffi: cannot export fn `count_opaques`: generic parameter `std::iter::Iterator`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1222:1: rust2-ffi: cannot export fn `unpin_resolver`: trait object `dyn tls::held::Resolver + std::marker::Unpin`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1227:1: rust2-ffi: cannot export fn `borrowed_resolver`: returned object that borrows `std::sync::Arc<(dyn tls::held::Resolver + 'a)>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1232:1: rust2-ffi: cannot export fn `peek_resolver`: reference to a handle inside a value `std::option::Option>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1237:1: rust2-ffi: cannot export fn `peek_codec`: trait object `dyn tls::held::Codec`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1252:1: rust2-ffi: cannot export fn `pump_pipe`: generic parameter `tls::held::Stream`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1257:1: rust2-ffi: cannot export fn `pump_sink`: generic parameter `tls::held::Sink`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1262:1: rust2-ffi: cannot export fn `widen`: generic parameter `tls::held::Width`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1278:2: rust2-ffi: cannot export method `Counter::try_bump`: `&mut self` of a value returning a Result; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1283:2: rust2-ffi: cannot export method `Counter::bump_later`: `&mut self` of a value in a future; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1288:2: rust2-ffi: cannot export method `Counter::bump_ref`: returned borrow of a value written back `&u8`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:128:1: rust2-ffi: cannot export fn `poll_ready`: Poll with no Context or waiter parameter `std::task::Poll`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1293:1: rust2-ffi: cannot export fn `shared_env`: JNI type other than a parameter's &mut Env or JObject `jni::Env<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1298:1: rust2-ffi: cannot export fn `java_string`: JNI type other than a parameter's &mut Env or JObject `jni::objects::JString<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1307:1: rust2-ffi: cannot export fn `visit`: trait object `dyn Visit`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1303:1: note: C cannot implement `Visit`: JNI value in a callback `&mut jni::Env<'_>` -fixtures/reject/src/lib.rs:1313:1: rust2-ffi: cannot export fn `keep_object`: 'static borrow `jni::objects::JObject<'static>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1318:1: rust2-ffi: cannot export fn `bound_object`: 'static borrow `&mut jni::Env<'a>`; 'static borrow `jni::objects::JObject<'a>`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:1318:21: note: `'a` is `'static`, as a handle outlives every call - fixtures/reject/src/lib.rs:1318:21: note: `'a` is `'static`, as a handle outlives every call +fixtures/reject/src/lib.rs:1295:1: rust2-ffi: cannot export fn `object_alone`: JObject with no &mut jni::Env; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1300:1: rust2-ffi: cannot export fn `two_envs`: several JNI envs; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1305:1: rust2-ffi: cannot export fn `env_later`: JNI value lent to a future; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1310:1: rust2-ffi: cannot export fn `env_counted`: &mut of a value beside a JNI value; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1316:1: rust2-ffi: cannot export fn `shared_env`: JNI type other than a parameter's &mut Env or JObject `jni::Env<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1321:1: rust2-ffi: cannot export fn `java_string`: JNI type other than a parameter's &mut Env or JObject `jni::objects::JString<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1330:1: rust2-ffi: cannot export fn `visit`: trait object `dyn Visit`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1326:1: note: C cannot implement `Visit`: JNI value in a callback `&mut jni::Env<'_>` +fixtures/reject/src/lib.rs:1336:1: rust2-ffi: cannot export fn `keep_object`: 'static borrow `jni::objects::JObject<'static>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1341:1: rust2-ffi: cannot export fn `bound_object`: 'static borrow `&mut jni::Env<'a>`; 'static borrow `jni::objects::JObject<'a>`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:1341:21: note: `'a` is `'static`, as a handle outlives every call + fixtures/reject/src/lib.rs:1341:21: note: `'a` is `'static`, as a handle outlives every call fixtures/reject/src/lib.rs:145:2: rust2-ffi: cannot export method `Link::session`: borrowed return that is not Clone `tls::held::Handoff`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:162:1: rust2-ffi: cannot export fn `measure`: foreign type `tls::held::Local`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:167:1: rust2-ffi: cannot export instance `Cell>`: type argument `std::vec::Vec`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:167:1: rust2-ffi: cannot export instance `Cell`: type argument `tls::held::Cert`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:16:1: rust2-ffi: cannot export fn `generic`: generic parameter `std::marker::Copy`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:170:1: rust2-ffi: cannot export fn `sealed`: refused type `reject::Cell`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:176:1: rust2-ffi: cannot export fn `echo`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:181:1: rust2-ffi: cannot export fn `blame`: borrowing output of a future from a function that is not async `std::result::Result>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:186:1: rust2-ffi: cannot export fn `relay`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:191:1: rust2-ffi: cannot export fn `resolve`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:196:1: rust2-ffi: cannot export fn `spend`: closure signature `tls::held::Token`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:201:1: rust2-ffi: cannot export fn `mint`: closure signature `tls::held::Token`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:211:1: rust2-ffi: cannot export fn `spent`: reference to a handle inside a value `[tls::held::Token]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:187:2: rust2-ffi: cannot export method `Slot::lock`: returned object that borrows `SlotGuard<'_, u8>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:199:1: rust2-ffi: cannot export fn `echo`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:204:1: rust2-ffi: cannot export fn `blame`: borrowing output of a future from a function that is not async `std::result::Result>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:209:1: rust2-ffi: cannot export fn `relay`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:214:1: rust2-ffi: cannot export fn `resolve`: borrowing output of a future from a function that is not async `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:219:1: rust2-ffi: cannot export fn `spend`: closure signature `tls::held::Token`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:21:1: rust2-ffi: cannot export fn `nested`: nested Option `std::option::Option>>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:221:1: rust2-ffi: cannot export fn `adopt`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:233:2: rust2-ffi: cannot export method `Cell::nest`: instance holding an instance of a type inside it `reject::Cell>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:252:2: rust2-ffi: cannot export method `Pong>::ping`: instance holding an instance of a type inside it `reject::Ping>>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:263:1: rust2-ffi: cannot export instance `Grow<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:266:2: rust2-ffi: cannot export method `Grow::grow`: refused type `reject::Grow<(u32,)>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:277:1: rust2-ffi: cannot export instance `Shell<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:286:1: rust2-ffi: cannot export fn `shell`: refused type `reject::Shell<(u32,)>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:297:1: rust2-ffi: cannot export fn `probe`: const generic `N`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:302:1: rust2-ffi: cannot export fn `excluded_cell`: excluded type `reject::Cell`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:307:1: rust2-ffi: cannot export fn `listed`: refused type `reject::Cell>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:224:1: rust2-ffi: cannot export fn `mint`: closure signature `tls::held::Token`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:234:1: rust2-ffi: cannot export fn `spent`: reference to a handle inside a value `[tls::held::Token]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:244:1: rust2-ffi: cannot export fn `adopt`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:256:2: rust2-ffi: cannot export method `Cell::nest`: instance holding an instance of a type inside it `reject::Cell>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:275:2: rust2-ffi: cannot export method `Pong>::ping`: instance holding an instance of a type inside it `reject::Ping>>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:286:1: rust2-ffi: cannot export instance `Grow<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:289:2: rust2-ffi: cannot export method `Grow::grow`: refused type `reject::Grow<(u32,)>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:300:1: rust2-ffi: cannot export instance `Shell<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:309:1: rust2-ffi: cannot export fn `shell`: refused type `reject::Shell<(u32,)>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:30:1: rust2-ffi: cannot export fn `peek`: reference to a handle inside a value `std::boxed::Box`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:312:1: rust2-ffi: cannot export instance `Fixed<'a'>`: const argument `'a'`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:315:1: rust2-ffi: cannot export fn `fixed`: refused type `reject::Fixed<'a'>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:320:9: rust2-ffi: cannot export use `HashSet`: re-export of the standard library's item `std`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:323:1: rust2-ffi: cannot export fn `lend`: Waker outside a parameter or a return; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:331:2: rust2-ffi: cannot export field `Tag::label`: 'static borrow `&'static str`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:341:1: rust2-ffi: cannot export fn `gather`: closure signature `std::vec::Vec`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:346:1: rust2-ffi: cannot export fn `attempt`: closure signature `std::result::Result`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:351:1: rust2-ffi: cannot export fn `consume`: reader not lent as &mut `impl std::io::Read + std::io::Seek`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:356:1: rust2-ffi: cannot export fn `retain`: reader kept past the call `R`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:320:1: rust2-ffi: cannot export fn `probe`: const generic `N`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:325:1: rust2-ffi: cannot export fn `excluded_cell`: excluded type `reject::Cell`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:330:1: rust2-ffi: cannot export fn `listed`: refused type `reject::Cell>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:335:1: rust2-ffi: cannot export instance `Fixed<'a'>`: const argument `'a'`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:338:1: rust2-ffi: cannot export fn `fixed`: refused type `reject::Fixed<'a'>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:343:9: rust2-ffi: cannot export use `HashSet`: re-export of the standard library's item `std`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:346:1: rust2-ffi: cannot export fn `lend`: Waker outside a parameter or a return; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:354:2: rust2-ffi: cannot export field `Tag::label`: 'static borrow `&'static str`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:35:1: rust2-ffi: cannot export static `COUNT`: static; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:361:1: rust2-ffi: cannot export fn `rewrite`: reader bound `std::io::Write`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:366:1: rust2-ffi: cannot export fn `read_later`: reader kept past the call `impl std::io::Read + std::io::Seek`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:371:1: rust2-ffi: cannot export fn `twice`: closure bound `std::clone::Clone`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:377:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:383:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:388:1: rust2-ffi: cannot export fn `count_words`: Vec of references `&str`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:393:1: rust2-ffi: cannot export fn `count_some`: Vec of references `std::option::Option<&str>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:398:1: rust2-ffi: cannot export fn `count_slice`: slice of references `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:364:1: rust2-ffi: cannot export fn `gather`: closure signature `std::vec::Vec`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:369:1: rust2-ffi: cannot export fn `attempt`: closure signature `std::result::Result`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:374:1: rust2-ffi: cannot export fn `consume`: reader not lent as &mut `impl std::io::Read + std::io::Seek`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:379:1: rust2-ffi: cannot export fn `retain`: reader kept past the call `R`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:384:1: rust2-ffi: cannot export fn `rewrite`: reader bound `std::io::Write`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:389:1: rust2-ffi: cannot export fn `read_later`: reader kept past the call `impl std::io::Read + std::io::Seek`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:394:1: rust2-ffi: cannot export fn `twice`: closure bound `std::clone::Clone`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:39:1: rust2-ffi: cannot export macro `noop`: macro; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:403:1: rust2-ffi: cannot export fn `peek_all`: reference to a handle inside a value `[Opaque]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:408:1: rust2-ffi: cannot export fn `none_opaque`: borrowed return that is not Clone `[Opaque]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:420:1: rust2-ffi: cannot export fn `stash`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:435:1: rust2-ffi: cannot export fn `hold`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:440:1: rust2-ffi: cannot export fn `bound`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:445:1: rust2-ffi: cannot export fn `kept`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:459:1: rust2-ffi: cannot export fn `holder`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:473:1: rust2-ffi: cannot export fn `lender`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:400:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:406:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:411:1: rust2-ffi: cannot export fn `count_words`: Vec of references `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:416:1: rust2-ffi: cannot export fn `count_some`: Vec of references `std::option::Option<&str>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:421:1: rust2-ffi: cannot export fn `count_slice`: slice of references `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:426:1: rust2-ffi: cannot export fn `peek_all`: reference to a handle inside a value `[Opaque]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:431:1: rust2-ffi: cannot export fn `none_opaque`: borrowed return that is not Clone `[Opaque]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:443:1: rust2-ffi: cannot export fn `stash`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:458:1: rust2-ffi: cannot export fn `hold`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:463:1: rust2-ffi: cannot export fn `bound`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:468:1: rust2-ffi: cannot export fn `kept`: generic parameter `std::convert::Into`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:47:1: rust2-ffi: cannot export fn `excluded`: excluded type `Excluded`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:488:1: rust2-ffi: cannot export fn `outlived`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:503:1: rust2-ffi: cannot export fn `paired`: generic parameter `std::convert::Into`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:526:1: rust2-ffi: cannot export fn `pinner`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:482:1: rust2-ffi: cannot export fn `holder`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:496:1: rust2-ffi: cannot export fn `lender`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:511:1: rust2-ffi: cannot export fn `outlived`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:526:1: rust2-ffi: cannot export fn `paired`: generic parameter `std::convert::Into`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:52:1: rust2-ffi: cannot export fn `letters`: Range `char`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:541:1: rust2-ffi: cannot export fn `grows`: growing conversion `Grows`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:533:1: note: applies past the recursion limit -fixtures/reject/src/lib.rs:571:1: rust2-ffi: cannot export fn `call`: ambiguous conversion `std::vec::Vec`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:548:1: note: `std::string::String` converts too, which C passes alike -fixtures/reject/src/lib.rs:576:1: rust2-ffi: cannot export fn `alphabet`: Range `char`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:581:1: rust2-ffi: cannot export fn `naturals`: foreign type `std::ops::RangeFrom`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:586:1: rust2-ffi: cannot export fn `opaques`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:591:1: rust2-ffi: cannot export fn `tried`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:596:1: rust2-ffi: cannot export fn `eventually`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:622:1: rust2-ffi: cannot export fn `tallied`: unmet bound `u32: std::convert::From`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:627:1: rust2-ffi: cannot export fn `drain`: trait object `dyn Source`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:601:1: note: C cannot implement `Source`: callback signature `items` -fixtures/reject/src/lib.rs:632:1: rust2-ffi: cannot export fn `summed_forever`: 'static borrow `&'static [u32]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:637:1: rust2-ffi: cannot export fn `retext_last`: 'static borrow `&'a [std::string::String]`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:637:20: note: `'a` is `'static`, as a handle outlives every call -fixtures/reject/src/lib.rs:644:1: rust2-ffi: cannot export fn `outlast`: 'static borrow `&'b [std::string::String]`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:644:20: note: `'b` is `'static`, as a handle outlives every call -fixtures/reject/src/lib.rs:651:1: rust2-ffi: cannot export fn `retext_into`: 'static borrow `&'a [std::string::String]`; exclude it in the config to leave it out - fixtures/reject/src/lib.rs:651:20: note: `'a` is `'static`, as a handle outlives every call -fixtures/reject/src/lib.rs:660:1: rust2-ffi: cannot export fn `ones`: foreign type `std::iter::Repeat`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:666:1: rust2-ffi: cannot export fn `long_words`: foreign type `std::iter::Filter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:549:1: rust2-ffi: cannot export fn `pinner`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:564:1: rust2-ffi: cannot export fn `grows`: growing conversion `Grows`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:556:1: note: applies past the recursion limit +fixtures/reject/src/lib.rs:594:1: rust2-ffi: cannot export fn `call`: ambiguous conversion `std::vec::Vec`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:571:1: note: `std::string::String` converts too, which C passes alike +fixtures/reject/src/lib.rs:599:1: rust2-ffi: cannot export fn `alphabet`: Range `char`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:604:1: rust2-ffi: cannot export fn `naturals`: foreign type `std::ops::RangeFrom`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:609:1: rust2-ffi: cannot export fn `opaques`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:614:1: rust2-ffi: cannot export fn `tried`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:619:1: rust2-ffi: cannot export fn `eventually`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:645:1: rust2-ffi: cannot export fn `tallied`: unmet bound `u32: std::convert::From`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:650:1: rust2-ffi: cannot export fn `drain`: trait object `dyn Source`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:624:1: note: C cannot implement `Source`: callback signature `items` +fixtures/reject/src/lib.rs:655:1: rust2-ffi: cannot export fn `summed_forever`: 'static borrow `&'static [u32]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:660:1: rust2-ffi: cannot export fn `retext_last`: 'static borrow `&'a [std::string::String]`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:660:20: note: `'a` is `'static`, as a handle outlives every call +fixtures/reject/src/lib.rs:667:1: rust2-ffi: cannot export fn `outlast`: 'static borrow `&'b [std::string::String]`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:667:20: note: `'b` is `'static`, as a handle outlives every call fixtures/reject/src/lib.rs:66:2: rust2-ffi: cannot export method `Text::view`: returned object that borrows `Text<'_>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:672:1: rust2-ffi: cannot export fn `kept_in`: foreign type `std::iter::Filter`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:689:1: rust2-ffi: cannot export fn `launched`: foreign type `std::iter::Chain`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:674:1: rust2-ffi: cannot export fn `retext_into`: 'static borrow `&'a [std::string::String]`; exclude it in the config to leave it out + fixtures/reject/src/lib.rs:674:20: note: `'a` is `'static`, as a handle outlives every call +fixtures/reject/src/lib.rs:683:1: rust2-ffi: cannot export fn `ones`: foreign type `std::iter::Repeat`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:689:1: rust2-ffi: cannot export fn `long_words`: foreign type `std::iter::Filter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:695:1: rust2-ffi: cannot export fn `kept_in`: foreign type `std::iter::Filter`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:6:1: rust2-ffi: cannot export fn `pair`: tuple of references `(&str, u8)`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:706:1: rust2-ffi: cannot export fn `launches`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:729:1: rust2-ffi: cannot export fn `pumped`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:743:1: rust2-ffi: cannot export fn `skipped`: foreign type `std::iter::Skip`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:748:1: rust2-ffi: cannot export fn `merged`: foreign type `std::iter::Copied`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:755:1: rust2-ffi: cannot export fn `values`: foreign type `std::collections::btree_map::IntoValues`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:761:1: rust2-ffi: cannot export fn `floats`: collection not built from its items `std::collections::BTreeSet`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:766:1: rust2-ffi: cannot export fn `zeros`: foreign type `std::iter::RepeatN`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:771:1: rust2-ffi: cannot export fn `successes`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:712:1: rust2-ffi: cannot export fn `launched`: foreign type `std::iter::Chain`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:729:1: rust2-ffi: cannot export fn `launches`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:752:1: rust2-ffi: cannot export fn `pumped`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:766:1: rust2-ffi: cannot export fn `skipped`: foreign type `std::iter::Skip`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:771:1: rust2-ffi: cannot export fn `merged`: foreign type `std::iter::Copied`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:778:1: rust2-ffi: cannot export fn `values`: foreign type `std::collections::btree_map::IntoValues`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:77:1: rust2-ffi: cannot export fn `first`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:791:1: rust2-ffi: cannot export fn `allocated`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:798:1: rust2-ffi: cannot export fn `flattened`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:822:1: rust2-ffi: cannot export fn `lent`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:840:1: rust2-ffi: cannot export fn `halves`: foreign type `std::str::Split`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:845:1: rust2-ffi: cannot export fn `settled`: Poll with no Context or waiter parameter `std::task::Poll`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:850:1: rust2-ffi: cannot export fn `poll_named`: poll's parameter taken by value that is not Copy `std::string::String`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:856:1: rust2-ffi: cannot export fn `poll_first`: borrowed Poll `&u8`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:862:1: rust2-ffi: cannot export fn `poll_until`: poll's parameter taken by value that is not Copy `impl Fn(u8) -> bool`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:873:1: rust2-ffi: cannot export fn `context_later`: Context of a function returning a future; future not Send; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:892:1: rust2-ffi: cannot export fn `poll_ticket`: Poll with no Context or waiter parameter `std::task::Poll`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:897:1: rust2-ffi: cannot export fn `poll_both`: several task parameters; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:904:1: rust2-ffi: cannot export fn `poll_bump`: &mut of a value in a future; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:911:1: rust2-ffi: cannot export fn `peek_some`: reference to a handle inside a value `std::boxed::Box`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:916:1: rust2-ffi: cannot export fn `bump_some`: &mut of a value inside an Option `std::option::Option<&mut u32>`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:923:1: rust2-ffi: cannot export fn `rest`: returned borrow of a value written back `&[u8]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:928:1: rust2-ffi: cannot export fn `fill`: returned borrow of a value written back `impl std::future::Future`; &mut of a value in a future; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:933:1: rust2-ffi: cannot export fn `zero`: &mut of a slice `[u8]`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:938:1: rust2-ffi: cannot export fn `reseat`: &mut of another crate's Arc `std::sync::Arc`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:951:2: rust2-ffi: cannot export method `Keeper::check`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:962:9: rust2-ffi: cannot export use `dep`: re-export of a whole crate `dep`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:965:9: rust2-ffi: cannot export use `renamed`: re-export of a whole crate `dep`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:976:2: rust2-ffi: cannot export method `Tally::poll_bump`: `&mut self` of a value; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:994:2: rust2-ffi: cannot export method `Risky::new`: unsafe fn; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:784:1: rust2-ffi: cannot export fn `floats`: collection not built from its items `std::collections::BTreeSet`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:789:1: rust2-ffi: cannot export fn `zeros`: foreign type `std::iter::RepeatN`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:794:1: rust2-ffi: cannot export fn `successes`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:814:1: rust2-ffi: cannot export fn `allocated`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:821:1: rust2-ffi: cannot export fn `flattened`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:845:1: rust2-ffi: cannot export fn `lent`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:863:1: rust2-ffi: cannot export fn `halves`: foreign type `std::str::Split`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:868:1: rust2-ffi: cannot export fn `settled`: Poll with no Context or waiter parameter `std::task::Poll`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:873:1: rust2-ffi: cannot export fn `poll_named`: poll's parameter taken by value that is not Copy `std::string::String`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:879:1: rust2-ffi: cannot export fn `poll_first`: borrowed Poll `&u8`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:885:1: rust2-ffi: cannot export fn `poll_until`: poll's parameter taken by value that is not Copy `impl Fn(u8) -> bool`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:896:1: rust2-ffi: cannot export fn `context_later`: Context of a function returning a future; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:915:1: rust2-ffi: cannot export fn `poll_ticket`: Poll with no Context or waiter parameter `std::task::Poll`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:920:1: rust2-ffi: cannot export fn `poll_both`: several task parameters; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:927:1: rust2-ffi: cannot export fn `poll_bump`: &mut of a value in a future; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:934:1: rust2-ffi: cannot export fn `peek_some`: reference to a handle inside a value `std::boxed::Box`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:939:1: rust2-ffi: cannot export fn `bump_some`: &mut of a value inside an Option `std::option::Option<&mut u32>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:946:1: rust2-ffi: cannot export fn `rest`: returned borrow of a value written back `&[u8]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:951:1: rust2-ffi: cannot export fn `fill`: returned borrow of a value written back `impl std::future::Future`; &mut of a value in a future; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:956:1: rust2-ffi: cannot export fn `zero`: &mut of a slice `[u8]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:961:1: rust2-ffi: cannot export fn `reseat`: &mut of another crate's Arc `std::sync::Arc`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:974:2: rust2-ffi: cannot export method `Keeper::check`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:985:9: rust2-ffi: cannot export use `dep`: re-export of a whole crate `dep`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:988:9: rust2-ffi: cannot export use `renamed`: re-export of a whole crate `dep`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:999:2: rust2-ffi: cannot export method `Tally::poll_bump`: `&mut self` of a value; exclude it in the config to leave it out fixtures/reject/src/lib.rs:99:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future>`; exclude it in the config to leave it out diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index 8913820a9..d9f75ba6c 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -171,6 +171,29 @@ pub fn sealed(cert: tls::held::Cert) -> Cell { Cell(cert) } +/// A generic type whose guard borrows it. +pub struct Slot(T); + +/// A guard borrowing its slot, an instance of which an export names with the lifetime its +/// signature binds. +pub struct SlotGuard<'a, T>(&'a mut T); + +impl Slot { + pub fn new() -> Self { + Self(0) + } + + /// A guard that borrows its handle, which a new handle would outlive. + pub fn lock(&mut self) -> SlotGuard<'_, u8> { + SlotGuard(&mut self.0) + } + + /// A guard of a slot of its own, borrowing nothing, so C holds one. + pub fn leaked() -> SlotGuard<'static, u8> { + SlotGuard(Box::leak(Box::new(0))) + } +} + /// A future returned by a function that is not async, whose output borrows what the /// future holds only until it completes. pub fn echo(tag: &str) -> impl std::future::Future + '_ { diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index e23b44fb7..3206e638b 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -4,10 +4,6 @@ # only what the facade re-exports. exclude = [ "bytes", - "clock::Deadlines::enter", - "clock::Entered", - "clock::Rng", - "clock::rng", "hang::moq_net", "kio", "moq_quic::VarInt::from_u64_unchecked", @@ -23,10 +19,6 @@ exclude = [ "moq_tokio::websocket::Listener::accept", "mp4_atom::TrunSamples::iter", "mp4_atom::Tx3g::box_record", - "mux::catalog::Guard", - "mux::catalog::Producer::modify", - "mux::catalog::Producer::modify", - "mux::catalog::Producer::modify", "mux::container::Producer::modify", "mux::container::Producer::modify", "mux::container::Producer::modify", diff --git a/ffi/runtime/dart.rs b/ffi/runtime/dart.rs index c1ee55f26..6e2e0dd73 100644 --- a/ffi/runtime/dart.rs +++ b/ffi/runtime/dart.rs @@ -226,17 +226,20 @@ impl Home { // SAFETY: this thread waits below until the job ran, so `run` outlives its use. run: Mutex::new(Some(unsafe { Run::new(&mut run) })), thread: rt::thread(), + waiter: std::thread::current(), done: Mutex::new(false), ran: Condvar::new(), }); if !self.push(Task::Call(job.clone())) { rt::fail(what, "the Dart isolate it calls into has exited"); } - let mut done = job.done.lock().unwrap_or_else(|e| e.into_inner()); - while !*done { - done = job.ran.wait(done).unwrap_or_else(|e| e.into_inner()); + // A home runs meanwhile what the Dart it calls asks of the values it holds. + if !rt::Thread::serve_until(|| *job.done.lock().unwrap_or_else(|e| e.into_inner())) { + let mut done = job.done.lock().unwrap_or_else(|e| e.into_inner()); + while !*done { + done = job.ran.wait(done).unwrap_or_else(|e| e.into_inner()); + } } - drop(done); // SAFETY: the job is done only once it ran. unsafe { out.assume_init() } } @@ -404,6 +407,8 @@ pub struct Job { run: Mutex>, /// The waiting thread, which the isolate's acts as while it runs the call. thread: usize, + /// The waiting thread, which a home's wait parks. + waiter: std::thread::Thread, done: Mutex, ran: Condvar, } @@ -419,6 +424,7 @@ impl Job { } *self.done.lock().unwrap_or_else(|e| e.into_inner()) = true; self.ran.notify_all(); + self.waiter.unpark(); } } diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 85bee7168..9c05fd0ed 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -711,8 +711,9 @@ pub unsafe fn give(node: *mut Node) -> rt::Waker { lend(node) } -/// Poll a future with a waker, writing its result into the writer if it finished. -pub type Poll = unsafe fn(*mut c_void, rt::Waker, &mut Writer) -> bool; +/// Poll a future with a waker, writing its result into the writer if it finished, or a +/// failed check's status into the last, for a future that is not `Send`. +pub type Poll = unsafe fn(*mut c_void, rt::Waker, &mut Writer, &mut i32) -> bool; /// Poll the future just made, and free it if it finished: the call and its first poll are /// one crossing. Its writer holds a bool, true when finished, then the result, or else the @@ -720,7 +721,15 @@ pub type Poll = unsafe fn(*mut c_void, rt::Waker, &mut Writer) -> bool; /// /// # Safety /// `future` is a live future that `poll` and `free` take, and `signal` a Kotlin `Waker`. -pub unsafe fn start(env: *mut JNIEnv, future: *mut c_void, signal: jobject, poll: Poll, free: unsafe fn(*mut c_void)) -> Writer { +/// None once a poll's check failed, which throws as the native method returns. +pub unsafe fn start( + env: *mut JNIEnv, + future: *mut c_void, + signal: jobject, + poll: Poll, + free: unsafe fn(*mut c_void), + what: &str, +) -> Option { let mut w = Writer::default(); w.put(true); // Most futures are ready at once, so the first poll takes no waker: a node costs a @@ -730,28 +739,36 @@ pub unsafe fn start(env: *mut JNIEnv, future: *mut c_void, signal: jobject, poll drop: None, data: std::ptr::null_mut(), }; + let mut status = 0; // SAFETY: the caller's contract. - if unsafe { poll(future, none, &mut w) } { - // SAFETY: as above, and it finished. + let ready = unsafe { poll(future, none, &mut w, &mut status) }; + if ready || status != 0 { + // SAFETY: as above, and it finished, or is never polled again. unsafe { free(future) }; - return w; + return match status { + 0 => Some(w), + _ => { + refuse::<()>(env, status, what); + None + } + }; } // Pending, so polled again with the waker it parks on, which replaces the first. let node = node(env, signal); // SAFETY: as above, and `node` holds a reference for this. - if unsafe { poll(future, give(node), &mut w) } { + if unsafe { poll(future, give(node), &mut w, &mut status) } { // SAFETY: as above. unsafe { free(future); unref(node.cast()); } - return w; + return Some(w); } let mut w = Writer::default(); w.put(false); w.put(future); w.put(node.cast::()); - w + Some(w) } /// Poll `future` again with `node`, whose reference Rust holds from the first poll that @@ -759,11 +776,17 @@ pub unsafe fn start(env: *mut JNIEnv, future: *mut c_void, signal: jobject, poll /// /// # Safety /// `future` is a live future that `poll` takes, and Rust holds a reference to `node`. -pub unsafe fn poll(future: *mut c_void, node: *mut Node, poll: Poll) -> Option { +/// A failed check throws as the native method returns, which ignores its null. +pub unsafe fn poll(env: *mut JNIEnv, future: *mut c_void, node: *mut Node, poll: Poll, what: &str) -> Option { let mut w = Writer::default(); + let mut status = 0; // SAFETY: the caller's contract. - match unsafe { poll(future, lend(node), &mut w) } { + match unsafe { poll(future, lend(node), &mut w, &mut status) } { true => Some(w), + false if status != 0 => { + refuse::<()>(env, status, what); + None + } false => None, } } diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 50821a85f..edbe578f3 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -401,14 +401,7 @@ impl Scope { return Ok(()); } self.given(); - let message = format!("{what}: {}", rt::Status::text(status).to_string_lossy()); - // A value moved out or outlived is as a closed one, and any other check is a misuse. - // SAFETY: static exception types. - let exc = match rt::Status::of(status) { - Some(rt::Status::Moved | rt::Status::Outlived) => unsafe { PyExc_ValueError }, - _ => unsafe { PyExc_RuntimeError }, - }; - raise(exc, &message) + check_status(status, what) } /// Note that what the call was given is Rust's. @@ -427,6 +420,21 @@ impl Scope { } } +/// Raise for `what` unless its `status` is zero, as [`Scope::status`] does: a poll's. +pub fn check_status(status: i32, what: &str) -> R<()> { + if status == 0 { + return Ok(()); + } + let message = format!("{what}: {}", rt::Status::text(status).to_string_lossy()); + // A value moved out or outlived is as a closed one, and any other check is a misuse. + // SAFETY: static exception types. + let exc = match rt::Status::of(status) { + Some(rt::Status::Moved | rt::Status::Outlived) => unsafe { PyExc_ValueError }, + _ => unsafe { PyExc_RuntimeError }, + }; + raise(exc, &message) +} + // Popping rather than draining, as each call drops a scope whose lists are mostly empty. impl Drop for Scope { fn drop(&mut self) { diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 7c35f56f8..065de10d1 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -18,6 +18,9 @@ use std::task::{Context, Wake}; mod borrow; use borrow::{Borrow, EXCLUSIVE, RESERVED, TAKEN, Table}; +mod thread; +pub use thread::Thread; +use thread::Dropped; /// `value`'s `Hash`, through std's hasher with its fixed keys, so a value hashes alike on /// every call. @@ -50,17 +53,20 @@ pub enum Status { Lent = 5, /// A handle lent from another outlived it. Outlived = 6, + /// A value that is not `Send` is used on another thread than the one that made it. + NotSend = 7, } impl Status { /// Every status, by its number less one. - pub const ALL: [Self; 6] = [ + pub const ALL: [Self; 7] = [ Self::Moved, Self::Exclusive, Self::Borrowed, Self::Thread, Self::Lent, Self::Outlived, + Self::NotSend, ]; /// What broke, as a binding's error says it after the function's name. @@ -78,6 +84,7 @@ impl Status { Self::Outlived => { c"a handle lent from another outlived it: the other was borrowed exclusively, moved out, or freed" } + Self::NotSend => c"a value that is not Send is used on another thread than the one that made it", } } @@ -1008,6 +1015,68 @@ pub struct Object { epoch: AtomicUsize, /// For a handle a call returned lent from another's value, that handle. lender: Option, + /// For a value that is not `Send`, the thread it stays on, which alone borrows, moves, + /// or drops it. + owner: Option>, +} + +/// A value that is not `Send`, held by a handle that keeps it on the thread that made it: +/// every borrow, move, and drop of it checks it is on that thread, or for a drop, queues +/// it there. So a handle to it may cross threads, which the type says to the runtime. +#[repr(transparent)] +pub struct Unsent(T); + +// SAFETY: only an `Object` holds one, made by `Object::unsent` and its kin, whose every use +// of it runs on its owner's thread, or for a drop, queues it there. +unsafe impl Send for Unsent {} + +impl Unsent<()> { + /// The name of `U`, an `Unsent`, as the value it holds. + fn named() -> &'static str { + let name = std::any::type_name::(); + let inner = name.split_once("Unsent<").and_then(|(_, inner)| inner.strip_suffix('>')); + inner.unwrap_or(name) + } +} + +impl Unsent { + /// The value, moved out on its thread. + pub fn into_inner(self) -> T { + self.0 + } + + /// A borrowed value, as a borrowed `Unsent` of it. + fn from_ref(value: &T) -> &Self { + // SAFETY: `repr(transparent)`. + unsafe { &*std::ptr::from_ref(value).cast() } + } + + fn from_mut(value: &mut T) -> &mut Self { + // SAFETY: `repr(transparent)`. + unsafe { &mut *std::ptr::from_mut(value).cast() } + } +} + +impl Clone for Unsent { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} + +impl Copy for Unsent {} + +impl Deref for Unsent { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +impl DerefMut for Unsent { + fn deref_mut(&mut self) -> &mut T { + &mut self.0 + } } /// The handle a returned borrow was lent from, and its epoch then: the borrow lives while @@ -1094,6 +1163,13 @@ unsafe impl Sync for Object {} impl Object { /// A new handle owning `value`, which may outlive any borrow, so `value` holds none. pub fn new(value: T) -> *const c_void + where + T: 'static, + { + Self::owning(value, None) + } + + fn owning(value: T, owner: Option>) -> *const c_void where T: 'static, { @@ -1104,6 +1180,7 @@ impl Object { value: UnsafeCell::new(MaybeUninit::new(value)), epoch: AtomicUsize::new(0), lender: None, + owner, })) .cast() } @@ -1113,6 +1190,10 @@ impl Object { /// exclusively, moved out, or freed, after which a use fails. It is never borrowed /// exclusively nor moved out itself. pub fn lent(value: &T, lender: Lender) -> *const c_void { + Self::lent_by(value, lender, None) + } + + fn lent_by(value: &T, lender: Lender, owner: Option>) -> *const c_void { Arc::into_raw(Arc::new(Self { borrow: Borrow::new(0), depth: UnsafeCell::new(0), @@ -1120,6 +1201,7 @@ impl Object { value: UnsafeCell::new(MaybeUninit::uninit()), epoch: AtomicUsize::new(0), lender: Some(lender), + owner, })) .cast() } @@ -1204,6 +1286,7 @@ impl Object { /// Borrow a value that is not `Sync` shared: any number of times on one thread, as /// Rust allows, and on no other thread meanwhile. fn acquire_local(&self, column: &mut usize, level: u32) -> Result { + self.own()?; self.borrow.local(thread())?; // SAFETY: only the thread in the state touches `depth`. unsafe { *self.depth.get() += 1 }; @@ -1273,8 +1356,18 @@ impl Object { } } + /// Whether this thread may use the value: any may, unless it is not `Send`. + #[inline(always)] + fn own(&self) -> Result<(), Status> { + match &self.owner { + None => Ok(()), + Some(owner) => owner.own(), + } + } + /// Take the value from no borrow to `to`, as an exclusive borrow or a move does. fn acquire_from_zero(&self, to: usize) -> Result<(), Status> { + self.own()?; if self.lender.is_some() || (matches!(to, TAKEN | RESERVED) && !self.lent.is_null()) { return Err(Status::Lent); } @@ -1383,23 +1476,30 @@ impl Object { where T: Sync, { - Self::lending(std::ptr::from_ref(value).cast_mut(), 1, 0, what, f) + Self::lending(std::ptr::from_ref(value).cast_mut(), 1, 0, None, what, f) } /// Lend a value that is not `Sync` shared, as [`Self::lend`], on this thread alone. pub fn lend_local(value: &T, what: &str, f: impl FnOnce(*const c_void) -> R) -> R { - Self::lending(std::ptr::from_ref(value).cast_mut(), thread(), 1, what, f) + Self::lending(std::ptr::from_ref(value).cast_mut(), thread(), 1, None, what, f) } /// Lend `value` exclusively, as `&mut T` lends it. pub fn lend_mut(value: &mut T, what: &str, f: impl FnOnce(*const c_void) -> R) -> R { - Self::lending(std::ptr::from_mut(value), 0, 0, what, f) + Self::lending(std::ptr::from_mut(value), 0, 0, None, what, f) } /// Lend the value at `ptr` to C as a handle while `f` runs, holding the borrow `state` /// and `depth` describe, if any, so C borrows it no further than Rust lent it. C may /// neither move it out, nor free the handle, nor keep a borrow past `f`. - fn lending(ptr: *mut T, state: usize, depth: usize, what: &str, f: impl FnOnce(*const c_void) -> R) -> R { + fn lending( + ptr: *mut T, + state: usize, + depth: usize, + owner: Option>, + what: &str, + f: impl FnOnce(*const c_void) -> R, + ) -> R { let object = Arc::new(Self { borrow: Borrow::new(state), depth: UnsafeCell::new(depth), @@ -1407,6 +1507,7 @@ impl Object { value: UnsafeCell::new(MaybeUninit::uninit()), epoch: AtomicUsize::new(0), lender: None, + owner, }); // A second count, which a free by C would take, so the handle outlives it. let spare = Arc::clone(&object); @@ -1530,13 +1631,18 @@ impl Object { /// reserved it to move out, as Rust refuses a drop under a borrow, and does nothing once /// the value is gone. A handle a /// call returned lent from another's value only stops lending it; one lent to C for a - /// call fails, as Rust lent it. + /// call fails, as Rust lent it. A value that is not `Send`, closed on another thread than + /// its own, drops there later, as a free does. /// /// # Safety /// As [`Self::borrow`]. pub unsafe fn close(handle: *const c_void, what: &str) -> Result<(), Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; + let away = object.owner.as_ref().filter(|owner| !owner.is_here()); + if away.is_none() { + object.own()?; + } if !object.lent.is_null() && object.lender.is_none() { return Err(Status::Lent); } @@ -1547,8 +1653,16 @@ impl Object { } object.epoch.fetch_add(1, Ordering::Release); if object.lent.is_null() { - // SAFETY: TAKEN excludes every other access, and `Drop` skips a taken value. - unsafe { (*object.value.get()).assume_init_drop() }; + match away { + Some(owner) => { + // SAFETY: as below; the value moves out to drop on its own thread. + let value = unsafe { (*object.value.get()).assume_init_read() }; + // SAFETY: the value is the owner's. + unsafe { Dropped::new(value, owner.clone(), Unsent::<()>::named::()) }.send(); + } + // SAFETY: TAKEN excludes every other access, and `Drop` skips a taken value. + None => unsafe { (*object.value.get()).assume_init_drop() }, + } } Ok(()) } @@ -1586,10 +1700,69 @@ impl Object { impl Drop for Object { fn drop(&mut self) { + let mut owner = self.owner.take(); if self.lent.is_null() && *self.borrow.state.get_mut() != TAKEN { - // SAFETY: the last reference is gone, and the value is its own and never taken. - unsafe { self.value.get_mut().assume_init_drop() }; + let value = self.value.get_mut(); + match owner.take_if(|owner| !owner.is_here()) { + // A value that is not `Send` drops on its own thread. + Some(owner) => { + // SAFETY: as below; the value moves out to drop there, and nothing reads it here again. + let value = unsafe { value.assume_init_read() }; + // SAFETY: the value is the owner's. + unsafe { Dropped::new(value, owner, Unsent::<()>::named::()) }.send(); + } + // SAFETY: the last reference is gone, and the value is its own and never taken. + None => unsafe { value.assume_init_drop() }, + } + } + // A handle whose value dropped already, as one closed or lent, may hold the last + // record of its home but the home's own: letting go wakes the home to see so. + if let Some(owner) = owner { + Thread::let_go(owner); + } + } +} + +impl Object> { + /// A new handle owning `value`, which is not `Send`, so stays on this thread: a use on + /// another fails, and a free there drops it here. + pub fn unsent(value: T) -> *const c_void + where + T: 'static, + { + Self::owning(Unsent(value), Some(Thread::here())) + } + + /// [`Self::lent`] of a value that is not `Send`, on this thread. + pub fn lent_unsent(value: &T, lender: Lender) -> *const c_void { + Self::lent_by(Unsent::from_ref(value), lender, Some(Thread::here())) + } + + /// [`Self::lend_local`] of a value that is not `Send`, on this thread. + pub fn lend_unsent(value: &T, what: &str, f: impl FnOnce(*const c_void) -> R) -> R { + let ptr = std::ptr::from_ref(Unsent::from_ref(value)).cast_mut(); + Self::lending(ptr, thread(), 1, Some(Thread::here()), what, f) + } + + /// [`Self::lend_mut`] of a value that is not `Send`, on this thread. + pub fn lend_mut_unsent(value: &mut T, what: &str, f: impl FnOnce(*const c_void) -> R) -> R { + Self::lending(std::ptr::from_mut(Unsent::from_mut(value)), 0, 0, Some(Thread::here()), what, f) + } +} + +impl Object { + /// The thread `handle`'s value stays on, unless it is null: a call borrowing or moving + /// it runs there if that is a home elsewhere, as the thread that made it. + /// + /// # Safety + /// `handle` is null or a live handle of this type, kept alive through the call. + pub unsafe fn owner<'a>(handle: *const c_void) -> Option<&'a Thread> { + if handle.is_null() { + return None; } + // SAFETY: the caller's contract; `owner` never changes. + let object = unsafe { &*handle.cast::() }; + object.owner.as_deref() } } @@ -2095,6 +2268,8 @@ pub struct Future { /// The last foreign waker, and the Rust waker around it. waker: Option<(Waker, std::task::Waker)>, polling: AtomicBool, + /// For a future that is not `Send`, the thread it stays on, which alone polls or drops it. + owner: Option>, } /// The allocation behind a handle: its head first, so the handle points at both. @@ -2115,6 +2290,24 @@ impl Future { F: std::future::Future + Send + 'static, // Send, as any poll may finish the future on another thread. L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + Send + 'static, + { + Self::make(future, lower, None) + } + + /// A future that is not `Send`, which stays on this thread: a poll on another fails, + /// unless this is a home, which the poll then runs on, and a free there drops it here. + pub fn unsent(future: F, lower: L) -> *mut Self + where + F: std::future::Future + 'static, + L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, + { + Self::make(future, lower, Some(Thread::here())) + } + + fn make(future: F, lower: L, owner: Option>) -> *mut Self + where + F: std::future::Future + 'static, + L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, { let typed = Box::new(Typed { head: Self { @@ -2122,6 +2315,7 @@ impl Future { drop: drop_typed::, waker: None, polling: AtomicBool::new(false), + owner, }, future: Some(future), lower, @@ -2129,17 +2323,48 @@ impl Future { Box::into_raw(typed).cast() } - /// Poll it once with `waker`, writing the result through `outs` and `err` when ready. + /// Poll it once with `waker`, writing the result through `outs` and `err` when ready. A + /// future that is not `Send` polled on another thread than its own fails, writing the + /// status through `status`, or aborting if that is null; one on a home polls there. /// /// # Safety - /// `future` is a live future handle, and each of `outs` and `err` is null or points - /// where the function's result types go. - pub unsafe fn poll(future: *mut Self, waker: Waker, outs: &[*mut c_void], err: *mut c_void, what: &str) -> i32 { + /// `future` is a live future handle, each of `outs` and `err` is null or points where + /// the function's result types go, and `status` is null or points where an `int32_t` goes. + pub unsafe fn poll( + future: *mut Self, + waker: Waker, + outs: &[*mut c_void], + err: *mut c_void, + status: *mut i32, + what: &str, + ) -> i32 { if future.is_null() { fail(what, "a future is null"); } - // SAFETY: the caller's contract; only the flag is touched before it is held. + // SAFETY: the caller's contract; only the flag and the owner are touched before + // it is held. let head = unsafe { &*future }; + if let Some(owner) = &head.owner { + if owner.away() { + // SAFETY: a home, and this thread waits while it polls. + return unsafe { owner.run(|| Self::poll(future, waker, outs, err, status, what)) }; + } + if let Err(refused) = owner.own() { + if head.polling.swap(true, Ordering::Acquire) { + fail(what, "a future is polled concurrently"); + } + // A waker it never adopted is Rust's to drop, as a poll drops the one it + // replaces; the flag keeps the one it holds from changing meanwhile. + // SAFETY: the flag makes this the only access. + if !unsafe { &(*future).waker }.as_ref().is_some_and(|(last, _)| last.same(&waker)) { + // SAFETY: the caller gave it, and nothing else holds it. + unsafe { waker.release() }; + } + head.polling.store(false, Ordering::Release); + // SAFETY: the caller's contract. + return unsafe { failed(status, refused, what) }; + } + } if head.polling.swap(true, Ordering::Acquire) { fail(what, "a future is polled concurrently"); } @@ -2173,8 +2398,9 @@ impl Future { drop: None, data: std::ptr::null_mut(), }; - // SAFETY: the caller's contract. - let result = unsafe { Self::poll(future, none, outs, err, what) }; + // SAFETY: the caller's contract; the future was made on this thread, or one this + // call runs on. + let result = unsafe { Self::poll(future, none, outs, err, std::ptr::null_mut(), what) }; match result { // SAFETY: the caller's contract. PENDING => unsafe { handle.write(future) }, @@ -2192,10 +2418,29 @@ impl Future { /// # Safety /// `future` is null or a live future handle, freed once and not being polled. pub unsafe fn free(future: *mut Self) { - if !future.is_null() { - // SAFETY: the caller's contract. - unsafe { ((*future).drop)(future) }; + if future.is_null() { + return; + } + // SAFETY: the caller's contract. + let owner = unsafe { &(*future).owner }; + // A future that is not `Send` drops on its own thread. + if let Some(owner) = owner.as_ref().filter(|owner| !owner.is_here()) { + // SAFETY: the future is the owner's. + unsafe { Dropped::new(Freed(future), owner.clone(), "a future") }.send(); + return; } + // SAFETY: the caller's contract. + unsafe { ((*future).drop)(future) }; + } +} + +/// A future freed on another thread than its own, which drops it. +struct Freed(*mut Future); + +impl Drop for Freed { + fn drop(&mut self) { + // SAFETY: `Future::free`'s contract, on the future's own thread. + unsafe { ((*self.0).drop)(self.0) }; } } diff --git a/ffi/runtime/rt/thread.rs b/ffi/runtime/rt/thread.rs new file mode 100644 index 000000000..da3c0e602 --- /dev/null +++ b/ffi/runtime/rt/thread.rs @@ -0,0 +1,368 @@ +//! The threads values that are not `Send` stay on. Such a value, an object or a future, +//! records the thread that made it, which alone borrows, polls, moves, or drops it: a use +//! on another thread fails its check, and a free there queues the drop to its thread, +//! which runs it at its next check, when it exits, or at once if it waits for work. One +//! freed once its thread exited leaks, saying so, as nothing may drop it. +//! +//! A home is a thread Rust owns, for a binding with no thread of its own that stays put, +//! as a goroutine or a Dart isolate moves between threads: a call it makes on a value a +//! home owns runs there while the caller waits, so the value never leaves its thread. + +use std::cell::OnceCell; +use std::sync::OnceLock; +use std::mem::{ManuallyDrop, MaybeUninit}; +use std::panic::AssertUnwindSafe; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, MutexGuard}; + +/// A thread values that are not `Send` stay on. +pub struct Thread { + /// Set while `jobs` holds one, so a check on the thread finds it with a load. + pending: AtomicBool, + jobs: Mutex, + /// A home's thread, which parks for a job, unparked as one is queued. + parked: OnceLock, + /// Whether it is a home, which runs a call from any thread. + home: bool, +} + +/// What other threads left a thread to run. +struct Jobs { + queue: Vec, + /// Whether the thread exited, so that nothing queued runs. + gone: bool, +} + +enum Job { + Drop(Dropped), + /// A call another thread waits on, on a home. + Run(Run), +} + +// SAFETY: a job runs on the thread it is queued for alone, which its maker ensures. +unsafe impl Send for Job {} + +/// A value that is not `Send`, freed elsewhere, to drop on its own thread: erased, and +/// with the record of its thread, which it keeps alive until it drops. +pub struct Dropped { + data: *mut (), + drop: unsafe fn(*mut ()), + what: &'static str, + owner: Arc, +} + +impl Dropped { + /// `value`, `what` a leak names, to drop on `owner`'s thread. + /// + /// # Safety + /// `value` is `owner`'s: it may move to another thread only to drop there. + pub unsafe fn new(value: T, owner: Arc, what: &'static str) -> Self { + unsafe fn drop_box(data: *mut ()) { + // SAFETY: `new` boxed it, and it drops once. + drop(unsafe { Box::from_raw(data.cast::()) }); + } + Self { + data: Box::into_raw(Box::new(value)).cast(), + drop: drop_box::, + what, + owner, + } + } + + /// Queue it to its thread, or leak it, saying so, if that exited. + pub fn send(self) { + // Kept until the push returns, so its thread may run the drop first: then this is + // the last record, which lets the home go idle. + let owner = self.owner.clone(); + if let Err(Job::Drop(dropped)) = owner.push(Job::Drop(self)) { + eprintln!( + "rust2-ffi: {} leaks: it is not Send, and its thread exited before it was freed", + dropped.what + ); + } + Thread::let_go(owner); + } +} + +/// A call lent to a home, which another thread waits on. +struct Run { + call: unsafe fn(*mut ()), + data: *mut (), + done: *const Done, +} + +/// What a thread waiting on a call it lent a home waits for. +struct Done { + ran: AtomicBool, + waiter: std::thread::Thread, +} + +impl Job { + fn run(self) { + match self { + // SAFETY: queued for this thread, which owns it. + Job::Drop(dropped) => unsafe { (dropped.drop)(dropped.data) }, + Job::Run(run) => { + // A panic would leave the waiting thread waiting forever, so it aborts, as + // one through the C ABI does. + // SAFETY: the waiting thread keeps the call alive until `ran`. + if std::panic::catch_unwind(AssertUnwindSafe(|| unsafe { (run.call)(run.data) })).is_err() { + crate::rt::fail("a home", "a call panicked"); + } + // SAFETY: as above; once `ran`, the waiter may return, so it is read first. + let done = unsafe { &*run.done }; + let waiter = done.waiter.clone(); + done.ran.store(true, Ordering::Release); + waiter.unpark(); + } + } + } +} + +/// The record of the thread it is a field of, until the thread exits, then empty. +struct Here(OnceCell>); + +impl Drop for Here { + fn drop(&mut self) { + if let Some(thread) = self.0.take() { + thread.exit(); + } + } +} + +thread_local!(static HERE: Here = const { Here(OnceCell::new()) }); + +/// The homes no value lives on, idle until a call takes one. +static IDLE: Mutex>> = Mutex::new(Vec::new()); + +impl Thread { + fn new(home: bool, gone: bool) -> Self { + Self { + pending: AtomicBool::new(false), + jobs: Mutex::new(Jobs { + queue: Vec::new(), + gone, + }), + parked: OnceLock::new(), + home, + } + } + + /// This thread's record, which a value it makes keeps. A thread past its thread locals' + /// teardown has one that is gone, so a value it makes is used nowhere and leaks. + pub fn here() -> Arc { + HERE.try_with(|here| here.0.get_or_init(|| Arc::new(Self::new(false, false))).clone()) + .unwrap_or_else(|_| Arc::new(Self::new(false, true))) + } + + /// Whether this is its thread. + pub fn is_here(&self) -> bool { + HERE.try_with(|here| here.0.get().is_some_and(|thread| std::ptr::eq(&**thread, self))) + .unwrap_or(false) + } + + /// Whether this is its thread, so a value of it may be used, after dropping what other + /// threads freed of it meanwhile. + #[inline] + pub fn own(&self) -> Result<(), crate::rt::Status> { + if !self.is_here() { + return Err(crate::rt::Status::NotSend); + } + if self.pending.load(Ordering::Acquire) { + self.drain(); + } + Ok(()) + } + + /// Whether it is a home on another thread, which a call on its values runs on. + pub fn away(&self) -> bool { + self.home && !self.is_here() + } + + fn lock(&self) -> MutexGuard<'_, Jobs> { + self.jobs.lock().unwrap_or_else(|e| e.into_inner()) + } + + /// Queue `job` for this thread, telling it, unless it exited. + fn push(&self, job: Job) -> Result<(), Job> { + let mut jobs = self.lock(); + if jobs.gone { + return Err(job); + } + jobs.queue.push(job); + self.pending.store(true, Ordering::Release); + drop(jobs); + if let Some(thread) = self.parked.get() { + thread.unpark(); + } + Ok(()) + } + + /// Run what other threads queued for this one, on it. + #[cold] + fn drain(&self) { + let queue = { + let mut jobs = self.lock(); + self.pending.store(false, Ordering::Relaxed); + std::mem::take(&mut jobs.queue) + }; + queue.into_iter().for_each(Job::run); + } + + /// The thread exited: drop what was queued, and what those drops queue in turn, as a + /// future's drop does its borrowed value's, then leak what is freed later. + fn exit(&self) { + loop { + let queue = { + let mut jobs = self.lock(); + if jobs.queue.is_empty() { + jobs.gone = true; + return; + } + std::mem::take(&mut jobs.queue) + }; + queue.into_iter().for_each(Job::run); + } + } + + /// A home no value lives on: an idle one, or a new thread. [`Self::release`] gives it + /// back once the call that took it returns. + pub fn home() -> Arc { + if let Some(home) = IDLE.lock().unwrap_or_else(|e| e.into_inner()).pop() { + return home; + } + let home = Arc::new(Self::new(true, false)); + let serving = home.clone(); + let spawned = std::thread::Builder::new() + .name("rust2-ffi home".to_string()) + .spawn(move || serving.serve()); + if spawned.is_err() { + crate::rt::fail("a home", "its thread could not be spawned"); + } + home + } + + /// Give back a home [`Self::home`] took: idle at once if no value lives on it, else + /// once the last that does is dropped. + pub fn release(home: Arc) { + // The home's own thread holds the other count. A greater one may be another + // thread's for a moment, so the home sees for itself once this lets go. + match Arc::strong_count(&home) { + 2 => IDLE.lock().unwrap_or_else(|e| e.into_inner()).push(home), + _ => Self::let_go(home), + } + } + + /// Drop `this`, a record of a thread, waking it if it is a home, which then sees + /// whether any value still lives on it: no count another thread holds for a moment + /// keeps it from going idle. + pub fn let_go(this: Arc) { + let parked = this.home.then(|| this.parked.get().cloned()).flatten(); + drop(this); + if let Some(thread) = parked { + thread.unpark(); + } + } + + /// A home's thread: run each job queued for it, forever. Idle with no value living on + /// it, which a value's record would count. + fn serve(self: Arc) { + let me: *const Self = &*self; + let _ = self.parked.set(std::thread::current()); + HERE.with(|here| { + let _ = here.0.set(self); + }); + // SAFETY: this thread's record holds it for as long as the thread runs, which is forever. + let me = unsafe { &*me }; + loop { + if me.pending.load(Ordering::Acquire) { + me.drain(); + } + // Each wake, as by a record let go, checks whether the last record is its own. + // None other appears while it is: no value nor caller holds one, nor `IDLE`. + HERE.with(|here| { + let record = here.0.get().expect("a home's record is set"); + if Arc::strong_count(record) == 1 { + IDLE.lock().unwrap_or_else(|e| e.into_inner()).push(record.clone()); + } + }); + if !me.pending.load(Ordering::Acquire) { + std::thread::park(); + } + } + } + + /// This thread's record, if it is a home. + fn serving() -> Option> { + HERE.try_with(|here| here.0.get().filter(|thread| thread.home).cloned()).ok().flatten() + } + + /// Wait until `done`, running each job queued for `home`, this thread, meanwhile: + /// whatever makes `done` true then unparks it. + fn wait_serving(home: &Thread, done: impl Fn() -> bool) { + while !done() { + match home.pending.load(Ordering::Acquire) { + true => home.drain(), + false => std::thread::park(), + } + } + } + + /// Wait until `done`, running each call queued meanwhile if this thread is a home, as one + /// a call it waits on makes may be: false at once if it is none, to wait as it would. + /// Whatever makes `done` true then unparks this thread. + #[cfg(feature = "dart")] + pub fn serve_until(done: impl Fn() -> bool) -> bool { + let Some(home) = Self::serving() else { + return false; + }; + Self::wait_serving(&home, done); + true + } + + /// Run `f` on this home's thread, which this one waits for: at once if this is it. + /// + /// # Safety + /// It is a home, and what `f` captures may be used on its thread while this waits. + pub unsafe fn run(&self, f: impl FnOnce() -> R) -> R { + if self.is_here() { + return f(); + } + let mut out = MaybeUninit::uninit(); + let mut call = ManuallyDrop::new(|| { + out.write(f()); + }); + unsafe fn invoke(data: *mut ()) { + // SAFETY: `run`'s caller waits until it ran, once. + unsafe { ManuallyDrop::take(&mut *data.cast::>())() } + } + fn invoker(_: &ManuallyDrop) -> unsafe fn(*mut ()) { + invoke:: + } + let done = Done { + ran: AtomicBool::new(false), + waiter: std::thread::current(), + }; + let run = Run { + call: invoker(&call), + data: std::ptr::from_mut(&mut call).cast(), + done: &done, + }; + if self.push(Job::Run(run)).is_err() { + crate::rt::fail("a home", "its thread exited"); + } + let ran = || done.ran.load(Ordering::Acquire); + // A home waiting on another runs each call queued for it meanwhile, as one the call + // it waits on makes back may be, so two homes calling each other never both wait. + match Self::serving() { + Some(home) => Self::wait_serving(&home, ran), + None => { + while !ran() { + std::thread::park(); + } + } + } + // SAFETY: it ran, writing `out`. + unsafe { out.assume_init() } + } +} diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index b46673f65..caccb2f5f 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -108,6 +108,7 @@ pub enum Ty { } /// A C function: its name, parameters, and return type. +#[derive(Clone)] pub struct Sig { pub name: String, pub params: Vec<(String, Ty)>, @@ -115,6 +116,15 @@ pub struct Sig { } impl Sig { + /// Its twin that runs it on a home: `_homed`, of the same signature. + pub fn homed(&self) -> Sig { + Sig { + name: format!("{}_homed", self.name), + params: self.params.clone(), + ret: self.ret.clone(), + } + } + /// The `status` parameter it takes last, if a call can fail a check of a handle. pub fn status(&self) -> Option<&str> { self.params @@ -151,6 +161,35 @@ pub struct Export { pub named: String, /// For a future: its handle type and the functions over it. pub future: Option, + /// Whether it makes a value that is not `Send` from none: its call, or for a future its + /// start, has a twin of the same signature, `_homed`, that runs it on a home, a + /// thread Rust owns, which the value then stays on. A binding with no thread that stays + /// put calls that one. + pub homed: bool, +} + +impl Export { + /// The twin of its call that runs it on a home, if it makes a value that is not `Send`. + pub fn homed_call(&self) -> Option { + self.homed.then(|| self.call.homed()) + } + + /// It as a binding with no thread that stays put calls it: by its twins that run on a + /// home, if it has them. + pub fn on_home(mut self) -> Self { + if self.homed { + self.call = self.call.homed(); + if let Some(future) = &mut self.future { + future.start = future.start.homed(); + } + } + self + } + + /// The twin of its future's start that runs it on a home, as [`Self::homed_call`]. + pub fn homed_start(&self) -> Option { + (self.future.as_ref()).and_then(|future| self.homed.then(|| future.start.homed())) + } } /// How the glue calls a value's `&mut self` method for a language whose values are @@ -215,13 +254,17 @@ pub struct Future { /// Each `_status` a call can fail with, numbered from 1, and what it means, as /// the runtime's `Status` names them. -pub const STATUSES: [(&str, &str); 6] = [ +pub const STATUSES: [(&str, &str); 7] = [ ("moved", "the handle's value was moved out"), ("exclusive", "another call or future borrows the value exclusively"), ("borrowed", "another call or future borrows the value"), ("thread", "a value that is not Sync is borrowed on another thread"), ("lent", "a handle a call lent is borrowed exclusively or moved out"), ("outlived", "a handle lent from another outlived it"), + ( + "not_send", + "a value that is not Send is used on another thread than the one that made it", + ), ]; /// Whether a call can fail a check of each object's handle, by Rust's rules alone: its @@ -1011,6 +1054,10 @@ impl<'a> Abi<'a> { params: start_params, ret: Ty::Poll, }; + // A poll of a future that is not `Send` fails on another thread than its own. + if !function.send { + poll_params.push(("status".to_string(), Ty::Out(Box::new(Ty::Status)))); + } let poll = Sig { name: format!("{name}_poll"), params: poll_params, @@ -1034,6 +1081,7 @@ impl<'a> Abi<'a> { free, }), named, + homed: self.homed(function, owner), }; } let ret = match (&function.err, &function.ok) { @@ -1055,9 +1103,48 @@ impl<'a> Abi<'a> { call: Sig { name, params, ret }, future: None, named, + homed: self.homed(function, owner), } } + /// Whether a call of `function`, a method of `owner` if it has one, makes a value that + /// is not `Send`, an object or a future, without taking one: see [`Export::homed`]. + pub fn homed(&self, function: &Function, owner: Option<&Ty>) -> bool { + let unsent = |ty: &Type| { + let mut handles = Vec::new(); + self.api.handles(ty, &mut handles, &mut Vec::new()); + handles.iter().any(|&i| !self.api.objects[i].send) + }; + let makes = + (function.future && !function.send) || unsent(&function.ok) || function.err.as_ref().is_some_and(unsent); + let receiver = match (function.receiver, owner) { + (Some(_), Some(Ty::Handle { name, .. })) => { + (0..self.api.objects.len()).any(|i| &self.object(i) == name && !self.api.objects[i].send) + } + (Some(_), Some(owner)) => { + let records = (0..self.api.records.len()).map(Type::Record); + let enums = (0..self.api.enums.len()).map(Type::Enum); + records + .chain(enums) + .find(|ty| &self.ty(ty) == owner) + .is_some_and(|ty| unsent(&ty)) + } + _ => false, + }; + // A handle that may be absent leaves the call to a home when it is: the twin runs it + // where the first present one stays, else on a home. + let optional = |ty: &Type| match ty { + Type::Option(inner) => match &**inner { + Type::Object(i) => !self.api.objects[*i].send, + Type::Ref(_, inner) => matches!(&**inner, Type::Object(i) if !self.api.objects[*i].send), + _ => false, + }, + _ => false, + }; + // A JNI env is its thread's, so a call taking one stays there. + makes && !receiver && !function.jni() && function.params.iter().all(|p| optional(&p.ty) || !unsent(&p.ty)) + } + /// Whether a call of `function`, a method of `owner` if it has one, can fail a check of /// a handle it borrows or moves, so it takes a `status` to write why. pub fn status(&self, function: &Function, owner: Option<&Ty>) -> bool { @@ -1355,6 +1442,15 @@ impl<'a> Abi<'a> { ); } self.claim_traits(&item, &name, &what, object.span, object.traits, false); + // `Default` of a value that is not `Send` makes one from none. + if object.traits.default && !object.send { + self.claim( + format!("{name}_default_homed"), + what.clone(), + object.span, + Self::made(&item, &name), + ); + } self.claim( name.clone(), what, @@ -1429,6 +1525,9 @@ impl<'a> Abi<'a> { self.claim(made.clone(), what.clone(), function.span, Self::made(&item, &name)); } } + for homed in export.homed_call().into_iter().chain(export.homed_start()) { + self.claim(homed.name, what.clone(), function.span, Self::made(&item, &name)); + } types.extend(function.params.iter().map(|p| &p.ty)); // A tuple returned whole is written element by element, with no struct of its own. match &function.ok { diff --git a/ffi/src/c.rs b/ffi/src/c.rs index 9b99976eb..cf19adc7a 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -283,6 +283,9 @@ impl Header<'_> { } if t.default { self.line(format!("{name} *{name}_default(void);")); + if !object.send { + self.line(format!("{name} *{name}_default_homed(void);")); + } } let owner = Ty::Handle { name: name.clone(), @@ -535,14 +538,25 @@ impl Header<'_> { if let Some(future) = &export.future { let handle = &future.handle; self.line(format!("typedef struct {handle} {handle};")); - for sig in [&export.call, &future.start, &future.poll, &future.free] { - let prototype = self.prototype(sig); + let homed = export.homed_call().into_iter().chain(export.homed_start()); + for sig in [ + export.call.clone(), + future.start.clone(), + future.poll.clone(), + future.free.clone(), + ] + .into_iter() + .chain(homed) + { + let prototype = self.prototype(&sig); self.line(prototype); } return; } - let call = self.prototype(&export.call); - self.line(call); + for sig in std::iter::once(export.call.clone()).chain(export.homed_call()) { + let prototype = self.prototype(&sig); + self.line(prototype); + } } } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 652e5254c..4925796eb 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1710,7 +1710,9 @@ impl<'tcx> Collector<'tcx> { Decl::Object(i) => { *i = api.objects.len(); let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); - let sync = implements(tcx, ty, def_id, sym::Sync); + // Only the thread a value that is not `Send` stays on borrows it. + let send = implements(tcx, ty, def_id, sym::Send); + let sync = send && implements(tcx, ty, def_id, sym::Sync); let copy = implements(tcx, ty, def_id, sym::Copy); let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; api.objects.push(Object { @@ -1722,6 +1724,7 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs, sync, + send, copy, shared: false, lifetimes, @@ -1864,6 +1867,7 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs, sync: true, + send: true, copy: false, // A held value is moved out of its handle, as an object of the crate's is, and // its clone is no part of the API. @@ -1915,6 +1919,7 @@ impl<'tcx> Collector<'tcx> { eager: false, holds: false, task: None, + send: true, collect: false, lends: false, access: Access::Parse, @@ -1966,6 +1971,7 @@ impl<'tcx> Collector<'tcx> { eager: false, holds: false, task: None, + send: true, collect: false, lends: false, access: Access::Text, @@ -1994,6 +2000,7 @@ impl<'tcx> Collector<'tcx> { on: On::ALL, docs: docs.to_string(), sync: true, + send: true, copy: implements(tcx, self_ty, def_id, sym::Copy), shared: traits.clone && self.clone_shares(self_ty, def_id), lifetimes: 0, @@ -2094,10 +2101,10 @@ impl<'tcx> Collector<'tcx> { } } - /// An object, if the type can move across threads, or if it is also `Copy`, a value C - /// holds as its bytes, unless it may have padding, whose bytes the glue's `rustc` could - /// place anywhere (see [`full`]). Shared borrows of an object that is not `Sync` are - /// exclusive. + /// An object, or if the type is `Send` and `Copy`, a value C holds as its bytes, unless + /// it may have padding, whose bytes the glue's `rustc` could place anywhere (see + /// [`full`]). Shared borrows of an object that is not `Sync` are on one thread at a + /// time, and one that is not `Send` stays on the thread that made it. fn object(&self, site: &Site<'tcx>) -> Decl { let (tcx, ty, def_id) = (self.tcx, self.site_ty(site), site.key.def_id); let copy = implements(tcx, ty, def_id, sym::Copy) && full(tcx, ty); @@ -2108,10 +2115,10 @@ impl<'tcx> Collector<'tcx> { }; return Decl::Refused(why(construct, "")); } - match (implements(tcx, ty, def_id, sym::Send), copy) { - (true, true) => Decl::Value(0), - (true, false) => Decl::Object(0), - (false, _) => Decl::Refused(why("object not Send", "")), + // A value that is not `Send` is an object, whose handle keeps it on its thread. + match implements(tcx, ty, def_id, sym::Send) && copy { + true => Decl::Value(0), + false => Decl::Object(0), } } @@ -2616,6 +2623,7 @@ impl<'tcx> Collector<'tcx> { eager: false, holds: false, task: None, + send: true, collect: false, lends: false, access: Access::Field { shared, set }, @@ -3060,6 +3068,8 @@ impl<'tcx> Collector<'tcx> { _ => None, }; let mut tasks = Vec::new(); + // The object a poll's task is made into, which its future keeps between polls. + let mut made = None; let mut inputs = sig.inputs().iter().copied().map(normalize).enumerate(); let receiver = match has_self { false => None, @@ -3075,6 +3085,7 @@ impl<'tcx> Collector<'tcx> { && polled.is_some() && let Some(kind) = self.task(ty, true) => { + made = Some(*inner); tasks.push(Task { at: 0, kind }); None } @@ -3091,6 +3102,15 @@ impl<'tcx> Collector<'tcx> { }; // Where Rust's parameters after `self` start: at `self`, when the task takes its place. let first = usize::from(receiver.is_some()); + // A poll's future holds its receiver, each parameter, and the object its task is + // made into, so it is not `Send` where one of them is not. + let mut holds_send = match (receiver, self_ty) { + (Some(_), Some(self_ty)) => self.is_send(normalize(self_ty), env), + _ => true, + }; + if let Some(made) = made { + holds_send &= self.is_send(made, env); + } let again = |ty: Ty<'tcx>| ty.is_ref() || tcx.type_is_copy_modulo_regions(env, ty); if polled.is_some() && receiver == Some(Receiver::Owned) @@ -3114,6 +3134,9 @@ impl<'tcx> Collector<'tcx> { .flatten() .map_or_else(|| format!("arg{i}"), |ident| ident.to_string()); if !callback && let Some(kind) = self.task(ty, polled.is_some()) { + if matches!(kind, TaskKind::Made { .. }) { + holds_send &= self.is_send(ty.peel_refs(), env); + } // C passes a waker for a context the future does not supply. if polled.is_none() { params.push(Param { @@ -3134,6 +3157,11 @@ impl<'tcx> Collector<'tcx> { }); continue; } + // What a binding implements, through a type parameter or a trait object, is `Send`. + let held = ty.peel_refs(); + if polled.is_some() && !matches!(held.kind(), ty::Param(_) | ty::Dynamic(..)) { + holds_send &= self.is_send(held, env); + } if polled.is_some() && !again(ty) { errs.push(why("poll's parameter taken by value that is not Copy", ty.to_string())); continue; @@ -3193,6 +3221,7 @@ impl<'tcx> Collector<'tcx> { errs.push(why("several task parameters", "")); } let mut future = false; + let mut send = true; let mut eager = !tcx.asyncness(def_id).is_async(); if let Some(inner) = polled { if task.is_none() { @@ -3204,6 +3233,7 @@ impl<'tcx> Collector<'tcx> { output = normalize(inner); future = true; eager = false; + send = holds_send; } else if let Some(inner) = self.future_output(output, env) { // The context C's waker makes lives only for the call. if task.is_some() { @@ -3222,9 +3252,8 @@ impl<'tcx> Collector<'tcx> { ty::ParamEnv::new(tcx, env.param_env.caller_bounds().all_clauses(tcx).iter().chain(marked)), env.typing_mode(), ); - if !self.is_send(output, kept) { - errs.push(why("future not Send", "")); - } + // A future that is not `Send` stays on the thread that made it. + send = self.is_send(output, kept); // An eager future holds what it borrows only until it completes, so its output // may borrow none of it. if eager && borrows && self.output_borrows(unbound.output(), inner, &statics) { @@ -3236,6 +3265,11 @@ impl<'tcx> Collector<'tcx> { output = normalize(inner); future = true; } + // A future's last poll makes its output, so one whose output is not `Send` stays on + // the thread that made it, which a value it makes then stays on, as a home's. + if future && !self.is_send(output, env) { + send = false; + } // A future, polled or async, lifts each value into what it owns, so has nothing to // write back. if future && params.iter().any(|p: &Param| p.ty.written()) { @@ -3401,6 +3435,7 @@ impl<'tcx> Collector<'tcx> { eager: future && eager, holds: future && eager && borrows, task, + send, collect, lends, access: Access::Call, @@ -4806,10 +4841,16 @@ impl<'tcx> Collector<'tcx> { // A trait object a handle holds holds its own lifetime, as `dyn Trait + 'a` does. ty::Dynamic(_, region) if self.held_trait(ty).is_some() => held.push(*region), // An object, an opaque value, or another crate's value an opaque handle holds, holds - // what its lifetime parameters name. + // what its lifetime parameters name. Looked up with regions erased, since they may + // be the signature's bound ones, which a key never names. ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_))) - || self.opaque_value(ty).is_some() => + if matches!( + self.decl( + adt.did(), + ty::fold_regions(self.tcx, *args, |_, _| self.tcx.lifetimes.re_erased) + ), + Some(Decl::Object(_) | Decl::Value(_)) + ) || self.opaque_value(ty).is_some() => { held.extend(args.regions()) } @@ -5130,6 +5171,7 @@ impl<'tcx> Collector<'tcx> { eager: false, holds: false, task: None, + send: true, collect: false, lends: false, access: Access::Const, diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index ef669edcb..2bb00b114 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -1842,7 +1842,11 @@ impl<'a> Header<'a> { }; let name = self.name(handle); let output = self.output(function); - let outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); + let outs: Vec<(String, Ty)> = poll.params[2..] + .iter() + .filter(|(_, ty)| !abi::is_status(ty)) + .cloned() + .collect(); let (declared, ok, err) = self.outs(function, &outs, &output); let ok = match (&function.err, &function.ok) { (None, Type::Unit) => "out.emplace();".to_string(), @@ -1850,6 +1854,11 @@ impl<'a> Header<'a> { }; let mut args = vec!["f".to_string(), "w".to_string()]; args.extend(outs.iter().map(|(name, _)| format!("&c_{name}_"))); + // A poll on another thread than a future's that is not `Send` aborts, as C++ throws + // nothing. + if poll.status().is_some() { + args.push("nullptr".to_string()); + } let upper = abi.prefix.to_uppercase(); let mut cases = vec![format!("\t\tcase {upper}_POLL_READY:\n\t\t\t{ok}\n\t\t\tbreak;")]; if let Some(err) = err { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index d05090298..3f53999d6 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -1582,7 +1582,12 @@ impl<'a> Gen<'a> { }, false => c_ty.clone(), }; - let f = self.served_fn(&format!("{c}_default"), vec![], ret); + // A value that is not `Send` is made on a home, which its calls then run on. + let homed = match ty { + Type::Object(i) if !self.abi.api.objects[*i].send => "_homed", + _ => "", + }; + let f = self.served_fn(&format!("{c}_default{homed}"), vec![], ret); let value = match object { true => format!("{name}._({f}(_n.home))"), false => self.lift(ty, &format!("{f}(_n.home)")), @@ -2118,7 +2123,9 @@ impl<'a> Gen<'a> { self.handed.insert(export.call.name.clone(), handed); (Some(back), export) } - None => (None, abi.export(function, owner)), + // Called by a twin that runs it on a home, if it makes a value that is not `Send` + // from none, as an isolate moves between threads. + None => (None, abi.export(function, owner).on_home()), }; let function = handed.as_ref().unwrap_or(function); let c_name = export.call.name.clone(); @@ -2285,12 +2292,30 @@ impl<'a> Gen<'a> { let mut poll_args = vec!["_n.home".to_string(), "f".to_string(), "w".to_string()]; let poll_fn = self.served(poll); let free_fn = self.served(free); - let poll_outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); + let poll_outs: Vec<(String, Ty)> = poll.params[2..] + .iter() + .filter(|(_, ty)| !abi::is_status(ty)) + .cloned() + .collect(); let mut stmts: Vec = Vec::new(); for (n, ty) in &poll_outs { stmts.push(self.out_decl(n, ty)); poll_args.push(format!("o_{n}")); } + // A future that is not `Send` lives on a home, which polls it, so its check fails + // only for one a callback made elsewhere. + let polled = match poll.status() { + Some(_) => { + stmts.push("final s_ = _n.status;".to_string()); + poll_args.push("s_".to_string()); + format!( + "final p_ = {poll_fn}({});\n\t\t_status(s_.value, '{}');\n\t\tswitch (p_) {{", + poll_args.join(", "), + poll.name + ) + } + None => format!("switch ({poll_fn}({})) {{", poll_args.join(", ")), + }; let ready = match ok { Type::Unit => "null".to_string(), _ => self.lift_outs(&function.ok, &ok), @@ -2316,9 +2341,8 @@ impl<'a> Gen<'a> { self.helpers.insert( poll_name.clone(), format!( - "bool {poll_name}(ffi.Pointer f, _Waker w, _Call<{ok_dt}> c) {{\n\tfinal a_ = _n.stack;\n\tfinal m_ = a_.mark();\n\ttry {{\n\t\t{}\n\t\tswitch ({poll_fn}({})) {{\n{}\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}}\n\t}} finally {{\n\t\ta_.reset(m_);\n\t}}\n}}\n\n", + "bool {poll_name}(ffi.Pointer f, _Waker w, _Call<{ok_dt}> c) {{\n\tfinal a_ = _n.stack;\n\tfinal m_ = a_.mark();\n\ttry {{\n\t\t{}\n\t\t{polled}\n{}\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}}\n\t}} finally {{\n\t\ta_.reset(m_);\n\t}}\n}}\n\n", stmts.join("\n\t\t"), - poll_args.join(", "), cases.iter().map(|c| c.replace("\n\t\t", "\n\t\t\t").replacen("\t\tcase", "\t\t\tcase", 1)).collect::>().join("\n") ), ); diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 8ede644a3..87a18930a 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -22,6 +22,7 @@ use crate::ir::{ const RUNTIME: &str = include_str!("../runtime/rt.rs"); const BORROW: &str = include_str!("../runtime/rt/borrow.rs"); +const THREAD: &str = include_str!("../runtime/rt/thread.rs"); /// The crate's build script, which refuses to build for a target but `targets`, on which an /// item a target compiles would be missing, or one it leaves out left over, and tells the @@ -400,6 +401,7 @@ impl Crate<'_> { output.file(&out.join("build.rs"), &build(&self.abi.api.targets)); output.file(&out.join("src/rt.rs"), RUNTIME); output.file(&out.join("src/rt/borrow.rs"), BORROW); + output.file(&out.join("src/rt/thread.rs"), THREAD); output.file(&out.join("include").join(format!("{}.h", self.abi.prefix)), &header); for (path, header) in &cpp.files { output.file(&out.join("include").join(path), header); @@ -772,9 +774,21 @@ impl Gen<'_> { } } - /// An object's Rust type: its lifetimes `'static`, as the handle holds it. + /// What an object's handle holds: its Rust type, with its lifetimes `'static`, wrapped + /// in `rt::Unsent` if it is not `Send`, which keeps it on its thread. fn object_path(&self, i: usize) -> String { - self.rust(&Type::Object(i)) + match self.abi.api.objects[i].send { + true => self.rust(&Type::Object(i)), + false => format!("rt::Unsent<{}>", self.rust(&Type::Object(i))), + } + } + + /// A new handle owning `e`, a value of object `i`'s type. + fn new_object(&self, i: usize, e: &str) -> String { + match self.abi.api.objects[i].send { + true => format!("rt::Object::new({e})"), + false => format!("rt::Object::unsent({e})"), + } } /// The Rust spelling of a type a callback's method takes or returns, or a type @@ -973,6 +987,7 @@ impl Gen<'_> { match object.opaque { Some(Held::Arc) => format!("{value}.arc(w)"), Some(Held::Value) => format!("{value}.take(w)"), + None if !object.send => format!("rt::Unsent::into_inner({value})"), None => value, } } @@ -1008,7 +1023,9 @@ impl Gen<'_> { // Each object the closure borrows, and how it is lent. let lent = |ty: &Type| match ty { Type::Ref(m, inner) => match (&**inner, m) { + (&Type::Object(i), Mutability::Mut) if !self.abi.api.objects[i].send => Some((i, "lend_mut_unsent")), (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut")), + (&Type::Object(i), Mutability::Not) if !self.abi.api.objects[i].send => Some((i, "lend_unsent")), (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend")), (&Type::Object(i), Mutability::Not) => Some((i, "lend_local")), _ => None, @@ -1145,7 +1162,11 @@ impl Gen<'_> { Type::Ref(_, inner) => match &**inner { Type::Str | Type::Slice | Type::Path => self.lower(inner, e), Type::Object(i) if self.lends => { - format!("rt::Object::<{}>::lent({e}, lender)", self.object_path(*i)) + let lent = match self.abi.api.objects[*i].send { + true => "lent", + false => "lent_unsent", + }; + format!("rt::Object::<{}>::{lent}({e}, lender)", self.object_path(*i)) } // A handle in the clone is a new handle, and a slice's clone a `Vec`. inner => self.lower(inner, &format!("::std::borrow::ToOwned::to_owned({e})")), @@ -1156,7 +1177,7 @@ impl Gen<'_> { Held::Arc => format!("rt::Object::new(rt::Opaque::new({e}))"), Held::Value => format!("rt::Object::new(rt::Owned::new({e}))"), }, - Type::Object(_) => format!("rt::Object::new({e})"), + Type::Object(i) => self.new_object(*i, e), Type::Callback(..) | Type::Closure(..) => unreachable!("a callback is never returned"), Type::Waker => format!("rt::Waker::lower({e})"), Type::Future | Type::Reader | Type::Jni(_) => unreachable!("{ty:?} is never returned"), @@ -1329,9 +1350,22 @@ impl Gen<'_> { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *const ::std::ffi::c_void) {{\n\tunsafe {{ rt::Object::<{path}>::free(this) }}\n}}\n" )); + // A call on a value that is not `Send` runs on its home, if it has one elsewhere. + let away = |name: &str, params: &[&str]| match object.send { + true => String::new(), + false => { + let params: Vec = params.iter().map(|p| p.to_string()).collect(); + let this = params[0].split_once(": ").map_or("this", |(name, _)| name); + away(name, ¶ms, &[(path.clone(), this.to_string())]) + } + }; if self.abi.checked[i] { + let away = away( + &format!("{name}_drop"), + &["this: *const ::std::ffi::c_void", "status: *mut i32"], + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_drop(this: *const ::std::ffi::c_void, status: *mut i32) {{\n\tlet w = \"{name}_drop\";\n\tif let Err(s) = unsafe {{ rt::Object::<{path}>::close(this, w) }} {{\n\t\tunsafe {{ rt::failed::<()>(status, s, w) }}\n\t}}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_drop(this: *const ::std::ffi::c_void, status: *mut i32) {{\n\tlet w = \"{name}_drop\";\n\t{away}if let Err(s) = unsafe {{ rt::Object::<{path}>::close(this, w) }} {{\n\t\tunsafe {{ rt::failed::<()>(status, s, w) }}\n\t}}\n}}\n" )); } if object.shared { @@ -1349,7 +1383,7 @@ impl Gen<'_> { }; let borrowing: Vec = ["this", "a", "b"].into_iter().map(borrowing).collect(); self.checking.set(false); - self.traits(&name, &path, object.traits, status, &borrowing); + self.traits(i, object.traits, status, &borrowing); let owner = Ty::Handle { name: name.clone(), constant: false, @@ -1363,64 +1397,99 @@ impl Gen<'_> { } } - /// The functions an object's std traits export, borrowing its handles `this`, `a`, + /// The functions object `i`'s std traits export, borrowing its handles `this`, `a`, /// and `b` as `borrow` does, which takes `status` if a borrow can fail. - fn traits(&mut self, name: &str, path: &str, traits: Traits, status: Option<&str>, borrow: &[String]) { + fn traits(&mut self, i: usize, traits: Traits, status: Option<&str>, borrow: &[String]) { + let name = self.abi.object(i); + let holder = self.object_path(i); + let path = self.rust(&Type::Object(i)); + let send = self.abi.api.objects[i].send; let handle = "*const ::std::ffi::c_void"; let [this, a, b] = borrow else { unreachable!("three handles") }; + // Through the borrow, then its `rt::Unsent` if it is not `Send`. + let deref = match send { + true => "&*", + false => "&**", + }; let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); + // Each runs on the home of its first handle's value, if that is elsewhere. + let away = |function: &str, handles: &[&str]| { + if send { + return String::new(); + } + let mut params: Vec = handles.iter().map(|h| format!("{h}: {handle}")).collect(); + params.extend(status.map(|s| format!("{s}: *mut i32"))); + let held: Vec<(String, String)> = handles.iter().map(|h| (holder.clone(), h.to_string())).collect(); + away(function, ¶ms, &held) + }; if traits.clone { let body = wrap( status, handle, - &format!("let this = {this};\n\trt::Object::new(<{path} as ::std::clone::Clone>::clone(&*this))"), + &format!( + "let this = {this};\n\t{}", + self.new_object(i, &format!("<{path} as ::std::clone::Clone>::clone({deref}this)")) + ), ); + let away = away(&format!("{name}_clone"), &["this"]); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}{param}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\t{body}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}{param}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\t{away}{body}\n}}\n" )); } if traits.eq { let body = wrap( status, "bool", - &format!("let a = {a};\n\tlet b = {b};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)"), + &format!("let a = {a};\n\tlet b = {b};\n\t<{path} as ::std::cmp::PartialEq>::eq({deref}a, {deref}b)"), ); + let away = away(&format!("{name}_eq"), &["a", "b"]); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{away}{body}\n}}\n" )); } if traits.hash { - let body = wrap(status, "u64", &format!("let this = {this};\n\trt::hash(&*this)")); + let body = wrap( + status, + "u64", + &format!("let this = {this};\n\trt::hash::<{path}>({deref}this)"), + ); + let away = away(&format!("{name}_hash"), &["this"]); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: {handle}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{body}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: {handle}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{away}{body}\n}}\n" )); } if traits.display { let body = wrap( status, "rt::Buf", - &format!("let this = {this};\n\trt::Buf::string(::std::string::ToString::to_string(&*this))"), + &format!("let this = {this};\n\trt::Buf::string(::std::string::ToString::to_string({deref}this))"), ); + let away = away(&format!("{name}_to_string"), &["this"]); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{away}{body}\n}}\n" )); } if traits.debug { let body = wrap( status, "rt::Buf", - &format!("let this = {this};\n\trt::Buf::string(format!(\"{{:?}}\", &*this))"), + &format!("let this = {this};\n\trt::Buf::string(format!(\"{{:?}}\", {deref}this))"), ); + let away = away(&format!("{name}_debug"), &["this"]); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{away}{body}\n}}\n" )); } if traits.default { self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_default() -> {handle} {{\n\trt::Object::new(<{path} as ::std::default::Default>::default())\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_default() -> {handle} {{\n\t{}\n}}\n", + self.new_object(i, &format!("<{path} as ::std::default::Default>::default()")) )); + if !send { + self.export(homed(&format!("{name}_default"), &[], handle, &[])); + } } } @@ -2039,9 +2108,13 @@ impl Gen<'_> { Type::Ref(m, inner) => match (&**inner, m, future) { (Type::Object(i), _, _) => { let (binding, borrow) = self.borrow(*i, *m, c, future); - let pass = match (m, self.abi.api.objects[*i].opaque) { + let object = &self.abi.api.objects[*i]; + let pass = match (m, object.opaque) { (Mutability::Not, Some(_)) => format!("{local}.get(w)"), (Mutability::Mut, Some(_)) => format!("{local}.get_mut(w)"), + // Through the borrow, then its `rt::Unsent`. + (Mutability::Mut, None) if !object.send => format!("&mut **{local}"), + (Mutability::Not, None) if !object.send => format!("&**{local}"), (Mutability::Mut, None) => format!("&mut *{local}"), (Mutability::Not, None) => format!("&*{local}"), }; @@ -2084,9 +2157,12 @@ impl Gen<'_> { Type::Option(opt) if let Type::Ref(m, inner) = &**opt => match (&**inner, future) { (Type::Object(i), _) => { let (binding, borrow) = self.borrow(*i, *m, c, future); - let pass = match (m, self.abi.api.objects[*i].opaque) { + let object = &self.abi.api.objects[*i]; + let pass = match (m, object.opaque) { (Mutability::Not, Some(_)) => format!("{local}.as_deref().map(|v| v.get(w))"), (Mutability::Mut, Some(_)) => format!("{local}.as_deref_mut().map(|v| v.get_mut(w))"), + (Mutability::Mut, None) if !object.send => format!("{local}.as_deref_mut().map(|v| &mut **v)"), + (Mutability::Not, None) if !object.send => format!("{local}.as_deref().map(|v| &**v)"), (Mutability::Mut, None) => format!("{local}.as_deref_mut()"), (Mutability::Not, None) => format!("{local}.as_deref()"), }; @@ -2147,9 +2223,9 @@ impl Gen<'_> { (false, true) => "borrow_arc", (true, true) => "borrow_mut_arc", }; - let binding = match exclusive { - true => "let mut", - false => "let", + let binding = match m { + Mutability::Mut => "let mut", + Mutability::Not => "let", }; // A type no export can misuse is borrowed shared for a call uncounted. if !exclusive && !future && !self.abi.checked[i] { @@ -2450,7 +2526,7 @@ impl Gen<'_> { TaskKind::Made { object, make } => { made = format!( "let t_made = t_held.insert(<{}>::{make}(::std::task::Context::waker(t_cx).clone()));\n\t\t\t", - self.object_path(*object) + self.rust(&Type::Object(*object)) ); "&*t_made".to_string() } @@ -2519,6 +2595,23 @@ impl Gen<'_> { )); } let body = stmts.join("\n\t"); + // The handles it takes of values that is not `Send`, the first present of which + // decides the thread it runs on; a JNI env is its thread's, so a call taking one + // stays on that thread. + let mut unsent: Vec<(String, String)> = Vec::new(); + if !function.jni() { + if let Some(((_, Type::Object(i)), _)) = &receiver_ty + && !self.abi.api.objects[*i].send + { + unsent.push((self.object_path(*i), "this".to_string())); + } + for (n, param) in function.params.iter().enumerate() { + if let Some(i) = self.unsent_object(¶m.ty) { + unsent.push((self.object_path(i), format!("p{n}"))); + } + } + } + let away = |name: &str, params: &[String]| away(name, params, &unsent); if let Some(abi::Future { start, poll, free, .. }) = &export.future { // A function that is not `async` runs when called, and what it returns // borrows nothing, so it is called now; an `async fn` runs when polled. @@ -2567,16 +2660,25 @@ impl Gen<'_> { // The make's body, emitted again inside `_start` so its aborts name the // function C called. let make = format!( - "{body}{now}\n\trt::Future::new(async move {{ {later} }}, move |result, outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\tlet _ = (outs, err);\n\t\t{bind}{}\n\t}})", + "{body}{now}\n\trt::Future::{}(async move {{ {later} }}, move |result, outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\tlet _ = (outs, err);\n\t\t{bind}{}\n\t}})", + match function.send { + true => "new", + false => "unsent", + }, self.finish(function, &ok, ("v", "rt::READY", "rt::ERROR")) ); self.checking.set(false); let status_param = status.map(|status| format!("{status}: *mut i32")); + let make_params: Vec = params.iter().chain(&status_param).cloned().collect(); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", - params.iter().chain(&status_param).cloned().collect::>().join(", "), + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}{}\n}}\n", + make_params.join(", "), + away(&name, &make_params), wrap(status, "*mut rt::Future", &make) )); + if export.homed { + self.export(homed(&name, &make_params, "*mut rt::Future", &unsent)); + } let poll_params: Vec = poll.params[2..] .iter() .map(|(n, ty)| format!("{n}: {}", rs(ty))) @@ -2587,8 +2689,10 @@ impl Gen<'_> { Some(_) => "err.cast()", None => "::std::ptr::null_mut()", }; + // Only a poll of a future that is not `Send` can fail, on another thread. + let poll_status = poll.status().unwrap_or("::std::ptr::null_mut()"); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}(future: *mut rt::Future, waker: rt::Waker{}) -> i32 {{\n\tunsafe {{ rt::Future::poll(future, waker, &[{outs}], {err}, \"{}\") }}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}(future: *mut rt::Future, waker: rt::Waker{}) -> i32 {{\n\tunsafe {{ rt::Future::poll(future, waker, &[{outs}], {err}, {poll_status}, \"{}\") }}\n}}\n", poll.name, poll_params.iter().map(|p| format!(", {p}")).collect::(), poll.name @@ -2598,9 +2702,11 @@ impl Gen<'_> { free.name )); // The call and its first poll in one: most futures are ready at once. A failed - // check writes its status and returns before `future` is written. + // check writes its status and returns before `future` is written. It runs on the + // thread that made it, so takes no status of its poll's. + let poll_outs = poll_params.len() - usize::from(poll.status().is_some()); let start_params: Vec = (params.iter().cloned()) - .chain(poll_params) + .chain(poll_params.into_iter().take(poll_outs)) .chain(["future: *mut *mut rt::Future".to_string()]) .chain(status_param) .collect(); @@ -2608,12 +2714,16 @@ impl Gen<'_> { "let made = {{\n\t{make}\n\t}};\n\tunsafe {{ rt::Future::start(made, &[{outs}], {err}, future, w) }}" ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}({}) -> i32 {{\n\tlet w = \"{}\";\n\tlet _ = w;\n\t{}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}({}) -> i32 {{\n\tlet w = \"{}\";\n\tlet _ = w;\n\t{}{}\n}}\n", start.name, start_params.join(", "), start.name, + away(&start.name, &start_params), wrap(status, "i32", &started) )); + if export.homed { + self.export(homed(&start.name, &start_params, "i32", &unsent)); + } return; } let outs: Vec = export.call.params[params.len()..] @@ -2654,10 +2764,28 @@ impl Gen<'_> { }; self.checking.set(false); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}{}\n}}\n", params.join(", "), + away(&name, ¶ms), wrap(status, &ret, &format!("{body}\n\tlet result = {call};{pending}{writes}\n\t{finish}")) )); + if export.homed { + self.export(homed(&name, ¶ms, &ret, &unsent)); + } + } + + /// The object that is not `Send` whose handle an argument of `ty` is at its top: by + /// value, borrowed, or as `Some` of either. + fn unsent_object(&self, ty: &Type) -> Option { + match ty { + Type::Object(i) if !self.abi.api.objects[*i].send => Some(*i), + Type::Ref(_, inner) | Type::Option(inner) => match &**inner { + Type::Object(i) if !self.abi.api.objects[*i].send => Some(*i), + Type::Ref(_, inner) => self.unsent_object(inner), + _ => None, + }, + _ => None, + } } } @@ -2674,6 +2802,59 @@ fn wrap(status: Option<&str>, ret: &str, body: &str) -> String { ) } +/// The names of a C function's parameters, `name: type` each, as its arguments. +fn names(params: &[String]) -> String { + let names: Vec<&str> = (params.iter()) + .map(|p| p.split_once(": ").map_or(p.as_str(), |(name, _)| name)) + .collect(); + names.join(", ") +} + +/// The value that is not `Send` the first present of `handles`, each an object's holder +/// and a handle, stays on: its thread's record, if any is present. +fn owner(handles: &[(String, String)]) -> String { + let homes: Vec = (handles.iter()) + .map(|(holder, handle)| format!("unsafe {{ rt::Object::<{holder}>::owner({handle}) }}")) + .collect(); + (homes.iter().skip(1)).fold(homes[0].clone(), |chain, next| format!("{chain}.or_else(|| {next})")) +} + +/// The statement that runs the C function `name` taking `params` on the home that the +/// value of the first present of `handles` stays on, if that is another thread, calling it +/// again there and returning what it returns: so a call on a value that is not `Send` from +/// a binding with no thread that stays put runs on the value's thread. Nothing if it takes +/// no such handle. +fn away(name: &str, params: &[String], handles: &[(String, String)]) -> String { + if handles.is_empty() { + return String::new(); + } + format!( + "if let Some(home) = {}.filter(|home| home.away()) {{\n\t\treturn unsafe {{ home.run(move || {name}({})) }};\n\t}}\n\t", + owner(handles), + names(params) + ) +} + +/// The twin of the C function `name` taking `params` and returning `ret`, which runs it on +/// a home, a thread Rust owns, which the value that is not `Send` it makes stays on. When +/// one of `handles`, each optional, is present, it runs where that value stays instead, as +/// the function itself does. +fn homed(name: &str, params: &[String], ret: &str, handles: &[(String, String)]) -> String { + let present = match handles.is_empty() { + true => String::new(), + false => format!( + "if {}.is_some() {{\n\t\treturn unsafe {{ {name}({}) }};\n\t}}\n\t", + owner(handles), + names(params) + ), + }; + format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_homed({}) -> {ret} {{\n\t{present}let home = rt::Thread::home();\n\tlet made = unsafe {{ home.run(move || {name}({})) }};\n\trt::Thread::release(home);\n\tmade\n}}\n", + params.join(", "), + names(params) + ) +} + /// `body`, statements then a value, with the value wrapped in `Ok`. fn ok_tail(body: &str) -> String { format!("Ok({{\n\t{body}\n\t}})") diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 54de47b74..878dbceff 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -1775,8 +1775,13 @@ impl<'a> Gen<'a> { } let default = t.default.then(|| { let func = format!("{}Default", name.trim_start_matches('*')); + // A value that is not `Send` is made on a home, which its calls then run on. + let homed = match ty { + Type::Object(i) if !self.abi.api.objects[*i].send => "_homed", + _ => "", + }; let lift = match object { - true => format!("own_{c}(C.{c}_default())"), + true => format!("own_{c}(C.{c}_default{homed}())"), false => format!("lift_{c}(C.{c}_default())"), }; let (results, ok) = match limited { @@ -2523,7 +2528,7 @@ impl<'a> Gen<'a> { /// and a receiver, or else a function of the package. fn function(&mut self, function: &Function, owner: Option<&Ty>, owner_ty: Option<&Type>) -> Method { let abi = self.abi; - let export = abi.export(function, owner); + let export = abi.export(function, owner).on_home(); let c_name = export.call.name.clone(); let owner_c = match owner { Some(Ty::Struct(n) | Ty::Enum(n) | Ty::Handle { name: n, .. }) => Some(n.clone()), @@ -2884,10 +2889,18 @@ impl<'a> Gen<'a> { let cases = cases.join("\n"); // Lifts what a call or a poll wrote, once either reports the future finished. let (call, value) = self.call_sig(poll, &["f_".to_string(), "w_".to_string()]); - let polled = indent(&then( - &call, - &format!("switch {value} {{\n{cases}\n}}\nreturn false, nil"), - )); + let mut rest = format!("switch {value} {{\n{cases}\n}}\nreturn false, nil"); + // A future that is not `Send` lives on a home, which polls it, so its check fails + // only for one a callback made elsewhere. + if poll.status().is_some() { + let error = format!( + "{}(int32(c_status_), \"{}\")", + self.qualify(Packages::RT, "Status_"), + poll.name + ); + rest = format!("if c_status_ != 0 {{\n\treturn true, {error}\n}}\n{rest}"); + } + let polled = indent(&then(&call, &rest)); if function.eager { // An eager call runs Rust, and any callback it makes, before its future // exists, and a callback may cancel the context: so it is made, then the diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 629e3e6cb..1c2c87501 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -317,8 +317,14 @@ pub struct Object { /// The targets it compiles on. pub on: On, pub docs: String, - /// Whether it is `Sync`: if not, `&self` borrows it on one thread at a time. + /// Whether it is `Sync`: if not, `&self` borrows it on one thread at a time. One that is + /// not `Send` is not, as only its thread borrows it. pub sync: bool, + /// Whether it is `Send`: if not, its value stays on the thread that made it, which alone + /// borrows, moves, or drops it. A baseline written before it has none, as every + /// object was. + #[serde(default = "yes")] + pub send: bool, /// Whether it is `Copy`, which only one that may have padding under some layout is, any /// other being a value: taken by value, it is copied out of its handle, as Rust copies /// it, rather than moved out. @@ -428,6 +434,11 @@ pub struct Function { /// The parameter it polls its task through, if any: C passes a waker for it or, for a /// future made of a returned `Poll`, the future's poll supplies it. pub task: Option, + /// For a future, whether it is `Send`: if not, it stays on the thread that made it, + /// which alone polls or drops it. A baseline written before it has none, as every + /// future was. + #[serde(default = "yes")] + pub send: bool, /// Whether it returns another crate's iterator, whose items the export collects into /// the array it returns. pub collect: bool, @@ -815,6 +826,11 @@ impl Type { } } +/// `true`, the default of a flag a baseline written before it lacks. +fn yes() -> bool { + true +} + #[cfg(test)] mod tests { use super::*; diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 225405ca7..525b25b46 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -2554,11 +2554,17 @@ impl<'a> Gen<'a> { let value; // The C call, with the out pointers after its arguments. let status_arg = status.then(|| "&mut status".to_string()); + // A future that is not `Send` made from none is made on a home, so a coroutine that + // resumes on another thread still polls it there. + let made = match (&export.future, export.homed_call()) { + (Some(_), Some(homed)) => homed.name, + _ => c_name.clone(), + }; let rust_call = |outs: &[String]| { let args: Vec = rust_args.iter().chain(outs).chain(&status_arg).cloned().collect(); let call = match &handed { Some(h) => h.call(&args), - None => format!("crate::{c_name}({})", args.join(", ")), + None => format!("crate::{made}({})", args.join(", ")), }; // An exception the Rust left pending, which the C function dropped its result // for, is thrown once the native method returns with nothing else done. @@ -2572,7 +2578,16 @@ impl<'a> Gen<'a> { // else the future, which a native poll and free take. let poll_bare = self.bare(&poll.name).to_string(); let free_bare = self.bare(&free.name).to_string(); - let poll_outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); + // A future that is not `Send` takes a status its poll writes on another thread. + let ps = match poll.status() { + Some(_) => ", ps", + None => "", + }; + let poll_outs: Vec<(String, Ty)> = poll.params[2..] + .iter() + .filter(|(_, ty)| *ty != Ty::Out(Box::new(Ty::Status))) + .cloned() + .collect(); let (decl, ptrs, write) = self.glue_outs(&poll_outs, &[], &function.ok, function.err.as_ref()); let ok_set = match function.err.is_some() { true => "let ok = ret == 1;\n\t", @@ -2580,7 +2595,7 @@ impl<'a> Gen<'a> { }; let _ = write!( self.glue, - "unsafe fn jni_{}(f: *mut c_void, waker: crate::rt::Waker, w: &mut rt::Writer) -> bool {{\n\tlet _ = &w;\n\tunsafe {{\n\t{decl}\n\tlet ret = crate::{}(f as _, waker{});\n\tif ret == 0 {{\n\t\treturn false;\n\t}}\n\t{ok_set}{write}\n\ttrue\n\t}}\n}}\n\nunsafe fn jni_{}(f: *mut c_void) {{\n\tunsafe {{ crate::{}(f as _) }}\n}}\n\n", + "unsafe fn jni_{}(f: *mut c_void, waker: crate::rt::Waker, w: &mut rt::Writer, ps: &mut i32) -> bool {{\n\tlet _ = (&w, &ps);\n\tunsafe {{\n\t{decl}\n\tlet ret = crate::{}(f as _, waker{}{ps});\n\tif ret == 0 {{\n\t\treturn false;\n\t}}\n\t{ok_set}{write}\n\ttrue\n\t}}\n}}\n\nunsafe fn jni_{}(f: *mut c_void) {{\n\tunsafe {{ crate::{}(f as _) }}\n}}\n\n", poll.name, poll.name, ptrs.iter().map(|p| format!(", {p}")).collect::(), @@ -2611,10 +2626,10 @@ impl<'a> Gen<'a> { ); } self.native(&bare, ¶ms, ("jobject", "Any?"), &format!( - "{}\n\t\tlet f = {};{check}\n\t\tlet w = rt::start(env, f.cast(), {p_signal}, jni_{}, jni_{});\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{start_factory}\", std::ptr::null_mut())", - rust_pre.join("\n\t\t"), rust_call(&[]), poll.name, free.name)); + "{}\n\t\tlet f = {};{check}\n\t\tlet Some(w) = rt::start(env, f.cast(), {p_signal}, jni_{}, jni_{}, \"{}\") else {{ return std::ptr::null_mut() }};\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{start_factory}\", std::ptr::null_mut())", + rust_pre.join("\n\t\t"), rust_call(&[]), poll.name, free.name, poll.name)); self.native(&poll_bare, &[("jlong", "Long"), ("jlong", "Long")], ("jobject", "Any?"), &format!( - "match rt::poll(p0 as usize as _, p1 as usize as _, jni_{}) {{\n\t\t Some(w) => {{\n\t\t\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\t\t\tw.value(env, &FACTORY, c\"{poll_factory}\", std::ptr::null_mut())\n\t\t\t}},\n\t\t\tNone => std::ptr::null_mut(),\n\t\t}}", poll.name)); + "match rt::poll(env, p0 as usize as _, p1 as usize as _, jni_{}, \"{}\") {{\n\t\t Some(w) => {{\n\t\t\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\t\t\tw.value(env, &FACTORY, c\"{poll_factory}\", std::ptr::null_mut())\n\t\t\t}},\n\t\t\tNone => std::ptr::null_mut(),\n\t\t}}", poll.name, poll.name)); self.native( &free_bare, diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index 86a2a1eb4..877ed9059 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -845,6 +845,7 @@ mod tests { eager: false, holds: false, task: None, + send: true, collect: false, lends: false, access: Access::Call, @@ -897,6 +898,7 @@ mod tests { on: On::ALL, docs: String::new(), sync: true, + send: true, copy: false, shared: false, lifetimes: 0, diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 658ef8281..82687250a 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -1778,9 +1778,18 @@ impl<'a> Gen<'a> { let err = function.err.as_ref().map(strip_refs); let exception = err.as_ref().map(|e| self.exception(e)); if let Some(abi::Future { poll, free, .. }) = &export.future { - let poll_outs = &poll.params[2..]; + let poll_outs: Vec<(String, Ty)> = poll.params[2..] + .iter() + .filter(|(_, ty)| !abi::is_status(ty)) + .cloned() + .collect(); + // A poll of a future that is not `Send` on another thread raises. + let (ps, ps_check) = match poll.status() { + Some(_) => (", &mut ps", format!("\n\t\trt::check_status(ps, \"{}\")?;", poll.name)), + None => ("", String::new()), + }; let (decl, ptrs, value) = self.outs( - poll_outs, + &poll_outs, &[], &function.ok, err.as_ref(), @@ -1789,7 +1798,7 @@ impl<'a> Gen<'a> { ); let _ = write!( self.glue, - "unsafe fn poll_{c_name}(f: *mut std::ffi::c_void, w: crate::rt::Waker) -> R> {{\n\tunsafe {{\n\t\t{decl}\n\t\tlet r = rt::call(|| crate::{}(f as _, w{}));\n\t\tif r == 0 {{\n\t\t\treturn Ok(None);\n\t\t}}\n\t\t{value}.map(Some)\n\t}}\n}}\n\nunsafe fn free_{c_name}(f: *mut std::ffi::c_void) {{\n\tunsafe {{ crate::{}(f as _) }}\n}}\n\n", + "unsafe fn poll_{c_name}(f: *mut std::ffi::c_void, w: crate::rt::Waker) -> R> {{\n\tunsafe {{\n\t\tlet mut ps = 0;\n\t\tlet _ = &mut ps;\n\t\t{decl}\n\t\tlet r = rt::call(|| crate::{}(f as _, w{}{ps}));{ps_check}\n\t\tif r == 0 {{\n\t\t\treturn Ok(None);\n\t\t}}\n\t\t{value}.map(Some)\n\t}}\n}}\n\nunsafe fn free_{c_name}(f: *mut std::ffi::c_void) {{\n\tunsafe {{ crate::{}(f as _) }}\n}}\n\n", poll.name, ptrs.iter().map(|p| format!(", {p}")).collect::(), free.name, diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 61654fc57..bdcccaf28 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -58,6 +58,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result { thrown: BTreeMap, /// The exported types thrown as errors, which conform to `Swift.Error`. conforms: BTreeSet, + /// Those of them that are not `Sendable`, which `Swift.Error` requires: each conforms + /// unchecked, as its run-time checks keep it on its thread. + unsent_errors: BTreeSet, /// The objects that are noncopyable structs: each but those `Copy` or thrown. structs: BTreeSet, fresh: usize, @@ -769,16 +773,26 @@ impl<'a> Gen<'a> { self.top.push('\n'); } } + // `Swift.Error` is `Sendable`, so an error holding a value Swift keeps on one thread + // is so unchecked: its value's run-time checks still keep it there. for (name, ty) in &self.thrown.clone() { let sw = self.sw(ty); + let unchecked = match self.sendable(ty).is_empty() { + true => ", @unchecked Sendable", + false => "", + }; let _ = write!( self.types, - "/// A `{}` Rust returned as an error, or a closure throws for Rust to return.\npublic struct {name}: Swift.Error, CustomStringConvertible {{\n\tpublic let error: {sw}\n\n\tpublic init(error: {sw}) {{\n\t\tself.error = error\n\t}}\n\n\tpublic var description: Swift.String {{\n\t\t\"\\(error)\"\n\t}}\n}}\n\n", + "/// A `{}` Rust returned as an error, or a closure throws for Rust to return.\npublic struct {name}: Swift.Error{unchecked}, CustomStringConvertible {{\n\tpublic let error: {sw}\n\n\tpublic init(error: {sw}) {{\n\t\tself.error = error\n\t}}\n\n\tpublic var description: Swift.String {{\n\t\t\"\\(error)\"\n\t}}\n}}\n\n", sw.trim_start_matches("Swift.") ); } for name in &self.conforms.clone() { - let _ = write!(self.types, "extension {name}: Swift.Error {{}}\n\n"); + let unchecked = match self.unsent_errors.contains(name) { + true => ", @unchecked Sendable", + false => "", + }; + let _ = write!(self.types, "extension {name}: Swift.Error{unchecked} {{}}\n\n"); } } @@ -795,9 +809,13 @@ impl<'a> Gen<'a> { .collect(); let assigns: String = (0..elements.len()).map(|n| format!("\t\tself._{n} = _{n}\n")).collect(); let shown: Vec<&str> = types.iter().map(|t| t.trim_start_matches("Swift.")).collect(); + let sendable = match self.sendable(&Type::Tuple(elements.to_vec())).is_empty() { + true => "", + false => ", Sendable", + }; let _ = write!( self.types, - "/// The tuple `({})`, as a struct, since it holds a value Swift cannot copy. Frozen, so\n/// another module may move each element out.\n@frozen public struct {name}: ~Copyable, Sendable {{\n{fields}\n\tpublic init({}) {{\n{assigns}\t}}\n}}\n\n", + "/// The tuple `({})`, as a struct, since it holds a value Swift cannot copy. Frozen, so\n/// another module may move each element out.\n@frozen public struct {name}: ~Copyable{sendable} {{\n{fields}\n\tpublic init({}) {{\n{assigns}\t}}\n}}\n\n", shown.join(", "), params.join(", ") ); @@ -1056,7 +1074,7 @@ impl<'a> Gen<'a> { .collect(); let assigns: String = fields.iter().map(|(f, _)| format!("\t\tself.{f} = {f}\n")).collect(); body.push(format!("\tpublic init({}) {{\n{assigns}\t}}\n", params.join(", "))); - let mut protocols = vec!["Sendable"]; + let mut protocols = self.sendable(&ty); self.traits(&ty, &c, record.traits, &mut body, &mut members, &mut protocols); self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); self.distinct(&rust, &members); @@ -1096,9 +1114,9 @@ impl<'a> Gen<'a> { let text = format!("{rust}\n\n{}", record.docs); let _ = write!( self.types, - "{}public struct {name}: {} {{\n{}}}\n\n", + "{}public struct {name}{} {{\n{}}}\n\n", docs(text.trim(), ""), - protocols.join(", "), + conform(&protocols), body.join("\n") ); } @@ -1116,7 +1134,7 @@ impl<'a> Gen<'a> { let mut body = vec![format!( "\t/// Its bytes, which only Rust reads.\n\tlet c_: {c}\n\n\tprivate init(c_: {c}) {{\n\t\tself.c_ = c_\n\t}}\n" )]; - let mut protocols = vec!["Sendable"]; + let mut protocols = self.sendable(&ty); self.traits(&ty, &c, record.traits, &mut body, &mut members, &mut protocols); self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); self.distinct(&rust, &members); @@ -1127,9 +1145,9 @@ impl<'a> Gen<'a> { let text = format!("{rust}\n\n{}", record.docs); let _ = write!( self.types, - "{}public struct {name}: {} {{\n{}}}\n\n", + "{}public struct {name}{} {{\n{}}}\n\n", docs(text.trim(), ""), - protocols.join(", "), + conform(&protocols), body.join("\n") ); } @@ -1204,7 +1222,7 @@ impl<'a> Gen<'a> { } } let mut body = cases; - let mut protocols = vec!["Sendable"]; + let mut protocols = self.sendable(&ty); self.traits(&ty, &c, e.traits, &mut body, &mut members, &mut protocols); let owner = abi.ty(&ty); self.methods(&e.methods, &owner, &ty, &mut body, &mut members); @@ -1238,13 +1256,33 @@ impl<'a> Gen<'a> { let text = format!("{rust}\n\n{}", e.docs); let _ = write!( self.types, - "{}public enum {name}: {} {{\n{}}}\n\n", + "{}public enum {name}{} {{\n{}}}\n\n", docs(text.trim(), ""), - protocols.join(", "), + conform(&protocols), body.join("\n") ); } + /// `Sendable`, for a value whose every handle Rust lets cross threads: see + /// [`Self::sends`]. + fn sendable(&self, ty: &Type) -> Vec<&'static str> { + let mut handles = Vec::new(); + self.abi.api.handles(ty, &mut handles, &mut Vec::new()); + match handles.iter().all(|&i| self.sends(i)) { + true => vec!["Sendable"], + false => Vec::new(), + } + } + + /// Whether object `i` is `Sendable`: a class shares its value between the threads that + /// hold it, so only one Rust shares, `Send` and `Sync`; a noncopyable struct only moves + /// to another, so one that is `Send`. Swift's compiler then keeps any other on one + /// thread at a time, as its run-time checks do. + fn sends(&self, i: usize) -> bool { + let object = &self.abi.api.objects[i]; + object.send && (object.sync || self.unique(&Type::Object(i))) + } + fn object(&mut self, i: usize) { let abi = self.abi; let object = &abi.api.objects[i]; @@ -1264,7 +1302,12 @@ impl<'a> Gen<'a> { "\tlet handle: Native.Handle\n\n\tinit(raw: OpaquePointer?) {{\n\t\thandle = Native.Handle(raw, {c}_free, \"{name}\")\n\t}}\n\n\tinit(lent raw: OpaquePointer?) {{\n\t\thandle = Native.Handle(raw, nil, \"{name}\")\n\t}}\n\n\tdeinit {{\n\t\thandle.drop()\n\t}}\n" ), }]; - let mut protocols = vec!["@unchecked Sendable"]; + // Rust checks each borrow at run time, so it is `Sendable` unless its value stays on + // one thread, or is shared on one at a time, which Swift's compiler then enforces. + let mut protocols = match self.sends(i) { + true => vec!["@unchecked Sendable"], + false => Vec::new(), + }; self.traits(&ty, &c, object.traits, &mut body, &mut members, &mut protocols); let owner = Ty::Handle { name: c.clone(), @@ -1278,14 +1321,18 @@ impl<'a> Gen<'a> { if !unique { let _ = write!( self.types, - "{docs}public final class {name}: {} {{\n{body}}}\n\n", - protocols.join(", ") + "{docs}public final class {name}{} {{\n{body}}}\n\n", + conform(&protocols) ); return; } + let sendable = match self.sends(i) { + true => ", @unchecked Sendable", + false => "", + }; let _ = write!( self.types, - "{docs}public struct {name}: ~Copyable, @unchecked Sendable {{\n{body}}}\n\n" + "{docs}public struct {name}: ~Copyable{sendable} {{\n{body}}}\n\n" ); // Swift 6.4 takes a noncopyable type's `Equatable` and `Hashable`, but not yet its // `CustomStringConvertible` or `CustomDebugStringConvertible`, whose members it keeps. @@ -1834,8 +1881,11 @@ impl<'a> Gen<'a> { true => format!("a_.ran({call})"), false => call, }; + // A future that is not `Send` made from none is made on a home, where it stays, so + // a poll from any thread of the task's executor runs there. + let make = export.homed_call().map_or_else(|| c_name.clone(), |homed| homed.name); let call = reached(format!( - "{c_name}({})", + "{make}({})", exprs.iter().chain(&status_arg).cloned().collect::>().join(", ") )); let made = check @@ -1847,7 +1897,9 @@ impl<'a> Gen<'a> { Some(made) => format!("{made}\n{}(f_)", free.name), None => format!("{}({call})", free.name), }); - let poll_outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); + // A poll that can fail, of a future that is not `Send`, throws. + let poll_status = poll.status().is_some(); + let poll_outs: Vec<(String, Ty)> = poll.params[2..poll.params.len() - usize::from(poll_status)].to_vec(); let decls: Vec = poll_outs.iter().map(|(n, ty)| self.out_decl(n, ty)).collect(); let ptrs: Vec = poll_outs.iter().map(|(n, _)| format!("&c_{n}_")).collect(); let ready = match ok { @@ -1872,7 +1924,10 @@ impl<'a> Gen<'a> { .chain(["&f_".to_string()]) .chain(status_arg.iter().cloned()) .collect(); - let started = reached(format!("{}({})", start.name, start_args.join(", "))); + let start = export + .homed_start() + .map_or_else(|| start.name.clone(), |homed| homed.name); + let started = reached(format!("{start}({})", start_args.join(", "))); match &check { Some(check) => { inner.push(format!("let c_poll_ = {started}")); @@ -1882,12 +1937,21 @@ impl<'a> Gen<'a> { None => inner.push(format!("switch {started} {{\n{cases}\n}}")), } let mut body = decls; - let call_args: Vec = ["f_".to_string(), "w_".to_string()].into_iter().chain(ptrs).collect(); - body.push(format!( - "switch {}({}) {{\n{cases}\n}}", - poll.name, - call_args.join(", ") - )); + let mut call_args: Vec = ["f_".to_string(), "w_".to_string()].into_iter().chain(ptrs).collect(); + match poll_status { + true => { + body.push("var c_ps_: Swift.Int32 = 0".to_string()); + call_args.push("&c_ps_".to_string()); + body.push(format!("let c_poll_ = {}({})", poll.name, call_args.join(", "))); + body.push(format!("try Native.status(c_ps_, \"{}\")", poll.name)); + body.push(format!("switch c_poll_ {{\n{cases}\n}}")); + } + false => body.push(format!( + "switch {}({}) {{\n{cases}\n}}", + poll.name, + call_args.join(", ") + )), + } tail = Some(format!( "return try await Native.run(f_, {}) {{ f_, w_ in\n{}\n}}", free.name, @@ -2148,6 +2212,9 @@ impl<'a> Gen<'a> { match err.base() { Type::Record(_) | Type::Enum(_) | Type::Object(_) => { self.conforms.insert(self.sw(err)); + if self.sendable(err.base()).is_empty() { + self.unsent_errors.insert(self.sw(err)); + } Thrown::Itself } _ => { @@ -2455,6 +2522,14 @@ fn indent(code: &str) -> String { out } +/// The conformances a type declares, as `: A, B`, or nothing for none. +fn conform(protocols: &[&str]) -> String { + match protocols.is_empty() { + true => String::new(), + false => format!(": {}", protocols.join(", ")), + } +} + #[cfg(test)] mod tests { use super::{RUNTIME, TAKEN}; diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 0809e3b62..661316a5d 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -4,9 +4,12 @@ * With an argument, it instead breaks one contract of the ABI, which must abort. */ #include +#include +#include #include #include #include +#include #ifdef __linux__ #include @@ -495,6 +498,34 @@ static bool closures(void) { } } printf("farther hop %u\n", (unsigned)api_farther_hop((api_Pair_non_zero_u16){.left = 3, .right = UINT16_MAX})); + { + api_status local_status = 0; + api_Local *local = api_Local_new(); + api_Local *shared = api_local_or_new(local, &local_status); + uint32_t once = api_Local_bump(local, &local_status); + printf("local %u %u\n", once, api_Local_bump(shared, &local_status)); + api_Local *fresh = api_local_or_new(NULL, &local_status); + printf("local fresh %u\n", api_Local_bump(fresh, &local_status)); + printf("local either %u\n", api_bump_either(NULL, shared, &local_status)); + const api_Local *froms[2] = {local, NULL}; + const char *labels[2] = {"local later", "local later fresh"}; + for (int i = 0; i < 2; i++) { + api_local_or_new_later_future *later = api_local_or_new_later(froms[i], &local_status); + api_Local *made = NULL; + while (local_status == 0 && + api_local_or_new_later_poll(later, (api_waker){0}, &made, &local_status) == API_POLL_PENDING) { + } + printf("%s %u\n", labels[i], made ? api_Local_bump(made, &local_status) : 0); + api_Local_free(made); + api_local_or_new_later_future_free(later); + } + if (local_status != 0) { + ok = false; + } + api_Local_free(fresh); + api_Local_free(shared); + api_Local_free(local); + } return ok; } @@ -2120,6 +2151,332 @@ static bool move_lent_status(void *data, api_Counter *counter) { } /* Each check a call fails writes its status through a non-null one, and changes nothing. */ +/* What a second thread does with the main thread's local and future: each use fails as + * not Send, and each free queues the drop back to the main thread. */ +typedef struct { + api_Local *local; + api_local_later_future *later; + api_Local_poll_count_future *count; + api_status bumped; + api_status polled; + api_status counted; + api_poll poll; +} elsewhere; + +static void *use_elsewhere(void *data) { + elsewhere *e = data; + api_Local_bump(e->local, &e->bumped); + uint32_t out = 0; + e->poll = api_local_later_poll(e->later, (api_waker){0}, &out, &e->polled); + api_Local_poll_count_poll(e->count, (api_waker){0}, &out, &e->counted); + api_Local_free(e->local); + api_local_later_future_free(e->later); + api_Local_poll_count_future_free(e->count); + return NULL; +} + +/* A local a second thread makes, which it keeps until told it was freed: then it exits, + * which drops it there. */ +typedef struct { + api_Local *local; + /* Whether it also makes a future borrowing the local, which then holds it last. */ + bool later_too; + api_Local_later_future *later; + atomic_bool made; + atomic_bool freed; +} kept; + +static void *keep_local(void *data) { + kept *k = data; + k->local = api_Local_new(); + if (k->later_too) { + api_status status = 0; + k->later = api_Local_later(k->local, &status); + } + atomic_store(&k->made, true); + while (!atomic_load(&k->freed)) { + } + return NULL; +} + +/* A local a second thread bumps, writing why it could not. */ +typedef struct { + api_Local *local; + uint32_t count; + api_status status; +} bumped; + +static void *bump_local(void *data) { + bumped *b = data; + b->status = 0; + b->count = api_Local_bump(b->local, &b->status); + return NULL; +} + +/* A future making a local, which a second thread polls until it finishes or fails. */ +typedef struct { + api_local_or_new_later_future *future; + api_Local *out; + api_poll poll; + api_status status; +} polled_later; + +static void *poll_later(void *data) { + polled_later *p = data; + p->status = 0; + do { + p->poll = api_local_or_new_later_poll(p->future, (api_waker){0}, &p->out, &p->status); + } while (p->poll == API_POLL_PENDING && p->status == 0); + return NULL; +} + +/* Two locals on two homes, each visit calling into the other's. */ +typedef struct { + api_Local *a; + api_Local *b; + api_status status; +} homes; + +static uint32_t bump_a(void *data) { + homes *h = data; + return api_Local_bump(h->a, &h->status); +} + +static uint32_t visit_b(void *data) { + homes *h = data; + return api_Local_visit(h->b, bump_a, h, &h->status); +} + +static void *make_local(void *data) { + *(api_Local **)data = api_Local_new(); + return NULL; +} + +/* A value that is not Send stays on the thread that made it. */ +static int not_send(void) { + int failed = 0; + api_status status = 0; + api_Local *local = api_Local_new(); + api_Local *shared = api_Local_share(local, &status); + api_Local_bump(local, &status); + if (api_Local_bump(shared, &status) != 2 || status != 0 || !api_Local_eq(local, shared, &status) || + api_Local_hash(local, &status) != api_Local_hash(shared, &status) || status != 0) { + fprintf(stderr, "a shared local counted apart\n"); + failed = 1; + } + api_local_later_future *later = api_local_later(3); + uint32_t out = 0; + if (api_local_later_poll(later, (api_waker){0}, &out, &status) != API_POLL_PENDING || status != 0) { + fprintf(stderr, "a local future finished at once\n"); + failed = 1; + } + uint32_t before = api_locals_dropped(); + elsewhere e = {.local = local, .later = later, .count = api_Local_poll_count(local, &status)}; + pthread_t thread; + pthread_create(&thread, NULL, use_elsewhere, &e); + pthread_join(thread, NULL); + if (e.bumped != API_STATUS_NOT_SEND || e.polled != API_STATUS_NOT_SEND || e.counted != API_STATUS_NOT_SEND || + e.poll != API_POLL_PENDING || + strcmp(api_status_message(e.bumped), "a value that is not Send is used on another thread than the one that made it") != 0) { + fprintf(stderr, "a local used elsewhere wrote %d and %d\n", e.bumped, e.polled); + failed = 1; + } + /* Freed elsewhere, it drops here at this thread's next check. */ + if (api_locals_dropped() != before) { + fprintf(stderr, "a local freed elsewhere dropped there\n"); + failed = 1; + } + if (api_Local_bump(shared, &status) != 3 || status != 0 || api_locals_dropped() != before + 1) { + fprintf(stderr, "a local freed elsewhere never dropped on its thread\n"); + failed = 1; + } + api_Local_free(shared); + + /* Freed while its thread runs, it drops there as the thread exits. */ + kept k = {0}; + before = api_locals_dropped(); + pthread_create(&thread, NULL, keep_local, &k); + while (!atomic_load(&k.made)) { + } + api_Local_free(k.local); + atomic_store(&k.freed, true); + pthread_join(thread, NULL); + if (api_locals_dropped() != before + 1) { + fprintf(stderr, "a local freed elsewhere never dropped as its thread exited\n"); + failed = 1; + } + + /* A future freed elsewhere that borrows a local freed before it: dropped as the thread + * exits, the future lets go of the local, whose drop runs there too. */ + kept l = {.later_too = true}; + before = api_locals_dropped(); + pthread_create(&thread, NULL, keep_local, &l); + while (!atomic_load(&l.made)) { + } + api_Local_free(l.local); + api_Local_later_future_free(l.later); + atomic_store(&l.freed, true); + pthread_join(thread, NULL); + if (api_locals_dropped() != before + 1) { + fprintf(stderr, "a local a future freed elsewhere borrowed never dropped as its thread exited\n"); + failed = 1; + } + + /* Made on a home, a thread Rust owns, it stays there, and each call runs there. */ + api_Local *homed = api_Local_new_homed(); + status = 0; + if (api_Local_bump(homed, &status) != 1 || !api_Local_on_own_thread(homed, &status) || status != 0) { + fprintf(stderr, "a homed local ran here, writing %d\n", status); + failed = 1; + } + api_local_later_future *homed_later = NULL; + api_poll poll = api_local_later_start_homed(4, &out, &homed_later); + while (poll == API_POLL_PENDING) { + poll = api_local_later_poll(homed_later, (api_waker){0}, &out, &status); + } + if (out != 8 || status != 0) { + fprintf(stderr, "a homed future returned %u, writing %d\n", out, status); + failed = 1; + } + if (homed_later != NULL) { + api_local_later_future_free(homed_later); + } + /* A maker whose optional handle is absent runs on a home, as one taking none does, so + * any thread may use what it makes; with the handle present, it runs where that stays. */ + uint32_t before_made = api_locals_dropped(); + bumped b = {.local = api_local_or_new_homed(NULL, &status)}; + pthread_create(&thread, NULL, bump_local, &b); + pthread_join(thread, NULL); + if (status != 0 || b.status != 0 || b.count != 1) { + fprintf(stderr, "a local made with no handle stayed here, writing %d and %d\n", status, b.status); + failed = 1; + } + api_Local *mine = api_Local_new(); + bumped m = {.local = api_local_or_new_homed(mine, &status)}; + pthread_create(&thread, NULL, bump_local, &m); + pthread_join(thread, NULL); + if (status != 0 || m.status != API_STATUS_NOT_SEND || api_Local_bump(mine, &status) != 1 || status != 0) { + fprintf(stderr, "a local made from one here left it, writing %d and %d\n", status, m.status); + failed = 1; + } + api_Local_free(m.local); + api_Local_free(mine); + api_Local_free(b.local); + while (api_locals_dropped() != before_made + 3) { + } + /* So does such a future, which a second thread then polls: on its home with the + * handle absent, and failing its check with it present, as it stays here. */ + before_made = api_locals_dropped(); + polled_later none = {0}; + if (api_local_or_new_later_start_homed(NULL, &none.out, &none.future, &status) != API_POLL_PENDING || + status != 0) { + fprintf(stderr, "a future made with no handle finished at once, writing %d\n", status); + failed = 1; + } + pthread_create(&thread, NULL, poll_later, &none); + pthread_join(thread, NULL); + if (none.poll != API_POLL_READY || none.status != 0 || api_Local_bump(none.out, &status) != 1 || status != 0) { + fprintf(stderr, "a future made with no handle left its home, writing %d and %d\n", none.status, status); + failed = 1; + } + api_Local *here = api_Local_new(); + polled_later some = {0}; + if (api_local_or_new_later_start_homed(here, &some.out, &some.future, &status) != API_POLL_PENDING || + status != 0) { + fprintf(stderr, "a future made from a local here finished at once, writing %d\n", status); + failed = 1; + } + pthread_create(&thread, NULL, poll_later, &some); + pthread_join(thread, NULL); + if (some.status != API_STATUS_NOT_SEND) { + fprintf(stderr, "a future made from a local here was polled elsewhere, writing %d\n", some.status); + failed = 1; + } + poll_later(&some); + if (some.poll != API_POLL_READY || some.status != 0 || api_Local_bump(some.out, &status) != 1 || + api_Local_bump(here, &status) != 2 || status != 0) { + fprintf(stderr, "a future made from a local here did not share it, writing %d and %d\n", some.status, status); + failed = 1; + } + api_local_or_new_later_future_free(none.future); + api_local_or_new_later_future_free(some.future); + api_Local_free(none.out); + api_Local_free(some.out); + api_Local_free(here); + while (api_locals_dropped() != before_made + 3) { + } + /* A call on one home's value whose callback calls on another's, which calls back on + * the first's: the first home runs that while it waits on the second. */ + homes h = {.a = api_Local_new_homed(), .b = api_Local_new_homed()}; + if (api_Local_visit(h.a, visit_b, &h, &status) != 1 || status != 0 || h.status != 0) { + fprintf(stderr, "two homes calling each other wrote %d and %d\n", status, h.status); + failed = 1; + } + before_made = api_locals_dropped(); + api_Local_free(h.a); + api_Local_free(h.b); + while (api_locals_dropped() != before_made + 2) { + } + /* A home whose value was closed, then freed elsewhere, goes idle, so a later value made + * from none lives there: one of the next few, as the home sees it in its own time. */ + api_Local *closed = api_Local_new_homed(); + uint64_t closed_on = api_Local_thread_number(closed, &status); + api_Local_drop(closed, &status); + api_Local_free(closed); + api_Local *made[100]; + size_t count = 0; + bool reused = false; + while (count < 100 && !reused) { + made[count] = api_Local_new_homed(); + reused = api_Local_thread_number(made[count], &status) == closed_on; + count++; + struct timespec start, now; + timespec_get(&start, TIME_UTC); + do { + timespec_get(&now, TIME_UTC); + } while (!reused && (now.tv_sec - start.tv_sec) * 1000000000L + (now.tv_nsec - start.tv_nsec) < 20000000L); + } + if (!reused || status != 0) { + fprintf(stderr, "a home whose value was closed never went idle, writing %d\n", status); + failed = 1; + } + before_made = api_locals_dropped(); + for (size_t i = 0; i < count; i++) { + api_Local_free(made[i]); + } + while (api_locals_dropped() != before_made + count) { + } + /* A poll through a waiter that is not Send keeps it, so its future is not Send either. */ + api_LocalWaiter_poll_ready_future *waited = NULL; + uint32_t ready = 0; + if (api_LocalWaiter_poll_ready_start(2, &ready, &waited) != API_POLL_READY || ready != 2) { + fprintf(stderr, "a waiter's own poll returned %u\n", ready); + failed = 1; + } + api_poll_tripled_future *tripled = NULL; + if (api_poll_tripled_start(3, &ready, &tripled) != API_POLL_READY || ready != 9) { + fprintf(stderr, "a poll through a waiter returned %u\n", ready); + failed = 1; + } + /* Freed here, it drops on its home, which waits for work. */ + uint32_t before_homed = api_locals_dropped(); + api_Local_free(homed); + while (api_locals_dropped() != before_homed + 1) { + } + return failed; +} + +/* A local freed once its thread exited leaks, saying so, since nothing may drop it. */ +static int orphan(void) { + uint32_t before = api_locals_dropped(); + api_Local *local = NULL; + pthread_t thread; + pthread_create(&thread, NULL, make_local, &local); + pthread_join(thread, NULL); + api_Local_free(local); + return api_locals_dropped() != before; +} + static int statuses(void) { int failed = 0; api_status status = 0; @@ -2310,6 +2667,12 @@ static int broken(const char *which) { if (strcmp(which, "statuses") == 0) { return statuses(); } + if (strcmp(which, "threads") == 0) { + return not_send(); + } + if (strcmp(which, "orphan") == 0) { + return orphan(); + } if (strcmp(which, "moved") == 0) { api_Counter *c = api_Counter_new(str("c"), 0); api_Label label = api_Counter_into_label(c, (api_option_Point){0}, NULL); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 97e956fc0..24e813740 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -1507,6 +1507,24 @@ int script() { } } std::printf("farther hop %u\n", static_cast(api::farther_hop(api::Pair_non_zero_u16{3, UINT16_MAX}))); + { + api::Local local = api::Local::new_(); + api::Local shared = api::local_or_new(&local); + std::uint32_t bumped = local.bump(); + std::printf("local %u %u\n", static_cast(bumped), static_cast(shared.bump())); + std::printf("local fresh %u\n", static_cast(api::local_or_new(nullptr).bump())); + std::printf("local either %u\n", static_cast(api::bump_either(nullptr, shared))); + api::waker waker([]() {}); + const api::Local *froms[] = {&local, nullptr}; + for (const api::Local *from : froms) { + api::local_or_new_later_future later = api::local_or_new_later(from); + auto made = later.poll(waker); + while (!made) { + made = later.poll(waker); + } + std::printf("local later%s %u\n", from ? "" : " fresh", static_cast(made->bump())); + } + } // A returned `Vec<&Token>` holds clones, which the vector drops. api::Token twice = api::Token::new_(9); @@ -1684,6 +1702,26 @@ int futures() { check(got == 7, "a wake during the poll polls again"); } + // A value that is not Send stays on the thread that made it, whose executor polls its + // futures; destroyed on another thread, it drops on its own at its next call. + { + api::Local local = api::Local::new_(); + api::Local shared = local.share(); + local.bump(); + check(shared.bump() == 2, "a shared local counts together"); + std::uint32_t got = 0; + api::task later = local.later().then(q.executor(), [&](std::uint32_t value) { got = value; }); + q.drain(); + check(got == 3, "a local's future runs on its thread"); + api::task doubled = api::local_later(5).then(q.executor(), [&](std::uint32_t value) { got = value; }); + q.drain(); + check(got == 10, "a future that is not Send runs on its thread"); + std::uint32_t before = api::locals_dropped(); + std::thread([&]() { api::Local gone = std::move(local); }).join(); + check(api::locals_dropped() == before, "a local destroyed elsewhere waits for its thread"); + check(shared.bump() == 4 && api::locals_dropped() == before + 1, "a local destroyed elsewhere drops on its thread"); + } + // Cancelled between its wake and its poll, a receive loses nothing: the payload stays // for the next. { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 5d013028b..dc82e9e9d 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -927,6 +927,14 @@ Future script() async { print(next == null ? 'hop None' : 'hop Some(${next.toRadixString(10)})'); } print('farther hop ${api.fartherHop(const api.PairNonZeroU16(left: 3, right: 0xFFFF))}'); + final local = api.Local(); + final shared = api.localOrNew(local); + final bumped = local.bump(); + print('local $bumped ${shared.bump()}'); + print('local fresh ${api.localOrNew(null).bump()}'); + print('local either ${api.bumpEither(null, shared)}'); + print('local later ${(await api.localOrNewLater(local)).bump()}'); + print('local later fresh ${(await api.localOrNewLater(null)).bump()}'); } /// A reader whose reads fail, as the transcript's: each seek lands at 0. @@ -1059,6 +1067,28 @@ Future futures() async { check(dropLog.closes == 1, 'a cancel closes the implementation its future held'); await cancelled(dropping, 'a cancelled future that never finishes completes with Cancelled'); + // A value that is not Send lives on a home, a thread Rust owns, which runs each call on + // it: the isolate's thread may change between turns of its event loop, and a call runs + // on a worker while Rust holds an implementation. A close drops it there. + final local = api.Local(); + final shared = local.share(); + local.bump(); + check(shared.bump() == 2, 'a shared local counts together'); + check(await local.later() == 3, "a local's future runs on its home"); + check(await api.localLater(5) == 10, 'a future that is not Send runs on a home'); + await step(); + check(local.onOwnThread(), 'a call after a suspension runs on its home'); + final relayLog = CountLog(); + final relayed = api.logOnDrop(relayLog); + check(local.bump() == 4 && local.onOwnThread(), 'a call through the relay runs on its home'); + check(local.visit(() => local.bump()) == 5, 'a callback on a home calls back into its local'); + relayed.cancel(); + await cancelled(relayed, 'a cancelled future that held a relay completes with Cancelled'); + final localsBefore = api.localsDropped(); + local.close(); + check(api.localsDropped() == localsBefore + 1, 'a closed local drops on its home'); + shared.close(); + // A wake that lands while the future polls, before it returns pending, as each of // `yielded`'s does, is kept: the future polls again rather than waiting for a wake that // already happened. diff --git a/ffi/tests/api.go b/ffi/tests/api.go index c8c53f703..ffd081f1b 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1060,6 +1060,14 @@ func script() { } } fmt.Printf("farther hop %d\n", must(api.FartherHop(api.PairNonZeroU16{Left: 3, Right: math.MaxUint16}))) + local := api.NewLocal() + shared := must(api.LocalOrNew(local)) + bumped := must(local.Bump()) + fmt.Printf("local %d %d\n", bumped, must(shared.Bump())) + fmt.Printf("local fresh %d\n", must(must(api.LocalOrNew(nil)).Bump())) + fmt.Printf("local either %d\n", must(api.BumpEither(nil, shared))) + fmt.Printf("local later %d\n", must(must(api.LocalOrNewLater(ctx, local)).Bump())) + fmt.Printf("local later fresh %d\n", must(must(api.LocalOrNewLater(ctx, nil)).Bump())) } // farReader is a reader whose reads fail, which seeks anywhere from its start, as Rust's @@ -1247,6 +1255,55 @@ func futures() { // call polls again rather than waiting for a wake that already happened. check(must(api.Yielded(ctx, 7)) == 7, "a wake during the poll polls again") + // A value that is not Send lives on a home, a thread Rust owns, which runs each call on + // it from any goroutine, and drops it there once closed or collected. + local := api.NewLocal() + shared := must(local.Share()) + must(local.Bump()) + check(must(shared.Bump()) == 2, "a shared local counts together") + check(must(local.Later(ctx)) == 3, "a local's future runs on its home") + check(must(api.LocalLater(ctx, 5)) == 10, "a future that is not Send runs on a home") + check(must(api.BumpEither(nil, local)) == 4, "a call whose first local is absent runs on its second's home") + check(must(local.Visit(func() uint32 { return must(local.Bump()) })) == 5, "a callback on a home calls back into its local") + other := api.NewLocal() + nested := must(local.Visit(func() uint32 { + return must(other.Visit(func() uint32 { return must(local.Bump()) })) + })) + check(nested == 6, "a home waiting on another runs a call back into its own local") + ok(other.Close()) + made := must(api.LocalAfter(ctx)) + check(must(made.OnOwnThread()), "a future's output that is not Send is made on its home") + ok(made.Close()) + fresh := must(api.LocalOrNew(nil)) + var wg sync.WaitGroup + for range 8 { + wg.Add(1) + go func() { + defer wg.Done() + check(must(local.OnOwnThread()), "a local's call from any goroutine runs on its home") + check(must(fresh.OnOwnThread()), "a local made with its optional local absent lives on a home") + }() + } + wg.Wait() + ok(fresh.Close()) + before := api.LocalsDropped() + wg.Add(1) + go func() { + defer wg.Done() + ok(local.Close()) + }() + wg.Wait() + check(api.LocalsDropped() == before+1, "a local closed on another goroutine drops on its home") + // Collected only from here, after the count before it. + runtime.KeepAlive(shared) + shared = nil + deadline := time.Now().Add(10 * time.Second) + for api.LocalsDropped() < before+2 { + check(time.Now().Before(deadline), "a collected local drops on its home") + runtime.GC() + runtime.Gosched() + } + // A future's output of None is nil. half := must(api.Halved(ctx, 4)) check(half != nil && *half == 2, "a future's Some output reads back") diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 79fe7a52a..9eb13d6c9 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -782,6 +782,14 @@ fun script() { println("hop ${nextHop(hop)?.let { "Some($it)" } ?: "None"}") } println("farther hop ${fartherHop(PairNonZeroU16(3u, UShort.MAX_VALUE))}") + val local = Local.new() + val shared = localOrNew(local) + val bumped = local.bump() + println("local $bumped ${shared.bump()}") + println("local fresh ${localOrNew(null).bump()}") + println("local either ${bumpEither(null, shared)}") + println("local later ${runBlocking { localOrNewLater(local) }.bump()}") + println("local later fresh ${runBlocking { localOrNewLater(null) }.bump()}") } // A channel whose reads fail, which seeks anywhere from its start, as Rust's `Cursor` does. @@ -1033,6 +1041,33 @@ fun futures() { check(got == 7u, "a wake during the poll polls again") } + // A value that is not Send stays on the thread that made it: a use elsewhere throws, and + // its future polls on its thread. Closed elsewhere, it drops there at its next call. One + // such future made from none lives on a home, so it polls from any thread. + run { + val local = Local.new() + val shared = local.share() + local.bump() + check(shared.bump() == 2u, "a shared local counts together") + var got = 0u + scope.launch { got = local.later() } + q.drain() + check(got == 3u, "a local's future runs on its thread") + got = runBlocking(Dispatchers.Default) { localLater(5u) } + check(got == 10u, "a future that is not Send made from none polls on a pool") + var thrown: Throwable? = null + thread { thrown = runCatching { local.bump() }.exceptionOrNull() }.join() + check( + thrown is IllegalStateException && thrown?.message == "api_Local_bump: a value that is not Send is used on another thread than the one that made it", + "a local used elsewhere throws: $thrown", + ) + val before = localsDropped() + thread { local.close() }.join() + check(localsDropped() == before, "a local closed elsewhere waits for its thread") + check(shared.bump() == 4u && localsDropped() == before + 1u, "a local closed elsewhere drops on its thread") + shared.close() + } + // Cancelled between its wake and its poll, a receive loses nothing: the payload stays // for the next. run { diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 87f3734a7..fc62a9947 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -846,6 +846,16 @@ def drive(now, waiter): next = api.next_hop(hop) print("hop None" if next is None else f"hop Some({next})") print(f"farther hop {api.farther_hop(api.PairNonZeroU16(3, 0xFFFF))}") + local = api.Local.new() + shared = api.local_or_new(local) + bumped = local.bump() + print(f"local {bumped} {shared.bump()}") + print(f"local fresh {api.local_or_new(None).bump()}") + print(f"local either {api.bump_either(None, shared)}") + loop = asyncio.new_event_loop() + print(f"local later {loop.run_until_complete(api.local_or_new_later(local)).bump()}") + print(f"local later fresh {loop.run_until_complete(api.local_or_new_later(None)).bump()}") + loop.close() class Elementwise(array.array): @@ -924,6 +934,35 @@ async def recv(): except api.StringException as e: check(e.error == "malformed: ", "a future's borrowing error is its message") + # A value that is not Send stays on the thread that made it: a use elsewhere raises, and + # its future polls on its thread alone. Freed elsewhere, it drops there at its next call. + local = api.Local.new() + shared = local.share() + local.bump() + check(shared.bump() == 2, "a shared local counts together") + check(loop.run_until_complete(local.later()) == 3, "a local's future runs on its thread") + check(loop.run_until_complete(api.local_later(5)) == 10, "a future that is not Send runs on its thread") + raised = [] + + def elsewhere(): + try: + local.bump() + except RuntimeError as e: + raised.append(str(e)) + + thread = threading.Thread(target=elsewhere) + thread.start() + thread.join() + check(raised == ["api_Local_bump: a value that is not Send is used on another thread than the one that made it"], "a local used elsewhere raises") + before = api.locals_dropped() + held = [local] + del local + thread = threading.Thread(target=held.clear) + thread.start() + thread.join() + check(api.locals_dropped() == before, "a local freed elsewhere waits for its thread") + check(shared.bump() == 4 and api.locals_dropped() == before + 1, "a local freed elsewhere drops on its thread") + # Cancelled between its wake and its poll, a receive loses nothing: the payload stays # for the next. called = False diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index d9491ffd0..f17d15bc4 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -183,6 +183,15 @@ final class Cell: @unchecked Sendable { /// A queue of jobs that a test runs when it chooses: the executor its tasks prefer, so a /// call resumes on it. +/// A value that is not Send, which a test hands another thread to misuse. +final class Held: @unchecked Sendable { + let local: Local + + init(_ local: consuming Local) { + self.local = local + } +} + final class Queue: TaskExecutor, @unchecked Sendable { private let lock = NSLock() private var jobs: [UnownedJob] = [] @@ -1025,6 +1034,21 @@ func script() { print("hop \(nextHop(hop.map { try! NonZero($0) }).map { "Some(\($0))" } ?? "None")") } print("farther hop \(fartherHop(PairNonZeroU16(left: try! NonZero(3), right: try! NonZero(.max))))") + do { + let local = Local.new() + let shared = try! localOrNew(local: local) + let bumped = try! local.bump() + print("local \(bumped) \(try! shared.bump())") + print("local fresh \(try! localOrNew().bump())") + print("local either \(try! bumpEither(shared))") + // Made on this thread, so awaited on the executor the script drains on it. + let held = Held(local) + let later = Cell(0) + q.launch { later.value = try await localOrNewLater(local: held.local).bump() } + q.drain() + print("local later \(later.value)") + print("local later fresh \(block { try await localOrNewLater().bump() })") + } // A zero, which Swift's integer holds and Rust's `NonZero` does not, makes none, and a // record holding one keeps the protocols its traits give. check(refused("NonZero(_:)") { _ = try NonZero(0) }, "a zero makes no NonZero") @@ -1165,6 +1189,44 @@ func futures() { check(got.value == 7, "a wake during the poll polls again") } + // A value that is not Send stays on the thread that made it: a use elsewhere throws, and + // a release elsewhere drops it on its own thread at its next call. A future that is not + // Send made from none lives on a home, so any executor awaits it. + do { + let held = Cell(Held(Local.new())) + let shared = try! held.value!.local.share() + _ = try! held.value!.local.bump() + check(try! shared.bump() == 2, "a shared local counts together") + let got = Cell(0) + q.launch { got.value = try await held.value!.local.later() } + q.drain() + check(got.value == 3, "a local's future runs on its thread") + check(block { try await localLater(5) } == 10, "a future that is not Send made from none awaits anywhere") + let thrown = Cell(nil) + let done = DispatchSemaphore(value: 0) + Thread.detachNewThread { + do { + _ = try held.value!.local.bump() + } catch let e as CheckError { + thrown.value = "\(e.function): \(e.message)" + } catch {} + done.signal() + } + done.wait() + check( + thrown.value == "api_Local_bump: a value that is not Send is used on another thread than the one that made it", + "a local used elsewhere throws: \(String(describing: thrown.value))" + ) + let before = localsDropped() + Thread.detachNewThread { + held.value = nil + done.signal() + } + done.wait() + check(localsDropped() == before, "a local released elsewhere waits for its thread") + check(try! shared.bump() == 4 && localsDropped() == before + 1, "a local released elsewhere drops on its thread") + } + // A future whose output is `None` is ready, not pending. do { let got = Cell(nil) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 54ff6fa79..1b75107b1 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -219,6 +219,13 @@ fn api_c() { ); // A non-null status takes each failed check, which changed nothing. run(Command::new(&exe).arg("statuses")); + // A value that is not Send stays on its thread; one freed after that exited leaks. + run(Command::new(&exe).arg("threads")); + let orphan = run(Command::new(&exe).arg("orphan")); + assert_eq!( + String::from_utf8_lossy(&orphan.stderr), + "rust2-ffi: api::Local leaks: it is not Send, and its thread exited before it was freed\n" + ); // Each broken contract aborts, naming the function and what broke. for (case, message) in [ @@ -313,7 +320,7 @@ fn sanitized(sanitizer: &str) { ("TSAN_OPTIONS", "halt_on_error=1"), ]; for (compiler, std, src, args) in [ - ("clang", "-std=c11", "api.c", &[][..]), + ("clang", "-std=c11", "api.c", &["threads"][..]), ("clang++", "-std=c++17", "api.cpp", &["futures", "lifetimes"][..]), ] { let exe = dir.join(src.replace('.', "-")); @@ -350,7 +357,7 @@ fn api_miri() { std::fs::copy(api.out.join(file), copy.join(file)).unwrap(); } std::fs::create_dir_all(copy.join("src").join("rt")).unwrap(); - for file in ["rt.rs", "rt/borrow.rs", "lib.rs"] { + for file in ["rt.rs", "rt/borrow.rs", "rt/thread.rs", "lib.rs"] { std::fs::copy(api.out.join("src").join(file), copy.join("src").join(file)).unwrap(); } let mut lib = std::fs::read_to_string(copy.join("src").join("lib.rs")).unwrap(); diff --git a/quest/m1/exclusive-lend.md b/quest/m1/exclusive-lend.md new file mode 100644 index 000000000..d0b511c13 --- /dev/null +++ b/quest/m1/exclusive-lend.md @@ -0,0 +1,36 @@ +# [L] Export what a mutable method returns borrowing its receiver + +## Goal + +[rust2-ffi](/ffi/README.md) exports a method taking `&mut self` that returns an object +borrowing its receiver, such as moq-mux's `catalog::Producer::modify(&mut self) -> +Result>`, in every language. The census then no longer lists `catalog::Guard` +under what C can never hold, and moq's switch can drop the three `modify` calls in +moq-ffi's hand-written wrapper (`rs/moq-ffi/src/media.rs`). Adding exports breaks no generated +API, so this does not hold [moq's pin](/quest/m1/ffi-export/ship.md). + +## Plan + +Decided with the maintainer on 2026-11-16, picking +[kixel-ai/rust2#844](https://github.com/kixel-ai/rust2/pull/844) to merge: ✅ an exclusive +lend of the receiver, held until the returned object closes. Rejected: leaving `Guard` out. + +- Today a `&self` method's returned borrowing object is a lent handle that holds its + receiver and checks it on each use. The `&mut self` case takes the receiver's exclusive + borrow for the lent handle's whole life, as a call borrowing it as `&mut` does for one + call: any other use of the receiver while it is open fails `_STATUS_BORROWED`, + and closing the lent handle, or its consuming method such as `Guard::commit(self)`, gives + the borrow back. The receiver's `_free` while lent leaves the drop to the lent handle's + close, as a `&self` lend does. +- `Guard` is only useful through `Deref>` and `DerefMut`: moq-ffi calls + `guard.set_section(..)` and `guard.video.set_properties(..)`, so the exporter must reach + the target too. Recommended: a type with `Deref` gets its target's `&self` methods and + public fields on its own handle, and with `DerefMut` its `&mut self` methods and field + setters, each calling through `deref` or `deref_mut`, as Rust's auto-deref does. That + keeps `Guard`'s `updated` flag exact, since only `deref_mut` sets it. Rejected: a getter + returning the target cloned and a setter writing it back, which calls `deref_mut` on + every write and leaves `video.set_properties` unreachable on a copy. +- Prove it with the api fixture: an object whose `&mut self` method returns a guard + borrowing it, with a `Deref`, a `DerefMut`, and a consuming method, used in every + language and compared with native Rust in the transcript, plus a use of the receiver + while the guard is open, which fails. diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 0914d9c5f..216ffd500 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -35,8 +35,8 @@ Decided with the maintainer on 2026-11-09, in the quest audit: ([#730](https://github.com/kixel-ai/rust2/pull/730)). Rejected: catching panics at the boundary; aborting on every failed check. - ✅ A type or future that is not `Send` exports, checked by thread - ([not-send](/quest/m1/ffi-export/not-send.md)). Rejected: a Rust-owned thread for every - such object; keeping the refusal. + ([#844](https://github.com/kixel-ai/rust2/pull/844)). Rejected: a Rust-owned thread for + every such object; keeping the refusal. - ✅ No opt-out: `#[doc(hidden)]` items stay left out, as they are no part of a crate's API, but no quest or PR recommends the attribute to get an item past the exporter, and moq bans it ([moq-dev/moq#8392](https://github.com/moq-dev/moq/pull/8392)). diff --git a/quest/m1/ffi-export/not-send.md b/quest/m1/ffi-export/not-send.md deleted file mode 100644 index 9c1a337b4..000000000 --- a/quest/m1/ffi-export/not-send.md +++ /dev/null @@ -1,56 +0,0 @@ -# [L] Types that are not Send export - -## Goal - -[rust2-ffi](/ffi/README.md) exports an object or future that is not `Send`, bound to the -thread that made it: a call, poll, or drop from another thread never runs Rust code on -that value there. Three of moq's own are refused for it at moq `ffcb7cd9`: `clock::Entered`, -`clock::Rng`, and `catalog::Guard` -([#675](https://github.com/kixel-ai/rust2/pull/675)). At moq's `main` (`a11a584`, measured on 2026-11-14 in -[#802](https://github.com/kixel-ai/rust2/pull/802)), moq-tokio's `server::Listener::accept_with` -is a fourth: its future borrows `&self` of a `Listener` that is not `Sync`, through a -`FuturesUnordered` of boxed futures that are not, so the future is not `Send`. Proven per language by a fixture -type holding an `Rc`, called, awaited, and dropped from a second thread. - -## Plan - -Decided with the maintainer on 2026-11-09, in the quest audit: ✅ a thread check, with a -shim only where a language has no stable thread. Rejected: a Rust-owned thread for every -such object, which costs two thread handoffs a call even from a single-threaded caller -such as Python's event loop; keeping the refusal, as the -small-shapes quest had it. The maintainer: -an assert is fine "where a language has no concept of something being thread local". -On 2026-11-14, at [#802](https://github.com/kixel-ai/rust2/pull/802)'s pick, the maintainer kept -`server::Listener::accept_with` in this quest: ✅ keep (rejected: drop). - -- The handle records its owner thread, and each call compares it: no atomics, so it costs - less than a `Sync` type's borrow. The wrong thread - raises, as each failed check does. -- A free from another thread never drops the value there. GC finalizers run elsewhere: - Kotlin's phantom-reference daemon thread, Go's `runtime.AddCleanup` goroutine, Python's - collector, Swift's `deinit` on the releasing thread, and Dart's finalizer with no - current isolate. The free queues to the owner, which drains it at its next call, and a - queue must not wait on a call that never comes: where the binding has an event loop on - the owner thread (Python's loop, a C++ executor), queueing a free schedules a drain - there. Open: what drains it elsewhere, and once the owner thread has exited. - Recommended: drop the queue from the thread's exit hook (a thread-local's destructor), - and leak with a logged message only where the thread is already gone, as PyO3's - `unsendable` does. -- Where the language can say it, its compiler enforces the rule instead: a Swift object - that is `Send` but not `Sync` is not `Sendable` (today every object is - `@unchecked Sendable`, `ffi/src/swift.rs`). Confinement is not thread affinity, though: - a Swift actor's jobs run on any thread of its pool, and a Dart isolate need not keep its - OS thread, while Rust cannot tell an `Rc` from a guard pinned to its thread, as - [#675](https://github.com/kixel-ai/rust2/pull/675) calls moq's three. So a `!Send` value needs the check or the owner thread below in - Swift and Dart too, unless an executor pinned to the owner thread holds it; test a call - after a suspension, and through Dart's relay. -- Where a language has no stable thread, a Rust-owned thread holds the value and runs - its calls, two handoffs each: Go, whose goroutines move between OS threads, and a - `!Send` future awaited on a pool, as Kotlin's dispatchers, Swift's executors, and Go's - goroutines poll it. The future is spawned on the owner thread and the binding polls a - `Send` handle to it. Dart's relay (`ffi/runtime/dart.rs`'s `Home`) is the same - machinery in the other direction. -- wasm32 has one thread, so [rust2-wasm](/quest/m2/wasm/README.md) exports these with no - check. - -Public API: the items that now export, and Swift's `Sendable` conformances (breaking). diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index fc7012032..c669734b9 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -20,4 +20,3 @@ then see change. A sibling planned later adds itself to `Required` here. ## Required - [Refusals](/quest/m1/ffi-refusals/README.md) - the census lists no refusals at the moq pin, and `ffi.toml` is gone -- [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread diff --git a/quest/m1/ffi-refusals/README.md b/quest/m1/ffi-refusals/README.md index 22a5c4003..7bc6c8ef3 100644 --- a/quest/m1/ffi-refusals/README.md +++ b/quest/m1/ffi-refusals/README.md @@ -80,13 +80,14 @@ Decided with the maintainer on 2026-11-12, in the quest audit: (exported since by rules for another crate's value, a borrowing future of a function that is not `async`, and an object field that is not `Clone`), and what the excludes still hide at moq's main, exported since (`IpAddr` by its family, as - `SocketAddr` is) but for `server::Listener::accept_with`, which - [not-send](/quest/m1/ffi-export/not-send.md) owns; + `SocketAddr` is, and `server::Listener::accept_with` as its future need not be `Send`); `just ffi census` fails on an exclude that names what exports. The - bump is then only the bump. The types refused for not being `Send` (`clock::Entered`, - `clock::Rng`, `catalog::Guard`) clear with [not-send](/quest/m1/ffi-export/not-send.md). + bump is then only the bump. Of the types refused for not being `Send`, `clock::Entered` + and `clock::Rng` export since [#844](https://github.com/kixel-ai/rust2/pull/844), and `catalog::Guard` is left out, as + `Producer::modify` returns it borrowing its receiver mutably, which no handle may hold + until [an exclusive lend](/quest/m1/exclusive-lend.md) exports it. - ✅ The closing work is a child, [no config](/quest/m1/ffi-refusals/no-config.md), as [the exporter epic's](/quest/m1/ffi-export/ship.md) is: an epic's `Required` would hold - back every child, and this epic's end needs not-send, which is another epic's. + back every child. Public API: the exporter's command line and the C API of every item that exports. diff --git a/quest/m1/ffi-refusals/no-config.md b/quest/m1/ffi-refusals/no-config.md index 5134f1bbe..4ae566670 100644 --- a/quest/m1/ffi-refusals/no-config.md +++ b/quest/m1/ffi-refusals/no-config.md @@ -30,4 +30,3 @@ that passes them). ## Required - [The moq pin bump](/quest/m1/ffi-refusals/moq-pin.md) - the pin is at moq's `main`, where the census is taken -- [Not Send](/quest/m1/ffi-export/not-send.md) - exports the three types the census still refuses for not being `Send` diff --git a/quest/m1/ffi-speed/loop-drain.md b/quest/m1/ffi-speed/loop-drain.md new file mode 100644 index 000000000..93189680e --- /dev/null +++ b/quest/m1/ffi-speed/loop-drain.md @@ -0,0 +1,34 @@ +# [M] A free elsewhere drains on an idle Python loop or C++ executor + +## Goal + +A [rust2-ffi](/ffi/README.md) value that is not `Send`, freed on another thread, drops +promptly on its owner thread even while that thread's Python event loop or C++ executor +calls nothing into Rust. Today the drop waits in the owner's queue for its next call or +its exit ([kixel-ai/rust2#844](https://github.com/kixel-ai/rust2/pull/844)), so an idle +loop holds what was freed, such as a closed session's sockets, for as long as it stays +idle. The Goal's measurement is that delay: unbounded today, one loop turn after. + +## Plan + +Decided with the maintainer on 2026-11-16, picking +[kixel-ai/rust2#844](https://github.com/kixel-ai/rust2/pull/844) to merge: ✅ schedule a +drain on Python's wake queue and on a C++ executor when a free elsewhere queues one. +Rejected: waiting for the owner's next call or exit, as #844 does. + +- `rt/thread.rs`'s owner record gains an optional wake the binding sets, which the queue + calls once when it goes from empty to non-empty. Go and Dart already send every such + value to a home, which drains on each wake, so they set none. Kotlin and Swift home only + a future made from none; a value they make stays on the caller's thread, which has no + loop Rust can post to, so it keeps draining at its next call or exit, outside this quest. +- Python: the owner's wake queues a drain token on the wake queue its futures use, with + the same byte to the loop's socket, and the loop's dispatch drains on the loop thread. + The binding sets the wake when a value that is not `Send` is first made on a thread + with a running loop. +- C++: an executor may run its job on any thread, so a drain posted to one runs only if + it lands on the owner. Recommended: post the drain job to the executor of the owner's + latest `then` or `via`, and drain if the job runs on the owner thread, else leave the + queue for the next call. Rejected: a generated `api::drain()` for the caller to + schedule, which is new API for a case the runtime can mostly cover. +- Prove it in `api.py` and `api.cpp`: free a `Local` on another thread while the owner's + loop is idle, and observe its drop with no further call into Rust. diff --git a/quest/m2/wasm/backend.md b/quest/m2/wasm/backend.md index 7dd4236c7..fa3d29dd3 100644 --- a/quest/m2/wasm/backend.md +++ b/quest/m2/wasm/backend.md @@ -48,7 +48,7 @@ measurement on [the benchmark](/quest/m2/wasm/routes.md): schedules the next poll on a microtask, never inside the wake. Cancelling frees the future and loses nothing. JSPI is not needed, since no Rust code blocks on JS. - One thread: no atomics, no shared memory, no COOP or COEP headers. A type that is not - `Send` exports with no check ([not-send](/quest/m1/ffi-export/not-send.md)). + `Send` exports with no thread check, which the native targets make ([#844](https://github.com/kixel-ai/rust2/pull/844)). - The runtime's `fail` writes its message to `console.error`. - A struct passed by value is passed indirectly in wasm's C ABI, which the binding must match from the target's layouts, not the host's.