diff --git a/Cargo.toml b/Cargo.toml index 1fd40dde..55756d44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ assert_cmd = "2.0" [features] default = ["log"] use-aerugo-cortex-m = ["aerugo-cortex-m", "aerugo-samv71-hal"] -use-aerugo-x86 = ["aerugo-x86", "aerugo-x86-hal"] +use-aerugo-x86 = ["aerugo-x86", "aerugo-x86-hal", "critical-section/std"] test-aerugo-cortex-m = ["use-aerugo-x86"] rt = ["aerugo-samv71-hal?/rt"] log = ["aerugo-cortex-m?/log", "aerugo-x86?/log"] diff --git a/arch/cortex-m/aerugo-cortex-m/Cargo.toml b/arch/cortex-m/aerugo-cortex-m/Cargo.toml index d89b68a8..a4a7a2b7 100644 --- a/arch/cortex-m/aerugo-cortex-m/Cargo.toml +++ b/arch/cortex-m/aerugo-cortex-m/Cargo.toml @@ -10,7 +10,6 @@ license.workspace = true description = "Cortex-M specific implementation for Aerugo" [dependencies] -cortex-m = "0.7.7" rtt-target = "0.4.0" [features] diff --git a/arch/cortex-m/aerugo-cortex-m/src/lib.rs b/arch/cortex-m/aerugo-cortex-m/src/lib.rs index ebd0ba8e..84f0e615 100644 --- a/arch/cortex-m/aerugo-cortex-m/src/lib.rs +++ b/arch/cortex-m/aerugo-cortex-m/src/lib.rs @@ -8,8 +8,6 @@ Cortex-M specific implementation for Aerugo. #[cfg(feature = "log")] mod logger; -mod mutex; #[cfg(feature = "log")] pub use self::logger::{init_log, log, logln}; -pub use self::mutex::Mutex; diff --git a/arch/x86/aerugo-x86/src/lib.rs b/arch/x86/aerugo-x86/src/lib.rs index 16e3bead..52ba5643 100644 --- a/arch/x86/aerugo-x86/src/lib.rs +++ b/arch/x86/aerugo-x86/src/lib.rs @@ -7,8 +7,6 @@ x86 specific implementation for Aerugo. #[cfg(feature = "log")] mod logger; -mod mutex; #[cfg(feature = "log")] pub use self::logger::{init_log, log, logln}; -pub use self::mutex::Mutex; diff --git a/arch/x86/aerugo-x86/src/mutex.rs b/arch/x86/aerugo-x86/src/mutex.rs deleted file mode 100644 index dd99e542..00000000 --- a/arch/x86/aerugo-x86/src/mutex.rs +++ /dev/null @@ -1,57 +0,0 @@ -//! Mutex for x86. -use core::cell::UnsafeCell; - -/// Mutex for x86 -/// -/// # Generic Parameters -/// * `T` - Type of the stored value. -#[repr(transparent)] -pub struct Mutex(UnsafeCell); - -/// x86 works only on one thread. -unsafe impl Sync for Mutex {} - -impl Mutex { - /// Creates new mutex with given value - /// - /// # Parameters - /// * `value` - Value to initialize the cell with. - #[inline(always)] - pub const fn new(value: T) -> Self { - Mutex(UnsafeCell::new(value)) - } -} - -impl Mutex { - /// Gives access to the value by locking the mutex - /// - /// # Parameters - /// * `f` - Lambda to execute. - /// - /// # Return - /// Result of the executed lambda. - #[inline(always)] - #[allow(dead_code)] - pub fn lock(&self, f: impl FnOnce(&mut T) -> R) -> R { - unsafe { f(self.as_mut_ref()) } - } - - /// Returns a mutable reference to the stored value. - #[inline(always)] - #[allow(clippy::mut_from_ref)] - unsafe fn as_mut_ref(&self) -> &mut T { - &mut *self.0.get() - } -} - -impl Default for Mutex { - fn default() -> Mutex { - Mutex::new(Default::default()) - } -} - -impl From for Mutex { - fn from(t: T) -> Mutex { - Mutex::new(t) - } -} diff --git a/src/boolean_condition.rs b/src/boolean_condition.rs index 62291899..5f68a922 100644 --- a/src/boolean_condition.rs +++ b/src/boolean_condition.rs @@ -11,10 +11,10 @@ pub use self::boolean_condition_storage::BooleanConditionStorage; use crate::aerugo::{Aerugo, AERUGO}; use crate::api::{InitError, SystemApi}; -use crate::arch::Mutex; use crate::data_provider::DataProvider; use crate::internal_list::InternalList; use crate::tasklet::TaskletPtr; +use crate::Mutex; /// List of tasklets registered to a condition type TaskletList = InternalList; diff --git a/src/event/event_set.rs b/src/event/event_set.rs index 870131b3..3f751c40 100644 --- a/src/event/event_set.rs +++ b/src/event/event_set.rs @@ -4,12 +4,12 @@ use heapless::spsc::Queue; use crate::aerugo::AERUGO; use crate::api::{RuntimeError, SystemApi}; -use crate::arch::Mutex; use crate::data_provider::DataProvider; use crate::event::EventId; use crate::event_manager::EventManager; use crate::tasklet::TaskletPtr; use crate::utils::max; +use crate::Mutex; /// Type for event queue. type EventQueue = Queue; diff --git a/src/executor.rs b/src/executor.rs index 3f107949..66934894 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -9,8 +9,8 @@ use heapless::binary_heap::{BinaryHeap, Max}; use crate::aerugo::{Aerugo, AERUGO}; use crate::api::{RuntimeApi, RuntimeError, SystemApi}; -use crate::arch::Mutex; use crate::tasklet::{TaskletPtr, TaskletStatus}; +use crate::Mutex; /// Type for the tasklet execution queue type TaskletQueue = BinaryHeap; diff --git a/src/lib.rs b/src/lib.rs index b5dbf196..a69f0e48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ mod execution_monitoring; mod executor; mod internal_list; mod message_queue; +mod mutex; mod stubs; mod tasklet; mod time_manager; @@ -33,13 +34,16 @@ pub use self::event::{EventEnabler, EventId}; pub use self::message_queue::{MessageQueueHandle, MessageQueueStorage}; pub use self::tasklet::{TaskletConfig, TaskletStorage}; pub use aerugo_hal::{time, Duration, Instant, SystemHardwareConfig}; +pub use mutex::Mutex; #[cfg(feature = "use-aerugo-cortex-m")] +#[cfg(feature = "log")] pub(crate) use aerugo_cortex_m as arch; #[cfg(feature = "use-aerugo-cortex-m")] pub use aerugo_samv71_hal as hal; #[cfg(feature = "use-aerugo-x86")] +#[cfg(feature = "log")] pub(crate) use aerugo_x86 as arch; #[cfg(feature = "use-aerugo-x86")] pub use aerugo_x86_hal as hal; diff --git a/src/message_queue.rs b/src/message_queue.rs index 3af8764a..039317ae 100644 --- a/src/message_queue.rs +++ b/src/message_queue.rs @@ -10,10 +10,10 @@ pub(crate) use self::message_queue_storage::QueueData; use crate::aerugo::{Aerugo, AERUGO}; use crate::api::{InitError, RuntimeError, SystemApi}; -use crate::arch::Mutex; use crate::data_provider::DataProvider; use crate::internal_list::InternalList; use crate::tasklet::TaskletPtr; +use crate::Mutex; /// List of tasklets registered to a queue type TaskletList = InternalList; diff --git a/src/message_queue/message_queue_storage.rs b/src/message_queue/message_queue_storage.rs index afbdc534..41c96d4d 100644 --- a/src/message_queue/message_queue_storage.rs +++ b/src/message_queue/message_queue_storage.rs @@ -10,8 +10,8 @@ use core::cell::OnceCell; use heapless::Vec; use crate::api::InitError; -use crate::arch::Mutex; use crate::message_queue::MessageQueueHandle; +use crate::Mutex; /// Type of the queue buffer storage. pub(crate) type QueueBuffer = Vec>() }>; diff --git a/arch/cortex-m/aerugo-cortex-m/src/mutex.rs b/src/mutex.rs similarity index 95% rename from arch/cortex-m/aerugo-cortex-m/src/mutex.rs rename to src/mutex.rs index ba978fff..bb387e5c 100644 --- a/arch/cortex-m/aerugo-cortex-m/src/mutex.rs +++ b/src/mutex.rs @@ -6,8 +6,6 @@ use core::cell::UnsafeCell; -use cortex_m::interrupt; - /// Mutex based on the critical section. /// /// # Generic Parameters @@ -46,7 +44,7 @@ impl Mutex { #[inline(always)] #[allow(dead_code)] pub fn lock(&self, f: impl FnOnce(&mut T) -> R) -> R { - unsafe { interrupt::free(|_| f(self.as_mut_ref())) } + unsafe { critical_section::with(|_| f(self.as_mut_ref())) } } /// Returns a mutable reference to the stored value. diff --git a/src/tasklet.rs b/src/tasklet.rs index 5f07e39d..ca9a1677 100644 --- a/src/tasklet.rs +++ b/src/tasklet.rs @@ -28,10 +28,10 @@ use core::cell::{OnceCell, UnsafeCell}; use crate::aerugo::AERUGO; use crate::api::{InitError, RuntimeApi}; -use crate::arch::Mutex; use crate::boolean_condition::BooleanConditionSet; use crate::data_provider::DataProvider; use crate::Instant; +use crate::Mutex; /// Type of function that is executed by the tasklet in its step. pub(crate) type StepFn = fn(T, &mut C, &'static dyn RuntimeApi);