Skip to content

Commit

Permalink
[rust] expose init_tokio (#3061)
Browse files Browse the repository at this point in the history
EW has more complicated setup process and needs to initialize tokio
within sbox.
  • Loading branch information
mikea authored Nov 5, 2024
1 parent b2c31bd commit 3b9fc88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/rust/cxx-integration/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ mod ffi {
}

pub fn init() {
tokio::init();
init_tokio(None);
}

/// Initialize tokio runtime.
/// Should not be called directly but as a part of a downstream cxx-integration init.
pub fn init_tokio(worker_threads: Option<usize>) {
tokio::init(worker_threads);
}

fn trigger_panic(msg: &str) {
Expand Down
12 changes: 9 additions & 3 deletions src/rust/cxx-integration/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ static TOKIO_RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();

/// Initialize tokio runtime.
/// Must be called after all forking and sandbox setup is finished.
pub(crate) fn init() {
pub(crate) fn init(worker_threads: Option<usize>) {
assert!(TOKIO_RUNTIME.get().is_none());

let runtime = tokio::runtime::Builder::new_multi_thread()
let mut builder = tokio::runtime::Builder::new_multi_thread();

if let Some(worker_threads) = worker_threads {
builder.worker_threads(worker_threads);
}

let runtime = builder
.enable_time()
.enable_io()
.build()
Expand Down Expand Up @@ -58,7 +64,7 @@ mod test {

#[test]
fn test_tokio_init() {
init();
init(None);
let join = spawn(async move {
tokio::time::sleep(Duration::from_millis(1)).await;
42
Expand Down

0 comments on commit 3b9fc88

Please sign in to comment.