Skip to content

Commit

Permalink
Merge pull request #106 from Expyron/main
Browse files Browse the repository at this point in the history
Replace `lazy_static` with `once_cell`
  • Loading branch information
palfrey committed Jan 27, 2024
2 parents bba3db8 + 2f67a8d commit 5a3aba1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.
15 changes: 5 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion serial_test/Cargo.toml
Expand Up @@ -11,7 +11,7 @@ categories = ["development-tools::testing"]
keywords = ["sequential", "testing", "parallel"]

[dependencies]
lazy_static = "1.2"
once_cell = "^1.19"
parking_lot = "^0.12"
serial_test_derive = { version = "~3.0.0", path = "../serial_test_derive" }
fslock = { version = "0.2", optional = true }
Expand Down
18 changes: 10 additions & 8 deletions serial_test/src/code_lock.rs
@@ -1,9 +1,9 @@
use crate::rwlock::{Locks, MutexGuardWrapper};
use dashmap::{try_result::TryResult, DashMap};
use lazy_static::lazy_static;
#[cfg(feature = "logging")]
use log::debug;
use std::sync::{atomic::AtomicU32, Arc};
use once_cell::sync::OnceCell;
use std::sync::atomic::AtomicU32;
#[cfg(feature = "logging")]
use std::time::Instant;

Expand Down Expand Up @@ -40,12 +40,14 @@ impl UniqueReentrantMutex {
}
}

lazy_static! {
pub(crate) static ref LOCKS: Arc<DashMap<String, UniqueReentrantMutex>> =
Arc::new(DashMap::new());
static ref MUTEX_ID: Arc<AtomicU32> = Arc::new(AtomicU32::new(1));
#[inline]
pub(crate) fn global_locks() -> &'static DashMap<String, UniqueReentrantMutex> {
static LOCKS: OnceCell<DashMap<String, UniqueReentrantMutex>> = OnceCell::new();
LOCKS.get_or_init(DashMap::new)
}

static MUTEX_ID: AtomicU32 = AtomicU32::new(1);

