Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReentrantMutex::bump() lock count #390

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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