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