Add corex library with resource-aware execution primitives
CI / Rustfmt (push) Canceled after 0s
CI / Clippy (push) Canceled after 0s
CI / Test (default features) (push) Canceled after 0s
CI / Test (all features) (push) Canceled after 0s
CI / Test (bg only) (push) Canceled after 0s
CI / Test (compute only) (push) Canceled after 0s
CI / Test (io only) (push) Canceled after 0s
CI / Run example (push) Canceled after 0s
CI / Docs check (push) Canceled after 0s
CI / Cargo package dry-run (push) Canceled after 0s
CI / Rustfmt (push) Canceled after 0s
CI / Clippy (push) Canceled after 0s
CI / Test (default features) (push) Canceled after 0s
CI / Test (all features) (push) Canceled after 0s
CI / Test (bg only) (push) Canceled after 0s
CI / Test (compute only) (push) Canceled after 0s
CI / Test (io only) (push) Canceled after 0s
CI / Run example (push) Canceled after 0s
CI / Docs check (push) Canceled after 0s
CI / Cargo package dry-run (push) Canceled after 0s
- Implement CI workflow for formatting, linting, testing, and documentation checks. - Create publish workflow for automated publishing to crates.io. - Add .gitignore to exclude build artifacts and editor files. - Define Cargo.toml for corex and corex-core with dependencies and metadata. - Add README.md files for corex and corex-core with usage instructions and licensing. - Implement core execution primitives: spawn_io, compute, and spawn_bg with panic isolation. - Establish global engine context for resource management based on logical CPU cores. - Introduce error handling for compute panics.
This commit is contained in:
commit
d8144e249e
17 files changed
+898
No files matched your search
@@ -0,0 +1,53 @@
|
||||
//! Bounded-concurrency background task execution.
|
||||
|
||||
use tracing::Instrument;
|
||||
|
||||
use crate::context::context;
|
||||
|
||||
/// Spawns `future` as a background task once a concurrency permit is
|
||||
/// available, returning its [`tokio::task::JoinHandle`].
|
||||
///
|
||||
/// At most [`crate::context::EngineContext::bg_concurrency`] background
|
||||
/// tasks run at any one time; awaiting `spawn_bg` blocks the caller until a
|
||||
/// slot frees up, which is what provides the bound. A task's permit is held
|
||||
/// for the task's full lifetime and released automatically when it
|
||||
/// completes.
|
||||
///
|
||||
/// Panic isolation is provided by Tokio itself: a panicking background task
|
||||
/// cannot crash the runtime or any sibling task, and is surfaced to the
|
||||
/// caller as `Err(JoinError)` when the returned handle is awaited, exactly
|
||||
/// as with [`crate::io::spawn_io`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if called outside the context of a running Tokio runtime.
|
||||
pub async fn spawn_bg<F>(future: F) -> tokio::task::JoinHandle<F::Output>
|
||||
where
|
||||
F: std::future::Future + Send + 'static,
|
||||
F::Output: Send + 'static,
|
||||
{
|
||||
let permit = context()
|
||||
.bg_semaphore
|
||||
.acquire()
|
||||
.await
|
||||
.expect("corex: bg semaphore closed unexpectedly");
|
||||
let span = tracing::info_span!("corex_bg_task");
|
||||
tokio::spawn(
|
||||
async move {
|
||||
let _permit = permit;
|
||||
future.await
|
||||
}
|
||||
.instrument(span),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn spawn_bg_roundtrips_a_value() {
|
||||
let handle = spawn_bg(async { 9u32 }).await;
|
||||
assert_eq!(handle.await.unwrap(), 9);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user