os_trait/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4pub mod mutex;
5pub mod notifier;
6pub mod notifier_impls;
7pub mod os_impls;
8pub mod prelude;
9
10pub use fugit;
11pub use mutex::{BlockingMutex, FakeRawMutex};
12pub use mutex_traits;
13pub use mutex_traits::{ConstInit, RawMutex};
14pub use notifier::*;
15pub use notifier_impls::*;
16pub use os_impls::{FakeOs, StdOs};
17pub use portable_atomic;
18pub use timeout_trait::{self, *};
19
20#[cfg(feature = "alloc")]
21extern crate alloc;
22
23/// The interface for different operating systems.
24///
25/// We use the [`mutex-traits`](https://crates.io/crates/mutex-traits) crate to provide mutex functionality.
26/// You can implement your own mutex by implementing the `RawMutex` trait from the `mutex-traits` crate.
27pub trait OsInterface: Send + Sync + Sized + 'static {
28    type RawMutex: ConstInit + RawMutex;
29    type Notifier: NotifierInterface;
30    type NotifyWaiter: NotifyWaiterInterface<Self>;
31    type Instant: TickInstant;
32    type Delay: DelayNs;
33
34    /// It's used to avoid writing `foo::<OS, _, _, _>(...)`
35    const O: Self;
36
37    fn yield_thread();
38
39    #[inline(always)]
40    fn yield_task() {
41        Self::yield_thread()
42    }
43
44    fn delay() -> Self::Delay;
45    fn notify() -> (Self::Notifier, Self::NotifyWaiter);
46
47    #[inline]
48    fn mutex<T>(d: T) -> Mutex<Self, T> {
49        Mutex::<Self, T>::new(d)
50    }
51}
52
53pub type Mutex<OS, T> = BlockingMutex<<OS as OsInterface>::RawMutex, T>;
54pub type Notifier<OS> = <OS as OsInterface>::Notifier;
55pub type NotifyWaiter<OS> = <OS as OsInterface>::NotifyWaiter;
56pub type Instant<OS> = <OS as OsInterface>::Instant;
57pub type Duration<OS> = TickDuration<Instant<OS>>;
58pub type Timeout<OS> = TickTimeout<Instant<OS>>;
59pub type Delay<OS> = <OS as OsInterface>::Delay;
60
61/// Use this macro to alias the OS-specific types for greater convenience,
62/// or manually alias only the ones you need.
63#[macro_export]
64macro_rules! os_type_alias {
65    ($YOUR_OS:ty) => {
66        pub type Mutex<T> = $crate::Mutex<$YOUR_OS, T>;
67        pub type Notifier = $crate::Notifier<$YOUR_OS>;
68        pub type NotifyWaiter = $crate::NotifyWaiter<$YOUR_OS>;
69        pub type Instant = $crate::Instant<$YOUR_OS>;
70        pub type Duration = $crate::Duration<$YOUR_OS>;
71        pub type Timeout = $crate::Timeout<$YOUR_OS>;
72        pub type Delay = $crate::Delay<$YOUR_OS>;
73    };
74}