some_global_executor

Cross-platform global executor implementation for the some_executor framework.
This crate provides a thread pool-based executor that works seamlessly on both standard platforms and WebAssembly (WASM) targets. It implements the SomeExecutor trait from the some_executor framework and provides efficient task scheduling with configurable parallelism.
Architecture Overview
The executor uses platform-specific implementations through the sys module:
- Standard platforms: Uses OS threads with
crossbeam-channelfor task distribution - WebAssembly: Uses web workers for parallelism in browser environments
The platform abstraction is completely transparent to users - the same API works across all supported platforms.
Key Features
- Cross-platform support: Automatic platform detection and optimal implementation selection
- Dynamic thread pools: Create executors with custom thread counts and resize them at runtime
- Task observation: Monitor task execution state through the observer pattern
- Graceful shutdown: Both synchronous and asynchronous draining of pending tasks
- Global executor support: Set executors as global or thread-local defaults
- Zero-cost abstractions: Platform-specific code is conditionally compiled
Usage Patterns
Basic Task Spawning
The most common use case is creating an executor and spawning tasks:
use Executor;
use SomeExecutor;
use ;
// Create an executor with 4 worker threads
let mut executor = new;
// Spawn a simple async task
let task = without_notifications;
let observer = executor.spawn;
// Task is now running in the background on one of the worker threads
// Wait for all tasks to complete before shutting down
executor.drain;
Observing Task Progress
Tasks can be observed to monitor their execution state:
use Executor;
use SomeExecutor;
use ;
use ;
let mut executor = new;
let task = without_notifications;
let observer = executor.spawn;
// Poll the observer to check task state
loop
executor.drain;
Global Executor Pattern
Set an executor as the global default for the application:
use Executor;
// Create and configure the global executor
let executor = new;
executor.set_as_global_executor;
// Now tasks can be spawned using the global executor from anywhere
// in the application without passing executor references
Dynamic Thread Pool Management
Adjust executor capacity based on workload:
use Executor;
let mut executor = new;
// Scale up for heavy workload
executor.resize;
// Scale down during idle periods
executor.resize;
executor.drain;
Performance Considerations
- Thread pool sizing: Default to
num_cpus::get()for CPU-bound work - For I/O-bound tasks, consider using more threads than CPU cores
- WASM targets have platform-specific limitations on parallelism
- Use
drain_async()in async contexts to avoid blocking
Logging
This crate uses the logwise framework for structured logging. Internal operations are logged at various levels for debugging and monitoring:
// Executor creation and operations are automatically logged
let executor = new;
// Logs: "Creating executor with name logged-executor and 4 threads"
Build and Development
Standard Testing
# Run all tests
# Run tests with output
WebAssembly Testing
# Run WASM tests with special flags for atomics support
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER="wasm-bindgen-test-runner" \
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
Documentation
# Generate documentation with warnings
RUSTDOCFLAGS="-D warnings"
# Open documentation in browser
Dependencies
The project uses several local dependencies that may be patched in Cargo.toml:
logwise: Logging frameworksome_executor: Core executor traitstest_executors: Testing utilitieswasm_safe_mutex: WASM-safe mutex implementation
License
MIT or Apache-2.0, at your option.