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 (feature = "parking_lot" )] |
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 = RandomState::new(); |
41 | |
42 | let mut hasher: DefaultHasher = 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 (feature = "parking_lot" )] |
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(feature = "parking_lot" ))] |
66 | #[allow (unused_imports)] |
67 | pub(crate) use std::sync::{Condvar, MutexGuard, RwLock, RwLockReadGuard, WaitTimeoutResult}; |
68 | |
69 | #[cfg (not(feature = "parking_lot" ))] |
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 | const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS" ; |
88 | |
89 | match std::env::var(ENV_WORKER_THREADS) { |
90 | Ok(s) => { |
91 | let n = s.parse().unwrap_or_else(|e| { |
92 | panic!( |
93 | " \"{}\" must be usize, error: {}, value: {}" , |
94 | ENV_WORKER_THREADS, e, s |
95 | ) |
96 | }); |
97 | assert!(n > 0, " \"{}\" cannot be set to 0" , ENV_WORKER_THREADS); |
98 | n |
99 | } |
100 | Err(std::env::VarError::NotPresent) => usize::max(1, num_cpus::get()), |
101 | Err(std::env::VarError::NotUnicode(e)) => { |
102 | panic!( |
103 | " \"{}\" must be valid unicode, error: {:?}" , |
104 | ENV_WORKER_THREADS, e |
105 | ) |
106 | } |
107 | } |
108 | } |
109 | |
110 | #[cfg (not(feature = "rt-multi-thread" ))] |
111 | pub(crate) fn num_cpus() -> usize { |
112 | 1 |
113 | } |
114 | } |
115 | |
116 | pub(crate) mod thread { |
117 | #[inline ] |
118 | pub(crate) fn yield_now() { |
119 | std::hint::spin_loop(); |
120 | } |
121 | |
122 | #[allow (unused_imports)] |
123 | pub(crate) use std::thread::{ |
124 | current, panicking, park, park_timeout, sleep, spawn, AccessError, Builder, JoinHandle, |
125 | LocalKey, Result, Thread, ThreadId, |
126 | }; |
127 | } |
128 | |