Coverage Report

Created: 2021-01-22 16:54

crossbeam-utils/src/lib.rs
Line
Count
Source
1
1
//! Miscellaneous tools for concurrent programming.//! Miscellaneous tools for concurrent programming.
2
//!
3
//! ## Atomics
4
//!
5
//! * [`AtomicCell`], a thread-safe mutable memory location.
6
//! * [`AtomicConsume`], for reading from primitive atomic types with "consume" ordering.
7
//!
8
//! ## Thread synchronization
9
//!
10
//! * [`Parker`], a thread parking primitive.
11
//! * [`ShardedLock`], a sharded reader-writer lock with fast concurrent reads.
12
//! * [`WaitGroup`], for synchronizing the beginning or end of some computation.
13
//!
14
//! ## Utilities
15
//!
16
//! * [`Backoff`], for exponential backoff in spin loops.
17
//! * [`CachePadded`], for padding and aligning a value to the length of a cache line.
18
//! * [`scope`], for spawning threads that borrow local variables from the stack.
19
//!
20
//! [`AtomicCell`]: atomic::AtomicCell
21
//! [`AtomicConsume`]: atomic::AtomicConsume
22
//! [`Parker`]: sync::Parker
23
//! [`ShardedLock`]: sync::ShardedLock
24
//! [`WaitGroup`]: sync::WaitGroup
25
//! [`scope`]: thread::scope
26
27
#![doc(test(
28
    no_crate_inject,
29
    attr(
30
        deny(warnings, rust_2018_idioms),
31
        allow(dead_code, unused_assignments, unused_variables)
32
    )
33
))]
34
#![warn(
35
    missing_docs,
36
    missing_debug_implementations,
37
    rust_2018_idioms,
38
    unreachable_pub
39
)]
40
#![cfg_attr(not(feature = "std"), no_std)]
41
#![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]
42
// matches! requires Rust 1.42
43
#![allow(clippy::match_like_matches_macro)]
44
45
#[cfg(loom_crossbeam)]
46
#[allow(unused_imports)]
47
mod primitive {
48
    pub(crate) mod sync {
49
        pub(crate) mod atomic {
50
            pub(crate) use loom::sync::atomic::spin_loop_hint;
51
            pub(crate) use loom::sync::atomic::{
52
                AtomicBool, AtomicU16, AtomicU32, AtomicU64, AtomicU8, AtomicUsize,
53
            };
54
55
            // FIXME: loom does not support compiler_fence at the moment.
56
            // https://github.com/tokio-rs/loom/issues/117
57
            // we use fence as a stand-in for compiler_fence for the time being.
58
            // this may miss some races since fence is stronger than compiler_fence,
59
            // but it's the best we can do for the time being.
60
            pub(crate) use loom::sync::atomic::fence as compiler_fence;
61
        }
62
        pub(crate) use loom::sync::{Arc, Condvar, Mutex};
63
    }
64
}
65
#[cfg(not(loom_crossbeam))]
66
#[allow(unused_imports)]
67
mod primitive {
68
    pub(crate) mod sync {
69
        pub(crate) mod atomic {
70
            pub(crate) use core::sync::atomic::compiler_fence;
71
            // TODO(taiki-e): once we bump the minimum required Rust version to 1.49+,
72
            // use [`core::hint::spin_loop`] instead.
73
            #[allow(deprecated)]
74
            pub(crate) use core::sync::atomic::spin_loop_hint;
75
            pub(crate) use core::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};
76
            #[cfg(has_atomic_u16)]
77
            pub(crate) use core::sync::atomic::{AtomicI16, AtomicU16};
78
            #[cfg(has_atomic_u32)]
79
            pub(crate) use core::sync::atomic::{AtomicI32, AtomicU32};
80
            #[cfg(has_atomic_u64)]
81
            pub(crate) use core::sync::atomic::{AtomicI64, AtomicU64};
82
            #[cfg(has_atomic_u8)]
83
            pub(crate) use core::sync::atomic::{AtomicI8, AtomicU8};
84
        }
85
86
        #[cfg(feature = "std")]
87
        pub(crate) use std::sync::{Arc, Condvar, Mutex};
88
    }
89
}
90
91
cfg_if! {
92
    if #[cfg(feature = "alloc")] {
93
        extern crate alloc;
94
    } else if #[cfg(feature = "std")] {
95
        extern crate std as alloc;
96
    }
97
}
98
99
#[cfg_attr(feature = "nightly", cfg(target_has_atomic = "ptr"))]
100
pub mod atomic;
101
102
mod cache_padded;
103
pub use crate::cache_padded::CachePadded;
104
105
mod backoff;
106
pub use crate::backoff::Backoff;
107
108
use cfg_if::cfg_if;
109
110
cfg_if! {
111
    if #[cfg(feature = "std")] {
112
        pub mod sync;
113
114
        #[cfg(not(loom_crossbeam))]
115
        pub mod thread;
116
    }
117
}