#![warn(missing_docs)]
use std::marker::PhantomData;
use std::sync::Arc;
use std::thread::JoinHandle;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::{mpsc, oneshot};
use tokio::task::LocalSet;
#[derive(Debug)]
pub struct SendError<T>(pub T);
impl<T> SendError<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> std::fmt::Display for SendError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("task receiver closed")
}
}
impl<T: std::fmt::Debug> std::error::Error for SendError<T> {}
#[derive(Debug)]
#[non_exhaustive]
pub enum TrySendError<T> {
Full(T),
Closed(T),
}
impl<T> TrySendError<T> {
pub fn into_inner(self) -> T {
match self {
Self::Full(t) | Self::Closed(t) => t,
}
}
}
impl<T> std::fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Full(_) => f.write_str("channel full"),
Self::Closed(_) => f.write_str("task receiver closed"),
}
}
}
impl<T: std::fmt::Debug> std::error::Error for TrySendError<T> {}
#[derive(Debug)]
pub struct SpawnError;
impl std::fmt::Display for SpawnError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("reactor background thread has shut down")
}
}
impl std::error::Error for SpawnError {}
pub trait Task<Ctx>: 'static {
fn handle(&mut self, ctx: Ctx) -> impl std::future::Future<Output = ()>;
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Capacity {
Bounded(usize),
Unbounded,
}
pub(crate) enum DynSender<T> {
Bounded(mpsc::Sender<T>),
Unbounded(mpsc::UnboundedSender<T>),
}
impl<T> Clone for DynSender<T> {
fn clone(&self) -> Self {
match self {
Self::Bounded(s) => Self::Bounded(s.clone()),
Self::Unbounded(s) => Self::Unbounded(s.clone()),
}
}
}
impl<T> DynSender<T> {
fn try_send(&self, value: T) -> Result<(), TrySendError<T>> {
match self {
Self::Bounded(s) => s.try_send(value).map_err(|e| match e {
mpsc::error::TrySendError::Full(v) => TrySendError::Full(v),
mpsc::error::TrySendError::Closed(v) => TrySendError::Closed(v),
}),
Self::Unbounded(s) => s.send(value).map_err(|e| TrySendError::Closed(e.0)),
}
}
async fn send_async(&self, value: T) -> Result<(), SendError<T>> {
match self {
Self::Bounded(s) => s.send(value).await.map_err(|e| SendError(e.0)),
Self::Unbounded(s) => s.send(value).map_err(|e| SendError(e.0)),
}
}
}
#[non_exhaustive]
pub enum DynReceiver<T> {
#[non_exhaustive]
Bounded(mpsc::Receiver<T>),
#[non_exhaustive]
Unbounded(mpsc::UnboundedReceiver<T>),
}
impl<T> DynReceiver<T> {
pub fn try_recv(&mut self) -> Result<T, mpsc::error::TryRecvError> {
match self {
Self::Bounded(r) => r.try_recv(),
Self::Unbounded(r) => r.try_recv(),
}
}
pub fn poll_recv(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Option<T>> {
match self {
Self::Bounded(r) => r.poll_recv(cx),
Self::Unbounded(r) => r.poll_recv(cx),
}
}
}
fn make_channel<T>(cap: Capacity) -> (DynSender<T>, DynReceiver<T>) {
match cap {
Capacity::Bounded(n) => {
let (tx, rx) = mpsc::channel(n.max(1));
(DynSender::Bounded(tx), DynReceiver::Bounded(rx))
}
Capacity::Unbounded => {
let (tx, rx) = mpsc::unbounded_channel();
(DynSender::Unbounded(tx), DynReceiver::Unbounded(rx))
}
}
}
pub struct Channel<Ctx> {
idx: usize,
_marker: PhantomData<fn(Ctx)>,
}
impl<Ctx> Clone for Channel<Ctx> {
fn clone(&self) -> Self {
*self
}
}
impl<Ctx> Copy for Channel<Ctx> {}
impl<Ctx> std::fmt::Debug for Channel<Ctx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Channel").field("idx", &self.idx).finish()
}
}
#[derive(Clone, Copy, Debug)]
struct ChannelSpec {
priority: u8,
capacity: Capacity,
}
pub struct TaskBuilder<Ctx> {
channels: Vec<ChannelSpec>,
_marker: PhantomData<fn(Ctx)>,
}
impl<Ctx> Default for TaskBuilder<Ctx> {
fn default() -> Self {
Self::new()
}
}
impl<Ctx> TaskBuilder<Ctx> {
pub fn new() -> Self {
Self {
channels: Vec::new(),
_marker: PhantomData,
}
}
pub fn add_channel(&mut self, priority: u8, capacity: Capacity) -> Channel<Ctx> {
let idx = self.channels.len();
self.channels.push(ChannelSpec { priority, capacity });
Channel {
idx,
_marker: PhantomData,
}
}
pub fn add_unbounded(&mut self, priority: u8) -> Channel<Ctx> {
self.add_channel(priority, Capacity::Unbounded)
}
pub fn add_bounded(&mut self, priority: u8, capacity: usize) -> Channel<Ctx> {
self.add_channel(priority, Capacity::Bounded(capacity))
}
pub fn channel_count(&self) -> usize {
self.channels.len()
}
}
pub struct TaskHandler<Ctx> {
senders: Arc<[DynSender<Ctx>]>,
}
impl<Ctx> Clone for TaskHandler<Ctx> {
fn clone(&self) -> Self {
Self {
senders: self.senders.clone(),
}
}
}
impl<Ctx> TaskHandler<Ctx> {
pub fn try_send(&self, ch: Channel<Ctx>, ctx: Ctx) -> Result<(), TrySendError<Ctx>> {
self.senders[ch.idx].try_send(ctx)
}
pub fn send(&self, ch: Channel<Ctx>, ctx: Ctx) -> Result<(), TrySendError<Ctx>> {
self.try_send(ch, ctx)
}
pub async fn send_async(&self, ch: Channel<Ctx>, ctx: Ctx) -> Result<(), SendError<Ctx>> {
self.senders[ch.idx].send_async(ctx).await
}
pub fn channel_count(&self) -> usize {
self.senders.len()
}
}
pub trait Scheduler<Ctx>: Send + 'static {
fn next_ctx<'a>(
&'a mut self,
rxs: &'a mut [DynReceiver<Ctx>],
priorities: &'a [u8],
) -> impl std::future::Future<Output = Option<Ctx>> + 'a;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct StrictPriority;
impl<Ctx> Scheduler<Ctx> for StrictPriority {
async fn next_ctx<'a>(
&'a mut self,
rxs: &'a mut [DynReceiver<Ctx>],
priorities: &'a [u8],
) -> Option<Ctx> {
let order = sort_order(priorities);
if let Some(v) = try_recv_in_order(rxs, &order) {
return Some(v);
}
await_any(rxs, &order).await.map(|(v, _)| v)
}
}
#[derive(Clone, Copy, Debug)]
pub struct FairPriority {
budget: u32,
top_streak: u32,
}
impl FairPriority {
pub fn new(budget: u32) -> Self {
Self {
budget,
top_streak: 0,
}
}
pub fn budget(&self) -> u32 {
self.budget
}
}
impl Default for FairPriority {
fn default() -> Self {
Self::new(16)
}
}
impl<Ctx> Scheduler<Ctx> for FairPriority {
async fn next_ctx<'a>(
&'a mut self,
rxs: &'a mut [DynReceiver<Ctx>],
priorities: &'a [u8],
) -> Option<Ctx> {
let order = sort_order(priorities);
if order.is_empty() {
return None;
}
let top_idx = order[0];
if self.top_streak >= self.budget {
self.top_streak = 0;
if let Some(v) = try_recv_lowest_nonempty(rxs, &order) {
return Some(v);
}
}
if let Some((v, idx)) = try_recv_in_order_tagged(rxs, &order) {
self.top_streak = if idx == top_idx {
self.top_streak.saturating_add(1)
} else {
0
};
return Some(v);
}
match await_any(rxs, &order).await {
Some((v, is_top)) => {
self.top_streak = if is_top {
self.top_streak.saturating_add(1)
} else {
0
};
Some(v)
}
None => None,
}
}
}
fn sort_order(priorities: &[u8]) -> Vec<usize> {
let mut order: Vec<usize> = (0..priorities.len()).collect();
order.sort_by_key(|&i| priorities[i]);
order
}
fn try_recv_in_order<Ctx>(rxs: &mut [DynReceiver<Ctx>], order: &[usize]) -> Option<Ctx> {
for &idx in order {
if let Ok(v) = rxs[idx].try_recv() {
return Some(v);
}
}
None
}
fn try_recv_in_order_tagged<Ctx>(
rxs: &mut [DynReceiver<Ctx>],
order: &[usize],
) -> Option<(Ctx, usize)> {
for &idx in order {
if let Ok(v) = rxs[idx].try_recv() {
return Some((v, idx));
}
}
None
}
fn try_recv_lowest_nonempty<Ctx>(rxs: &mut [DynReceiver<Ctx>], order: &[usize]) -> Option<Ctx> {
for &idx in order.iter().rev() {
if let Ok(v) = rxs[idx].try_recv() {
return Some(v);
}
}
None
}
async fn await_any<Ctx>(rxs: &mut [DynReceiver<Ctx>], order: &[usize]) -> Option<(Ctx, bool)> {
use std::future::poll_fn;
use std::task::Poll;
if order.is_empty() {
return None;
}
let top = order[0];
poll_fn(|cx| {
let mut all_closed = true;
for &idx in order {
match rxs[idx].poll_recv(cx) {
Poll::Ready(Some(v)) => return Poll::Ready(Some((v, idx == top))),
Poll::Ready(None) => { }
Poll::Pending => all_closed = false,
}
}
if all_closed {
Poll::Ready(None)
} else {
Poll::Pending
}
})
.await
}
type SpawnRequest = Box<dyn FnOnce() + Send>;
pub struct Reactor<Ctx, T> {
ctrl: Option<mpsc::UnboundedSender<SpawnRequest>>,
thread: Option<JoinHandle<()>>,
_marker: PhantomData<fn(Ctx, T)>,
}
impl<Ctx, T> Reactor<Ctx, T>
where
Ctx: Send + 'static,
T: Task<Ctx> + Send + 'static,
{
pub fn new_current() -> std::io::Result<Self> {
Self::new(None)
}
pub fn new(runtime: Option<Arc<Runtime>>) -> std::io::Result<Self> {
let (ctrl_tx, mut ctrl_rx) = mpsc::unbounded_channel::<SpawnRequest>();
let rt = match runtime {
Some(r) => r,
None => Arc::new(Builder::new_current_thread().enable_all().build()?),
};
let thread = std::thread::Builder::new()
.name("hyperfile-reactor".into())
.spawn(move || {
let local = LocalSet::new();
local.spawn_local(async move {
while let Some(req) = ctrl_rx.recv().await {
req();
}
});
rt.block_on(local);
})?;
Ok(Self {
ctrl: Some(ctrl_tx),
thread: Some(thread),
_marker: PhantomData,
})
}
pub fn spawn(
&self,
task: T,
builder: TaskBuilder<Ctx>,
) -> Result<TaskHandler<Ctx>, SpawnError> {
self.spawn_with(task, builder, FairPriority::default())
}
pub fn spawn_with<S>(
&self,
task: T,
builder: TaskBuilder<Ctx>,
scheduler: S,
) -> Result<TaskHandler<Ctx>, SpawnError>
where
S: Scheduler<Ctx>,
{
let rx = self.submit(task, builder, scheduler)?;
rx.blocking_recv().map_err(|_| SpawnError)
}
pub async fn spawn_async(
&self,
task: T,
builder: TaskBuilder<Ctx>,
) -> Result<TaskHandler<Ctx>, SpawnError> {
self.spawn_with_async(task, builder, FairPriority::default())
.await
}
pub async fn spawn_with_async<S>(
&self,
task: T,
builder: TaskBuilder<Ctx>,
scheduler: S,
) -> Result<TaskHandler<Ctx>, SpawnError>
where
S: Scheduler<Ctx>,
{
let rx = self.submit(task, builder, scheduler)?;
rx.await.map_err(|_| SpawnError)
}
fn submit<S>(
&self,
task: T,
builder: TaskBuilder<Ctx>,
scheduler: S,
) -> Result<oneshot::Receiver<TaskHandler<Ctx>>, SpawnError>
where
S: Scheduler<Ctx>,
{
let ctrl = self.ctrl.as_ref().ok_or(SpawnError)?;
let (tx, rx) = oneshot::channel::<TaskHandler<Ctx>>();
let req: SpawnRequest = Box::new(move || {
let handler = start_task(task, builder, scheduler);
let _ = tx.send(handler);
});
ctrl.send(req).map_err(|_| SpawnError)?;
Ok(rx)
}
pub fn shutdown(mut self) -> std::thread::Result<()> {
drop(self.ctrl.take());
if let Some(h) = self.thread.take() {
h.join()
} else {
Ok(())
}
}
}
impl<Ctx, T> Drop for Reactor<Ctx, T> {
fn drop(&mut self) {
drop(self.ctrl.take());
if let Some(h) = self.thread.take() {
let _ = h.join();
}
}
}
fn start_task<Ctx, T, S>(
mut task: T,
builder: TaskBuilder<Ctx>,
mut scheduler: S,
) -> TaskHandler<Ctx>
where
Ctx: Send + 'static,
T: Task<Ctx> + 'static,
S: Scheduler<Ctx>,
{
let cap_hint = builder.channels.len().max(1);
let mut senders: Vec<DynSender<Ctx>> = Vec::with_capacity(cap_hint);
let mut receivers: Vec<DynReceiver<Ctx>> = Vec::with_capacity(cap_hint);
let mut priorities: Vec<u8> = Vec::with_capacity(cap_hint);
if builder.channels.is_empty() {
let (tx, rx) = make_channel::<Ctx>(Capacity::Unbounded);
senders.push(tx);
receivers.push(rx);
priorities.push(0);
} else {
for spec in &builder.channels {
let (tx, rx) = make_channel::<Ctx>(spec.capacity);
senders.push(tx);
receivers.push(rx);
priorities.push(spec.priority);
}
}
let handler = TaskHandler {
senders: senders.into(),
};
tokio::task::spawn_local(async move {
while let Some(ctx) = scheduler.next_ctx(&mut receivers, &priorities).await {
task.handle(ctx).await;
tokio::task::consume_budget().await;
}
});
handler
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
use std::time::Duration;
fn run_with_gate<S>(channels: Vec<(u8, Capacity, Vec<u32>)>, scheduler: S) -> Vec<u32>
where
S: Scheduler<u32>,
{
use tokio::sync::Notify;
let log = Arc::new(Mutex::new(Vec::<u32>::new()));
struct Gated {
log: Arc<Mutex<Vec<u32>>>,
gate: Arc<Notify>,
gate_passed: bool,
}
impl Task<u32> for Gated {
async fn handle(&mut self, ctx: u32) {
if !self.gate_passed {
self.gate.notified().await;
self.gate_passed = true;
}
self.log.lock().unwrap().push(ctx);
}
}
let gate = Arc::new(Notify::new());
let task = Gated {
log: log.clone(),
gate: gate.clone(),
gate_passed: false,
};
let reactor = Reactor::<u32, Gated>::new_current().unwrap();
let mut builder = TaskBuilder::<u32>::new();
let tokens: Vec<Channel<u32>> = channels
.iter()
.map(|(prio, cap, _)| builder.add_channel(*prio, *cap))
.collect();
let handler = reactor.spawn_with(task, builder, scheduler).unwrap();
handler.send(tokens[0], u32::MAX).unwrap();
std::thread::sleep(Duration::from_millis(50));
for (i, (_prio, _cap, msgs)) in channels.iter().enumerate() {
for m in msgs {
handler.send(tokens[i], *m).unwrap();
}
}
gate.notify_one();
let expected: usize = 1 + channels.iter().map(|(_, _, m)| m.len()).sum::<usize>();
let deadline = std::time::Instant::now() + Duration::from_secs(5);
loop {
if log.lock().unwrap().len() >= expected {
break;
}
if std::time::Instant::now() >= deadline {
panic!(
"timeout: got {} / {} items",
log.lock().unwrap().len(),
expected
);
}
std::thread::sleep(Duration::from_millis(10));
}
drop(handler);
reactor.shutdown().unwrap();
let mut recorded = log.lock().unwrap().clone();
assert_eq!(recorded.remove(0), u32::MAX);
recorded
}
#[test]
fn fair_priority_order_default() {
let result = run_with_gate(
vec![
(0, Capacity::Unbounded, vec![10, 11, 12]),
(1, Capacity::Unbounded, vec![20, 21, 22]),
(2, Capacity::Unbounded, vec![30, 31, 32]),
],
FairPriority::default(),
);
assert_eq!(result, vec![10, 11, 12, 20, 21, 22, 30, 31, 32]);
}
#[test]
fn strict_priority_scheduler() {
let result = run_with_gate(
vec![
(0, Capacity::Unbounded, (0..50).collect()),
(2, Capacity::Unbounded, vec![999]),
],
StrictPriority,
);
let pos = result.iter().position(|&v| v == 999).unwrap();
assert_eq!(pos, 50, "low-prio must come last under StrictPriority");
}
#[test]
fn fair_priority_anti_starvation() {
let high: Vec<u32> = (0..40).map(|i| 1000 + i).collect();
let low = vec![9999_u32];
let result = run_with_gate(
vec![
(0, Capacity::Unbounded, high.clone()),
(2, Capacity::Unbounded, low),
],
FairPriority::new(16),
);
let pos = result.iter().position(|&v| v == 9999).unwrap();
assert!(
pos <= 16,
"FairPriority must relieve starvation within budget: pos={pos}"
);
let highs: Vec<u32> = result.iter().copied().filter(|&v| v != 9999).collect();
let mut sorted = highs.clone();
sorted.sort();
assert_eq!(sorted, high);
}
#[test]
fn bounded_channel_applies_backpressure() {
use tokio::sync::Notify;
struct Slow {
release: Arc<Notify>,
blocked_once: bool,
}
impl Task<u32> for Slow {
async fn handle(&mut self, _ctx: u32) {
if !self.blocked_once {
self.blocked_once = true;
self.release.notified().await;
}
}
}
let release = Arc::new(Notify::new());
let reactor = Reactor::<u32, Slow>::new_current().unwrap();
let mut builder = TaskBuilder::<u32>::new();
let ch = builder.add_bounded(0, 1);
let handler = reactor
.spawn(
Slow {
release: release.clone(),
blocked_once: false,
},
builder,
)
.unwrap();
handler.try_send(ch, 1).expect("send 1");
std::thread::sleep(Duration::from_millis(100));
handler.try_send(ch, 2).expect("send 2 fills buffer");
match handler.try_send(ch, 3) {
Err(TrySendError::Full(3)) => { }
other => panic!("expected Full(3), got {:?}", other.map(|_| ())),
}
release.notify_one();
drop(handler);
reactor.shutdown().expect("shutdown");
}
#[tokio::test(flavor = "current_thread")]
async fn send_async_awaits_capacity() {
struct Trickle {
log: Arc<Mutex<Vec<u32>>>,
}
impl Task<u32> for Trickle {
async fn handle(&mut self, ctx: u32) {
tokio::time::sleep(Duration::from_millis(20)).await;
self.log.lock().unwrap().push(ctx);
}
}
let log = Arc::new(Mutex::new(Vec::<u32>::new()));
let reactor = Reactor::<u32, Trickle>::new_current().unwrap();
let mut builder = TaskBuilder::<u32>::new();
let ch = builder.add_bounded(0, 1);
let handler = reactor
.spawn_async(Trickle { log: log.clone() }, builder)
.await
.unwrap();
for i in 0..5u32 {
handler.send_async(ch, i).await.unwrap();
}
let deadline = std::time::Instant::now() + Duration::from_secs(2);
loop {
if log.lock().unwrap().len() == 5 {
break;
}
if std::time::Instant::now() >= deadline {
panic!("didn't drain in time");
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(*log.lock().unwrap(), vec![0, 1, 2, 3, 4]);
drop(handler);
tokio::task::spawn_blocking(move || reactor.shutdown().unwrap())
.await
.unwrap();
}
#[test]
fn shutdown_is_clean() {
struct Noop;
impl Task<()> for Noop {
async fn handle(&mut self, _ctx: ()) {}
}
let reactor = Reactor::<(), Noop>::new_current().unwrap();
let mut builder = TaskBuilder::<()>::new();
let ch = builder.add_unbounded(0);
let handler = reactor.spawn(Noop, builder).unwrap();
for _ in 0..10 {
handler.send(ch, ()).unwrap();
}
drop(handler);
reactor.shutdown().expect("shutdown clean");
}
#[test]
fn handler_resubmit_does_not_starve_spawned_timer() {
use tokio::sync::Notify;
enum Msg {
Init {
handler: TaskHandler<Msg>,
ch: Channel<Msg>,
},
Start,
Retry,
Done,
}
struct Worker {
handler: Option<TaskHandler<Msg>>,
ch: Option<Channel<Msg>>,
armed: bool,
done: Arc<Notify>,
}
impl Task<Msg> for Worker {
async fn handle(&mut self, msg: Msg) {
match msg {
Msg::Init { handler, ch } => {
self.handler = Some(handler);
self.ch = Some(ch);
}
Msg::Start => {
let h = self.handler.clone().unwrap();
let ch = self.ch.unwrap();
if !self.armed {
self.armed = true;
let h_for_timer = h.clone();
tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
let _ = h_for_timer.send(ch, Msg::Done);
});
}
let _ = h.send(ch, Msg::Retry);
}
Msg::Retry => {
let h = self.handler.as_ref().unwrap();
let ch = self.ch.unwrap();
let _ = h.send(ch, Msg::Retry);
}
Msg::Done => {
self.done.notify_one();
}
}
}
}
let done = Arc::new(Notify::new());
let reactor = Reactor::<Msg, Worker>::new_current().unwrap();
let mut builder = TaskBuilder::<Msg>::new();
let cb = builder.add_channel(0, Capacity::Unbounded);
let ext = builder.add_channel(1, Capacity::Unbounded);
let handler = reactor
.spawn(
Worker {
handler: None,
ch: None,
armed: false,
done: done.clone(),
},
builder,
)
.unwrap();
handler
.send(
ext,
Msg::Init {
handler: handler.clone(),
ch: cb,
},
)
.ok()
.expect("init send");
handler.send(ext, Msg::Start).ok().expect("start send");
let waited = std::thread::scope(|s| {
let done = done.clone();
let h = s.spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
tokio::time::timeout(Duration::from_secs(2), done.notified())
.await
.is_ok()
})
});
h.join().unwrap()
});
assert!(
waited,
"spawned timer did not fire within 2 s: scheduler is starving the runtime"
);
std::mem::forget(handler);
std::mem::forget(reactor);
}
}