#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
use atomic_counter::RelaxedCounter;
use crate::worker::{run, Advancer, LockedTaskHeap};
use std::cmp::Ordering;
use parking_lot::Mutex;
use std::collections::VecDeque;
use std::fmt::Debug;
use std::sync::Arc;
use std::thread;
pub mod engine;
pub mod err;
pub mod network;
pub mod phold;
pub mod spsc;
pub mod worker;
pub use self::err::{PopError, PushError};
#[derive(Debug)]
pub struct FrozenActor<T, R>
where
T: Ord + Copy + num::Zero,
{
time: T,
actor: Box<dyn Advancer<T, R> + Send>,
}
impl<T, R> Ord for FrozenActor<T, R>
where
T: Ord + Copy + num::Zero,
{
fn cmp(&self, other: &Self) -> Ordering {
other.time.cmp(&self.time)
}
}
impl<T, R> PartialOrd for FrozenActor<T, R>
where
T: Ord + Copy + num::Zero,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(other.time.cmp(&self.time))
}
}
impl<T, R> PartialEq for FrozenActor<T, R>
where
T: Ord + Copy + num::Zero,
{
fn eq(&self, other: &Self) -> bool {
self.time == other.time
}
}
impl<T, R> Eq for FrozenActor<T, R> where T: Ord + Copy + num::Zero {}
pub fn start<T: 'static + Ord + Copy + Debug + Send + num::Zero, R: 'static + Send + Copy>(
num_cpus: usize,
mut actors: Vec<Box<dyn Advancer<T, R> + Send>>,
) -> Vec<R> {
let n_actors = actors.len();
let shared_counter = Arc::new(RelaxedCounter::new(0));
let n_heaps = 8;
let mut heaps = Vec::new();
for _ in 0..n_heaps {
let task_heap: LockedTaskHeap<T, R> = Arc::new(Mutex::new(VecDeque::new()));
heaps.push(task_heap);
}
for (i, actor) in actors.drain(..).enumerate() {
let heap_ix = i % n_heaps;
let frozen = FrozenActor {
time: T::zero(),
actor,
};
heaps[heap_ix].lock().push_back(frozen);
}
let mut handles = Vec::new();
for i in 0..num_cpus {
handles.push({
let cloned_heaps = heaps.iter().map(|x| Arc::clone(&x)).collect();
let counter_clone = Arc::clone(&shared_counter);
thread::spawn(move || run(i, counter_clone, n_actors, cloned_heaps))
});
}
let mut counts = Vec::new();
for h in handles {
let local_counts: Vec<R> = h.join().unwrap();
counts.extend(local_counts);
}
counts
}