osiris_process/
communication.rs

1//! This module provides a way for a [crate::processor::Cpu] to communicate with the "exterior world".
2//!
3//! This means it can put messages in an output queue, and these messages can be transmitted by any "supervisor" of the Cpus to the right recipient.
4
5use std::collections::VecDeque;
6use osiris_data::data::composite::{Array, ProduceConsume};
7
8/// Helps a supervisor track an identified [crate::processor::Cpu].
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10pub struct CpuId(u16);
11
12impl CpuId {
13    pub const fn new(raw: u16) -> Self { Self(raw) }
14    pub const fn to_u16(&self) -> u16 { self.0 }
15}
16
17#[derive(Clone, Debug)]
18pub struct Message {
19    recipient: CpuId,
20    data: Array,
21}
22
23impl Message {
24    pub fn new(recipient: CpuId, data: Array) -> Self {
25        Message { recipient, data }
26    }
27
28    pub fn is_recipient(&self, cpu_id: CpuId) -> bool { self.recipient == cpu_id }
29    pub fn recipient(&self) -> CpuId { self.recipient }
30    pub fn get_data(&self) -> Array { self.data.clone() }
31
32}
33
34#[derive(Clone, Debug, Default)]
35pub struct MessageQueue {
36    queue: VecDeque<Message>
37}
38
39impl ProduceConsume<Message> for MessageQueue {
40    fn produce(&mut self, data: Message) {
41        self.queue.push_back(data);
42    }
43
44    fn consume(&mut self) -> Option<Message> { self.queue.pop_front() }
45
46    fn len(&self) -> usize { self.queue.len() }
47
48    fn is_empty(&self) -> bool { self.queue.is_empty() }
49}