clang_ast/
dedup.rs

1use std::cell::Cell;
2
3thread_local! {
4    static REFCOUNT: Cell<usize> = const { Cell::new(0) };
5}
6
7pub(crate) struct Guard {
8    _private: (),
9}
10
11pub(crate) fn activate() -> Guard {
12    REFCOUNT.with(|refcount| refcount.set(refcount.get() + 1));
13    Guard { _private: () }
14}
15
16impl Drop for Guard {
17    fn drop(&mut self) {
18        let prev = REFCOUNT.with(|refcount| refcount.replace(refcount.get() - 1));
19        if prev == 1 {
20            crate::loc::thread_local_reset();
21        }
22    }
23}