Skip to content

Commit

Permalink
refact!: remove thread local support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromfedricci committed Sep 29, 2024
1 parent ee20977 commit ea141c9
Show file tree
Hide file tree
Showing 10 changed files with 5 additions and 349 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ jobs:
run: rustup toolchain install stable
- name: Run raw example
run: cargo run --example raw
- name: Run thread_local example
run: cargo run --example thread_local --features thread_local

linter:
name: Linter
Expand Down
9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An implementation of Craig and, indenpendently, Magnussen, Landin, and
Hagersten queue lock for mutual exclusion, referred to as CLH lock.
"""
name = "clhlock"
version = "0.2.0"
version = "0.2.0-rc1"
edition = "2021"
# NOTE: Rust 1.70 is required for `AtomicPtr::as_ptr`.
rust-version = "1.70.0"
Expand All @@ -17,9 +17,8 @@ categories = ["algorithms", "concurrency", "no-std"]
keywords = ["mutex", "no_std", "spinlock", "synchronization"]

[features]
# NOTE: Features `yield` and `thread_local` require std.
# NOTE: Feature `yield` requires std.
yield = []
thread_local = []

[target.'cfg(loom)'.dev-dependencies.loom]
version = "0.7"
Expand All @@ -31,7 +30,3 @@ rustdoc-args = ["--cfg", "docsrs"]
[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ["cfg(tarpaulin)", "cfg(tarpaulin_include)", "cfg(loom)"]

[[example]]
name = "thread_local"
required-features = ["thread_local"]
48 changes: 2 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Or add a entry under the `[dependencies]` section in your `Cargo.toml`:
# Cargo.toml

[dependencies]
# Available features: `yield` and `thread_local`.
clhlock = { version = "0.2", features = ["yield", "thread_local"] }
# Available features: `yield`.
clhlock = { version = "0.2.0-rc1", features = ["yield"] }
```

## Documentation
Expand Down Expand Up @@ -104,40 +104,6 @@ fn main() {
}
```

## Thread local raw CLH spinlock nodes

Enables [`raw::Mutex`] locking APIs that operate over queue node handles
that are stored at the thread local storage. These locking APIs require a
static reference to a [`raw::LocalMutexNode`] key. Keys must be generated by
the [`thread_local_node!`] macro. Thread local nodes are not `no_std`
compatible and can be enabled through the `thread_local` feature.

```rust
use std::sync::Arc;
use std::thread;

// `spins::Mutex` simply spins during contention.
use clhlock::raw::spins::Mutex;

// Requires `thread_local` feature.
clhlock::thread_local_node!(static NODE);

fn main() {
let mutex = Arc::new(Mutex::new(0));
let c_mutex = Arc::clone(&mutex);
thread::spawn(move || {
// Local node handles are provided by reference.
// Critical section must be defined as a closure.
c_mutex.lock_with_local(&NODE, |mut guard| *guard = 10);
})
.join().expect("thread::spawn failed");

// Local node handles are provided by reference.
// Critical section must be defined as a closure.
assert_eq!(mutex.lock_with_local(&NODE, |guard| *guard), 10);
}
```

## Features

This crate dos not provide any default features. Features that can be enabled
Expand All @@ -154,14 +120,6 @@ this feature if your intention is to to actually do optimistic spinning. The
default implementation calls [`core::hint::spin_loop`], which does in fact
just simply busy-waits. This feature is not `not_std` compatible.

### thread_local

The `thread_local` feature enables [`raw::Mutex`] locking APIs that operate
over queue node handles that are stored at the thread local storage. These
locking APIs require a static reference to [`raw::LocalMutexNode`] keys.
Keys must be generated by the [`thread_local_node!`] macro. This feature is
not `no_std` compatible.

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on a Minimum Supported Rust Version (MSRV)
Expand Down Expand Up @@ -209,8 +167,6 @@ each of your dependencies, including this one.
[`raw`]: https://docs.rs/clhlock/latest/clhlock/raw/index.html
[`raw::Mutex`]: https://docs.rs/clhlock/latest/clhlock/raw/struct.Mutex.html
[`raw::MutexNode`]: https://docs.rs/clhlock/latest/clhlock/raw/struct.MutexNode.html
[`raw::LocalMutexNode`]: https://docs.rs/clhlock/latest/clhlock/raw/struct.LocalMutexNode.html
[`thread_local_node!`]: https://docs.rs/clhlock/latest/clhlock/macro.thread_local_node.html

[alloc]: https://doc.rust-lang.org/alloc/index.html
[`std::sync::Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
Expand Down
51 changes: 0 additions & 51 deletions examples/thread_local.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,4 @@ pub mod thread {

#[cfg(all(loom, test))]
pub use loom::thread::yield_now;

#[cfg(all(feature = "thread_local", not(all(loom, test))))]
pub use std::thread::LocalKey;

#[cfg(all(feature = "thread_local", loom, test))]
pub use loom::thread::LocalKey;
}
5 changes: 0 additions & 5 deletions src/inner/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ use crate::cfg::atomic::{fence, AtomicPtr, UnsyncLoad};
use crate::cfg::cell::{Cell, CellNullMut, UnsafeCell, WithUnchecked};
use crate::lock::{Lock, Wait};

#[cfg(feature = "thread_local")]
mod thread_local;
#[cfg(feature = "thread_local")]
pub use thread_local::LocalMutexNode;

/// The heap allocated queue node, which is managed by the [`MutexNode`] type.
#[derive(Debug)]
struct MutexNodeInner<L> {
Expand Down
125 changes: 0 additions & 125 deletions src/inner/raw/thread_local.rs

This file was deleted.

55 changes: 1 addition & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,6 @@
//! assert_eq!(*mutex.lock(&mut node), 10);
//! ```
//!
//! ## Thread local raw CLH spinlock nodes
//!
//! Enables [`raw::Mutex`] locking APIs that operate over queue node handles
//! that are stored at the thread local storage. These locking APIs require a
//! static reference to a [`raw::LocalMutexNode`] key. Keys must be generated by
//! the [`thread_local_node!`] macro. Thread local nodes are not `no_std`
//! compatible and can be enabled through the `thread_local` feature.
//!
//! ```
//! # #[cfg(feature = "thread_local")]
//! # {
//! use std::sync::Arc;
//! use std::thread;
//!
//! // `spins::Mutex` simply spins during contention.
//! use clhlock::raw::spins::Mutex;
//!
//! // Requires `thread_local` feature.
//! clhlock::thread_local_node!(static NODE);
//!
//! let mutex = Arc::new(Mutex::new(0));
//! let c_mutex = Arc::clone(&mutex);
//!
//! thread::spawn(move || {
//! // Local node handles are provided by reference.
//! // Critical section must be defined as a closure.
//! c_mutex.lock_with_local(&NODE, |mut guard| *guard = 10);
//! })
//! .join().expect("thread::spawn failed");
//!
//! // Local node handles are provided by reference.
//! // Critical section must be defined as a closure.
//! assert_eq!(mutex.lock_with_local(&NODE, |guard| *guard), 10);
//! # }
//! # #[cfg(not(feature = "thread_local"))]
//! # fn main() {}
//! ```
//!
//! ## Features
//!
//! This crate dos not provide any default features. Features that can be enabled
Expand All @@ -123,25 +85,14 @@
//! default implementation calls [`core::hint::spin_loop`], which does in fact
//! just simply busy-waits. This feature is not `not_std` compatible.
//!
//! ### thread_local
//!
//! The `thread_local` feature enables [`raw::Mutex`] locking APIs that operate
//! over queue node handles that are stored at the thread local storage. These
//! locking APIs require a static reference to [`raw::LocalMutexNode`] keys.
//! Keys must be generated by the [`thread_local_node!`] macro. This feature is
//! not `no_std` compatible.
//!
//! [lock]: https://en.wikipedia.org/wiki/Lock_(computer_science)
//! [Craig]: https://dada.cs.washington.edu/research/tr/1993/02/UW-CSE-93-02-02.pdf
//! [Magnussen, Landin, and Hagersten]: https://www2.it.uu.se/research/group/uart/pub/magnusson_1994_jan/magnusson_1994_jan.pdf
//! [spinlocks are usually not what you want]: https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html
//!
//! [`parking_lot::Mutex`]: https://docs.rs/parking_lot/latest/parking_lot/type.Mutex.html

#![cfg_attr(
all(not(feature = "yield"), not(feature = "thread_local"), not(loom), not(test)),
no_std
)]
#![cfg_attr(all(not(feature = "yield"), not(loom), not(test)), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::inline_always)]
Expand All @@ -151,10 +102,6 @@

extern crate alloc;

#[cfg(feature = "thread_local")]
#[macro_use]
pub(crate) mod thread_local;

pub mod raw;
pub mod relax;

Expand Down
Loading

0 comments on commit ea141c9

Please sign in to comment.