From e68c938b995c214d00b9c5844af3f61d32c4260a Mon Sep 17 00:00:00 2001 From: Anshul Malik Date: Wed, 12 Sep 2018 12:37:11 +0530 Subject: [PATCH] format components/remutex --- components/remutex/lib.rs | 49 +++++++++++++++++++++++-------- components/remutex/tests/smoke.rs | 6 ++-- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/components/remutex/lib.rs b/components/remutex/lib.rs index cfdb1b2c7dce..6f8f11ac498d 100644 --- a/components/remutex/lib.rs +++ b/components/remutex/lib.rs @@ -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; @@ -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)] @@ -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>); *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) { @@ -116,7 +121,9 @@ 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)] @@ -124,14 +131,21 @@ impl HandOverHandMutex { 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 { self.owner.load(Ordering::Relaxed) @@ -185,7 +199,7 @@ impl ReentrantMutex { match err { TryLockError::WouldBlock => { trace!("{:?} Would block.", ThreadId::current()); - return Err(TryLockError::WouldBlock) + return Err(TryLockError::WouldBlock); }, TryLockError::Poisoned(_) => { trace!("{:?} Poison!", ThreadId::current()); @@ -200,7 +214,11 @@ impl ReentrantMutex { 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 { @@ -210,7 +228,11 @@ impl ReentrantMutex { } fn mk_guard(&self) -> ReentrantMutexGuard { - 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 } @@ -218,7 +240,10 @@ impl ReentrantMutex { } #[must_use] -pub struct ReentrantMutexGuard<'a, T> where T: 'static { +pub struct ReentrantMutexGuard<'a, T> +where + T: 'static, +{ mutex: &'a ReentrantMutex, } diff --git a/components/remutex/tests/smoke.rs b/components/remutex/tests/smoke.rs index edd5ceb71f6e..31ac9d48402d 100644 --- a/components/remutex/tests/smoke.rs +++ b/components/remutex/tests/smoke.rs @@ -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(); } @@ -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(); @@ -88,4 +89,3 @@ fn poison_works() { let r = m.lock().err().unwrap().into_inner(); assert_eq!(*r.borrow(), 42); } -