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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Re-implementation of [`std::thread`].

#[cfg(target_feature = "atomics")]
mod atomics;
#[cfg(feature = "audio-worklet")]
pub(crate) mod audio_worklet;
mod builder;
mod global;
mod js;
mod scope;
mod spawn;
#[cfg(not(target_feature = "atomics"))]
mod unsupported;
mod yield_now;

use std::cell::OnceCell;
use std::io::{self, Error, ErrorKind};
use std::num::{NonZeroU64, NonZeroUsize};
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use r#impl::Parker;

#[cfg(target_feature = "atomics")]
use self::atomics as r#impl;
pub use self::builder::Builder;
use self::global::Global;
pub use self::scope::{scope, Scope, ScopedJoinHandle};
pub(crate) use self::scope::{scope_async, ScopeFuture};
pub use self::spawn::{spawn, JoinHandle};
#[cfg(not(target_feature = "atomics"))]
use self::unsupported as r#impl;
pub use self::yield_now::yield_now;
pub(crate) use self::yield_now::YieldNowFuture;

/// See [`std::thread::Thread`].
#[derive(Clone, Debug)]
pub struct Thread(Pin<Arc<ThreadInner>>);

/// Inner shared wrapper for [`Thread`].
#[derive(Debug)]
struct ThreadInner {
	/// [`ThreadId`].
	id: ThreadId,
	/// Name of the thread.
	name: Option<String>,
	/// Parker implementation.
	parker: Parker,
}

thread_local! {
	/// Holds this threads [`Thread`].
	static THREAD: OnceCell<Thread> = const { OnceCell::new() };
}

impl Thread {
	/// Create a new [`Thread`].
	fn new() -> Self {
		let name = Global::with(|global| match global {
			Global::Dedicated(worker) => Some(worker.name()),
			Global::Shared(worker) => Some(worker.name()),
			Global::Window(_)
			| Global::Service(_)
			| Global::Worklet
			| Global::Worker(_)
			| Global::Unknown => None,
		})
		.filter(|name| !name.is_empty());

		Self::new_with_name(name)
	}

	/// Create a new [`Thread`].
	fn new_with_name(name: Option<String>) -> Self {
		let id = ThreadId::new();
		Self(Arc::pin(ThreadInner {
			id,
			name,
			parker: Parker::new(id),
		}))
	}

	/// See [`std::thread::Thread::id()`].
	#[must_use]
	pub fn id(&self) -> ThreadId {
		self.0.id
	}

	/// See [`std::thread::Thread::name()`].
	#[must_use]
	pub fn name(&self) -> Option<&str> {
		self.0.name.as_deref()
	}

	/// See [`std::thread::Thread::unpark()`].
	#[inline]
	pub fn unpark(&self) {
		Pin::new(&self.0.parker).unpark();
	}
}

/// See [`std::thread::ThreadId`].
#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub struct ThreadId(NonZeroU64);

impl ThreadId {
	/// Create a new [`ThreadId`].
	fn new() -> Self {
		// See <https://github.com/rust-lang/rust/blob/1.75.0/library/std/src/thread/mod.rs#L1177-L1218>.

		/// Separate failed [`ThreadId`] to apply `#[cold]` to it.
		#[cold]
		fn exhausted() -> ! {
			panic!("failed to generate unique thread ID: bitspace exhausted")
		}

		/// Global counter for [`ThreadId`].
		static COUNTER: AtomicU64 = AtomicU64::new(0);

		let mut last = COUNTER.load(Ordering::Relaxed);
		loop {
			let Some(id) = last.checked_add(1) else {
				exhausted();
			};

			match COUNTER.compare_exchange_weak(last, id, Ordering::Relaxed, Ordering::Relaxed) {
				Ok(_) => return Self(NonZeroU64::new(id).expect("unexpected `0` `ThreadId`")),
				Err(id) => last = id,
			}
		}
	}
}

/// See [`std::thread::available_parallelism()`].
///
/// # Notes
///
/// Browsers might return lower values, a common case is to prevent
/// fingerprinting.
///
/// # Errors
///
/// This function will return an error if called from a worklet or any other
/// unsupported thread type.
#[allow(clippy::missing_panics_doc)]
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
	let value = Global::with(|global| match global {
		Global::Window(window) => Ok(window.navigator().hardware_concurrency()),
		Global::Dedicated(worker) => Ok(worker.navigator().hardware_concurrency()),
		Global::Shared(worker) => Ok(worker.navigator().hardware_concurrency()),
		Global::Service(worker) | Global::Worker(worker) => {
			Ok(worker.navigator().hardware_concurrency())
		}
		Global::Worklet => Err(Error::new(
			ErrorKind::Unsupported,
			"operation not supported in worklets",
		)),
		Global::Unknown => Err(Error::new(
			ErrorKind::Unsupported,
			"encountered unsupported thread type",
		)),
	})?;

	#[allow(
		clippy::as_conversions,
		clippy::cast_possible_truncation,
		clippy::cast_sign_loss
	)]
	let value = value as usize;
	let value = NonZeroUsize::new(value)
		.expect("`Navigator.hardwareConcurrency` returned an unexpected value of `0`");

	Ok(value)
}

