Skip to content

Commit

Permalink
Remove once_cell dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Apr 23, 2024
1 parent 6920311 commit d7f100a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
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

0 comments on commit d7f100a

Please sign in to comment.