mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
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.
This commit is contained in:
@@ -65,12 +65,7 @@ impl ParkThread {
|
|||||||
pub(crate) fn park_timeout(&mut self, duration: Duration) {
|
pub(crate) fn park_timeout(&mut self, duration: Duration) {
|
||||||
#[cfg(loom)]
|
#[cfg(loom)]
|
||||||
CURRENT_THREAD_PARK_COUNT.with(|count| count.fetch_add(1, SeqCst));
|
CURRENT_THREAD_PARK_COUNT.with(|count| count.fetch_add(1, SeqCst));
|
||||||
|
|
||||||
// Wasm doesn't have threads, so just sleep.
|
|
||||||
#[cfg(not(target_family = "wasm"))]
|
|
||||||
self.inner.park_timeout(duration);
|
self.inner.park_timeout(duration);
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
std::thread::sleep(duration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn shutdown(&mut self) {
|
pub(crate) fn shutdown(&mut self) {
|
||||||
@@ -158,12 +153,20 @@ impl Inner {
|
|||||||
Err(actual) => panic!("inconsistent park_timeout state; actual = {actual}"),
|
Err(actual) => panic!("inconsistent park_timeout state; actual = {actual}"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(all(target_family = "wasm", not(target_feature = "atomics"))))]
|
||||||
// Wait with a timeout, and if we spuriously wake up or otherwise wake up
|
// Wait with a timeout, and if we spuriously wake up or otherwise wake up
|
||||||
// from a notification, we just want to unconditionally set the state back to
|
// from a notification, we just want to unconditionally set the state back to
|
||||||
// empty, either consuming a notification or un-flagging ourselves as
|
// empty, either consuming a notification or un-flagging ourselves as
|
||||||
// parked.
|
// parked.
|
||||||
let (_m, _result) = self.condvar.wait_timeout(m, dur).unwrap();
|
let (_m, _result) = self.condvar.wait_timeout(m, dur).unwrap();
|
||||||
|
|
||||||
|
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]
|
||||||
|
// Wasm without atomics doesn't have threads, so just sleep.
|
||||||
|
{
|
||||||
|
let _m = m;
|
||||||
|
std::thread::sleep(dur);
|
||||||
|
}
|
||||||
|
|
||||||
match self.state.swap(EMPTY, SeqCst) {
|
match self.state.swap(EMPTY, SeqCst) {
|
||||||
NOTIFIED => {} // got a notification, hurray!
|
NOTIFIED => {} // got a notification, hurray!
|
||||||
PARKED => {} // no notification, alas
|
PARKED => {} // no notification, alas
|
||||||
|
|||||||
Reference in New Issue
Block a user