-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(review): move wasm/napi-specific task JoinHandle stuff to cross…
…target-utils
- Loading branch information
Showing
11 changed files
with
120 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod spawn; | ||
pub mod task; | ||
pub mod time; |
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,46 @@ | ||
use futures::Future; | ||
use tokio::sync::broadcast::{self}; | ||
|
||
pub struct JoinHandle<T> { | ||
handle: tokio::task::JoinHandle<T>, | ||
|
||
sx_exit: Option<broadcast::Sender<()>>, | ||
} | ||
|
||
impl<T> JoinHandle<T> { | ||
pub fn abort(&mut self) { | ||
if let Some(sx_exit) = self.sx_exit.as_ref() { | ||
sx_exit.send(()).ok(); | ||
} | ||
|
||
self.handle.abort(); | ||
} | ||
} | ||
|
||
pub fn spawn<T>(future: T) -> JoinHandle<T::Output> | ||
where | ||
T: Future + Send + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
spawn_with_sx_exit::<T>(future, None) | ||
} | ||
|
||
pub fn spawn_controlled<T>(future_fn: Box<dyn FnOnce(broadcast::Receiver<()>) -> T>) -> JoinHandle<T::Output> | ||
where | ||
T: Future + Send + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
let (sx_exit, rx_exit) = tokio::sync::broadcast::channel::<()>(1); | ||
let future = future_fn(rx_exit); | ||
|
||
spawn_with_sx_exit::<T>(future, Some(sx_exit)) | ||
} | ||
|
||
fn spawn_with_sx_exit<T>(future: T, sx_exit: Option<broadcast::Sender<()>>) -> JoinHandle<T::Output> | ||
where | ||
T: Future + Send + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
let handle = tokio::spawn(future); | ||
JoinHandle { handle, sx_exit } | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod spawn; | ||
pub mod task; | ||
pub mod time; |
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,64 @@ | ||
use futures::Future; | ||
use tokio::sync::{ | ||
broadcast::{self}, | ||
oneshot::{self}, | ||
}; | ||
|
||
// Wasm-compatible alternative to `tokio::task::JoinHandle<T>`. | ||
// `pin_project` enables pin-projection and a `Pin`-compatible implementation of the `Future` trait. | ||
#[pin_project::pin_project] | ||
pub struct JoinHandle<T> { | ||
#[pin] | ||
receiver: oneshot::Receiver<T>, | ||
|
||
sx_exit: Option<broadcast::Sender<()>>, | ||
} | ||
|
||
impl<T> Future for JoinHandle<T> { | ||
type Output = Result<T, oneshot::error::RecvError>; | ||
|
||
fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> { | ||
// the `self.project()` method is provided by the `pin_project` macro | ||
core::pin::Pin::new(&mut self.receiver).poll(cx) | ||
} | ||
} | ||
|
||
impl<T> JoinHandle<T> { | ||
pub fn abort(&mut self) { | ||
if let Some(sx_exit) = self.sx_exit.as_ref() { | ||
sx_exit.send(()).ok(); | ||
} | ||
} | ||
} | ||
|
||
pub fn spawn<T>(future: T) -> JoinHandle<T::Output> | ||
where | ||
T: Future + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
spawn_with_sx_exit::<T>(future, None) | ||
} | ||
|
||
pub fn spawn_controlled<T>(future_fn: Box<dyn FnOnce(broadcast::Receiver<()>) -> T>) -> JoinHandle<T::Output> | ||
where | ||
T: Future + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
let (sx_exit, rx_exit) = tokio::sync::broadcast::channel::<()>(1); | ||
let future = future_fn(rx_exit); | ||
spawn_with_sx_exit::<T>(future, Some(sx_exit)) | ||
} | ||
|
||
fn spawn_with_sx_exit<T>(future: T, sx_exit: Option<broadcast::Sender<()>>) -> JoinHandle<T::Output> | ||
where | ||
T: Future + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
let (sender, receiver) = oneshot::channel(); | ||
wasm_bindgen_futures::spawn_local(async move { | ||
let result = future.await; | ||
sender.send(result).ok(); | ||
}); | ||
|
||
JoinHandle { receiver, sx_exit } | ||
} |
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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
query-engine/core/src/interactive_transactions/actor_manager.rs
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