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

Use more recent Rust features and bump MSRV #64

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ rust-version = "1.59"
nightly = []

[dependencies]
once_cell = "1.5.2"
# this is required to gate `nightly` related code paths
cfg-if = "1.0.0"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ thread_local = "1.1"

## Minimum Rust version

This crate's minimum supported Rust version (MSRV) is 1.59.0.
This crate's minimum supported Rust version (MSRV) is 1.70.0.

## License

Expand Down
36 changes: 12 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,24 @@
//!
//! ```rust
//! use thread_local::ThreadLocal;
//! use std::sync::Arc;
//! use std::cell::Cell;
//! use std::thread;
//!
//! let tls = Arc::new(ThreadLocal::new());
//! let tls = ThreadLocal::new();
//!
//! // Create a bunch of threads to do stuff
//! for _ in 0..5 {
//! let tls2 = tls.clone();
//! thread::spawn(move || {
//! // Increment a counter to count some event...
//! let cell = tls2.get_or(|| Cell::new(0));
//! cell.set(cell.get() + 1);
//! }).join().unwrap();
//! }
//! thread::scope(|scope| {
//! for _ in 0..5 {
//! scope.spawn(|| {
//! // Increment a counter to count some event...
//! let cell = tls.get_or(|| Cell::new(0));
//! cell.set(cell.get() + 1);
//! });
//! }
//! });
//!
//! // Once all threads are done, collect the counter values and return the
//! // sum of all thread-local counter values.
//! let tls = Arc::try_unwrap(tls).unwrap();
//! let total = tls.into_iter().fold(0, |x, y| x + y.get());
//! assert_eq!(total, 5);
//! ```
Expand All @@ -69,7 +68,6 @@

mod cached;
mod thread_id;
mod unreachable;

#[allow(deprecated)]
pub use cached::{CachedIntoIter, CachedIterMut, CachedThreadLocal};
Expand All @@ -83,15 +81,8 @@ use std::panic::UnwindSafe;
use std::ptr;
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize, Ordering};
use thread_id::Thread;
use unreachable::UncheckedResultExt;

// Use usize::BITS once it has stabilized and the MSRV has been bumped.
#[cfg(target_pointer_width = "16")]
const POINTER_WIDTH: u8 = 16;
#[cfg(target_pointer_width = "32")]
const POINTER_WIDTH: u8 = 32;
#[cfg(target_pointer_width = "64")]
const POINTER_WIDTH: u8 = 64;
const POINTER_WIDTH: u8 = usize::BITS as u8;

/// The total number of buckets stored in each thread local.
/// All buckets combined can hold up to `usize::MAX - 1` entries.
Expand Down Expand Up @@ -187,10 +178,7 @@ impl<T: Send> ThreadLocal<T> {
where
F: FnOnce() -> T,
{
unsafe {
self.get_or_try(|| Ok::<T, ()>(create()))
.unchecked_unwrap_ok()
}
unsafe { self.get_or_try(|| Ok::<T, ()>(create())).unwrap_unchecked() }
}

/// Returns the element for the current thread, or creates it if it doesn't
Expand Down
17 changes: 9 additions & 8 deletions src/thread_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
// copied, modified, or distributed except according to those terms.

use crate::POINTER_WIDTH;
use once_cell::sync::Lazy;
use std::cell::Cell;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::sync::Mutex;
use std::sync::{Mutex, OnceLock};

/// Thread ID manager which allocates thread IDs. It attempts to aggressively
/// reuse thread IDs where possible to avoid cases where a ThreadLocal grows
Expand Down Expand Up @@ -41,9 +40,11 @@ impl ThreadIdManager {
fn free(&mut self, id: usize) {
self.free_list.push(Reverse(id));
}
fn singleton() -> &'static Mutex<Self> {
static THREAD_ID_MANAGER: OnceLock<Mutex<ThreadIdManager>> = OnceLock::new();
THREAD_ID_MANAGER.get_or_init(|| Mutex::new(ThreadIdManager::new()))
}
}
static THREAD_ID_MANAGER: Lazy<Mutex<ThreadIdManager>> =
Lazy::new(|| Mutex::new(ThreadIdManager::new()));

/// Data which is unique to the current thread while it is running.
/// A thread ID may be reused after a thread exits.
Expand Down Expand Up @@ -99,7 +100,7 @@ cfg_if::cfg_if! {
unsafe {
THREAD = None;
}
THREAD_ID_MANAGER.lock().unwrap().free(self.id.get());
ThreadIdManager::singleton().lock().unwrap().free(self.id.get());
}
}

Expand All @@ -116,7 +117,7 @@ cfg_if::cfg_if! {
/// Out-of-line slow path for allocating a thread ID.
#[cold]
fn get_slow() -> Thread {
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
let new = Thread::new(ThreadIdManager::singleton().lock().unwrap().alloc());
unsafe {
THREAD = Some(new);
}
Expand Down Expand Up @@ -145,7 +146,7 @@ cfg_if::cfg_if! {
// will go through get_slow which will either panic or
// initialize a new ThreadGuard.
let _ = THREAD.try_with(|thread| thread.set(None));
THREAD_ID_MANAGER.lock().unwrap().free(self.id.get());
ThreadIdManager::singleton().lock().unwrap().free(self.id.get());
}
}

Expand All @@ -164,7 +165,7 @@ cfg_if::cfg_if! {
/// Out-of-line slow path for allocating a thread ID.
#[cold]
fn get_slow(thread: &Cell<Option<Thread>>) -> Thread {
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
let new = Thread::new(ThreadIdManager::singleton().lock().unwrap().alloc());
thread.set(Some(new));
THREAD_GUARD.with(|guard| guard.id.set(new.id));
new
Expand Down
57 changes: 0 additions & 57 deletions src/unreachable.rs

This file was deleted.