Skip to content

Commit

Permalink
Merge pull request #300 from tamird/once-lock-std
Browse files Browse the repository at this point in the history
Replace `once_cell` with std's `OnceLock`
  • Loading branch information
Manishearth authored Dec 27, 2024
2 parents b388262 + fbf56ed commit 8dc0163
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ diff = "0.1.10"
filetime = "0.2"
getopts = "0.2"
log = "0.4"
once_cell = "1.20"
regex = "1.0"
rustfix = "0.8"
rustversion = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use filetime::FileTime;
use regex::Regex;
use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};

use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
Expand All @@ -33,20 +32,21 @@ use std::io::{self, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Output, Stdio};
use std::str;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{Arc, Mutex, OnceLock, RwLock};

use crate::extract_gdb_version;

fn get_or_create_coverage_file(path: &Path, create: impl FnOnce() -> File) -> Arc<Mutex<File>> {
static COVERAGE_FILE_LOCKS: Lazy<RwLock<HashMap<PathBuf, Arc<Mutex<File>>>>> =
Lazy::new(Default::default);
static COVERAGE_FILE_LOCKS: OnceLock<RwLock<HashMap<PathBuf, Arc<Mutex<File>>>>> =
OnceLock::new();

let locks = COVERAGE_FILE_LOCKS.get_or_init(Default::default);
{
let locks = COVERAGE_FILE_LOCKS.read().unwrap();
let locks = locks.read().unwrap();
locks.get(path).map(Arc::clone)
}
.unwrap_or_else(|| {
let mut locks = COVERAGE_FILE_LOCKS.write().unwrap();
let mut locks = locks.write().unwrap();
locks
.entry(path.to_path_buf())
.or_insert_with(|| Arc::new(Mutex::new(create())))
Expand Down
4 changes: 2 additions & 2 deletions tests/bless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ extern crate compiletest_rs as compiletest;

mod test_support;
use crate::compiletest::Config;
use crate::test_support::{testsuite, TestsuiteBuilder, GLOBAL_ROOT};
use crate::test_support::{global_root, testsuite, TestsuiteBuilder};

fn setup(mode: &str) -> (Config, TestsuiteBuilder) {
let builder = testsuite(mode);
let mut config = Config::default();
let cfg_mode = mode.parse().expect("Invalid mode");
config.mode = cfg_mode;
config.src_base = builder.root.clone();
config.build_base = GLOBAL_ROOT.join("build_base");
config.build_base = global_root().join("build_base");

(config, builder)
}
Expand Down
32 changes: 19 additions & 13 deletions tests/test_support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
//!
//! Inspired by cargo's `cargo-test-support` crate:
//! https://github.com/rust-lang/cargo/tree/master/crates/cargo-test-support
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{
atomic::{AtomicUsize, Ordering},
OnceLock,
};

static COMPILETEST_INTEGRATION_TEST_DIR: &str = "cit";

thread_local! {
static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
}

pub static GLOBAL_ROOT: Lazy<PathBuf> = Lazy::new(|| {
let mut path = env::current_exe().unwrap();
path.pop(); // chop off exe name
path.pop(); // chop off 'deps' part
path.pop(); // chop off 'debug'

path.push(COMPILETEST_INTEGRATION_TEST_DIR);
path.mkdir_p();
path
});
pub fn global_root() -> &'static Path {
static GLOBAL_ROOT: OnceLock<PathBuf> = OnceLock::new();
GLOBAL_ROOT.get_or_init(|| {
let mut path = env::current_exe().unwrap();
path.pop(); // chop off exe name
path.pop(); // chop off 'deps' part
path.pop(); // chop off 'debug'

path.push(COMPILETEST_INTEGRATION_TEST_DIR);
path.mkdir_p();
path
})
}

pub fn testsuite(mode: &str) -> TestsuiteBuilder {
let builder = TestsuiteBuilder::new(mode);
Expand All @@ -42,7 +48,7 @@ impl TestsuiteBuilder {

let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
TEST_ID.with(|n| *n.borrow_mut() = Some(id));
let root = GLOBAL_ROOT
let root = global_root()
.join(format!("id{}", TEST_ID.with(|n| n.borrow().unwrap())))
.join(mode);
root.mkdir_p();
Expand Down

0 comments on commit 8dc0163

Please sign in to comment.