Skip to content

Commit 4e9adfe

Browse files
committed
Use OnceLock in JobTokenServer::new
Also impls `Send`, `Sync`, `RefUnwindSafe` and `UnwindSafed` when the `T` meets the criterior. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent e9ce514 commit 4e9adfe

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/parallel/job_token.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{marker::PhantomData, mem::MaybeUninit, sync::Once};
1+
use std::marker::PhantomData;
22

3-
use crate::Error;
3+
use crate::{parallel::once_lock::OnceLock, Error};
44

55
pub(crate) struct JobToken(PhantomData<()>);
66

@@ -34,19 +34,13 @@ impl JobTokenServer {
3434
/// that has to be static so that it will be shared by all cc
3535
/// compilation.
3636
fn new() -> &'static Self {
37-
// TODO: Replace with a OnceLock once MSRV is 1.70
38-
static INIT: Once = Once::new();
39-
static mut JOBSERVER: MaybeUninit<JobTokenServer> = MaybeUninit::uninit();
40-
41-
unsafe {
42-
INIT.call_once(|| {
43-
let server = inherited_jobserver::JobServer::from_env()
44-
.map(Self::Inherited)
45-
.unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new()));
46-
JOBSERVER.write(server);
47-
});
48-
JOBSERVER.assume_init_ref()
49-
}
37+
static JOBSERVER: OnceLock<JobTokenServer> = OnceLock::new();
38+
39+
JOBSERVER.get_or_init(|| {
40+
unsafe { inherited_jobserver::JobServer::from_env() }
41+
.map(Self::Inherited)
42+
.unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new()))
43+
})
5044
}
5145
}
5246

@@ -76,12 +70,9 @@ impl ActiveJobTokenServer {
7670
}
7771

7872
mod inherited_jobserver {
79-
use super::JobToken;
73+
use super::{JobToken, OnceLock};
8074

81-
use crate::{
82-
parallel::{async_executor::YieldOnce, once_lock::OnceLock},
83-
Error, ErrorKind,
84-
};
75+
use crate::{parallel::async_executor::YieldOnce, Error, ErrorKind};
8576

8677
use std::{
8778
io, mem,

src/parallel/once_lock.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
//! is initialised and MSRV is high enoguh to use it.
44
55
use std::{
6-
cell::UnsafeCell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, sync::Once,
6+
cell::UnsafeCell,
7+
convert::Infallible,
8+
marker::PhantomData,
9+
mem::MaybeUninit,
10+
panic::{RefUnwindSafe, UnwindSafe},
11+
sync::Once,
712
};
813

914
pub struct OnceLock<T> {
@@ -31,6 +36,12 @@ pub struct OnceLock<T> {
3136
_marker: PhantomData<T>,
3237
}
3338

39+
unsafe impl<T: Sync + Send> Sync for OnceLock<T> {}
40+
unsafe impl<T: Send> Send for OnceLock<T> {}
41+
42+
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T> {}
43+
impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
44+
3445
impl<T> OnceLock<T> {
3546
pub const fn new() -> OnceLock<T> {
3647
OnceLock {
@@ -49,7 +60,6 @@ impl<T> OnceLock<T> {
4960
}
5061
}
5162

52-
#[allow(dead_code)]
5363
pub fn get_or_init<F>(&self, f: F) -> &T
5464
where
5565
F: FnOnce() -> T,

0 commit comments

Comments
 (0)