-
Notifications
You must be signed in to change notification settings - Fork 111
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
Add support for lock_api #161
Open
mystor
wants to merge
2
commits into
tokio-rs:master
Choose a base branch
from
mystor:lock_api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//! Mock implementation of the `lock_api` and `parking_lot` crates. | ||
//! | ||
//! _These types are only available if loom is built with the `"lock_api"` | ||
//! feature_ | ||
|
||
use crate::rt; | ||
use lock_api_ as lock_api; | ||
use once_cell::sync::OnceCell; | ||
|
||
/// Mock implementation of `lock_api::RawMutex` | ||
#[allow(missing_debug_implementations)] | ||
pub struct RawMutex { | ||
object: OnceCell<rt::Mutex>, | ||
} | ||
|
||
impl RawMutex { | ||
// Unfortunately, we're required to have a `const INIT` in order to | ||
// implement `lock_api::RawMutex`, so we need to lazily create the actual | ||
// `rt::Mutex`. | ||
fn object(&self) -> &rt::Mutex { | ||
self.object.get_or_init(|| rt::Mutex::new(true)) | ||
} | ||
} | ||
|
||
unsafe impl lock_api::RawMutex for RawMutex { | ||
const INIT: RawMutex = RawMutex { | ||
object: OnceCell::new(), | ||
}; | ||
|
||
type GuardMarker = lock_api::GuardNoSend; | ||
|
||
fn lock(&self) { | ||
self.object().acquire_lock(); | ||
} | ||
|
||
fn try_lock(&self) -> bool { | ||
self.object().try_acquire_lock() | ||
} | ||
|
||
unsafe fn unlock(&self) { | ||
self.object().release_lock() | ||
} | ||
|
||
fn is_locked(&self) -> bool { | ||
self.object().is_locked() | ||
} | ||
} | ||
|
||
/// Mock implementation of `lock_api::RawRwLock` | ||
#[allow(missing_debug_implementations)] | ||
pub struct RawRwLock { | ||
object: OnceCell<rt::RwLock>, | ||
} | ||
|
||
impl RawRwLock { | ||
// Unfortunately we're required to have a `const INIT` in order to implement | ||
// `lock_api::RawRwLock`, so we need to lazily create the actual | ||
// `rt::RwLock`. | ||
fn object(&self) -> &rt::RwLock { | ||
self.object.get_or_init(|| rt::RwLock::new()) | ||
} | ||
} | ||
|
||
unsafe impl lock_api::RawRwLock for RawRwLock { | ||
const INIT: RawRwLock = RawRwLock { | ||
object: OnceCell::new(), | ||
}; | ||
|
||
type GuardMarker = lock_api::GuardNoSend; | ||
|
||
fn lock_shared(&self) { | ||
self.object().acquire_read_lock() | ||
} | ||
|
||
fn try_lock_shared(&self) -> bool { | ||
self.object().try_acquire_read_lock() | ||
} | ||
|
||
unsafe fn unlock_shared(&self) { | ||
self.object().release_read_lock() | ||
} | ||
|
||
fn lock_exclusive(&self) { | ||
self.object().acquire_write_lock() | ||
} | ||
|
||
fn try_lock_exclusive(&self) -> bool { | ||
self.object().try_acquire_write_lock() | ||
} | ||
|
||
unsafe fn unlock_exclusive(&self) { | ||
self.object().release_write_lock() | ||
} | ||
|
||
fn is_locked(&self) -> bool { | ||
let object = self.object(); | ||
object.is_read_locked() || object.is_write_locked() | ||
} | ||
} | ||
|
||
/// Mock implementation of `lock_api::Mutex` | ||
pub type Mutex<T> = lock_api::Mutex<RawMutex, T>; | ||
|
||
/// Mock implementation of `lock_api::MutexGuard` | ||
pub type MutexGuard<'a, T> = lock_api::MutexGuard<'a, RawMutex, T>; | ||
|
||
/// Mock implementation of `lock_api::MappedMutexGuard` | ||
pub type MappedMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawMutex, T>; | ||
|
||
/// Mock implementation of `lock_api::RwLock` | ||
pub type RwLock<T> = lock_api::RwLock<RawRwLock, T>; | ||
|
||
/// Mock implementation of `lock_api::RwLockReadGuard` | ||
pub type RwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawRwLock, T>; | ||
|
||
/// Mock implementation of `lock_api::RwLockWriteGuard` | ||
pub type RwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawRwLock, T>; | ||
|
||
/// Mock implementation of `lock_api::MappedRwLockReadGuard` | ||
pub type MappedRwLockReadGuard<'a, T> = lock_api::MappedRwLockReadGuard<'a, RawRwLock, T>; | ||
|
||
/// Mock implementation of `lock_api::MappedRwLockWriteGuard` | ||
pub type MappedRwLockWriteGuard<'a, T> = lock_api::MappedRwLockWriteGuard<'a, RawRwLock, T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#![cfg(feature = "lock_api")] | ||
#![deny(warnings, rust_2018_idioms)] | ||
|
||
use loom::cell::UnsafeCell; | ||
use loom::lock_api::{Mutex, RwLock}; | ||
use loom::sync::atomic::AtomicUsize; | ||
use loom::thread; | ||
|
||
use std::rc::Rc; | ||
use std::sync::atomic::Ordering::SeqCst; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn mutex_enforces_mutal_exclusion() { | ||
loom::model(|| { | ||
let data = Rc::new((Mutex::new(0), AtomicUsize::new(0))); | ||
|
||
let ths: Vec<_> = (0..2) | ||
.map(|_| { | ||
let data = data.clone(); | ||
|
||
thread::spawn(move || { | ||
let mut locked = data.0.lock(); | ||
|
||
let prev = data.1.fetch_add(1, SeqCst); | ||
assert_eq!(prev, *locked); | ||
*locked += 1; | ||
}) | ||
}) | ||
.collect(); | ||
|
||
for th in ths { | ||
th.join().unwrap(); | ||
} | ||
|
||
let locked = data.0.lock(); | ||
|
||
assert_eq!(*locked, data.1.load(SeqCst)); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn mutex_establishes_seq_cst() { | ||
loom::model(|| { | ||
struct Data { | ||
cell: UnsafeCell<usize>, | ||
flag: Mutex<bool>, | ||
} | ||
|
||
let data = Rc::new(Data { | ||
cell: UnsafeCell::new(0), | ||
flag: Mutex::new(false), | ||
}); | ||
|
||
{ | ||
let data = data.clone(); | ||
|
||
thread::spawn(move || { | ||
unsafe { data.cell.with_mut(|v| *v = 1) }; | ||
*data.flag.lock() = true; | ||
}); | ||
} | ||
|
||
let flag = *data.flag.lock(); | ||
|
||
if flag { | ||
let v = unsafe { data.cell.with(|v| *v) }; | ||
assert_eq!(v, 1); | ||
} | ||
}); | ||
} | ||
|
||
#[test] | ||
fn rwlock_read_one() { | ||
loom::model(|| { | ||
let lock = Arc::new(RwLock::new(1)); | ||
let c_lock = lock.clone(); | ||
|
||
let n = lock.read(); | ||
assert_eq!(*n, 1); | ||
|
||
thread::spawn(move || { | ||
let _l = c_lock.read(); | ||
}) | ||
.join() | ||
.unwrap(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn rwlock_read_two_write_one() { | ||
loom::model(|| { | ||
let lock = Arc::new(RwLock::new(1)); | ||
|
||
for _ in 0..2 { | ||
let lock = lock.clone(); | ||
|
||
thread::spawn(move || { | ||
let _l = lock.read(); | ||
|
||
thread::yield_now(); | ||
}); | ||
} | ||
|
||
let _l = lock.write(); | ||
thread::yield_now(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn rwlock_try_read() { | ||
loom::model(|| { | ||
let lock = RwLock::new(1); | ||
|
||
match lock.try_read() { | ||
Some(n) => assert_eq!(*n, 1), | ||
None => unreachable!(), | ||
}; | ||
}); | ||
} | ||
|
||
#[test] | ||
fn rwlock_write() { | ||
loom::model(|| { | ||
let lock = RwLock::new(1); | ||
|
||
let mut n = lock.write(); | ||
*n = 2; | ||
|
||
assert!(lock.try_read().is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn rwlock_try_write() { | ||
loom::model(|| { | ||
let lock = RwLock::new(1); | ||
|
||
let n = lock.read(); | ||
assert_eq!(*n, 1); | ||
|
||
assert!(lock.try_write().is_none()); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't think of a cleaner way to satisfy this requirement, so I threw in a OnceCell.