pub(crate) struct CandidateGathering {
_private: (),
}
impl CandidateGathering {
pub(crate) fn new() -> Self {
depth::enter();
Self { _private: () }
}
pub(crate) fn active() -> bool {
depth::get() > 0
}
}
impl Drop for CandidateGathering {
fn drop(&mut self) {
depth::leave();
}
}
#[cfg(feature = "std")]
mod depth {
std::thread_local! {
static DEPTH: core::cell::Cell<usize> = const { core::cell::Cell::new(0) };
}
pub(super) fn get() -> usize {
DEPTH.with(core::cell::Cell::get)
}
pub(super) fn enter() {
DEPTH.with(|depth| depth.set(depth.get() + 1));
}
pub(super) fn leave() {
DEPTH.with(|depth| depth.set(depth.get() - 1));
}
}
#[cfg(not(feature = "std"))]
mod depth {
use cubecl_environment::sync::{AtomicUsize, Ordering};
static DEPTH: AtomicUsize = AtomicUsize::new(0);
pub(super) fn get() -> usize {
DEPTH.load(Ordering::Relaxed)
}
pub(super) fn enter() {
DEPTH.fetch_add(1, Ordering::Relaxed);
}
pub(super) fn leave() {
DEPTH.fetch_sub(1, Ordering::Relaxed);
}
}