Expand description
Utility functions for simulation testing and cancellation management.
This crate provides utilities for managing worker threads and cancellation tokens in simulation environments. It supports both thread-local and global cancellation, allowing tests to gracefully terminate simulations and async operations.
§Features
- Thread Management: Unique worker thread ID tracking
- Cancellation Tokens: Thread-local and global cancellation support
- Async Utilities: Run futures until simulation cancellation
§Example
use simvar_utils::{worker_thread_id, run_until_simulation_cancelled};
// Get unique thread ID
let thread_id = worker_thread_id();
println!("Worker thread ID: {}", thread_id);
// Run future until cancelled
let result = run_until_simulation_cancelled(async {
simulate_work().await
}).await;
match result {
Some(output) => println!("Completed: {}", output),
None => println!("Cancelled"),
}Functions§
- cancel_
global_ simulation - Cancels all simulations globally.
- cancel_
simulation - Cancels the current thread’s simulation.
- is_
global_ simulator_ cancelled - Checks if the global simulation has been cancelled.
- is_
simulator_ cancelled - Checks if the current thread’s simulation has been cancelled.
- reset_
global_ simulator_ cancellation_ token - Resets the global simulation cancellation token.
- reset_
simulator_ cancellation_ token - Resets the thread-local simulation cancellation token.
- run_
until_ simulation_ cancelled - Runs a future until it completes or simulation is cancelled.
- worker_
thread_ id - Returns the unique identifier for the current worker thread.