#![allow(clippy::missing_errors_doc)]
use core::sync::atomic::{AtomicU8, Ordering};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
pub enum CooperativeRouting {
#[default]
Auto,
ForceTree,
ForceMailbox,
ForceDeque,
}
const TAG_AUTO: u8 = 0;
const TAG_FORCE_TREE: u8 = 1;
const TAG_FORCE_MAILBOX: u8 = 2;
const TAG_FORCE_DEQUE: u8 = 3;
static ACTIVE_COOPERATIVE_TAG: AtomicU8 = AtomicU8::new(TAG_AUTO);
#[unsafe(no_mangle)]
pub static __flynnel_marker_adaptive_cooperative: u8 = 0;
#[inline]
pub fn active_cooperative_routing() -> CooperativeRouting {
match ACTIVE_COOPERATIVE_TAG.load(Ordering::Acquire) {
TAG_FORCE_TREE => CooperativeRouting::ForceTree,
TAG_FORCE_MAILBOX => CooperativeRouting::ForceMailbox,
TAG_FORCE_DEQUE => CooperativeRouting::ForceDeque,
_ => CooperativeRouting::Auto,
}
}
#[inline]
pub fn migrate_cooperative_routing(routing: CooperativeRouting) {
let tag = match routing {
CooperativeRouting::Auto => TAG_AUTO,
CooperativeRouting::ForceTree => TAG_FORCE_TREE,
CooperativeRouting::ForceMailbox => TAG_FORCE_MAILBOX,
CooperativeRouting::ForceDeque => TAG_FORCE_DEQUE,
};
ACTIVE_COOPERATIVE_TAG.store(tag, Ordering::Release);
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Mutex, MutexGuard, OnceLock};
fn global_test_lock() -> MutexGuard<'static, ()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
.lock()
.expect("global_test_lock poisoned by prior test panic")
}
struct TestGuard {
_lock: MutexGuard<'static, ()>,
}
impl TestGuard {
fn new() -> Self {
let lock = global_test_lock();
migrate_cooperative_routing(CooperativeRouting::Auto);
Self { _lock: lock }
}
}
impl Drop for TestGuard {
fn drop(&mut self) {
migrate_cooperative_routing(CooperativeRouting::Auto);
}
}
#[test]
fn default_active_routing_is_auto() {
let _guard = TestGuard::new();
assert_eq!(active_cooperative_routing(), CooperativeRouting::Auto);
}
#[test]
fn migration_changes_active_routing() {
let _guard = TestGuard::new();
migrate_cooperative_routing(CooperativeRouting::ForceTree);
assert_eq!(active_cooperative_routing(), CooperativeRouting::ForceTree);
migrate_cooperative_routing(CooperativeRouting::ForceMailbox);
assert_eq!(
active_cooperative_routing(),
CooperativeRouting::ForceMailbox
);
migrate_cooperative_routing(CooperativeRouting::ForceDeque);
assert_eq!(
active_cooperative_routing(),
CooperativeRouting::ForceDeque
);
migrate_cooperative_routing(CooperativeRouting::Auto);
assert_eq!(active_cooperative_routing(), CooperativeRouting::Auto);
}
#[test]
fn migration_propagates_across_threads() {
let _guard = TestGuard::new();
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
migrate_cooperative_routing(CooperativeRouting::ForceTree);
let observed_tree = Arc::new(AtomicBool::new(false));
let observed_mailbox = Arc::new(AtomicBool::new(false));
let ot = Arc::clone(&observed_tree);
let om = Arc::clone(&observed_mailbox);
let deadline = Instant::now() + Duration::from_millis(500);
let producer = thread::spawn(move || {
while Instant::now() < deadline {
match active_cooperative_routing() {
CooperativeRouting::ForceTree => {
ot.store(true, Ordering::Relaxed);
}
CooperativeRouting::ForceMailbox => {
om.store(true, Ordering::Relaxed);
}
_ => {}
}
if ot.load(Ordering::Relaxed) && om.load(Ordering::Relaxed) {
return;
}
std::hint::spin_loop();
}
});
std::thread::sleep(Duration::from_millis(5));
migrate_cooperative_routing(CooperativeRouting::ForceMailbox);
producer.join().expect("producer thread should not panic");
assert!(observed_tree.load(Ordering::Relaxed), "producer never saw ForceTree");
assert!(
observed_mailbox.load(Ordering::Relaxed),
"producer never saw ForceMailbox after migration"
);
}
#[test]
fn default_routing_value_is_auto() {
assert_eq!(CooperativeRouting::default(), CooperativeRouting::Auto);
}
}