forked from mozilla/uniffi-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The initial motivation for this was cancellation. PR#1697 made it so if an async function was cancelled we would eventually release the resources. However, it would be better if we could immedately release the resources. In order to implement that, the future FFI needed to change quite a bit and most of these changes are good. The new FFI is simpler overall and supports cancel and drop operations. It's actually quite close to the initial FFI that Hywan proposed. Cancel ensures that the foreign code will resume and break out of its async code. Drop ensures that all resources from the wrapped future are relased. Note: the new FFI has a cancel method, but no bindings use it yet. For Python/Kotlin we don't need to because they throw cancellation exceptions, which means cancellation support falls out from the new API without any extra work. This cancel method could be used for Swift, but we still need to think this through in a different PR. The new code does not use ForeignExecutor anymore, so that code is in a state of limbo. I hope to repurpose it for foreign dispatch queues (mozilla#1734). If that doesn't work out, we can just delete it. The FFI calls need some care since we pass a type-erased handle to the foreign code, while RustFuture is generic over an anonymous Future type: - The concrete RustFuture type implements the `RustFutureFFI<F>` trait. - We give the foreign side a leaked `Box<Arc<dyn RustFutureFFI<F>>>>`. - We hand-monomorphize, creating a scaffolding function for each RustFutureFFI method, for each possible FFI type. Updated proc macros lift arguments in 2 phases. First we call `try_lift` on all arguments, generating a `Result<LiftedArgsTuple>`. Then we call the rust function using that tuple or handle the error. This is needed because the new async code adds a `Send` bound futures. The `Send` bound is good because futures are going to be moving across threads as we poll/wake them. However, the lift code won't always be Send because it may deal with raw pointers. To deal with that, we perform the non-Send lifting outside of the future, then create a Send future from the result. This means that the lift phase is executed outside of the async context, but it should be very fast. This change also allows futures that return Results to attempt to downcast lift errors. More changes: - Updated the futures fixture tests for this to hold on to the mutex longer in the initial call. This makes it so they will fail unless the future is dropped while the mutex is still locked. Before they would only succeed as long as the mutex was dropped once the timeout expired. - Updated `RustCallStatus.code` field to be an enum. Added `Cancelled` as one of the variants. `Cancelled` is only used for async functions. - Removed the FutureCallback and invoke_future_callback from `FfiConverter`. - New syncronization handling code in RustFuture that's hopefully clearer, more correct, and more understandable than the old stuff. - Updated `UNIFFI_CONTRACT_VERSION` since this is an ABI change - Removed the `RustCallStatus` param from async scaffolding functions. These functions can't fail, so there's no need. - Added is_async() to the Callable trait. - Changed `handle_failed_lift` signature. Now, if a `Result<>` is able to downcast the error, it returns `Self::Err` directly instead of serializing the error into `RustBuffer`. Co-authored-by: Ivan Enderlin <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
- Loading branch information
1 parent
2a8213d
commit 20d4d8c
Showing
51 changed files
with
1,413 additions
and
1,101 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Async return type handlers | ||
|
||
internal const val UNIFFI_RUST_FUTURE_POLL_READY = 0.toShort() | ||
internal const val UNIFFI_RUST_FUTURE_POLL_MAYBE_READY = 1.toShort() | ||
|
||
internal val uniffiContinuationHandleMap = UniFfiHandleMap<CancellableContinuation<Short>>() | ||
|
||
// FFI type for Rust future continuations | ||
internal object uniffiRustFutureContinuationCallback: UniFffiRustFutureContinuationCallbackType { | ||
override fun callback(continuationHandle: USize, pollResult: Short) { | ||
uniffiContinuationHandleMap.remove(continuationHandle)?.resume(pollResult) | ||
} | ||
|
||
internal fun register(lib: _UniFFILib) { | ||
lib.{{ ci.ffi_rust_future_continuation_callback_set().name() }}(this) | ||
} | ||
} | ||
|
||
internal suspend fun<T, F, E: Exception> uniffiRustCallAsync( | ||
rustFuture: Pointer, | ||
pollFunc: (Pointer, USize) -> Unit, | ||
completeFunc: (Pointer, RustCallStatus) -> F, | ||
freeFunc: (Pointer) -> Unit, | ||
liftFunc: (F) -> T, | ||
errorHandler: CallStatusErrorHandler<E> | ||
): T { | ||
try { | ||
do { | ||
val pollResult = suspendCancellableCoroutine<Short> { continuation -> | ||
pollFunc( | ||
rustFuture, | ||
uniffiContinuationHandleMap.insert(continuation) | ||
) | ||
} | ||
} while (pollResult != UNIFFI_RUST_FUTURE_POLL_READY); | ||
|
||
return liftFunc( | ||
rustCallWithError(errorHandler, { status -> completeFunc(rustFuture, status) }) | ||
) | ||
} finally { | ||
freeFunc(rustFuture) | ||
} | ||
} | ||
|
47 changes: 0 additions & 47 deletions
47
uniffi_bindgen/src/bindings/kotlin/templates/AsyncTypes.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.