Remove unsafe flag usage in typst-timing (#3788)

This commit is contained in:
Wenzhuo Liu 2024-04-02 04:36:25 +08:00 committed by GitHub
parent dee8ccf048
commit eef3c3c5ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
use std::hash::Hash; use std::hash::Hash;
use std::io::Write; use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use std::thread::ThreadId; use std::thread::ThreadId;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
@ -11,13 +12,7 @@ use serde::{Serialize, Serializer};
use typst_syntax::Span; use typst_syntax::Span;
/// Whether the timer is enabled. Defaults to `false`. /// Whether the timer is enabled. Defaults to `false`.
/// static ENABLED: AtomicBool = AtomicBool::new(false);
/// # Safety
/// This is unsafe because it is a global variable that is not thread-safe.
/// But at worst, if we have a race condition, we will just be missing some
/// events. So it's not a big deal. And it avoids needing to do an atomic
/// operation every time we want to check if the timer is enabled.
static mut ENABLED: bool = false;
/// The global event recorder. /// The global event recorder.
static RECORDER: Mutex<Recorder> = Mutex::new(Recorder::new()); static RECORDER: Mutex<Recorder> = Mutex::new(Recorder::new());
@ -64,15 +59,13 @@ enum EventKind {
/// Enable the timer. /// Enable the timer.
#[inline] #[inline]
pub fn enable() { pub fn enable() {
unsafe { ENABLED.store(true, Relaxed);
ENABLED = true;
}
} }
/// Whether the timer is enabled. /// Whether the timer is enabled.
#[inline] #[inline]
pub fn is_enabled() -> bool { pub fn is_enabled() -> bool {
unsafe { ENABLED } ENABLED.load(Relaxed)
} }
/// Clears the recorded events. /// Clears the recorded events.