Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ weak = []
internal-test-strategies = []
# Possibly some strategies we are experimenting with. Currently empty. No stability guarantees are included about them.
experimental-strategies = []
# no_std doesn't have RwLock
no_std = ["parking_lot"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cargo recommendation for features is that they be additive, not subtractive: https://doc.rust-lang.org/cargo/reference/features.html#feature-unification

Specifically this would imply a std= feature, make the default features include std; though sadly cargo doesn't support managing the dependency properly : rust-lang/cargo#1839.

Perhaps a comment referencing that issue?


[dependencies]
# Optional dependency for supporting no_std
parking_lot = { version = "0.11.1", optional = true }

[dev-dependencies]
adaptive-barrier = "~0.1"
Expand Down
9 changes: 5 additions & 4 deletions src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@
//! work_with_usize(Constant(42)).join().unwrap();
//! ```

use std::marker::PhantomData;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;
use core::marker::PhantomData;
use core::ops::Deref;
use alloc::rc::Rc;
use alloc::sync::Arc;
use alloc::boxed::Box;

use super::ref_cnt::RefCnt;
use super::strategy::Strategy;
Expand Down
4 changes: 2 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//!
//! [Arc]: std::sync::Arc

use std::ops::Deref;
use std::sync::atomic::Ordering;
use core::ops::Deref;
use core::sync::atomic::Ordering;

use super::ref_cnt::RefCnt;
use super::strategy::Strategy;
Expand Down
6 changes: 3 additions & 3 deletions src/debt/fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
//! before the change and before any cleanup of the old pointer happened (in which case we know the
//! writer will see our debt).

use std::cell::Cell;
use std::slice::Iter;
use std::sync::atomic::Ordering::*;
use core::cell::Cell;
use alloc::slice::Iter;
use core::sync::atomic::Ordering::*;

use super::Debt;

Expand Down
8 changes: 4 additions & 4 deletions src/debt/helping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@
//! writer and that change is the destruction ‒ by that time, the destroying thread has exclusive
//! ownership and therefore there can be no new readers.

use std::cell::Cell;
use std::ptr;
use std::sync::atomic::Ordering::*;
use std::sync::atomic::{AtomicPtr, AtomicUsize};
use core::cell::Cell;
use core::ptr;
use core::sync::atomic::Ordering::*;
use core::sync::atomic::{AtomicPtr, AtomicUsize};

use super::Debt;
use crate::RefCnt;
Expand Down
11 changes: 6 additions & 5 deletions src/debt/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
//! at least as up to date value of the writers as when the cooldown started. That we if we see 0,
//! we know it must have happened since then.

use std::cell::Cell;
use std::ptr;
use std::slice::Iter;
use std::sync::atomic::Ordering::*;
use std::sync::atomic::{AtomicPtr, AtomicUsize};
use core::cell::Cell;
use core::ptr;
use alloc::slice::Iter;
use core::sync::atomic::Ordering::*;
use core::sync::atomic::{AtomicPtr, AtomicUsize};
use alloc::boxed::Box;

use super::fast::{Local as FastLocal, Slots as FastSlots};
use super::helping::{Local as HelpingLocal, Slots as HelpingSlots};
Expand Down
6 changes: 3 additions & 3 deletions src/debt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//! Each node has some fast (but fallible) nodes and a fallback node, with different algorithms to
//! claim them (see the relevant submodules).

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::*;
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering::*;

pub(crate) use self::list::{LocalNode, Node};
use super::RefCnt;
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Debt {

let all_slots = node
.fast_slots()
.chain(std::iter::once(node.helping_slot()));
.chain(core::iter::once(node.helping_slot()));
for slot in all_slots {
// Note: Release is enough even here. That makes sure the increment is
// visible to whoever might acquire on this slot and can't leak below this.
Expand Down
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
//!
//! [RwLock]: https://doc.rust-lang.org/std/sync/struct.RwLock.html

#![no_std]
extern crate alloc;

pub mod access;
mod as_raw;
pub mod cache;
Expand All @@ -135,14 +138,14 @@ pub mod strategy;
#[cfg(feature = "weak")]
mod weak;

use std::borrow::Borrow;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::ptr;
use std::sync::atomic::{AtomicPtr, Ordering};
use std::sync::Arc;
use core::borrow::Borrow;
use core::fmt::{Debug, Display, Formatter, Result as FmtResult};
use core::marker::PhantomData;
use core::mem;
use core::ops::Deref;
use core::ptr;
use core::sync::atomic::{AtomicPtr, Ordering};
use alloc::sync::Arc;

use crate::access::{Access, Map};
pub use crate::as_raw::AsRaw;
Expand Down
6 changes: 3 additions & 3 deletions src/ref_cnt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ptr;
use std::rc::Rc;
use std::sync::Arc;
use core::ptr;
use alloc::rc::Rc;
use alloc::sync::Arc;

/// A trait describing smart reference counted pointers.
///
Expand Down
12 changes: 6 additions & 6 deletions src/strategy/hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
//! See the [crate::debt] module for the actual slot manipulation. Here we just wrap them into the
//! strategy.

use std::borrow::Borrow;
use std::mem::{self, ManuallyDrop};
use std::ops::Deref;
use std::ptr;
use std::sync::atomic::AtomicPtr;
use std::sync::atomic::Ordering::*;
use core::borrow::Borrow;
use core::mem::{self, ManuallyDrop};
use core::ops::Deref;
use core::ptr;
use core::sync::atomic::AtomicPtr;
use core::sync::atomic::Ordering::*;

use super::sealed::{CaS, InnerStrategy, Protected};
use crate::debt::{Debt, LocalNode};
Expand Down
4 changes: 2 additions & 2 deletions src/strategy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
//! [`ArcSwap`]: crate::ArcSwap
//! [`load`]: crate::ArcSwapAny::load

use std::borrow::Borrow;
use std::sync::atomic::AtomicPtr;
use core::borrow::Borrow;
use core::sync::atomic::AtomicPtr;

use crate::ref_cnt::RefCnt;

Expand Down
15 changes: 14 additions & 1 deletion src/strategy/rw_lock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use std::sync::atomic::{AtomicPtr, Ordering};
use core::sync::atomic::{AtomicPtr, Ordering};

#[cfg(not(feature = "no_std"))]
extern crate std;
#[cfg(not(feature = "no_std"))]
use std::sync::RwLock;

#[cfg(feature = "no_std")]
use parking_lot::RwLock;

use super::sealed::{CaS, InnerStrategy, Protected};
use crate::as_raw::AsRaw;
use crate::ref_cnt::RefCnt;
Expand All @@ -20,7 +27,10 @@ impl<T: RefCnt> Protected<T> for T {
impl<T: RefCnt> InnerStrategy<T> for RwLock<()> {
type Protected = T;
unsafe fn load(&self, storage: &AtomicPtr<T::Base>) -> T {
#[cfg(not(feature = "no_std"))]
let _guard = self.read().expect("We don't panic in here");
#[cfg(feature = "no_std")]
let _guard = self.read();
let ptr = storage.load(Ordering::Acquire);
let ptr = T::from_ptr(ptr as *const T::Base);
T::inc(&ptr);
Expand All @@ -30,7 +40,10 @@ impl<T: RefCnt> InnerStrategy<T> for RwLock<()> {

unsafe fn wait_for_readers(&self, _: *const T::Base, _: &AtomicPtr<T::Base>) {
// By acquiring the write lock, we make sure there are no read locks present across it.
#[cfg(not(feature = "no_std"))]
drop(self.write().expect("We don't panic in here"));
#[cfg(feature = "no_std")]
drop(self.write());
}
}

Expand Down