diff --git a/src/runtime2/inbox2.rs b/src/runtime2/inbox2.rs index 028cd41ff27205289250e30341fb4b44d8502201..9a8754e20b72a68b488bf80f25adf85c699ad4c8 100644 --- a/src/runtime2/inbox2.rs +++ b/src/runtime2/inbox2.rs @@ -1,3 +1,6 @@ +use std::sync::Mutex; +use std::collections::VecDeque; + use crate::protocol::eval::ValueGroup; use crate::runtime2::branch::BranchId; use crate::runtime2::ConnectorId; @@ -77,4 +80,36 @@ pub(crate) enum MessageFancy { Data(DataMessageFancy), Sync(SyncMessageFancy), Control(ControlMessageFancy), +} + +/// The public inbox of a connector. The thread running the connector that owns +/// this inbox may retrieved from it. Non-owning threads may only put new +/// messages inside of it. +// TODO: @Optimize, lazy concurrency. Probably ringbuffer with read/write heads. +// Should behave as a MPSC queue. +pub struct PublicInbox { + messages: Mutex>, +} + +impl PublicInbox { + pub fn new() -> Self { + Self{ + messages: Mutex::new(VecDeque::new()), + } + } + + pub fn insert_message(&self, message: MessageFancy) { + let mut lock = self.messages.lock().unwrap(); + lock.push_back(message); + } + + pub fn take_message(&self) -> Option { + let mut lock = self.messages.lock().unwrap(); + return lock.pop_front(); + } + + pub fn is_empty(&self) -> bool { + let lock = self.messages.lock().unwrap(); + return lock.is_empty(); + } } \ No newline at end of file