Sha256: ded51b50923eb1d542432d216b56604b6cef564f664740a3ad8290ad8d88b8e6

Contents?: true

Size: 1.06 KB

Versions: 19

Compression:

Stored size: 1.06 KB

Contents

use std::sync::atomic::{fence, AtomicI32, Ordering};

#[doc(hidden)]
#[repr(transparent)]
#[derive(Default)]
pub struct RefCount(pub(crate) AtomicI32);

impl RefCount {
    /// Creates a new `RefCount` with an initial value of `1`.
    pub fn new(count: u32) -> Self {
        Self(AtomicI32::new(count as i32))
    }

    /// Increments the reference count, returning the new value.
    pub fn add_ref(&self) -> u32 {
        (self.0.fetch_add(1, Ordering::Relaxed) + 1) as u32
    }

    /// Decrements the reference count, returning the new value.
    ///
    /// This operation inserts an `Acquire` fence when the reference count reaches zero.
    /// This prevents reordering before the object is destroyed.
    pub fn release(&self) -> u32 {
        let remaining = self.0.fetch_sub(1, Ordering::Release) - 1;

        match remaining.cmp(&0) {
            std::cmp::Ordering::Equal => fence(Ordering::Acquire),
            std::cmp::Ordering::Less => panic!("Object has been over-released."),
            std::cmp::Ordering::Greater => {}
        }

        remaining as u32
    }
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-28.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-27.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-26.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-25.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-25.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-25.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-24.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-23.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-22.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-21.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-20.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-20.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-18.0.3 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-17.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-17.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-16.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-15.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs
wasmtime-15.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/imp/ref_count.rs