From e4282f379823f17b4f725bb2cbea117787e0ed0b Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 16 May 2024 14:23:25 -0500 Subject: [PATCH] Remove all locking from principal_parser() By inverting the order of storage, we can use an `OnceCell`/`unsync::Lazy` inside the Send/Sync `MainThread` and remove the need for a lock altogether. --- src/parser.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 3c4859263..e2c5207a0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,7 +32,6 @@ use crate::wchar::{wstr, WString, L}; use crate::wutil::{perror, wgettext, wgettext_fmt}; use crate::{function, FLOG}; use libc::c_int; -use once_cell::sync::Lazy; use printf_compat::sprintf; use std::cell::{Ref, RefCell, RefMut}; 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. pub fn principal_parser() -> &'static Parser { - static PRINCIPAL: Lazy> = - Lazy::new(|| MainThread::new(Parser::new(EnvStack::principal().clone(), true))); - PRINCIPAL.get() + use std::cell::OnceCell; + static PRINCIPAL: MainThread> = MainThread::new(OnceCell::new()); + &PRINCIPAL + .get() + .get_or_init(|| Parser::new(EnvStack::principal().clone(), true)) } /// Assert that this parser is allowed to execute on the current thread.