ifc_lite_core/execution_owner/
resource_context.rs1use super::WorkEpoch;
5use std::{
6 any::Any,
7 fmt,
8 sync::{Arc, Weak},
9};
10
11#[derive(Clone)]
14pub struct SourceResourceContext {
15 payload: Arc<dyn Any + Send + Sync>,
16 account: Option<Arc<dyn super::AllocationAccount>>,
17}
18impl SourceResourceContext {
19 pub fn new<T: Any + Send + Sync>(value: Arc<T>) -> Self {
20 Self {
21 payload: value,
22 account: None,
23 }
24 }
25 pub fn new_accounted<T: Any + Send + Sync + super::AllocationAccount>(value: Arc<T>) -> Self {
28 Self {
29 payload: value.clone(),
30 account: Some(value),
31 }
32 }
33 pub fn downcast<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
34 Arc::clone(&self.payload).downcast().ok()
35 }
36 pub fn is_accounted(&self) -> bool {
38 self.account.is_some()
39 }
40}
41impl fmt::Debug for SourceResourceContext {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 f.write_str("SourceResourceContext")
44 }
45}
46
47#[derive(Debug, Clone, Default)]
48pub(super) struct ActiveSource {
49 pub epoch: Weak<WorkEpoch>,
50 pub resources: Option<SourceResourceContext>,
51}
52
53#[derive(Debug, Clone)]
56pub struct SourceStopHandle(Arc<WorkEpoch>);
57impl SourceStopHandle {
58 pub(super) fn new(epoch: Arc<WorkEpoch>) -> Self {
59 Self(epoch)
60 }
61 #[inline]
62 pub fn check(&self) -> Result<(), crate::resource_stop::ProgramStop> {
63 if self.0.parent.is_none() && !self.0.stopped.load(std::sync::atomic::Ordering::Acquire) {
66 return Ok(());
67 }
68 self.check_chain()
69 }
70 #[cold]
71 #[inline(never)]
72 fn check_chain(&self) -> Result<(), crate::resource_stop::ProgramStop> {
73 let mut first = None;
76 let mut cursor = Some(self.0.as_ref());
77 while let Some(epoch) = cursor {
78 if epoch.stopped.load(std::sync::atomic::Ordering::Acquire) {
79 if let Some(stop) = *epoch
80 .resource_stop
81 .lock()
82 .unwrap_or_else(|e| e.into_inner())
83 {
84 first = Some(stop);
85 }
86 }
87 cursor = epoch.parent.as_deref();
88 }
89 first.map_or(Ok(()), Err)
90 }
91 pub fn stop(
92 &self,
93 stop: crate::resource_stop::ProgramStop,
94 ) -> crate::resource_stop::ProgramStop {
95 let stop = self.check().err().unwrap_or(stop);
96 let mut cursor = Some(self.0.as_ref());
97 while let Some(epoch) = cursor {
98 epoch
99 .resource_stop
100 .lock()
101 .unwrap_or_else(|e| e.into_inner())
102 .get_or_insert(stop);
103 epoch.stopped.store(true, std::sync::atomic::Ordering::Release);
104 cursor = epoch.parent.as_deref();
105 }
106 self.check().err().unwrap_or(stop)
107 }
108}