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

Remove once_cell dependency #76

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ repository = "https://github.com/Amanieu/thread_local-rs"
readme = "README.md"
keywords = ["thread_local", "concurrent", "thread"]
edition = "2021"
rust-version = "1.61"
rust-version = "1.63"

[features]
# this feature provides performance improvements using nightly features
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.61.0.
This crate's minimum supported Rust version (MSRV) is 1.63.0.

## License

Expand Down
20 changes: 12 additions & 8 deletions src/thread_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// 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;
Expand All @@ -17,17 +16,19 @@ use std::sync::Mutex;
/// indefinitely when it is used by many short-lived threads.
struct ThreadIdManager {
free_from: usize,
free_list: BinaryHeap<Reverse<usize>>,
free_list: Option<BinaryHeap<Reverse<usize>>>,
}

impl ThreadIdManager {
fn new() -> Self {
const fn new() -> Self {
Self {
free_from: 0,
free_list: BinaryHeap::new(),
free_list: None,
}
}

fn alloc(&mut self) -> usize {
if let Some(id) = self.free_list.pop() {
if let Some(id) = self.free_list.as_mut().and_then(|heap| heap.pop()) {
id.0
} else {
// `free_from` can't overflow as each thread takes up at least 2 bytes of memory and
Expand All @@ -38,12 +39,15 @@ impl ThreadIdManager {
id
}
}

fn free(&mut self, id: usize) {
self.free_list.push(Reverse(id));
self.free_list
.get_or_insert_with(BinaryHeap::new)
.push(Reverse(id));
}
}
static THREAD_ID_MANAGER: Lazy<Mutex<ThreadIdManager>> =
Lazy::new(|| Mutex::new(ThreadIdManager::new()));

static THREAD_ID_MANAGER: Mutex<ThreadIdManager> = 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