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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#![deny(clippy::all)]
#![deny(clippy::perf)]
#![warn(clippy::pedantic)]
#![allow(clippy::needless_return)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::semicolon_if_nothing_returned)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::wildcard_imports)]
#![allow(clippy::explicit_deref_methods)]
#![allow(clippy::match_bool)]
#![cfg_attr(test, allow(clippy::bool_assert_comparison))]
/* */
#![cfg_attr(feature = "nightly", feature(int_roundings, negative_impls, c_size_t))]
#![cfg_attr(all(feature = "nightly", feature = "alloc"), feature(new_uninit))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "alloc_api", feature(allocator_api))]
#![cfg_attr(feature = "const", feature(const_trait_impl))]
#![cfg_attr(docsrs, feature(doc_cfg))]

use core::fmt::Display;

use docfg::docfg;

#[cfg(feature = "alloc")]
pub(crate) extern crate alloc;

macro_rules! flat_mod {
    ($($i:ident),+) => {
        $(
            mod $i;
            pub use $i::*;
        )+
    };
}

cfg_if::cfg_if! {
    if #[cfg(feature = "alloc_api")] {
        #[doc(hidden)]
        pub use core::alloc::AllocError;
    } else {
        /// The `AllocError` error indicates an allocation failure
        /// that may be due to resource exhaustion or to
        /// something wrong when combining the given input arguments with this
        /// allocator.
        #[doc(hidden)]
        #[derive(Copy, Clone, PartialEq, Eq, Debug)]
        pub struct AllocError;

        #[cfg(feature = "std")]
        impl std::error::Error for AllocError {}

        // (we need this for downstream impl of trait Error)
        impl core::fmt::Display for AllocError {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                f.write_str("memory allocation failed")
            }
        }
    }
}

cfg_if::cfg_if! {
    if #[cfg(feature = "alloc")] {
        // #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        // pub mod semaphore;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub mod fill_queue;
        mod bitfield;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub mod flag;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub mod channel;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub mod notify;
        mod cell;
        // #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        // pub mod arc_cell;
        mod locks;

        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub use bitfield::AtomicBitBox;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub use cell::AtomicCell;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub use fill_queue::FillQueue;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub use locks::*;
        #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
        pub use bitfield::*;
    }
}

flat_mod!(take);

#[path = "trait.rs"]
pub mod traits;

pub mod prelude {
    #[docfg::docfg(feature = "alloc")]
    pub use crate::fill_queue::*;
    pub use crate::take::*;
    pub use crate::traits::Atomic;
}

cfg_if::cfg_if! {
    if #[cfg(target_has_atomic = "8")] {
        pub(crate) type InnerFlag = u8;
        pub(crate) type InnerAtomicFlag = core::sync::atomic::AtomicU8;
    } else if #[cfg(target_has_atomic = "16")] {
        pub(crate) type InnerFlag = u16;
        pub(crate) type InnerAtomicFlag = core::sync::atomic::AtomicU16;
    } else if #[cfg(target_has_atomic = "32")] {
        pub(crate) type InnerFlag = u32;
        pub(crate) type InnerAtomicFlag = core::sync::atomic::AtomicU32;
    } else if #[cfg(target_has_atomic = "64")] {
        pub(crate) type InnerFlag = u64;
        pub(crate) type InnerAtomicFlag = core::sync::atomic::AtomicU64;
    } else {
        pub(crate) type InnerFlag = usize;
        pub(crate) type InnerAtomicFlag = core::sync::atomic::AtomicUsize;
    }
}

pub(crate) const TRUE: InnerFlag = 1;
pub(crate) const FALSE: InnerFlag = 0;

/// Error returned when a timeout ocurrs before the main operation completes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Timeout;

impl Display for Timeout {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "The main operation timed out before it could be completed"
        )
    }
}

#[docfg(feature = "std")]
impl std::error::Error for Timeout {}

#[allow(unused)]
#[inline]
pub(crate) fn is_some_and<T, F: FnOnce(T) -> bool>(v: Option<T>, f: F) -> bool {
    match v {
        None => false,
        Some(x) => f(x),
    }
}

#[allow(unused)]
#[inline]
pub(crate) fn div_ceil(lhs: usize, rhs: usize) -> usize {
    cfg_if::cfg_if! {
        if #[cfg(feature = "nightly")] {
            return lhs.div_ceil(rhs)
        } else {
            let d = lhs / rhs;
            let r = lhs % rhs;
            if r > 0 && rhs > 0 {
                d + 1
            } else {
                d
            }
        }
    }
}