Replace Mutex with RwLock

This commit is contained in:
Michael Mueller 2019-06-20 08:24:57 +02:00
parent b1bd7950d9
commit c96915b4df
No known key found for this signature in database
GPG Key ID: 95756F716F423159
3 changed files with 13 additions and 10 deletions

View File

@ -213,7 +213,7 @@ impl ModuleInstance {
/// Access all globals. This is a non-standard API so it's unlikely to be
/// portable to other engines.
pub fn globals<'a>(&self) -> ::MyRef<Vec<GlobalRef>> {
pub fn globals<'a>(&self) -> ::MyRefRead<Vec<GlobalRef>> {
self.globals.borrow()
}

View File

@ -1,5 +1,6 @@
pub use alloc::rc::Rc as MyRc;
pub use alloc::rc::Weak as MyWeak;
pub use core::cell::Cell as MyCell;
pub use core::cell::Ref as MyRef;
pub use core::cell::Ref as MyRefRead;
pub use core::cell::Ref as MyRefWrite;
pub use core::cell::RefCell as MyRefCell;

View File

@ -1,31 +1,33 @@
extern crate atomic;
use alloc::sync::{Arc, Mutex};
use alloc::sync::{Arc, RwLock};
pub use self::atomic::{Atomic, Ordering::Relaxed as Ordering};
pub use alloc::sync::{Arc as MyRc, MutexGuard as MyRef, Weak as MyWeak};
pub use alloc::sync::{
Arc as MyRc, RwLockReadGuard as MyRefRead, RwLockWriteGuard as MyRefWrite, Weak as MyWeak,
};
/// Thread-safe wrapper which can be used in place of a `RefCell`.
#[derive(Debug)]
pub struct MyRefCell<T>(Arc<Mutex<T>>);
pub struct MyRefCell<T>(Arc<RwLock<T>>);
impl<T> MyRefCell<T> {
/// Create new wrapper object.
pub fn new(obj: T) -> MyRefCell<T> {
MyRefCell(Arc::new(Mutex::new(obj)))
MyRefCell(Arc::new(RwLock::new(obj)))
}
/// Borrow a `MyRef` to the inner value.
pub fn borrow(&self) -> ::MyRef<T> {
pub fn borrow(&self) -> ::MyRefRead<T> {
self.0
.lock()
.read()
.expect("failed to acquire lock while trying to borrow")
}
/// Borrow a mutable `MyRef` to the inner value.
pub fn borrow_mut(&self) -> ::MyRef<T> {
pub fn borrow_mut(&self) -> ::MyRefWrite<T> {
self.0
.lock()
.write()
.expect("failed to acquire lock while trying to borrow mutably")
}
}