Files
@ 1bc57ab68e0e
Branch filter:
Location: CSY/reowolf/src/runtime2/component/control_layer.rs
1bc57ab68e0e
11.6 KiB
application/rls-services+xml
Merge branch 'feat-builtin-ip' into 'master'
feat: Builtin internet component
See merge request nl-cwi-csy/reowolf!6
feat: Builtin internet component
See merge request nl-cwi-csy/reowolf!6
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | use crate::runtime2::runtime::*;
use crate::runtime2::communication::*;
use crate::runtime2::component::*;
use super::component_context::*;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(crate) struct ControlId(u32);
impl ControlId {
/// Like other invalid IDs, this one doesn't care any significance, but is
/// just set at u32::MAX to hopefully bring out bugs sooner.
fn new_invalid() -> Self {
return ControlId(u32::MAX);
}
}
struct ControlEntry {
id: ControlId,
ack_countdown: u32,
content: ControlContent,
}
enum ControlContent {
PeerChange(ContentPeerChange),
ScheduleComponent(CompId),
ClosedPort(PortId),
}
struct ContentPeerChange {
source_port: PortId,
source_comp: CompId,
old_target_port: PortId,
new_target_port: PortId,
new_target_comp: CompId,
schedule_entry_id: ControlId,
}
struct ControlClosedPort {
closed_port: PortId,
exit_entry_id: Option<ControlId>,
}
pub(crate) enum AckAction {
None,
SendMessage(CompId, ControlMessage),
ScheduleComponent(CompId),
}
/// Handling/sending control messages.
pub(crate) struct ControlLayer {
id_counter: ControlId,
entries: Vec<ControlEntry>,
}
impl ControlLayer {
pub(crate) fn should_reroute(&self, message: &mut Message) -> Option<CompId> {
// Safety note: rerouting should occur during the time when we're
// notifying a peer of a new component. During this period that
// component hasn't been executed yet, so cannot have died yet.
// FIX @NoDirectHandle
let target_port = message.target_port();
if target_port.is_none() {
return None;
}
let target_port = target_port.unwrap();
for entry in &self.entries {
if let ControlContent::PeerChange(entry) = &entry.content {
if entry.old_target_port == target_port {
message.modify_target_port(entry.new_target_port);
return Some(entry.new_target_comp);
}
}
}
return None;
}
/// Handles an acknowledgement. The returned action must be performed by the
/// caller. The optionally returned `ControlId` must be used and passed to
/// `handle_ack` again.
pub(crate) fn handle_ack(&mut self, entry_id: ControlId, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx) -> (AckAction, Option<ControlId>) {
let entry_index = self.get_entry_index_by_id(entry_id).unwrap();
let entry = &mut self.entries[entry_index];
debug_assert!(entry.ack_countdown > 0);
entry.ack_countdown -= 1;
if entry.ack_countdown != 0 {
return (AckAction::None, None);
}
let entry = self.entries.remove(entry_index);
// All `Ack`s received, take action based on the kind of entry
match entry.content {
ControlContent::PeerChange(content) => {
// If change of peer is ack'd. Then we are certain we have
// rerouted all of the messages, and the sender's port can now
// be unblocked again.
let target_comp_id = content.source_comp;
let message_to_send = ControlMessage{
id: ControlId::new_invalid(),
sender_comp_id: comp_ctx.id,
target_port_id: Some(content.source_port),
content: ControlMessageContent::PortPeerChangedUnblock(
content.new_target_port,
content.new_target_comp
)
};
let to_ack = content.schedule_entry_id;
return (
AckAction::SendMessage(target_comp_id, message_to_send),
Some(to_ack)
);
},
ControlContent::ScheduleComponent(to_schedule) => {
// If all change-of-peers are `Ack`d, then we're ready to
// schedule the component!
return (AckAction::ScheduleComponent(to_schedule), None);
},
ControlContent::ClosedPort(closed_port) => {
// If a closed port is Ack'd, then we remove the reference to
// that component.
let port_handle = comp_ctx.get_port_handle(closed_port);
let port_info = comp_ctx.get_port(port_handle);
let port_peer_comp_id = port_info.peer_comp_id;
debug_assert_eq!(port_info.state, PortState::Closed);
comp_ctx.remove_peer(sched_ctx, port_handle, port_peer_comp_id, true); // remove if closed
return (AckAction::None, None);
}
}
}
pub(crate) fn has_acks_remaining(&self) -> bool {
return !self.entries.is_empty();
}
// -------------------------------------------------------------------------
// Port transfer (due to component creation)
// -------------------------------------------------------------------------
/// Adds an entry that, when completely ack'd, will schedule a component.
pub(crate) fn add_schedule_entry(&mut self, to_schedule_id: CompId) -> ControlId {
let entry_id = self.take_id();
self.entries.push(ControlEntry{
id: entry_id,
ack_countdown: 0, // incremented by calls to `add_reroute_entry`
content: ControlContent::ScheduleComponent(to_schedule_id),
});
return entry_id;
}
/// Removes a schedule entry. Only used if the caller preemptively called
/// `add_schedule_entry`, but ended up not calling `add_reroute_entry`,
/// hence the `ack_countdown` in the scheduling entry is at 0.
pub(crate) fn remove_schedule_entry(&mut self, schedule_entry_id: ControlId) {
let index = self.get_entry_index_by_id(schedule_entry_id).unwrap();
debug_assert_eq!(self.entries[index].ack_countdown, 0);
self.entries.remove(index);
}
pub(crate) fn add_reroute_entry(
&mut self, creator_comp_id: CompId,
source_port_id: PortId, source_comp_id: CompId,
old_target_port_id: PortId, new_target_port_id: PortId, new_comp_id: CompId,
schedule_entry_id: ControlId,
) -> Message {
let entry_id = self.take_id();
self.entries.push(ControlEntry{
id: entry_id,
ack_countdown: 1,
content: ControlContent::PeerChange(ContentPeerChange{
source_port: source_port_id,
source_comp: source_comp_id,
old_target_port: old_target_port_id,
new_target_port: new_target_port_id,
new_target_comp: new_comp_id,
schedule_entry_id,
}),
});
// increment counter on schedule entry
let entry_index = self.get_entry_index_by_id(schedule_entry_id).unwrap();
self.entries[entry_index].ack_countdown += 1;
return Message::Control(ControlMessage{
id: entry_id,
sender_comp_id: creator_comp_id,
target_port_id: Some(source_port_id),
content: ControlMessageContent::PortPeerChangedBlock(source_port_id)
})
}
// -------------------------------------------------------------------------
// Blocking, unblocking, and closing ports
// -------------------------------------------------------------------------
pub(crate) fn has_close_port_entry(&self, port_handle: LocalPortHandle, comp_ctx: &CompCtx) -> Option<ControlId> {
let port = comp_ctx.get_port(port_handle);
let port_id = port.self_id;
for entry in self.entries.iter() {
if let ControlContent::ClosedPort(entry_port_id) = &entry.content {
if *entry_port_id == port_id {
return Some(entry.id);
}
}
}
return None;
}
/// Initiates the control message procedures for closing a port. Caller must
/// make sure that the port state has already been set to `Closed`.
pub(crate) fn initiate_port_closing(&mut self, port_handle: LocalPortHandle, comp_ctx: &CompCtx) -> (LocalPeerHandle, ControlMessage) {
let port = comp_ctx.get_port(port_handle);
let peer_port_id = port.peer_port_id;
debug_assert!(port.state == PortState::Closed);
// Construct the port-closing entry
let entry_id = self.take_id();
self.entries.push(ControlEntry{
id: entry_id,
ack_countdown: 1,
content: ControlContent::ClosedPort(port.self_id),
});
// Return the messages notifying peer of the closed port
let peer_handle = comp_ctx.get_peer_handle(port.peer_comp_id);
return (
peer_handle,
ControlMessage{
id: entry_id,
sender_comp_id: comp_ctx.id,
target_port_id: Some(peer_port_id),
content: ControlMessageContent::ClosePort(peer_port_id),
}
);
}
/// Generates the control message used to indicate to a peer that a port
/// should be blocked (expects the caller to have set the port's state to
/// blocked).
pub(crate) fn initiate_port_blocking(&mut self, comp_ctx: &CompCtx, port_handle: LocalPortHandle) -> (LocalPeerHandle, ControlMessage) {
let port_info = comp_ctx.get_port(port_handle);
debug_assert_eq!(port_info.kind, PortKind::Getter); // because we're telling the putter to block
debug_assert_eq!(port_info.state, PortState::BlockedDueToFullBuffers); // contract with caller
let peer_port_id = port_info.peer_port_id;
let peer_comp_id = port_info.peer_comp_id;
let peer_handle = comp_ctx.get_peer_handle(peer_comp_id);
return (
peer_handle,
ControlMessage{
id: ControlId::new_invalid(),
sender_comp_id: comp_ctx.id,
target_port_id: Some(port_info.peer_port_id),
content: ControlMessageContent::BlockPort(peer_port_id),
}
);
}
/// Generates a messages used to indicate to a peer that a port should be
/// unblocked again.
pub(crate) fn cancel_port_blocking(&mut self, comp_ctx: &CompCtx, port_handle: LocalPortHandle) -> (LocalPeerHandle, ControlMessage) {
let port_info = comp_ctx.get_port(port_handle);
debug_assert_eq!(port_info.kind, PortKind::Getter); // because we're initiating the unblocking
debug_assert_eq!(port_info.state, PortState::Open); // contract with caller, the locally stored entry ensures we were blocked before
let peer_handle = comp_ctx.get_peer_handle(port_info.peer_comp_id);
return (
peer_handle,
ControlMessage{
id: ControlId::new_invalid(),
sender_comp_id: comp_ctx.id,
target_port_id: Some(port_info.peer_port_id),
content: ControlMessageContent::UnblockPort(port_info.peer_port_id)
}
);
}
// -------------------------------------------------------------------------
// Internal utilities
// -------------------------------------------------------------------------
fn take_id(&mut self) -> ControlId {
let id = self.id_counter;
self.id_counter.0 = self.id_counter.0.wrapping_add(1);
return id;
}
fn get_entry_index_by_id(&self, entry_id: ControlId) -> Option<usize> {
for (index, entry) in self.entries.iter().enumerate() {
if entry.id == entry_id {
return Some(index);
}
}
return None;
}
}
impl Default for ControlLayer {
fn default() -> Self {
return ControlLayer{
id_counter: ControlId(0),
entries: Vec::new(),
}
}
}
|