forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libra-infallible] std::sync::RwLock -> wrapped RwLock
- Loading branch information
1 parent
ee2db72
commit dc70234
Showing
4 changed files
with
76 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) The Libra Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::{RwLock as RwLockImpl, RwLockReadGuard, RwLockWriteGuard}; | ||
|
||
/// A simple wrapper around the lock() function of a std::sync::RwLock | ||
/// The only difference is that you don't need to call unwrap() on it. | ||
#[derive(Debug, Default)] | ||
pub struct RwLock<T>(RwLockImpl<T>); | ||
|
||
impl<T> RwLock<T> { | ||
/// creates a read-write lock | ||
pub fn new(t: T) -> Self { | ||
Self(RwLockImpl::new(t)) | ||
} | ||
|
||
/// lock the rwlock in read mode | ||
pub fn read(&self) -> RwLockReadGuard<'_, T> { | ||
self.0 | ||
.read() | ||
.expect("libra cannot currently handle a poisoned lock") | ||
} | ||
|
||
/// lock the rwlock in write mode | ||
pub fn write(&self) -> RwLockWriteGuard<'_, T> { | ||
self.0 | ||
.write() | ||
.expect("libra cannot currently handle a poisoned lock") | ||
} | ||
|
||
/// return the owned type consuming the lock | ||
pub fn into_inner(self) -> T { | ||
self.0 | ||
.into_inner() | ||
.expect("libra cannot currently handle a poisoned lock") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
use std::{sync::Arc, thread}; | ||
|
||
#[test] | ||
fn test_libra_rwlock() { | ||
let a = 7u8; | ||
let rwlock = Arc::new(RwLock::new(a)); | ||
let rwlock2 = rwlock.clone(); | ||
let rwlock3 = rwlock.clone(); | ||
|
||
let thread1 = thread::spawn(move || { | ||
let mut b = rwlock2.write(); | ||
*b = 8; | ||
}); | ||
let thread2 = thread::spawn(move || { | ||
let mut b = rwlock3.write(); | ||
*b = 9; | ||
}); | ||
|
||
let _ = thread1.join(); | ||
let _ = thread2.join(); | ||
|
||
let _ = rwlock.read(); | ||
} | ||
} |
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