| 1 |
#![cfg_attr(any(not(feature = "full"), loom), allow(unused_imports, dead_code))] |
| 2 |
|
| 3 |
mod atomic_u16; |
| 4 |
mod atomic_u32; |
| 5 |
mod atomic_u64; |
| 6 |
mod atomic_usize; |
| 7 |
mod barrier; |
| 8 |
mod mutex; |
| 9 |
#[cfg(all(feature = "parking_lot", not(miri)))] |
| 10 |
mod parking_lot; |
| 11 |
mod unsafe_cell; |
| 12 |
|
| 13 |
pub(crate) mod cell { |
| 14 |
pub(crate) use super::unsafe_cell::UnsafeCell; |
| 15 |
} |
| 16 |
|
| 17 |
#[cfg(any( |
| 18 |
feature = "net", |
| 19 |
feature = "process", |
| 20 |
feature = "signal", |
| 21 |
feature = "sync", |
| 22 |
))] |
| 23 |
pub(crate) mod future { |
| 24 |
pub(crate) use crate::sync::AtomicWaker; |
| 25 |
} |
| 26 |
|
| 27 |
pub(crate) mod hint { |
| 28 |
pub(crate) use std::hint::spin_loop; |
| 29 |
} |
| 30 |
|
| 31 |
pub(crate) mod rand { |
| 32 |
use std::collections::hash_map::RandomState; |
| 33 |
use std::hash::{BuildHasher, Hash, Hasher}; |
| 34 |
use std::sync::atomic::AtomicU32; |
| 35 |
use std::sync::atomic::Ordering::Relaxed; |
| 36 |
|
| 37 |
static COUNTER: AtomicU32 = AtomicU32::new(1); |
| 38 |
|
| 39 |
pub(crate) fn seed() -> u64 { |
| 40 |
let rand_state = RandomState::new(); |
| 41 |
|
| 42 |
let mut hasher = rand_state.build_hasher(); |
| 43 |
|
| 44 |
// Hash some unique-ish data to generate some new state |
| 45 |
COUNTER.fetch_add(1, Relaxed).hash(&mut hasher); |
| 46 |
|
| 47 |
// Get the seed |
| 48 |
hasher.finish() |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
pub(crate) mod sync { |
| 53 |
pub(crate) use std::sync::{Arc, Weak}; |
| 54 |
|
| 55 |
// Below, make sure all the feature-influenced types are exported for |
| 56 |
// internal use. Note however that some are not _currently_ named by |
| 57 |
// consuming code. |
| 58 |
|
| 59 |
#[cfg(all(feature = "parking_lot", not(miri)))] |
| 60 |
#[allow(unused_imports)] |
| 61 |
pub(crate) use crate::loom::std::parking_lot::{ |
| 62 |
Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, WaitTimeoutResult, |
| 63 |
}; |
| 64 |
|
| 65 |
#[cfg(not(all(feature = "parking_lot", not(miri))))] |
| 66 |
#[allow(unused_imports)] |
| 67 |
pub(crate) use std::sync::{Condvar, MutexGuard, RwLock, RwLockReadGuard, WaitTimeoutResult}; |
| 68 |
|
| 69 |
#[cfg(not(all(feature = "parking_lot", not(miri))))] |
| 70 |
pub(crate) use crate::loom::std::mutex::Mutex; |
| 71 |
|
| 72 |
pub(crate) mod atomic { |
| 73 |
pub(crate) use crate::loom::std::atomic_u16::AtomicU16; |
| 74 |
pub(crate) use crate::loom::std::atomic_u32::AtomicU32; |
| 75 |
pub(crate) use crate::loom::std::atomic_u64::{AtomicU64, StaticAtomicU64}; |
| 76 |
pub(crate) use crate::loom::std::atomic_usize::AtomicUsize; |
| 77 |
|
| 78 |
pub(crate) use std::sync::atomic::{fence, AtomicBool, AtomicPtr, AtomicU8, Ordering}; |
| 79 |
} |
| 80 |
|
| 81 |
pub(crate) use super::barrier::Barrier; |
| 82 |
} |
| 83 |
|
| 84 |
pub(crate) mod sys { |
| 85 |
#[cfg(feature = "rt-multi-thread")] |
| 86 |
pub(crate) fn num_cpus() -> usize { |
| 87 |
use std::num::NonZeroUsize; |
| 88 |
|
| 89 |
const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS"; |
| 90 |
|
| 91 |
match std::env::var(ENV_WORKER_THREADS) { |
| 92 |
Ok(s) => { |
| 93 |
let n = s.parse().unwrap_or_else(|e| { |
| 94 |
panic!( |
| 95 |
"\"{}\" must be usize, error: {}, value: {}", |
| 96 |
ENV_WORKER_THREADS, e, s |
| 97 |
) |
| 98 |
}); |
| 99 |
assert!(n > 0, "\"{}\" cannot be set to 0", ENV_WORKER_THREADS); |
| 100 |
n |
| 101 |
} |
| 102 |
Err(std::env::VarError::NotPresent) => { |
| 103 |
std::thread::available_parallelism().map_or(1, NonZeroUsize::get) |
| 104 |
} |
| 105 |
Err(std::env::VarError::NotUnicode(e)) => { |
| 106 |
panic!( |
| 107 |
"\"{}\" must be valid unicode, error: {:?}", |
| 108 |
ENV_WORKER_THREADS, e |
| 109 |
) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
#[cfg(not(feature = "rt-multi-thread"))] |
| 115 |
pub(crate) fn num_cpus() -> usize { |
| 116 |
1 |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
pub(crate) mod thread { |
| 121 |
#[inline] |
| 122 |
pub(crate) fn yield_now() { |
| 123 |
std::hint::spin_loop(); |
| 124 |
} |
| 125 |
|
| 126 |
#[allow(unused_imports)] |
| 127 |
pub(crate) use std::thread::{ |
| 128 |
current, panicking, park, park_timeout, sleep, spawn, AccessError, Builder, JoinHandle, |
| 129 |
LocalKey, Result, Thread, ThreadId, |
| 130 |
}; |
| 131 |
} |