starnix_sync/
atomic_time.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Provides an atomic wrapper around [`zx::MonotonicInstant`].
6
7use std::sync::atomic::{AtomicI64, Ordering};
8
9/// An atomic wrapper around [`zx::MonotonicInstant`].
10#[derive(Debug, Default)]
11pub struct AtomicMonotonicInstant(AtomicI64);
12
13impl From<zx::MonotonicInstant> for AtomicMonotonicInstant {
14    fn from(t: zx::MonotonicInstant) -> Self {
15        Self::new(t)
16    }
17}
18
19impl AtomicMonotonicInstant {
20    /// Creates an [`AtomicTime`].
21    pub fn new(time: zx::MonotonicInstant) -> Self {
22        Self(AtomicI64::new(time.into_nanos()))
23    }
24
25    /// Loads a [`zx::MonotonicInstant`].
26    pub fn load(&self, order: Ordering) -> zx::MonotonicInstant {
27        let Self(atomic_time) = self;
28        zx::Instant::from_nanos(atomic_time.load(order))
29    }
30
31    /// Stores a [`zx::MonotonicInstant`].
32    pub fn store(&self, val: zx::MonotonicInstant, order: Ordering) {
33        let Self(atomic_time) = self;
34        atomic_time.store(val.into_nanos(), order)
35    }
36}