1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::sync::atomic::{self, AtomicI32, Ordering};
#[repr(transparent)]
#[derive(Default)]
pub struct RefCount {
value: AtomicI32,
}
impl RefCount {
pub fn new() -> RefCount {
RefCount {
value: AtomicI32::new(1),
}
}
pub fn add_ref(&self) -> u32 {
(self.value.fetch_add(1, Ordering::Relaxed) + 1) as u32
}
pub fn release(&self) -> u32 {
let remaining = self.value.fetch_sub(1, Ordering::Release) - 1;
if remaining == 0 {
atomic::fence(Ordering::Acquire);
} else if remaining < 0 {
panic!("Object has been over-released.");
}
remaining as u32
}
}