Skip to content

Commit

Permalink
Replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed May 1, 2022
1 parent 450d237 commit 08e41e1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
12 changes: 3 additions & 9 deletions crossbeam-epoch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default = ["std"]

# Enable to use APIs that require `std`.
# This is enabled by default.
std = ["alloc", "crossbeam-utils/std", "lazy_static"]
std = ["alloc", "crossbeam-utils/std", "once_cell"]

# Enable to use APIs that require `alloc`.
# This is enabled by default and also enabled if the `std` feature is enabled.
Expand Down Expand Up @@ -48,6 +48,8 @@ autocfg = "1"
[dependencies]
cfg-if = "1"
memoffset = "0.6"
once_cell = { version = "1", optional = true }
scopeguard = { version = "1.1", default-features = false }

# Enable the use of loom for concurrency testing.
#
Expand All @@ -61,14 +63,6 @@ version = "0.8.5"
path = "../crossbeam-utils"
default-features = false

[dependencies.lazy_static]
version = "1.4.0"
optional = true

[dependencies.scopeguard]
version = "1.1.0"
default-features = false

[dev-dependencies]
rand = "0.8"
rustversion = "1"
12 changes: 10 additions & 2 deletions crossbeam-epoch/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

use crate::collector::{Collector, LocalHandle};
use crate::guard::Guard;
use crate::primitive::{lazy_static, thread_local};
use crate::primitive::thread_local;
#[cfg(not(crossbeam_loom))]
use once_cell::sync::Lazy;

lazy_static! {
/// The global data for the default garbage collector.
#[cfg(not(crossbeam_loom))]
static COLLECTOR: Lazy<Collector> = Lazy::new(Collector::new);
// FIXME: loom does not currently provide the equivalent of Lazy:
// https://github.com/tokio-rs/loom/issues/263
#[cfg(crossbeam_loom)]
loom::lazy_static! {
/// The global data for the default garbage collector.
static ref COLLECTOR: Collector = Collector::new();
}
Expand Down
4 changes: 0 additions & 4 deletions crossbeam-epoch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ mod primitive {
}
pub(crate) use loom::sync::Arc;
}
pub(crate) use loom::lazy_static;
pub(crate) use loom::thread_local;
}
#[cfg(not(crossbeam_no_atomic_cas))]
Expand Down Expand Up @@ -135,9 +134,6 @@ mod primitive {

#[cfg(feature = "std")]
pub(crate) use std::thread_local;

#[cfg(feature = "std")]
pub(crate) use lazy_static::lazy_static;
}

#[cfg(not(crossbeam_no_atomic_cas))]
Expand Down
4 changes: 2 additions & 2 deletions crossbeam-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default = ["std"]

# Enable to use APIs that require `std`.
# This is enabled by default.
std = ["lazy_static"]
std = ["once_cell"]

# These features are no longer used.
# TODO: remove in the next major version.
Expand All @@ -32,7 +32,7 @@ nightly = []

[dependencies]
cfg-if = "1"
lazy_static = { version = "1.4.0", optional = true }
once_cell = { version = "1", optional = true }

# Enable the use of loom for concurrency testing.
#
Expand Down
10 changes: 5 additions & 5 deletions crossbeam-utils/src/sync/sharded_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::thread::{self, ThreadId};

use crate::CachePadded;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;

/// The number of shards per sharded lock. Must be a power of two.
const NUM_SHARDS: usize = 8;
Expand Down Expand Up @@ -583,13 +583,13 @@ struct ThreadIndices {
next_index: usize,
}

lazy_static! {
static ref THREAD_INDICES: Mutex<ThreadIndices> = Mutex::new(ThreadIndices {
static THREAD_INDICES: Lazy<Mutex<ThreadIndices>> = Lazy::new(|| {
Mutex::new(ThreadIndices {
mapping: HashMap::new(),
free_list: Vec::new(),
next_index: 0,
});
}
})
});

/// A registration of a thread with an index.
///
Expand Down

0 comments on commit 08e41e1

Please sign in to comment.