diff --git a/crates/erg_common/spawn.rs b/crates/erg_common/spawn.rs index e4070dac..b4413803 100644 --- a/crates/erg_common/spawn.rs +++ b/crates/erg_common/spawn.rs @@ -50,3 +50,8 @@ where .spawn(run) .unwrap() } + +pub fn safe_yield() { + std::thread::yield_now(); + std::thread::sleep(std::time::Duration::from_millis(10)); +} diff --git a/crates/erg_compiler/module/promise.rs b/crates/erg_compiler/module/promise.rs index bd5ac8bc..22ed0393 100644 --- a/crates/erg_compiler/module/promise.rs +++ b/crates/erg_compiler/module/promise.rs @@ -5,6 +5,7 @@ use std::thread::{current, JoinHandle, ThreadId}; use erg_common::dict::Dict; use erg_common::pathutil::NormalizedPathBuf; use erg_common::shared::Shared; +use erg_common::spawn::safe_yield; use super::SharedModuleGraph; @@ -155,7 +156,7 @@ impl SharedPromises { .get(path) .is_some_and(|p| !p.is_finished()) { - std::thread::yield_now(); + safe_yield(); } return Ok(()); } @@ -167,7 +168,7 @@ impl SharedPromises { pub fn join(&self, path: &Path) -> std::thread::Result<()> { while let Some(Promise::Joining) | None = self.promises.borrow().get(path) { - std::thread::yield_now(); + safe_yield(); } let promise = self.promises.borrow_mut().get_mut(path).unwrap().take(); self.join_checked(path, promise)