kithara-platform 0.0.1-alpha1

Platform-aware primitives (sync, traits, time) for native and wasm32 targets
Documentation
#[cfg(not(target_arch = "wasm32"))]
use parking_lot::Condvar as ParkingLotCondvar;

use super::MutexGuard;

#[cfg(not(target_arch = "wasm32"))]
pub struct Condvar(ParkingLotCondvar);

#[cfg(not(target_arch = "wasm32"))]
impl Condvar {
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self(ParkingLotCondvar::new())
    }

    #[inline]
    pub fn notify_all(&self) {
        self.0.notify_all();
    }

    #[inline]
    pub fn notify_one(&self) {
        self.0.notify_one();
    }

    #[inline]
    pub fn wait_sync<'a, T>(&self, mut guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
        self.0.wait(&mut guard.0);
        guard
    }

    #[inline]
    pub fn wait_sync_timeout<'a, T>(
        &self,
        mut guard: MutexGuard<'a, T>,
        deadline: web_time::Instant,
    ) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
        let result = self.0.wait_until(&mut guard.0, deadline);
        (guard, WaitTimeoutResult(result.timed_out()))
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Default for Condvar {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(target_arch = "wasm32")]
type WstCondvar = wasm_safe_thread::condvar::Condvar;

#[cfg(target_arch = "wasm32")]
pub struct Condvar(WstCondvar);

#[cfg(target_arch = "wasm32")]
impl Condvar {
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self(WstCondvar::new())
    }

    #[inline]
    pub fn notify_all(&self) {
        self.0.notify_all();
    }

    #[inline]
    pub fn notify_one(&self) {
        self.0.notify_one();
    }

    #[inline]
    pub fn wait_sync<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
        MutexGuard(self.0.wait_sync(guard.0))
    }

    #[inline]
    pub fn wait_sync_timeout<'a, T>(
        &self,
        guard: MutexGuard<'a, T>,
        deadline: web_time::Instant,
    ) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
        let (g, result) = self.0.wait_sync_timeout(guard.0, deadline);
        (MutexGuard(g), WaitTimeoutResult(result.timed_out()))
    }
}

#[cfg(target_arch = "wasm32")]
impl Default for Condvar {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of a timed wait on a [`Condvar`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaitTimeoutResult(bool);

impl WaitTimeoutResult {
    /// Returns `true` if the wait timed out (deadline elapsed).
    #[inline]
    #[must_use]
    pub fn did_time_out(&self) -> bool {
        self.0
    }
}