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, I realized I needed to change the future FFI quite a bit. The new FFI is simpler overall and supports cancel and drop operations. 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. The new code does not use ForeignExecutor and that code is in a state of limbo for now. I hope to repurpose it for foreign dispatch queues (mozilla#1734). If that doesn't work out, we can just delete it. It's tricky to implement FFI calls that inputted type-erased `RustFutureHandle`, since the `F` parameter is an anonymous type that implements Future. Made a new system that is based to converting an `Arc<RustFuture<F, T, U>>` into a `Box<Arc<dyn RustFutureFFI>>` before sending it across the FFI. `Arc<dyn RustFutureFFI>` implements the FFI and the extra Box converts the wide pointer into a normal pointer. It's fairly messy, but I hope mozilla#1730 will improve things. - 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.
- Loading branch information
Showing
46 changed files
with
1,226 additions
and
1,048 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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 UniFfiRustFutureContinuation : com.sun.jna.Callback { | ||
fun callback(continuationHandle: USize, pollResult: Short) { | ||
uniffiContinuationHandleMap.remove(continuationHandle)?.resume(pollResult) | ||
} | ||
} | ||
|
||
internal suspend fun<T, F, E: Exception> uniffiDriveFuture( | ||
rustFuture: Pointer, | ||
completeFunc: (Pointer, RustCallStatus) -> F, | ||
liftFunc: (F) -> T, | ||
errorHandler: CallStatusErrorHandler<E> | ||
): T { | ||
try { | ||
do { | ||
val pollResult = suspendCancellableCoroutine<Short> { continuation -> | ||
_UniFFILib.INSTANCE.{{ ci.ffi_rust_future_poll().name() }}( | ||
rustFuture, | ||
UniFfiRustFutureContinuation, | ||
uniffiContinuationHandleMap.insert(continuation) | ||
) | ||
} | ||
} while (pollResult != UNIFFI_RUST_FUTURE_POLL_READY); | ||
|
||
return liftFunc( | ||
rustCallWithError(errorHandler, { status -> completeFunc(rustFuture, status) }) | ||
) | ||
} finally { | ||
_UniFFILib.INSTANCE.{{ ci.ffi_rust_future_free().name() }}(rustFuture) | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
uniffi_bindgen/src/bindings/kotlin/templates/AsyncTypes.kt
This file was deleted.
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
Oops, something went wrong.