Replace Mutex with RwLock
This commit is contained in:
parent
b1bd7950d9
commit
c96915b4df
|
@ -213,7 +213,7 @@ impl ModuleInstance {
|
||||||
|
|
||||||
/// Access all globals. This is a non-standard API so it's unlikely to be
|
/// Access all globals. This is a non-standard API so it's unlikely to be
|
||||||
/// portable to other engines.
|
/// portable to other engines.
|
||||||
pub fn globals<'a>(&self) -> ::MyRef<Vec<GlobalRef>> {
|
pub fn globals<'a>(&self) -> ::MyRefRead<Vec<GlobalRef>> {
|
||||||
self.globals.borrow()
|
self.globals.borrow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
pub use alloc::rc::Rc as MyRc;
|
pub use alloc::rc::Rc as MyRc;
|
||||||
pub use alloc::rc::Weak as MyWeak;
|
pub use alloc::rc::Weak as MyWeak;
|
||||||
pub use core::cell::Cell as MyCell;
|
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;
|
pub use core::cell::RefCell as MyRefCell;
|
||||||
|
|
|
@ -1,31 +1,33 @@
|
||||||
extern crate atomic;
|
extern crate atomic;
|
||||||
|
|
||||||
use alloc::sync::{Arc, Mutex};
|
use alloc::sync::{Arc, RwLock};
|
||||||
|
|
||||||
pub use self::atomic::{Atomic, Ordering::Relaxed as Ordering};
|
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`.
|
/// Thread-safe wrapper which can be used in place of a `RefCell`.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MyRefCell<T>(Arc<Mutex<T>>);
|
pub struct MyRefCell<T>(Arc<RwLock<T>>);
|
||||||
|
|
||||||
impl<T> MyRefCell<T> {
|
impl<T> MyRefCell<T> {
|
||||||
/// Create new wrapper object.
|
/// Create new wrapper object.
|
||||||
pub fn new(obj: T) -> MyRefCell<T> {
|
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.
|
/// Borrow a `MyRef` to the inner value.
|
||||||
pub fn borrow(&self) -> ::MyRef<T> {
|
pub fn borrow(&self) -> ::MyRefRead<T> {
|
||||||
self.0
|
self.0
|
||||||
.lock()
|
.read()
|
||||||
.expect("failed to acquire lock while trying to borrow")
|
.expect("failed to acquire lock while trying to borrow")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Borrow a mutable `MyRef` to the inner value.
|
/// Borrow a mutable `MyRef` to the inner value.
|
||||||
pub fn borrow_mut(&self) -> ::MyRef<T> {
|
pub fn borrow_mut(&self) -> ::MyRefWrite<T> {
|
||||||
self.0
|
self.0
|
||||||
.lock()
|
.write()
|
||||||
.expect("failed to acquire lock while trying to borrow mutably")
|
.expect("failed to acquire lock while trying to borrow mutably")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue