From be8cc2454476946faf69ddba5909ac135ab2ef35 Mon Sep 17 00:00:00 2001 From: Fabing Li Date: Thu, 22 Aug 2024 15:11:43 +0800 Subject: [PATCH] Fix infinite waiting of range locks --- kernel/src/fs/utils/range_lock/mod.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kernel/src/fs/utils/range_lock/mod.rs b/kernel/src/fs/utils/range_lock/mod.rs index 198648da2..53b6e30c2 100644 --- a/kernel/src/fs/utils/range_lock/mod.rs +++ b/kernel/src/fs/utils/range_lock/mod.rs @@ -132,8 +132,10 @@ impl RangeLockItem { } /// Puts the current process in a wait state until the lock condition is satisfied - pub fn wait(&mut self) { - let cond = || None::<()>; + pub fn wait_until(&self, cond: F) + where + F: FnMut() -> Option<()>, + { self.waitqueue.wait_until(cond); } @@ -227,7 +229,7 @@ impl RangeLockList { req_lock, is_nonblocking ); loop { - let mut conflict_lock; + let conflict_lock; { let mut list = self.inner.write(); @@ -243,7 +245,14 @@ impl RangeLockList { } } - conflict_lock.wait(); + conflict_lock.wait_until(|| { + let list = self.inner.read(); + if list.iter().any(|l| req_lock.conflict_with(l)) { + None + } else { + Some(()) + } + }); } }