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
39
40
41
42
43
44
45
use crate::util::ObjectReference;
use crate::vm::VMBinding;
use crate::vm::VMLocalPinningBitSpec;
use std::sync::atomic::Ordering;

impl VMLocalPinningBitSpec {
    /// Pin an object by setting the pinning bit to 1.
    /// Return true if the object is pinned in this operation.
    pub fn pin_object<VM: VMBinding>(&self, object: ObjectReference) -> bool {
        let res = self.compare_exchange_metadata::<VM, u8>(
            object,
            0,
            1,
            None,
            Ordering::SeqCst,
            Ordering::SeqCst,
        );

        res.is_ok()
    }

    /// Unpin an object by clearing the pinning bit to 0.
    /// Return true if the object is unpinned in this operation.
    pub fn unpin_object<VM: VMBinding>(&self, object: ObjectReference) -> bool {
        let res = self.compare_exchange_metadata::<VM, u8>(
            object,
            1,
            0,
            None,
            Ordering::SeqCst,
            Ordering::SeqCst,
        );

        res.is_ok()
    }

    /// Check if an object is pinned.
    pub fn is_object_pinned<VM: VMBinding>(&self, object: ObjectReference) -> bool {
        if unsafe { self.load::<VM, u8>(object, None) == 1 } {
            return true;
        }

        false
    }
}