Skip to content

Commit a1520f5

Browse files
authored
runtime: fix thread parking on WebAssembly (#7041)
On WebAssembly the notification state was not checked before sleeping and thus wrongfully ignored. Additionally this refines the check whether threads are available on a particular WebAssembly target.
1 parent acd6627 commit a1520f5

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

tokio/src/runtime/park.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ impl ParkThread {
6565
pub(crate) fn park_timeout(&mut self, duration: Duration) {
6666
#[cfg(loom)]
6767
CURRENT_THREAD_PARK_COUNT.with(|count| count.fetch_add(1, SeqCst));
68-
69-
// Wasm doesn't have threads, so just sleep.
70-
#[cfg(not(target_family = "wasm"))]
7168
self.inner.park_timeout(duration);
72-
#[cfg(target_family = "wasm")]
73-
std::thread::sleep(duration);
7469
}
7570

7671
pub(crate) fn shutdown(&mut self) {
@@ -158,12 +153,20 @@ impl Inner {
158153
Err(actual) => panic!("inconsistent park_timeout state; actual = {actual}"),
159154
}
160155

156+
#[cfg(not(all(target_family = "wasm", not(target_feature = "atomics"))))]
161157
// Wait with a timeout, and if we spuriously wake up or otherwise wake up
162158
// from a notification, we just want to unconditionally set the state back to
163159
// empty, either consuming a notification or un-flagging ourselves as
164160
// parked.
165161
let (_m, _result) = self.condvar.wait_timeout(m, dur).unwrap();
166162

163+
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]
164+
// Wasm without atomics doesn't have threads, so just sleep.
165+
{
166+
let _m = m;
167+
std::thread::sleep(dur);
168+
}
169+
167170
match self.state.swap(EMPTY, SeqCst) {
168171
NOTIFIED => {} // got a notification, hurray!
169172
PARKED => {} // no notification, alas

0 commit comments

Comments
 (0)