diff options
| author | Wenzhuo Liu <mgt@oi-wiki.org> | 2024-04-02 04:36:25 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-01 20:36:25 +0000 |
| commit | eef3c3c5ff2e628317fdec5b07ea751bd55d9fca (patch) | |
| tree | ebf83a4ec155b2e9d324cc8a8f10d1227c3c7133 /crates/typst-timing | |
| parent | dee8ccf04810ee4032fd28b366a4f796b7bf3062 (diff) | |
Remove unsafe flag usage in typst-timing (#3788)
Diffstat (limited to 'crates/typst-timing')
| -rw-r--r-- | crates/typst-timing/src/lib.rs | 15 |
1 files changed, 4 insertions, 11 deletions
diff --git a/crates/typst-timing/src/lib.rs b/crates/typst-timing/src/lib.rs index fe9abb51..a972cf72 100644 --- a/crates/typst-timing/src/lib.rs +++ b/crates/typst-timing/src/lib.rs @@ -2,6 +2,7 @@ use std::hash::Hash; use std::io::Write; +use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use std::thread::ThreadId; use std::time::{Duration, SystemTime}; @@ -11,13 +12,7 @@ use serde::{Serialize, Serializer}; use typst_syntax::Span; /// Whether the timer is enabled. Defaults to `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; +static ENABLED: AtomicBool = AtomicBool::new(false); /// The global event recorder. static RECORDER: Mutex<Recorder> = Mutex::new(Recorder::new()); @@ -64,15 +59,13 @@ enum EventKind { /// Enable the timer. #[inline] pub fn enable() { - unsafe { - ENABLED = true; - } + ENABLED.store(true, Relaxed); } /// Whether the timer is enabled. #[inline] pub fn is_enabled() -> bool { - unsafe { ENABLED } + ENABLED.load(Relaxed) } /// Clears the recorded events. |
