mod blocks;
mod cases;
mod facts;
mod generated;
mod outcomes;
mod programs;
use crate::analysis::Analysis;
use blocks::Blocks;
use serde::Deserialize;
use serde_json::Value;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};
use std::time::Duration;
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct Snapshots {
#[serde(default)]
pub(crate) dir: Option<String>,
pub(crate) data: Value,
}
pub(crate) struct Limits {
pub(crate) timeout_ms: f64,
pub(crate) workers: usize,
pub(crate) network: bool,
}
pub(crate) struct Runner {
analysis: Arc<Analysis>,
limits: Limits,
snapshots: Snapshots,
manifests: Value,
slots: Slots,
blocks: Blocks,
cases: OnceLock<programs::Built>,
building: Mutex<()>,
}
impl Runner {
pub(crate) fn new(
analysis: Arc<Analysis>,
limits: Limits,
snapshots: Snapshots,
manifests: Value,
) -> Self {
Runner {
analysis,
slots: Slots { free: Mutex::new(limits.workers.max(1)), freed: Condvar::new() },
limits,
snapshots,
manifests,
blocks: Blocks::default(),
cases: OnceLock::new(),
building: Mutex::new(()),
}
}
pub(crate) fn wait_for_build(&self) {
drop(self.one_build());
}
fn one_build(&self) -> MutexGuard<'_, ()> {
self.building.lock().unwrap_or_else(|p| p.into_inner())
}
fn timeout(&self) -> Duration {
Duration::from_secs_f64(self.limits.timeout_ms / 1000.0)
}
fn timed_out(&self) -> String {
format!("timed out after {} ms", self.limits.timeout_ms)
}
fn running<T>(&self, program: impl FnOnce() -> T) -> T {
let _slot = self.slots.take();
program()
}
}
struct Slots {
free: Mutex<usize>,
freed: Condvar,
}
impl Slots {
fn take(&self) -> Slot<'_> {
let mut free = self.free.lock().unwrap_or_else(|p| p.into_inner());
while *free == 0 {
free = self.freed.wait(free).unwrap_or_else(|p| p.into_inner());
}
*free -= 1;
Slot { slots: self }
}
}
struct Slot<'a> {
slots: &'a Slots,
}
impl Drop for Slot<'_> {
fn drop(&mut self) {
*self.slots.free.lock().unwrap_or_else(|p| p.into_inner()) += 1;
self.slots.freed.notify_one();
}
}