From 8d70bb08ca9698791a8e9572c583e15e87aaba95 Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 12 Apr 2022 18:17:48 -0500 Subject: [PATCH] Messaging api done --- ableos/src/unicode_utils.rs | 1 + kernel/src/lib.rs | 1 + kernel/src/messaging.rs | 128 +++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/ableos/src/unicode_utils.rs b/ableos/src/unicode_utils.rs index 22369a2..11c95f0 100644 --- a/ableos/src/unicode_utils.rs +++ b/ableos/src/unicode_utils.rs @@ -1,2 +1,3 @@ pub const CONSOLE: &str = "\u{E795}"; pub const POWER_SIGN: &str = "\u{23FB}"; +pub const LAMBDA: &str = "λ"; diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 55a56b2..4290210 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![no_std] #![feature(prelude_import)] +#![feature(arbitrary_enum_discriminant)] pub mod device_interface; pub mod messaging; diff --git a/kernel/src/messaging.rs b/kernel/src/messaging.rs index 068919f..044b28a 100644 --- a/kernel/src/messaging.rs +++ b/kernel/src/messaging.rs @@ -1,7 +1,22 @@ //! Interprocess communication. +use alloc::vec::Vec; + use crate::{proccess::PID, time::Time}; +extern crate alloc; + +/// 131070 * 128 +pub const TINY_MESSAGE_COUNT: usize = 131070; +/// 16384 * 1024 +pub const SMALL_MESSAGE_COUNT: usize = 16384; +/// 65536 * 256 +pub const MEDIUM_MESSAGE_COUNT: usize = 256; +/// 262144 * 16 +pub const LARGE_MESSAGE_COUNT: usize = 16; +/// 16777216 * 4 +pub const HUGE_MESSAGE_COUNT: usize = 4; + /// 128 Bytes pub type Tiny = [u8; 128]; @@ -54,7 +69,18 @@ pub struct ProcessMessage { /// The time the message was received pub receiver_time: Time, } - +impl ProcessMessage { + /// Return the size of the message + pub fn size(&self) -> usize { + match &self.message { + Message::Tiny(_) => 128, + Message::Small(_) => 1024, + Message::Medium(_) => 65536, + Message::Large(_) => 1048576, + Message::Huge(_) => 16777216, + } + } +} #[derive(Debug)] /// An enum of all possible errors that can occur when sending a message pub enum MessagingError { @@ -65,3 +91,103 @@ pub enum MessagingError { /// The message Queue is full TooManyMessages, } + +/// A mailbox that holds messages and PipeState +pub struct Mailbox { + /// The messages in the mailbox + pub messages: Vec, + /// The count of messages in the mailbox + pub message_count: MessageCount, +} +impl Mailbox { + /// append a message to the mailbox + pub fn append(&mut self, message: ProcessMessage) -> Result<(), MessagingError> { + let msg_size = message.size(); + + if self.message_count.total() > 147730 { + return Err(MessagingError::TooManyMessages); + } + + match msg_size { + TINY_MESSAGE_COUNT => { + if self.message_count.tiny < TINY_MESSAGE_COUNT { + self.messages.push(message); + self.message_count.tiny += 1; + Ok(()) + } else { + Err(MessagingError::TooManyMessages) + } + } + SMALL_MESSAGE_COUNT => { + if self.message_count.small < SMALL_MESSAGE_COUNT { + self.messages.push(message); + self.message_count.small += 1; + Ok(()) + } else { + Err(MessagingError::TooManyMessages) + } + } + MEDIUM_MESSAGE_COUNT => { + if self.message_count.medium < MEDIUM_MESSAGE_COUNT { + self.messages.push(message); + self.message_count.medium += 1; + Ok(()) + } else { + Err(MessagingError::TooManyMessages) + } + } + LARGE_MESSAGE_COUNT => { + if self.message_count.large < LARGE_MESSAGE_COUNT { + self.messages.push(message); + self.message_count.large += 1; + Ok(()) + } else { + Err(MessagingError::TooManyMessages) + } + } + HUGE_MESSAGE_COUNT => { + if self.message_count.huge < HUGE_MESSAGE_COUNT { + self.messages.push(message); + self.message_count.huge += 1; + Ok(()) + } else { + return Err(MessagingError::TooManyMessages); + } + } + _ => Err(MessagingError::MessageTooLarge), + } + } +} + +/// A proper struct to list the number of messages in the mailbox +pub struct MessageCount { + /// The number of tiny messages in the mailbox + pub tiny: usize, + /// The number of small messages in the mailbox + pub small: usize, + /// The number of medium messages in the mailbox + pub medium: usize, + /// The number of large messages in the mailbox + pub large: usize, + /// The number of huge messages in the mailbox + pub huge: usize, +} + +impl MessageCount { + /// Return the total number of messages in the mailbox + pub fn total(&self) -> usize { + self.tiny + self.small + self.medium + self.large + self.huge + } +} + +impl Default for MessageCount { + fn default() -> Self { + MessageCount { + tiny: 0, + small: 0, + medium: 0, + large: 0, + huge: 0, + } + } +}