byteflow/scheduler/
process.rs1#![allow(missing_docs)]
4
5use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
6use std::sync::Arc;
7use std::time::Duration;
8
9use crate::bytecode::{Cap, CapRights, CapTarget, RestartPolicy, RevocationCell};
10use crate::vm::Vm;
11
12use super::mailbox::Mailbox;
13use super::oneshot;
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
28pub struct FlowId(pub(crate) u64);
29
30impl FlowId {
31 pub const HOST: FlowId = FlowId(0);
33
34 pub fn as_u64(self) -> u64 {
35 self.0
36 }
37
38 #[inline]
39 pub fn is_host(self) -> bool {
40 self.0 == 0
41 }
42}
43
44impl std::fmt::Display for FlowId {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 write!(f, "flow#{}", self.0)
47 }
48}
49
50static NEXT_FLOW_ID: AtomicU64 = AtomicU64::new(1);
51static FLOW_IDS_EXHAUSTED: AtomicBool = AtomicBool::new(false);
52
53pub fn next_flow_id() -> FlowId {
56 match try_next_flow_id() {
57 Ok(id) => id,
58 Err(_) => FlowId(u64::MAX),
59 }
60}
61
62pub(crate) fn try_next_flow_id() -> Result<FlowId, super::error::RuntimeError> {
66 if FLOW_IDS_EXHAUSTED.load(Ordering::Relaxed) {
67 return Err(super::error::RuntimeError::FlowIdExhausted);
68 }
69 let id = NEXT_FLOW_ID.fetch_add(1, Ordering::Relaxed);
70 if id == 0 {
71 FLOW_IDS_EXHAUSTED.store(true, Ordering::Relaxed);
72 return Err(super::error::RuntimeError::FlowIdExhausted);
73 }
74 Ok(FlowId(id))
75}
76
77#[derive(Debug, Default)]
80pub struct FlowMetrics {
81 pub instructions: AtomicU64,
82 pub messages_sent: AtomicU64,
84 pub messages_received: AtomicU64,
85 pub reschedules: AtomicU64,
86}
87
88pub struct Flow {
95 pub id: FlowId,
96 pub vm: Vm,
97 pub mailbox: Arc<Mailbox>,
98 pub metrics: Arc<FlowMetrics>,
99 pub restart_policy: RestartPolicy,
100 pub(crate) completion: oneshot::Sender<FlowOutcome>,
102 pub pending_message: Option<crate::bytecode::Value>,
104 pub last_receive_dest: Option<u8>,
107 pub(crate) pending_send: Option<PendingSend>,
109 pub(crate) hop_charge: usize,
112 pub(crate) supervisor: Option<super::supervisor::SupervisorLink>,
113 pub(crate) local_supervisor: Option<super::supervisor::SupervisorLink>,
116 pub(crate) authority: Cap,
118 pub(crate) cell: Arc<RevocationCell>,
119 pub(crate) quota: Arc<super::quota::FlowQuota>,
120}
121
122#[derive(Debug, Clone, Copy)]
124pub(crate) enum PendingSend {
125 FireAndForget,
126 Ask {
127 dest_reg: u8,
128 expect_request_id: u64,
129 expect_sender: u64,
130 timeout: Option<Duration>,
131 },
132}
133
134#[derive(Clone, Debug, PartialEq)]
137pub enum FlowOutcome {
138 Completed(crate::bytecode::Value),
139 Failed(String),
140}
141
142impl Flow {
143 pub fn new(
144 id: FlowId,
145 vm: Vm,
146 mailbox: Arc<Mailbox>,
147 restart_policy: RestartPolicy,
148 completion: oneshot::Sender<FlowOutcome>,
149 ) -> Self {
150 let cell = Arc::new(RevocationCell::new());
151 let authority = Cap::root(
152 CapTarget::Flow(id.as_u64()),
153 CapRights::NONE,
154 None,
155 cell.as_ref(),
156 );
157 Self {
158 id,
159 vm,
160 mailbox,
161 metrics: Arc::new(FlowMetrics::default()),
162 restart_policy,
163 completion,
164 pending_message: None,
165 last_receive_dest: None,
166 pending_send: None,
167 hop_charge: 0,
168 supervisor: None,
169 local_supervisor: None,
170 authority,
171 cell,
172 quota: Arc::new(super::quota::FlowQuota::from_config(
173 super::quota::QuotaConfig::default(),
174 )),
175 }
176 }
177
178 pub(crate) fn complete(self, outcome: FlowOutcome) {
179 self.completion.send(outcome);
180 }
181
182 pub(crate) fn take_hop_charge(&mut self) -> usize {
184 let bytes = self.hop_charge;
185 self.hop_charge = 0;
186 bytes
187 }
188}