Files
@ 06f259bf8031
Branch filter:
Location: CSY/reowolf/src/runtime/actors.rs
06f259bf8031
9.7 KiB
application/rls-services+xml
first
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | use crate::common::*;
use crate::runtime::{endpoint::*, *};
#[derive(Debug)]
pub(crate) struct MonoN {
pub ekeys: HashSet<Key>,
pub result: Option<(usize, HashMap<Key, Payload>)>,
}
#[derive(Debug)]
pub(crate) struct PolyN {
pub ekeys: HashSet<Key>,
pub branches: HashMap<Predicate, BranchN>,
}
#[derive(Debug, Clone)]
pub(crate) struct BranchN {
pub to_get: HashSet<Key>,
pub gotten: HashMap<Key, Payload>,
pub sync_batch_index: usize,
}
#[derive(Debug)]
pub struct MonoP {
pub state: ProtocolS,
pub ekeys: HashSet<Key>,
}
#[derive(Debug)]
pub(crate) struct PolyP {
pub incomplete: HashMap<Predicate, BranchP>,
pub complete: HashMap<Predicate, BranchP>,
pub ekeys: HashSet<Key>,
}
#[derive(Debug, Clone)]
pub(crate) struct BranchP {
pub inbox: HashMap<Key, Payload>,
pub state: ProtocolS,
}
//////////////////////////////////////////////////////////////////
impl PolyP {
pub(crate) fn poly_run(
&mut self,
m_ctx: PolyPContext,
protocol_description: &ProtocolD,
) -> Result<SyncRunResult, EndpointErr> {
let to_run: Vec<_> = self.incomplete.drain().collect();
self.poly_run_these_branches(m_ctx, protocol_description, to_run)
}
pub(crate) fn poly_run_these_branches(
&mut self,
mut m_ctx: PolyPContext,
protocol_description: &ProtocolD,
mut to_run: Vec<(Predicate, BranchP)>,
) -> Result<SyncRunResult, EndpointErr> {
use SyncRunResult as Srr;
while let Some((mut predicate, mut branch)) = to_run.pop() {
let mut r_ctx = BranchPContext {
m_ctx: m_ctx.reborrow(),
ekeys: &self.ekeys,
predicate: &predicate,
inbox: &branch.inbox,
};
use PolyBlocker as Sb;
let blocker = branch.state.sync_run(&mut r_ctx, protocol_description);
match blocker {
Sb::Inconsistent => {} // DROP
Sb::CouldntReadMsg(ekey) => {
assert!(self.ekeys.contains(&ekey));
let channel_id =
r_ctx.m_ctx.inner.endpoint_exts.get(ekey).unwrap().info.channel_id;
if predicate.replace_assignment(channel_id, true) != Some(false) {
// don't rerun now. Rerun at next `sync_run`
self.incomplete.insert(predicate, branch);
}
// ELSE DROP
}
Sb::CouldntCheckFiring(ekey) => {
assert!(self.ekeys.contains(&ekey));
let channel_id =
r_ctx.m_ctx.inner.endpoint_exts.get(ekey).unwrap().info.channel_id;
// split the branch!
let branch_f = branch.clone();
let mut predicate_f = predicate.clone();
if predicate_f.replace_assignment(channel_id, false).is_some() {
panic!("OI HANS QUERY FIRST!");
}
assert!(predicate.replace_assignment(channel_id, true).is_none());
to_run.push((predicate, branch));
to_run.push((predicate_f, branch_f));
}
Sb::SyncBlockEnd => {
// come up with the predicate for this local solution
let ekeys_channel_id_iter = self
.ekeys
.iter()
.map(|&ekey| m_ctx.inner.endpoint_exts.get(ekey).unwrap().info.channel_id);
predicate.batch_assign_nones(ekeys_channel_id_iter, false);
// report the local solution
m_ctx
.solution_storage
.submit_and_digest_subtree_solution(m_ctx.my_subtree_id, predicate.clone());
// store the solution for recovering later
self.complete.insert(predicate, branch);
}
Sb::PutMsg(ekey, payload) => {
assert!(self.ekeys.contains(&ekey));
let EndpointExt { info, endpoint } =
m_ctx.inner.endpoint_exts.get_mut(ekey).unwrap();
if predicate.replace_assignment(info.channel_id, true) != Some(false) {
let msg = CommMsgContents::SendPayload {
payload_predicate: predicate.clone(),
payload,
}
.into_msg(m_ctx.inner.round_index);
endpoint.send(msg)?;
to_run.push((predicate, branch));
}
// ELSE DROP
}
}
}
// all in self.incomplete most recently returned Blocker::CouldntReadMsg
Ok(if self.incomplete.is_empty() {
if self.complete.is_empty() {
Srr::NoBranches
} else {
Srr::AllBranchesComplete
}
} else {
Srr::BlockingForRecv
})
}
pub(crate) fn poly_recv_run(
&mut self,
m_ctx: PolyPContext,
protocol_description: &ProtocolD,
ekey: Key,
payload_predicate: Predicate,
payload: Payload,
) -> Result<SyncRunResult, EndpointErr> {
// try exact match
let to_run = if self.complete.contains_key(&payload_predicate) {
// exact match with stopped machine
vec![]
} else if let Some(mut branch) = self.incomplete.remove(&payload_predicate) {
// exact match with running machine
branch.inbox.insert(ekey, payload);
vec![(payload_predicate, branch)]
} else {
let mut incomplete2 = HashMap::<_, _>::default();
let to_run = self
.incomplete
.drain()
.filter_map(|(old_predicate, mut branch)| {
use CommonSatResult as Csr;
match old_predicate.common_satisfier(&payload_predicate) {
Csr::FormerNotLatter | Csr::Equivalent => {
// old_predicate COVERS the assumptions of payload_predicate
let was = branch.inbox.insert(ekey, payload.clone());
assert!(was.is_none()); // INBOX MUST BE EMPTY!
Some((old_predicate, branch))
}
Csr::New(unified) => {
// payload_predicate has new assumptions. FORK!
let mut payload_branch = branch.clone();
let was = payload_branch.inbox.insert(ekey, payload.clone());
assert!(was.is_none()); // INBOX MUST BE EMPTY!
// put the original back untouched
incomplete2.insert(old_predicate, branch);
Some((unified, payload_branch))
}
Csr::LatterNotFormer => {
// payload_predicate has new assumptions. FORK!
let mut payload_branch = branch.clone();
let was = payload_branch.inbox.insert(ekey, payload.clone());
assert!(was.is_none()); // INBOX MUST BE EMPTY!
// put the original back untouched
incomplete2.insert(old_predicate, branch);
Some((payload_predicate.clone(), payload_branch))
}
Csr::Nonexistant => {
// predicates contradict
incomplete2.insert(old_predicate, branch);
None
}
}
})
.collect();
std::mem::swap(&mut self.incomplete, &mut incomplete2);
to_run
};
self.poly_run_these_branches(m_ctx, protocol_description, to_run)
}
pub(crate) fn become_mono(
mut self,
decision: &Predicate,
all_inboxes: &mut HashMap<Key, Payload>,
) -> MonoP {
if let Some((_, branch)) = self.complete.drain().find(|(p, _)| decision.satisfies(p)) {
let BranchP { inbox, state } = branch;
for (key, payload) in inbox {
assert!(all_inboxes.insert(key, payload).is_none());
}
self.incomplete.clear();
MonoP { state, ekeys: self.ekeys }
} else {
panic!("No such solution!")
}
}
}
impl PolyN {
pub fn sync_recv(
&mut self,
ekey: Key,
payload: Payload,
solution_storage: &mut SolutionStorage,
) {
for (predicate, branch) in self.branches.iter_mut() {
if branch.to_get.remove(&ekey) {
branch.gotten.insert(ekey, payload.clone());
if branch.to_get.is_empty() {
solution_storage
.submit_and_digest_subtree_solution(SubtreeId::PolyN, predicate.clone());
}
}
}
}
pub fn become_mono(
mut self,
decision: &Predicate,
all_inboxes: &mut HashMap<Key, Payload>,
) -> MonoN {
if let Some((_, branch)) = self.branches.drain().find(|(p, _)| decision.satisfies(p)) {
let BranchN { gotten, sync_batch_index, .. } = branch;
for (&key, payload) in gotten.iter() {
assert!(all_inboxes.insert(key, payload.clone()).is_none());
}
MonoN { ekeys: self.ekeys, result: Some((sync_batch_index, gotten)) }
} else {
panic!("No such solution!")
}
}
}
|