From 6c48ebeeeeb5917251a4ba08d5c042cdd62e673d Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sun, 29 Jan 2023 20:16:23 +0100 Subject: [PATCH] Move common data structures to `rtic-common` --- rtic-channel/Cargo.toml | 3 ++- rtic-channel/src/lib.rs | 17 +++++++---------- rtic-common/.gitignore | 2 ++ rtic-common/CHANGELOG.md | 16 ++++++++++++++++ rtic-common/Cargo.toml | 13 +++++++++++++ rtic-common/src/lib.rs | 8 ++++++++ {rtic-channel => rtic-common}/src/wait_queue.rs | 3 +++ .../src/waker_registration.rs | 2 ++ 8 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 rtic-common/.gitignore create mode 100644 rtic-common/CHANGELOG.md create mode 100644 rtic-common/Cargo.toml create mode 100644 rtic-common/src/lib.rs rename {rtic-channel => rtic-common}/src/wait_queue.rs (98%) rename {rtic-channel => rtic-common}/src/waker_registration.rs (98%) diff --git a/rtic-channel/Cargo.toml b/rtic-channel/Cargo.toml index 5d4cbd0e08..a0955bc405 100644 --- a/rtic-channel/Cargo.toml +++ b/rtic-channel/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] heapless = "0.7" critical-section = "1" +rtic-common = { version = "1.0.0", path = "../rtic-common" } [dev-dependencies] tokio = { version = "1", features = ["rt", "macros", "time"] } @@ -15,4 +16,4 @@ tokio = { version = "1", features = ["rt", "macros", "time"] } [features] default = [] -testing = ["critical-section/std"] +testing = ["critical-section/std", "rtic-common/testing"] diff --git a/rtic-channel/src/lib.rs b/rtic-channel/src/lib.rs index acfa801315..2b237f669f 100644 --- a/rtic-channel/src/lib.rs +++ b/rtic-channel/src/lib.rs @@ -14,11 +14,8 @@ use core::{ task::{Poll, Waker}, }; use heapless::Deque; -use wait_queue::WaitQueue; -use waker_registration::CriticalSectionWakerRegistration as WakerRegistration; - -mod wait_queue; -mod waker_registration; +use rtic_common::wait_queue::{Link, WaitQueue}; +use rtic_common::waker_registration::CriticalSectionWakerRegistration as WakerRegistration; /// An MPSC channel for use in no-alloc systems. `N` sets the size of the queue. /// @@ -136,11 +133,11 @@ unsafe impl<'a, T, const N: usize> Send for Sender<'a, T, N> {} /// This is needed to make the async closure in `send` accept that we "share" /// the link possible between threads. #[derive(Clone)] -struct LinkPtr(*mut Option>); +struct LinkPtr(*mut Option>); impl LinkPtr { /// This will dereference the pointer stored within and give out an `&mut`. - unsafe fn get(&mut self) -> &mut Option> { + unsafe fn get(&mut self) -> &mut Option> { &mut *self.0 } } @@ -200,10 +197,10 @@ impl<'a, T, const N: usize> Sender<'a, T, N> { /// Send a value. If there is no place left in the queue this will wait until there is. /// If the receiver does not exist this will return an error. pub async fn send(&mut self, val: T) -> Result<(), NoReceiver> { - let mut link_ptr: Option> = None; + let mut link_ptr: Option> = None; // Make this future `Drop`-safe, also shadow the original definition so we can't abuse it. - let mut link_ptr = LinkPtr(&mut link_ptr as *mut Option>); + let mut link_ptr = LinkPtr(&mut link_ptr as *mut Option>); let mut link_ptr2 = link_ptr.clone(); let dropper = OnDrop::new(|| { @@ -236,7 +233,7 @@ impl<'a, T, const N: usize> Sender<'a, T, N> { } } else { // Place the link in the wait queue on first run. - let link_ref = link.insert(wait_queue::Link::new(cx.waker().clone())); + let link_ref = link.insert(Link::new(cx.waker().clone())); // SAFETY: The address to the link is stable as it is hidden behind // `link_ptr`, and `link_ptr` shadows the original making it unmovable. diff --git a/rtic-common/.gitignore b/rtic-common/.gitignore new file mode 100644 index 0000000000..1e7caa9ea8 --- /dev/null +++ b/rtic-common/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/rtic-common/CHANGELOG.md b/rtic-common/CHANGELOG.md new file mode 100644 index 0000000000..d3a9d846ee --- /dev/null +++ b/rtic-common/CHANGELOG.md @@ -0,0 +1,16 @@ +# Change Log + +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +For each category, *Added*, *Changed*, *Fixed* add new entries at the top! + +## [Unreleased] + +### Added + +### Changed + +### Fixed + +## [v1.0.0] - 2023-xx-xx diff --git a/rtic-common/Cargo.toml b/rtic-common/Cargo.toml new file mode 100644 index 0000000000..258caae06f --- /dev/null +++ b/rtic-common/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rtic-common" +version = "1.0.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +critical-section = "1" + +[features] +default = [] +testing = ["critical-section/std"] diff --git a/rtic-common/src/lib.rs b/rtic-common/src/lib.rs new file mode 100644 index 0000000000..3c75856c8a --- /dev/null +++ b/rtic-common/src/lib.rs @@ -0,0 +1,8 @@ +//! Crate + +#![no_std] +#![deny(missing_docs)] +//deny_warnings_placeholder_for_ci + +pub mod wait_queue; +pub mod waker_registration; diff --git a/rtic-channel/src/wait_queue.rs b/rtic-common/src/wait_queue.rs similarity index 98% rename from rtic-channel/src/wait_queue.rs rename to rtic-common/src/wait_queue.rs index 2de6311d17..ba8ff5471a 100644 --- a/rtic-channel/src/wait_queue.rs +++ b/rtic-common/src/wait_queue.rs @@ -7,6 +7,7 @@ use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; use core::task::Waker; use critical_section as cs; +/// A helper definition of a wait queue. pub type WaitQueue = LinkedList; /// A FIFO linked list for a wait queue. @@ -119,10 +120,12 @@ impl Link { } } + /// Return true if this link has been poped from the list. pub fn is_poped(&self) -> bool { self.is_poped.load(Self::R) } + /// Remove this link from a linked list. pub fn remove_from_list(&mut self, list: &LinkedList) { cs::with(|_| { // Make sure all previous writes are visible diff --git a/rtic-channel/src/waker_registration.rs b/rtic-common/src/waker_registration.rs similarity index 98% rename from rtic-channel/src/waker_registration.rs rename to rtic-common/src/waker_registration.rs index c30df7fe07..174765c2ca 100644 --- a/rtic-channel/src/waker_registration.rs +++ b/rtic-common/src/waker_registration.rs @@ -1,3 +1,5 @@ +//! Waker registration utility. + use core::cell::UnsafeCell; use core::task::Waker;