#![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(ACTIVE_COOPERATIVE_TAG_DEFAULT);
const ACTIVE_COOPERATIVE_TAG_DEFAULT: u8 = TAG_AUTO;
#[unsafe(no_mangle)]
pub static __flynnel_marker_adaptive_cooperative: u8 = 0;
#[inline]
pub fn active_cooperative_routing() -> CooperativeRouting {
routing_of_tag(ACTIVE_COOPERATIVE_TAG.load(Ordering::Acquire))
}
#[inline]
const fn routing_of_tag(tag: u8) -> CooperativeRouting {
match tag {
TAG_FORCE_TREE => CooperativeRouting::ForceTree,
TAG_FORCE_MAILBOX => CooperativeRouting::ForceMailbox,
TAG_FORCE_DEQUE => CooperativeRouting::ForceDeque,
_ => CooperativeRouting::Auto,
}
}
#[inline]
const fn tag_of_routing(routing: CooperativeRouting) -> u8 {
match routing {
CooperativeRouting::Auto => TAG_AUTO,
CooperativeRouting::ForceTree => TAG_FORCE_TREE,
CooperativeRouting::ForceMailbox => TAG_FORCE_MAILBOX,
CooperativeRouting::ForceDeque => TAG_FORCE_DEQUE,
}
}
#[inline]
pub fn migrate_cooperative_routing(routing: CooperativeRouting) {
ACTIVE_COOPERATIVE_TAG.store(tag_of_routing(routing), Ordering::Release);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_routing_survives_its_tag_and_the_cell_starts_auto() {
for routing in [
CooperativeRouting::Auto,
CooperativeRouting::ForceTree,
CooperativeRouting::ForceMailbox,
CooperativeRouting::ForceDeque,
] {
assert_eq!(routing_of_tag(tag_of_routing(routing)), routing);
}
assert_eq!(
routing_of_tag(ACTIVE_COOPERATIVE_TAG_DEFAULT),
CooperativeRouting::Auto
);
assert_eq!(routing_of_tag(200), CooperativeRouting::Auto);
}
#[test]
fn migration_propagates_across_threads() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
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_secs(10);
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();
}
});
let handshake = Instant::now() + Duration::from_secs(10);
while !observed_tree.load(Ordering::Relaxed) && Instant::now() < handshake {
std::hint::spin_loop();
}
assert!(
observed_tree.load(Ordering::Relaxed),
"producer never saw ForceTree in 10 s, so the migration did not reach another thread"
);
migrate_cooperative_routing(CooperativeRouting::ForceMailbox);
producer.join().expect("producer thread should not panic");
assert!(
observed_mailbox.load(Ordering::Relaxed),
"producer never saw ForceMailbox after migration"
);
migrate_cooperative_routing(CooperativeRouting::Auto);
}
#[test]
fn default_routing_value_is_auto() {
assert_eq!(CooperativeRouting::default(), CooperativeRouting::Auto);
}
}