Skip to content

Commit

Permalink
Merge pull request #390 from DavisVaughan/fix/reentrant-mutex-bump-count
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Jun 25, 2023
2 parents 04d6cfe + 92609ba commit d36110f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ impl<R: RawMutexFair, G: GetThreadId> RawReentrantMutex<R, G> {
if self.lock_count.get() == 1 {
let id = self.owner.load(Ordering::Relaxed);
self.owner.store(0, Ordering::Relaxed);
self.lock_count.set(0);
self.mutex.bump();
self.owner.store(id, Ordering::Relaxed);
self.lock_count.set(1);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ pub type MappedReentrantMutexGuard<'a, T> =
#[cfg(test)]
mod tests {
use crate::ReentrantMutex;
use crate::ReentrantMutexGuard;
use std::cell::RefCell;
use std::sync::Arc;
use std::thread;
use std::sync::mpsc::channel;

#[cfg(feature = "serde")]
use bincode::{deserialize, serialize};
Expand Down Expand Up @@ -134,6 +136,26 @@ mod tests {
assert_eq!(format!("{:?}", mutex), "ReentrantMutex { data: [0, 10] }");
}

#[test]
fn test_reentrant_mutex_bump() {
let mutex = Arc::new(ReentrantMutex::new(()));
let mutex2 = mutex.clone();

let mut guard = mutex.lock();

let (tx, rx) = channel();

thread::spawn(move || {
let _guard = mutex2.lock();
tx.send(()).unwrap();
});

// `bump()` repeatedly until the thread starts up and requests the lock
while rx.try_recv().is_err() {
ReentrantMutexGuard::bump(&mut guard);
}
}

#[cfg(feature = "serde")]
#[test]
fn test_serde() {
Expand Down

0 comments on commit d36110f

Please sign in to comment.