tokise/
lib.rs

1//! An asynchronous runtime compatible with WebAssembly and non-WebAssembly targets.
2//!
3//! # Rationale
4//!
5//! When designing components and libraries that works on both WebAssembly targets backed by
6//! JavaScript Runtime and non-WebAssembly targets with Native Runtimes. Developers usually face
7//! challenges that requires applying multiple feature flags throughout their application:
8//!
9//! 1. Select I/O and timers that works with the target runtime.
10//! 2. Native Runtimes usually require `Send` futures and WebAssembly types are usually `!Send`.
11//!
12//! # Implementation
13//!
14//! To alleviate these issues, Tokise implements a single-threaded runtime that executes `?Send`
15//! (`Send` or `!Send`) futures.
16//!
17//! On platforms with multi-threading support, tokise spawns multiple independent runtimes
18//! proportional to the CPU core number. When tasks are spawned with a runtime handle, it will
19//! randomly select a worker thread from the internal pool. All tasks spawned with `spawn_local`
20//! will run on the same thread as the thread the task was running. When the runtime runs in a
21//! WebAssembly target, all tasks will be scheduled on the main thread.
22//!
23//! This runtime is designed in favour of IO-bounded workload with similar runtime cost.
24//! When running I/O workloads, it would produce a slightly better performance as tasks are
25//! never moved to another thread. However, If a worker thread is busy,
26//! other threads will not be able to steal tasks scheduled on the busy thread.
27//! When you have a CPU-bounded task where CPU time is significantly
28//! more expensive, it should be spawned with a dedicated thread (or Web Worker) and communicates
29//! with the application using channels.
30//!
31//! Tokise provides the following components:
32//!
33//! 1. A Task Scheduler that is capable of running non-Send tasks.
34//! 2. A Timer that is compatible with the scheduler backend.
35//! 3. Task Synchronisation Mechanisms.
36//!
37//! # Runtime Backend
38//!
39//! Tokise runtime is implemented with different runtimes depending on the target platform and can
40//! use all features (timers / IO / task synchronisation) from the selected native runtime:
41//!
42//! - `wasm-bindgen-futures` (WebAssembly targets)
43//! - `tokio` (non-WebAssembly targets)
44
45#![cfg_attr(documenting, feature(doc_cfg))]
46#![cfg_attr(documenting, feature(doc_auto_cfg))]
47#![deny(
48    missing_docs,
49    missing_debug_implementations,
50    bare_trait_objects,
51    anonymous_parameters,
52    elided_lifetimes_in_paths
53)]
54
55pub mod fmt;
56pub mod pinned;
57pub mod time;
58
59#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
60#[path = "rt_wasm_bindgen/mod.rs"]
61mod imp;
62#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
63#[path = "rt_tokio/mod.rs"]
64mod imp;
65
66mod runtime;
67pub use runtime::*;