diff --git a/src/parallel/job_token.rs b/src/parallel/job_token.rs index f9f4d231..f164ebd0 100644 --- a/src/parallel/job_token.rs +++ b/src/parallel/job_token.rs @@ -1,6 +1,6 @@ -use std::{marker::PhantomData, mem::MaybeUninit, sync::Once}; +use std::marker::PhantomData; -use crate::Error; +use crate::{parallel::once_lock::OnceLock, Error}; pub(crate) struct JobToken(PhantomData<()>); @@ -34,19 +34,13 @@ impl JobTokenServer { /// that has to be static so that it will be shared by all cc /// compilation. fn new() -> &'static Self { - // TODO: Replace with a OnceLock once MSRV is 1.70 - static INIT: Once = Once::new(); - static mut JOBSERVER: MaybeUninit = MaybeUninit::uninit(); - - unsafe { - INIT.call_once(|| { - let server = inherited_jobserver::JobServer::from_env() - .map(Self::Inherited) - .unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new())); - JOBSERVER.write(server); - }); - JOBSERVER.assume_init_ref() - } + static JOBSERVER: OnceLock = OnceLock::new(); + + JOBSERVER.get_or_init(|| { + unsafe { inherited_jobserver::JobServer::from_env() } + .map(Self::Inherited) + .unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new())) + }) } } @@ -76,12 +70,9 @@ impl ActiveJobTokenServer { } mod inherited_jobserver { - use super::JobToken; + use super::{JobToken, OnceLock}; - use crate::{ - parallel::{async_executor::YieldOnce, once_lock::OnceLock}, - Error, ErrorKind, - }; + use crate::{parallel::async_executor::YieldOnce, Error, ErrorKind}; use std::{ io, mem, diff --git a/src/parallel/once_lock.rs b/src/parallel/once_lock.rs index cff4e77b..4e911ffd 100644 --- a/src/parallel/once_lock.rs +++ b/src/parallel/once_lock.rs @@ -3,7 +3,12 @@ //! is initialised and MSRV is high enoguh to use it. use std::{ - cell::UnsafeCell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, sync::Once, + cell::UnsafeCell, + convert::Infallible, + marker::PhantomData, + mem::MaybeUninit, + panic::{RefUnwindSafe, UnwindSafe}, + sync::Once, }; pub struct OnceLock { @@ -31,6 +36,12 @@ pub struct OnceLock { _marker: PhantomData, } +unsafe impl Sync for OnceLock {} +unsafe impl Send for OnceLock {} + +impl RefUnwindSafe for OnceLock {} +impl UnwindSafe for OnceLock {} + impl OnceLock { pub const fn new() -> OnceLock { OnceLock { @@ -49,7 +60,6 @@ impl OnceLock { } } - #[allow(dead_code)] pub fn get_or_init(&self, f: F) -> &T where F: FnOnce() -> T,