Skip to content

Commit

Permalink
format components/remutex
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshulMalik committed Sep 12, 2018
1 parent 8c3b192 commit e68c938
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
49 changes: 37 additions & 12 deletions components/remutex/lib.rs
Expand Up @@ -10,8 +10,10 @@
//! It provides the same interface as https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
//! so if those types are ever exported, we should be able to replace this implemtation.

#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;

use std::cell::{Cell, UnsafeCell};
use std::num::NonZeroUsize;
Expand All @@ -26,7 +28,9 @@ use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ThreadId(NonZeroUsize);

lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); }
lazy_static! {
static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1);
}

impl ThreadId {
#[allow(unsafe_code)]
Expand Down Expand Up @@ -96,7 +100,8 @@ impl HandOverHandMutex {
// that is the lifetime is 'a not 'static.
let guard_ptr = &mut *(self.guard.get() as *mut u8 as *mut Option<MutexGuard<'a, ()>>);
*guard_ptr = Some(guard);
self.owner.store(Some(ThreadId::current()), Ordering::Relaxed);
self.owner
.store(Some(ThreadId::current()), Ordering::Relaxed);
}
#[allow(unsafe_code)]
unsafe fn unset_guard_and_owner(&self) {
Expand All @@ -116,22 +121,31 @@ impl HandOverHandMutex {
Ok(guard) => (guard, Ok(())),
Err(err) => (err.into_inner(), Err(PoisonError::new(()))),
};
unsafe { self.set_guard_and_owner(guard); }
unsafe {
self.set_guard_and_owner(guard);
}
result
}
#[allow(unsafe_code)]
pub fn try_lock(&self) -> TryLockResult<()> {
let (guard, result) = match self.mutex.try_lock() {
Ok(guard) => (guard, Ok(())),
Err(TryLockError::WouldBlock) => return Err(TryLockError::WouldBlock),
Err(TryLockError::Poisoned(err)) => (err.into_inner(), Err(TryLockError::Poisoned(PoisonError::new(())))),
Err(TryLockError::Poisoned(err)) => (
err.into_inner(),
Err(TryLockError::Poisoned(PoisonError::new(()))),
),
};
unsafe { self.set_guard_and_owner(guard); }
unsafe {
self.set_guard_and_owner(guard);
}
result
}
#[allow(unsafe_code)]
pub fn unlock(&self) {
unsafe { self.unset_guard_and_owner(); }
unsafe {
self.unset_guard_and_owner();
}
}
pub fn owner(&self) -> Option<ThreadId> {
self.owner.load(Ordering::Relaxed)
Expand Down Expand Up @@ -185,7 +199,7 @@ impl<T> ReentrantMutex<T> {
match err {
TryLockError::WouldBlock => {
trace!("{:?} Would block.", ThreadId::current());
return Err(TryLockError::WouldBlock)
return Err(TryLockError::WouldBlock);
},
TryLockError::Poisoned(_) => {
trace!("{:?} Poison!", ThreadId::current());
Expand All @@ -200,7 +214,11 @@ impl<T> ReentrantMutex<T> {

fn unlock(&self) {
trace!("{:?} Unlocking.", ThreadId::current());
let count = self.count.get().checked_sub(1).expect("Underflowed lock count.");
let count = self
.count
.get()
.checked_sub(1)
.expect("Underflowed lock count.");
trace!("{:?} Decrementing count to {}.", ThreadId::current(), count);
self.count.set(count);
if count == 0 {
Expand All @@ -210,15 +228,22 @@ impl<T> ReentrantMutex<T> {
}

fn mk_guard(&self) -> ReentrantMutexGuard<T> {
let count = self.count.get().checked_add(1).expect("Overflowed lock count.");
let count = self
.count
.get()
.checked_add(1)
.expect("Overflowed lock count.");
trace!("{:?} Incrementing count to {}.", ThreadId::current(), count);
self.count.set(count);
ReentrantMutexGuard { mutex: self }
}
}

#[must_use]
pub struct ReentrantMutexGuard<'a, T> where T: 'static {
pub struct ReentrantMutexGuard<'a, T>
where
T: 'static,
{
mutex: &'a ReentrantMutex<T>,
}

Expand Down
6 changes: 3 additions & 3 deletions components/remutex/tests/smoke.rs
Expand Up @@ -60,7 +60,8 @@ fn trylock_works() {
thread::spawn(move || {
let lock = m2.try_lock();
assert!(lock.is_err());
}).join().unwrap();
}).join()
.unwrap();
let _lock3 = m.try_lock().unwrap();
}

Expand All @@ -75,7 +76,7 @@ impl<'a> Drop for Answer<'a> {
fn poison_works() {
let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
let mc = m.clone();
let result = thread::spawn(move ||{
let result = thread::spawn(move || {
let lock = mc.lock().unwrap();
*lock.borrow_mut() = 1;
let lock2 = mc.lock().unwrap();
Expand All @@ -88,4 +89,3 @@ fn poison_works() {
let r = m.lock().err().unwrap().into_inner();
assert_eq!(*r.borrow(), 42);
}

0 comments on commit e68c938

Please sign in to comment.