/// See [`std::thread::current()`].
#[must_use]
pub fn current() -> Thread {
	THREAD.with(|cell| cell.get_or_init(Thread::new).clone())
}

/// See [`std::thread::park()`].
///
/// # Notes
///
/// Unlike [`std::thread::park()`], when using the atomics target feature, this
/// will not panic on the main thread, worklet or any other unsupported thread
/// type. However, on supported thread types, this will function correctly even
/// without the atomics target feature.
///
/// Keep in mind that this call will do nothing unless the calling thread
/// supports blocking, see
/// [`web::has_block_support()`](crate::web::has_block_support).
pub fn park() {
	if has_block_support() {
		Pin::new(&current().0.parker).park();
	}
}

/// See [`std::thread::park_timeout()`].
///
/// # Notes
///
/// Unlike [`std::thread::park_timeout()`], when using the atomics target
/// feature, this will not panic on the main thread, worklet or any other
/// unsupported thread type. However, on supported thread types, this will
/// function correctly even without the atomics target feature.
///
/// Keep in mind that this call will do nothing unless the calling thread
/// supports blocking, see
/// [`web::has_block_support()`](crate::web::has_block_support).
pub fn park_timeout(dur: Duration) {
	if has_block_support() {
		Pin::new(&current().0.parker).park_timeout(dur);
	}
}

/// See [`std::thread::park_timeout_ms()`].
///
/// # Notes
///
/// Unlike [`std::thread::park_timeout_ms()`], when using the atomics target
/// feature, this will not panic on the main thread, worklet or any other
/// unsupported thread type. However, on supported thread types, this will
/// function correctly even without the atomics target feature.
///
/// Keep in mind that this call will do nothing unless the calling thread
/// supports blocking, see
/// [`web::has_block_support()`](crate::web::has_block_support).
#[deprecated(note = "replaced by `web_thread::park_timeout`")]
pub fn park_timeout_ms(ms: u32) {
	park_timeout(Duration::from_millis(ms.into()));
}

/// See [`std::thread::sleep()`].
///
/// # Panics
///
/// This call will panic if the calling thread doesn't support blocking, see
/// [`web::has_block_support()`](crate::web::has_block_support).
pub fn sleep(dur: Duration) {
	if has_block_support() {
		r#impl::sleep(dur);
	} else {
		panic!("current thread type cannot be blocked")
	}
}

/// See [`std::thread::sleep_ms()`].
///
/// # Panics
///
/// This call will panic if the calling thread doesn't support blocking, see
/// [`web::has_block_support()`](crate::web::has_block_support).
#[deprecated(note = "replaced by `web_thread::sleep`")]
pub fn sleep_ms(ms: u32) {
	sleep(Duration::from_millis(ms.into()));
}

/// Implementation for [`crate::web::has_block_support()`].
pub(crate) fn has_block_support() -> bool {
	thread_local! {
		static HAS_BLOCK_SUPPORT: bool = Global::with(|global| {
			match global {
				Global::Window(_) | Global::Worklet | Global::Service(_) => false,
				Global::Dedicated(_) => true,
				// Some browsers don't support blocking in shared workers, so for cross-browser
				// support we have to test manually.
				// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1359745>.
				Global::Shared(_) => {
					/// Cache if blocking on shared workers is supported.
					/// REASON: A Wasm module can never be shared between
					/// multiple shared workers, so this can never be
					/// initialized from multiple threads at the same time.
					#[allow(clippy::disallowed_methods)]
					static HAS_SHARED_WORKER_BLOCK_SUPPORT: OnceLock<bool> = OnceLock::new();

					*HAS_SHARED_WORKER_BLOCK_SUPPORT.get_or_init(r#impl::test_block_support)
				}
				// For unknown worker types we test manually.
				Global::Worker(_) | Global::Unknown => r#impl::test_block_support(),
			}
		});
	}

	HAS_BLOCK_SUPPORT.with(bool::clone)
}

/// Implementation for [`crate::web::has_spawn_support()`].
pub(crate) fn has_spawn_support() -> bool {
	r#impl::has_spawn_support()
}