Remove all locking from principal_parser()

By inverting the order of storage, we can use an `OnceCell`/`unsync::Lazy`
inside the Send/Sync `MainThread<T>` and remove the need for a lock altogether.
This commit is contained in:
Mahmoud Al-Qudsi 2024-05-16 14:23:25 -05:00
parent 5dc07c9e7e
commit e4282f3798

View File

@ -32,7 +32,6 @@ use crate::wchar::{wstr, WString, L};
use crate::wutil::{perror, wgettext, wgettext_fmt}; use crate::wutil::{perror, wgettext, wgettext_fmt};
use crate::{function, FLOG}; use crate::{function, FLOG};
use libc::c_int; use libc::c_int;
use once_cell::sync::Lazy;
use printf_compat::sprintf; use printf_compat::sprintf;
use std::cell::{Ref, RefCell, RefMut}; use std::cell::{Ref, RefCell, RefMut};
use std::ffi::{CStr, OsStr}; use std::ffi::{CStr, OsStr};
@ -402,9 +401,11 @@ impl Parser {
/// Get the "principal" parser, whatever that is. Can only be called by the main thread. /// Get the "principal" parser, whatever that is. Can only be called by the main thread.
pub fn principal_parser() -> &'static Parser { pub fn principal_parser() -> &'static Parser {
static PRINCIPAL: Lazy<MainThread<ParserRef>> = use std::cell::OnceCell;
Lazy::new(|| MainThread::new(Parser::new(EnvStack::principal().clone(), true))); static PRINCIPAL: MainThread<OnceCell<ParserRef>> = MainThread::new(OnceCell::new());
PRINCIPAL.get() &PRINCIPAL
.get()
.get_or_init(|| Parser::new(EnvStack::principal().clone(), true))
} }
/// Assert that this parser is allowed to execute on the current thread. /// Assert that this parser is allowed to execute on the current thread.