Skip to content

Commit 1ac1775

Browse files
committed
Also address safety issues with principal_parser()
1 parent 2ecbc56 commit 1ac1775

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/parser.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::parse_execution::{EndExecutionReason, ParseExecutionContext};
2727
use crate::parse_tree::{parse_source, ParsedSourceRef};
2828
use crate::proc::{job_reap, JobGroupRef, JobList, JobRef, ProcStatus};
2929
use crate::signal::{signal_check_cancel, signal_clear_cancel, Signal};
30-
use crate::threads::assert_is_main_thread;
30+
use crate::threads::{assert_is_main_thread, MainThread};
3131
use crate::util::get_time;
3232
use crate::wait_handle::WaitHandleStore;
3333
use crate::wchar::{wstr, WString, L};
@@ -414,19 +414,11 @@ impl Parser {
414414
false
415415
}
416416

417-
/// Get the "principal" parser, whatever that is.
417+
/// Get the "principal" parser, whatever that is. Can only be called by the main thread.
418418
pub fn principal_parser() -> &'static Parser {
419-
// XXX: We use `static mut` as a hack to work around the fact that Parser doesn't implement
420-
// Sync! Even though we are wrapping it in Lazy<> and it compiles without an error, that
421-
// doesn't mean this is safe to access across threads!
422-
static mut PRINCIPAL: Lazy<ParserRef> =
423-
Lazy::new(|| Parser::new(EnvStack::principal().clone(), true));
424-
// XXX: Creating and using multiple (read or write!) references to the same mutable static
425-
// is undefined behavior!
426-
unsafe {
427-
PRINCIPAL.assert_can_execute();
428-
&PRINCIPAL
429-
}
419+
static PRINCIPAL: Lazy<MainThread<ParserRef>> =
420+
Lazy::new(|| MainThread::new(Parser::new(EnvStack::principal().clone(), true)));
421+
PRINCIPAL.get()
430422
}
431423

432424
/// Assert that this parser is allowed to execute on the current thread.

0 commit comments

Comments
 (0)