clang_ast/
dedup.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::cell::Cell;

thread_local! {
    static REFCOUNT: Cell<usize> = const { Cell::new(0) };
}

pub(crate) struct Guard {
    _private: (),
}

pub(crate) fn activate() -> Guard {
    REFCOUNT.with(|refcount| refcount.set(refcount.get() + 1));
    Guard { _private: () }
}

impl Drop for Guard {
    fn drop(&mut self) {
        let prev = REFCOUNT.with(|refcount| refcount.replace(refcount.get() - 1));
        if prev == 1 {
            crate::loc::thread_local_reset();
        }
    }
}