tasks.rs (1952B)
1 //! Mechanical ownership of already bounded execution tasks. 2 use super::{HostBound, HostFuture}; 3 4 #[cfg(feature = "workspace_session")] 5 use super::host::Shared; 6 #[cfg(feature = "workspace_session")] 7 use std::sync::atomic::{AtomicUsize, Ordering}; 8 9 #[cfg(feature = "workspace_session")] 10 const LIVE_MAX_BYTES: usize = 4 * 1024 * 1024; 11 #[cfg(feature = "workspace_session")] 12 const INQUIRY_RESERVATION_BYTES: usize = 2 * 1024 * 1024; 13 14 #[cfg(feature = "workspace_session")] 15 #[derive(Clone)] 16 pub(crate) struct TaskBudget(Shared<AtomicUsize>); 17 #[cfg(feature = "workspace_session")] 18 impl TaskBudget { 19 pub(crate) fn new() -> Self { 20 Self(Shared::new(AtomicUsize::new(0))) 21 } 22 pub(crate) fn reserve(&self) -> crate::Result<TaskReservation> { 23 self.0 24 .try_update(Ordering::AcqRel, Ordering::Acquire, |used| { 25 used.checked_add(INQUIRY_RESERVATION_BYTES) 26 .filter(|next| *next <= LIVE_MAX_BYTES) 27 }) 28 .map_err(|_| crate::Error::session_unavailable(crate::Reason::OutOfRange))?; 29 Ok(TaskReservation(self.0.clone())) 30 } 31 } 32 #[cfg(feature = "workspace_session")] 33 pub(crate) struct TaskReservation(Shared<AtomicUsize>); 34 #[cfg(feature = "workspace_session")] 35 impl Drop for TaskReservation { 36 fn drop(&mut self) { 37 self.0 38 .fetch_sub(INQUIRY_RESERVATION_BYTES, Ordering::AcqRel); 39 } 40 } 41 42 /// Target-host task admission. The execution owner reserves input/result budget 43 /// and moves its resource permit into the task before submitting it. Success 44 /// transfers ownership: losing the observer must not cancel the accepted task. 45 /// This port confers no command, signer, journal or adapter qualification. 46 pub trait TaskHost: HostBound { 47 /// Accepts an unpolled owned task. Refusal executes no part of the task. 48 /// Implementations must bound their accepted task set. 49 fn submit(&self, task: HostFuture<'static, ()>) -> crate::Result<()>; 50 }