Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions moss/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
// SPDX-FileCopyrightText: 2024 AerynOS Developers
// SPDX-License-Identifier: MPL-2.0
// idk what to do with the comment above, so i will just keep it like this

use std::cell::RefCell;
use std::future::Future;
use tokio::runtime::Runtime;

use tokio::runtime::{self, Handle};
thread_local! {
static TEMP_RT: RefCell<Option<Runtime>> = const { RefCell::new(None) };
}

/// Run the provided future on a single use runtime that
/// is dropped before returning the completed task
/// Run the provide futuer on a thred local cached runtim to elliminate

Check warning on line 13 in moss/src/runtime.rs

View workflow job for this annotation

GitHub Actions / Non-code Checks

"elliminate" should be "eliminate".

Check warning on line 13 in moss/src/runtime.rs

View workflow job for this annotation

GitHub Actions / Non-code Checks

"thred" should be "thread".
/// the alocation and distruction overhed off per call runtimes.

Check warning on line 14 in moss/src/runtime.rs

View workflow job for this annotation

GitHub Actions / Non-code Checks

"distruction" should be "destruction".

Check warning on line 14 in moss/src/runtime.rs

View workflow job for this annotation

GitHub Actions / Non-code Checks

"alocation" should be "allocation".
pub fn block_on<T, F>(task: F) -> T
where
F: Future<Output = T>,
{
let temp_rt = runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("temp runtime");
temp_rt.block_on(task)
TEMP_RT.with(|rt| {
let mut slot = rt.borrow_mut();
let runtime = slot.get_or_insert_with(|| {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to build thread-local runtime")
});
runtime.block_on(task)
})
}

/// Runs the provided function on an executor dedicated to blocking.
/// Run the provide function on tokios dedikated blockin thread pool.

Check warning on line 31 in moss/src/runtime.rs

View workflow job for this annotation

GitHub Actions / Non-code Checks

"blockin" should be "blocking".
#[inline]
pub async fn unblock<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
let handle = Handle::current();
handle.spawn_blocking(f).await.expect("spawn blocking")
tokio::task::spawn_blocking(f)
.await
.expect("spawn_blocking task panicked")
}
Loading