use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use super::capacity::{CapacityRefusal, OwnerIdentity, ProcessCapacityPool};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IncarnationIdentity(std::num::NonZeroU64);
static NEXT_INCARNATION: AtomicU64 = AtomicU64::new(1);
impl IncarnationIdentity {
pub fn fresh() -> Self {
let raw = NEXT_INCARNATION.fetch_add(1, Ordering::Relaxed);
Self(std::num::NonZeroU64::new(raw).expect("incarnation counter starts at 1"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SurfaceKind {
Daemon,
Stdio,
Serve,
Embed,
}
impl SurfaceKind {
pub const ALL: [SurfaceKind; 4] = [Self::Daemon, Self::Stdio, Self::Serve, Self::Embed];
pub fn name(self) -> &'static str {
match self {
Self::Daemon => "daemon",
Self::Stdio => "stdio",
Self::Serve => "serve",
Self::Embed => "embed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RuntimeRefusal {
Capacity(CapacityRefusal),
SurfaceAlreadyRegistered {
surface: SurfaceKind,
},
SurfaceNotRegistered {
surface: SurfaceKind,
},
}
#[derive(Debug)]
pub struct ProcessIndexRuntime {
incarnation: IncarnationIdentity,
ledger: Arc<ProcessCapacityPool>,
root: OwnerIdentity,
surfaces: std::sync::Mutex<HashMap<SurfaceKind, OwnerIdentity>>,
}
impl ProcessIndexRuntime {
pub fn incarnate(process_bytes: u64) -> Arc<Self> {
let ledger = ProcessCapacityPool::new();
let root = ledger.root(process_bytes);
Arc::new(Self {
incarnation: IncarnationIdentity::fresh(),
ledger,
root,
surfaces: std::sync::Mutex::new(HashMap::new()),
})
}
pub fn incarnation(&self) -> IncarnationIdentity {
self.incarnation
}
pub fn ledger(&self) -> &Arc<ProcessCapacityPool> {
&self.ledger
}
pub fn root_owner(&self) -> OwnerIdentity {
self.root
}
pub fn attach(
self: &Arc<Self>,
surface: SurfaceKind,
bytes: u64,
) -> Result<OwnerIdentity, RuntimeRefusal> {
let mut surfaces = self.surfaces.lock().expect("process runtime mutex");
if surfaces.contains_key(&surface) {
return Err(RuntimeRefusal::SurfaceAlreadyRegistered { surface });
}
let owner = self
.ledger
.child(self.root, bytes)
.map_err(RuntimeRefusal::Capacity)?;
surfaces.insert(surface, owner);
Ok(owner)
}
pub fn detach(self: &Arc<Self>, surface: SurfaceKind) -> Result<u64, RuntimeRefusal> {
let mut surfaces = self.surfaces.lock().expect("process runtime mutex");
let owner = *surfaces
.get(&surface)
.ok_or(RuntimeRefusal::SurfaceNotRegistered { surface })?;
let returned = self
.ledger
.release_owner(owner)
.map_err(RuntimeRefusal::Capacity)?;
surfaces.remove(&surface);
Ok(returned)
}
pub fn owner_for(&self, surface: SurfaceKind) -> Option<OwnerIdentity> {
self.surfaces
.lock()
.expect("process runtime mutex")
.get(&surface)
.copied()
}
pub fn attached(&self) -> Vec<SurfaceKind> {
let surfaces = self.surfaces.lock().expect("process runtime mutex");
let mut names: Vec<SurfaceKind> = surfaces.keys().copied().collect();
names.sort();
names
}
pub fn available(&self) -> u64 {
self.ledger.available(self.root)
}
}