8000 Fix for corner case where `RwLock` could be acquired in write mode while write locked by Pointerbender · Pull Request #300 · tokio-rs/loom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix for corner case where RwLock could be acquired in write mode while write locked #300

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

Merged
merged 1 commit into from
Feb 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/rt/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ impl RwLock {
// Set the lock to the current thread
state.lock = match state.lock {
Some(Locked::Read(_)) => return false,
_ => Some(Locked::Write(thread_id)),
Some(Locked::Write(_)) => return false,
None => Some(Locked::Write(thread_id)),
};

state.synchronize.sync_load(&mut execution.threads, Acquire);
Expand Down
64 changes: 63 additions & 1 deletion tests/rwlock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use loom::sync::{Arc, RwLock};
use loom::sync::{Arc, RwLock, TryLockResult};
use loom::thread;

use std::rc::Rc;
use std::sync::TryLockError;

#[test]
fn rwlock_read_one() {
Expand Down Expand Up @@ -41,6 +42,67 @@ fn rwlock_read_two_write_one() {
});
}

#[test]
fn rwlock_write_three() {
loom::model(|| {
let lock = Arc::new(RwLock::new(1));

for _ in 0..2 {
let lock = lock.clone();
thread::spawn(move || {
let _l = lock.write().unwrap();

thread::yield_now();
});
}

let _l = lock.write().unwrap();
thread::yield_now();
});
}

#[test]
fn rwlock_write_then_try_write() {
loom::model(|| {
let lock = Arc::new(RwLock::new(1));

let _l1 = lock.write().unwrap();

assert!(matches!(
lock.try_write(),
TryLockResult::Err(TryLockError::WouldBlock)
));
});
}

#[test]
fn rwlock_write_then_try_read() {
loom::model(|| {
let lock = Arc::new(RwLock::new(1));

let _l1 = lock.write().unwrap();

assert!(matches!(
lock.try_read(),
TryLockResult::Err(TryLockError::WouldBlock)
));
});
}

#[test]
fn rwlock_read_then_try_write() {
loom::model(|| {
let lock = Arc::new(RwLock::new(1));

let _l1 = lock.write().unwrap();

assert!(matches!(
lock.try_write(),
TryLockResult::Err(TryLockError::WouldBlock)
));
});
}

#[test]
fn rwlock_try_read() {
loom::model(|| {
Expand Down
0