Skip to content

Commit

Permalink
Remove all locking from principal_parser()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mqudsi committed May 16, 2024
1 parent 5dc07c9 commit e4282f3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<MainThread<ParserRef>> =
Lazy::new(|| MainThread::new(Parser::new(EnvStack::principal().clone(), true)));
PRINCIPAL.get()
use std::cell::OnceCell;
static PRINCIPAL: MainThread<OnceCell<ParserRef>> = 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.
Expand Down

0 comments on commit e4282f3

Please sign in to comment.