impl Default for UniqueReentrantMutex {
fn default() -> Self {
Self {
Expand All @@ -65,7 +67,7 @@ pub(crate) fn check_new_key(name: &str) {
debug!("Waiting for '{}' {:?}", name, duration);
}
// Check if a new key is needed. Just need a read lock, which can be done in sync with everyone else
match LOCKS.try_get(name) {
match global_locks().try_get(name) {
TryResult::Present(_) => {
return;
}
Expand All @@ -76,7 +78,7 @@ pub(crate) fn check_new_key(name: &str) {
};

// This is the rare path, which avoids the multi-writer situation mostly
let try_entry = LOCKS.try_entry(name.to_string());
let try_entry = global_locks().try_entry(name.to_string());

if let Some(entry) = try_entry {
entry.or_default();
Expand Down
14 changes: 7 additions & 7 deletions serial_test/src/parallel_code_lock.rs
@@ -1,6 +1,6 @@
#![allow(clippy::await_holding_lock)]

use crate::code_lock::{check_new_key, LOCKS};
use crate::code_lock::{check_new_key, global_locks};
#[cfg(feature = "async")]
use futures::FutureExt;
use std::panic;
Expand All @@ -12,7 +12,7 @@ fn get_locks(
.into_iter()
.map(|name| {
check_new_key(name);
LOCKS.get(name).expect("key to be set")
global_locks().get(name).expect("key to be set")
})
.collect::<Vec<_>>()
}
Expand Down Expand Up @@ -89,7 +89,7 @@ mod tests {
#[cfg(feature = "async")]
use crate::{local_async_parallel_core, local_async_parallel_core_with_return};

use crate::{code_lock::LOCKS, local_parallel_core, local_parallel_core_with_return};
use crate::{code_lock::global_locks, local_parallel_core, local_parallel_core_with_return};
use std::{io::Error, panic};

#[test]
Expand All @@ -100,7 +100,7 @@ mod tests {
})
});
assert_eq!(
LOCKS
global_locks()
.get("unlock_on_assert_sync_without_return")
.unwrap()
.parallel_count(),
Expand All @@ -121,7 +121,7 @@ mod tests {
)
});
assert_eq!(
LOCKS
global_locks()
.get("unlock_on_assert_sync_with_return")
.unwrap()
.parallel_count(),
Expand Down Expand Up @@ -150,7 +150,7 @@ mod tests {
futures::executor::block_on(call_serial_test_fn());
});
assert_eq!(
LOCKS
global_locks()
.get("unlock_on_assert_async_without_return")
.unwrap()
.parallel_count(),
Expand Down Expand Up @@ -183,7 +183,7 @@ mod tests {
futures::executor::block_on(call_serial_test_fn());
});
assert_eq!(
LOCKS
global_locks()
.get("unlock_on_assert_async_with_return")
.unwrap()
.parallel_count(),
Expand Down
10 changes: 5 additions & 5 deletions serial_test/src/serial_code_lock.rs
@@ -1,6 +1,6 @@
#![allow(clippy::await_holding_lock)]

use crate::code_lock::{check_new_key, LOCKS};
use crate::code_lock::{check_new_key, global_locks};

#[doc(hidden)]
macro_rules! core_internal {
Expand All @@ -9,7 +9,7 @@ macro_rules! core_internal {
.into_iter()
.map(|name| {
check_new_key(name);
LOCKS.get(name).expect("key to be set")
global_locks().get(name).expect("key to be set")
})
.collect();
let _guards: Vec<_> = unlocks.iter().map(|unlock| unlock.lock()).collect();
Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn local_async_serial_core(
#[allow(clippy::print_stdout)]
mod tests {
use super::local_serial_core;
use crate::code_lock::{check_new_key, LOCKS};
use crate::code_lock::{check_new_key, global_locks};
use itertools::Itertools;
use parking_lot::RwLock;
use std::{
Expand All @@ -76,7 +76,7 @@ mod tests {
let barrier = Arc::new(Barrier::new(count));

for _ in 0..count {
let local_locks = LOCKS.clone();
let local_locks = global_locks();
let local_ptrs = ptrs.clone();
let c = barrier.clone();
threads.push(thread::spawn(move || {
Expand Down Expand Up @@ -113,6 +113,6 @@ mod tests {
assert!(false);
})
});
assert!(!LOCKS.get("assert").unwrap().is_locked());
assert!(!global_locks().get("assert").unwrap().is_locked());
}
}
5 changes: 3 additions & 2 deletions serial_test_test/Cargo.toml
Expand Up @@ -9,10 +9,11 @@ rust-version = "1.68.2"

[dependencies]
serial_test = { path="../serial_test", default_features = false }
lazy_static = "^1.2"
once_cell = "^1.19"
env_logger = "^0.10"
parking_lot = "^0.12"
wasm-bindgen-test = {version="0.3.0", optional=true}
lock_api = "^0.4.7"
wasm-bindgen-test = {version="0.3.20", optional=true}
scoped-tls = {version="1", optional=true}

[dev-dependencies]
Expand Down
51 changes: 26 additions & 25 deletions serial_test_test/src/lib.rs
Expand Up @@ -38,23 +38,17 @@
//! fn main() {}
//! ```

use lazy_static::lazy_static;
#[cfg(test)]
use serial_test::{parallel, serial};
use std::{
convert::TryInto,
env, fs,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
sync::atomic::{AtomicUsize, Ordering},
thread,
time::Duration,
};

lazy_static! {
static ref LOCK: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0));
}
static LOCK: AtomicUsize = AtomicUsize::new(0);

fn init() {
let _ = env_logger::builder().is_test(false).try_init();
Expand Down Expand Up @@ -95,22 +89,29 @@ mod parallel_attr_tests {}
#[cfg(test)]
mod tests {
use super::{init, test_fn};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use serial_test::{parallel, serial};
use std::{
sync::{Arc, Barrier},
thread,
time::Duration,
};
use std::{sync::Barrier, thread, time::Duration};
#[cfg(feature = "async")]
use wasm_bindgen_test::wasm_bindgen_test;

lazy_static! {
static ref THREAD_ORDERINGS: Arc<Mutex<Vec<bool>>> = Arc::new(Mutex::new(Vec::new()));
static ref FS_THREAD_ORDERINGS: Arc<Mutex<Vec<bool>>> = Arc::new(Mutex::new(Vec::new()));
static ref PARALLEL_BARRIER: Barrier = Barrier::new(3);
static ref FS_PARALLEL_BARRIER: Barrier = Barrier::new(3);
static THREAD_ORDERINGS: Mutex<Vec<bool>> = Mutex::new(Vec::new());

#[inline]
fn parallel_barrier() -> &'static Barrier {
static PARALLEL_BARRIER: OnceCell<Barrier> = OnceCell::new();
PARALLEL_BARRIER.get_or_init(|| Barrier::new(3))
}

#[cfg(feature = "file_locks")]
static FS_THREAD_ORDERINGS: Mutex<Vec<bool>> = Mutex::new(Vec::new());

#[cfg(feature = "file_locks")]
#[inline]
fn fs_parallel_barrier() -> &'static Barrier {
static FS_PARALLEL_BARRIER: OnceCell<Barrier> = OnceCell::new();
FS_PARALLEL_BARRIER.get_or_init(|| Barrier::new(3))
}

#[cfg(feature = "file_locks")]
Expand Down Expand Up @@ -264,7 +265,7 @@ mod tests {
fn parallel_with_key_1() {
thread::sleep(Duration::from_secs(1));
println!("Waiting barrier 1");
PARALLEL_BARRIER.wait();
parallel_barrier().wait();
println!("Waiting lock 1");
THREAD_ORDERINGS.lock().push(false);
}
Expand All @@ -274,7 +275,7 @@ mod tests {
fn parallel_with_key_2() {
thread::sleep(Duration::from_secs(2));
println!("Waiting barrier 2");
PARALLEL_BARRIER.wait();
parallel_barrier().wait();
println!("Waiting lock 2");
THREAD_ORDERINGS.lock().push(false);
}
Expand All @@ -284,7 +285,7 @@ mod tests {
fn parallel_with_key_3() {
thread::sleep(Duration::from_secs(3));
println!("Waiting barrier 3");
PARALLEL_BARRIER.wait();
parallel_barrier().wait();
println!("Waiting lock 3");
THREAD_ORDERINGS.lock().push(false);
}
Expand Down Expand Up @@ -322,7 +323,7 @@ mod tests {
init();
thread::sleep(Duration::from_secs(1));
println!("Waiting barrier 1");
FS_PARALLEL_BARRIER.wait();
fs_parallel_barrier().wait();
println!("Waiting lock 1");
FS_THREAD_ORDERINGS.lock().push(false);
}
Expand All @@ -334,7 +335,7 @@ mod tests {
init();
thread::sleep(Duration::from_secs(1));
println!("Waiting barrier 2");
FS_PARALLEL_BARRIER.wait();
fs_parallel_barrier().wait();
println!("Waiting lock 2");
FS_THREAD_ORDERINGS.lock().push(false);
}
Expand All @@ -346,7 +347,7 @@ mod tests {
init();
thread::sleep(Duration::from_secs(1));
println!("Waiting barrier 3");
FS_PARALLEL_BARRIER.wait();
fs_parallel_barrier().wait();
println!("Waiting lock 3");
FS_THREAD_ORDERINGS.lock().push(false);
}
Expand Down

0 comments on commit 5a3aba1

Please sign in to comment.