From ba0f2dbdf79cc34cbd52bcb4374cbcd8ccea7e8f Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sun, 14 Jan 2024 04:12:15 +0200 Subject: [PATCH] =?UTF-8?q?client:=20support=20`not(manual-lifetime)`=20mo?= =?UTF-8?q?de=20after=20all=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tracing-tracy/Cargo.toml | 2 ++ tracy-client/Cargo.toml | 3 ++- tracy-client/src/state.rs | 33 +++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/tracing-tracy/Cargo.toml b/tracing-tracy/Cargo.toml index 268782f..f992d4b 100644 --- a/tracing-tracy/Cargo.toml +++ b/tracing-tracy/Cargo.toml @@ -46,6 +46,8 @@ only-localhost = ["client/only-localhost"] sampling = ["client/sampling"] system-tracing = ["client/system-tracing"] callstack-inlines = ["client/callstack-inlines"] +manual-lifetime = ["client/manual-lifetime"] +delayed-init = ["client/delayed-init"] [package.metadata.docs.rs] rustdoc-args = ["--cfg", "tracing_tracy_docs"] diff --git a/tracy-client/Cargo.toml b/tracy-client/Cargo.toml index 16c1d2f..062192b 100644 --- a/tracy-client/Cargo.toml +++ b/tracy-client/Cargo.toml @@ -39,7 +39,6 @@ path = "../tracy-client-sys" package = "tracy-client-sys" version = ">=0.21.2, <0.23.0" # AUTO-UPDATE default-features = false -features = ["manual-lifetime", "delayed-init"] [target.'cfg(loom)'.dependencies.loom] version = "0.7" @@ -60,6 +59,8 @@ only-localhost = ["sys/only-localhost"] sampling = ["sys/sampling"] system-tracing = ["sys/system-tracing"] callstack-inlines = ["sys/callstack-inlines"] +manual-lifetime = ["sys/manual-lifetime"] +delayed-init = ["sys/delayed-init"] [package.metadata.docs.rs] rustdoc-args = ["--cfg", "tracy_client_docs"] diff --git a/tracy-client/src/state.rs b/tracy-client/src/state.rs index 59453e0..c945cff 100644 --- a/tracy-client/src/state.rs +++ b/tracy-client/src/state.rs @@ -1,4 +1,5 @@ use crate::Client; +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] use std::sync::atomic::Ordering; /// Enabling `Tracy` when it is already enabled, or Disabling when it is already disabled will @@ -26,7 +27,7 @@ use std::sync::atomic::Ordering; /// /// All that seems like a major pain to implement, and so we’ll punt on disabling entirely until /// somebody comes with a good use-case warranting that sort of complexity. -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] #[cfg(not(loom))] static CLIENT_STATE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); #[cfg(loom)] @@ -35,16 +36,16 @@ loom::lazy_static! { loom::sync::atomic::AtomicUsize::new(0); } -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] const STATE_STEP: usize = 1; // Move forward by 1 step in the FSM -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] const STATE_DISABLED: usize = 0; -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] const STATE_ENABLING: usize = STATE_DISABLED + STATE_STEP; -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] const STATE_ENABLED: usize = STATE_ENABLING + STATE_STEP; -#[cfg(feature = "enable")] +#[cfg(all(feature = "enable", feature = "manual-lifetime"))] #[inline(always)] fn spin_loop() { #[cfg(loom)] @@ -64,7 +65,10 @@ impl Client { /// The underlying client implementation will be started up only if it wasn't already running /// yet. /// - /// Note that there currently isn't a mechanism to stop the client once it has been started. + /// Note that when the `manual-lifetime` feature is used, it is a responsibility of the user + /// to stop `tracy` using the [`sys::___tracy_shutdown_profiler`] function. Keep in mind that + /// at the time this function is called there can be no other invocations to the tracy + /// profiler, even from other threads (or you may get a crash!) /// /// # Example /// @@ -75,7 +79,7 @@ impl Client { /// // } /// ``` pub fn start() -> Self { - #[cfg(feature = "enable")] + #[cfg(all(feature = "enable", feature = "manual-lifetime"))] { let mut old_state = CLIENT_STATE.load(Ordering::Relaxed); loop { @@ -120,7 +124,7 @@ impl Client { } } } - #[cfg(not(feature = "enable"))] + #[cfg(not(all(feature = "enable", feature = "manual-lifetime")))] Client(()) } @@ -136,10 +140,15 @@ impl Client { /// Is the client already running? pub fn is_running() -> bool { - #[cfg(feature = "enable")] - return CLIENT_STATE.load(Ordering::Relaxed) == STATE_ENABLED; + #![allow(unreachable_code)] #[cfg(not(feature = "enable"))] - return true; + return true; // If the client is disabled, produce a "no-op" one so that users don’t need + // to wory about conditional use of the instrumentation in their own code. + #[cfg(not(feature = "manual-lifetime"))] + return true; // The client is started in life-before-main (or upon first use in case of + // `delayed-init` + #[cfg(feature = "manual-lifetime")] + return CLIENT_STATE.load(Ordering::Relaxed) == STATE_ENABLED; } }