|
@@ -20,10 +20,43 @@ pub struct Mutex<T> {
|
|
|
locked: AtomicBool,
|
|
|
}
|
|
|
|
|
|
+impl<T> Mutex<T> {
|
|
|
+ /// Create a new mutex with some inner value.
|
|
|
+ #[inline]
|
|
|
+ pub const fn new(inner: T) -> Mutex<T> {
|
|
|
+ Mutex {
|
|
|
+ inner: UnsafeCell::new(inner),
|
|
|
+ locked: AtomicBool::new(false),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Lock this mutex.
|
|
|
+ ///
|
|
|
+ /// If another lock is held, this will block the thread until it is released.
|
|
|
+ #[inline]
|
|
|
+ pub fn lock(&self) -> MutexGuard<T> {
|
|
|
+ // Lock the mutex.
|
|
|
+ #[cfg(not(feature = "unsafe_no_mutex_lock"))]
|
|
|
+ while self.locked.compare_and_swap(false, true, atomic::Ordering::SeqCst) {
|
|
|
+ // ,___,
|
|
|
+ // {O,o}
|
|
|
+ // |)``)
|
|
|
+ // SRSLY?
|
|
|
+ sys::yield_now();
|
|
|
+ }
|
|
|
+
|
|
|
+ MutexGuard {
|
|
|
+ mutex: self,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// A mutex guard.
|
|
|
///
|
|
|
/// This acts as the lock.
|
|
|
+#[must_use]
|
|
|
pub struct MutexGuard<'a, T: 'a> {
|
|
|
+ /// The parent mutex.
|
|
|
mutex: &'a Mutex<T>,
|
|
|
}
|
|
|
|
|
@@ -54,37 +87,6 @@ impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<T> Mutex<T> {
|
|
|
- /// Create a new mutex with some inner value.
|
|
|
- #[inline]
|
|
|
- pub const fn new(inner: T) -> Mutex<T> {
|
|
|
- Mutex {
|
|
|
- inner: UnsafeCell::new(inner),
|
|
|
- locked: AtomicBool::new(false),
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// Lock this mutex.
|
|
|
- ///
|
|
|
- /// If another lock is held, this will block the thread until it is released.
|
|
|
- #[inline]
|
|
|
- pub fn lock(&self) -> MutexGuard<T> {
|
|
|
- // Lock the mutex.
|
|
|
- #[cfg(not(feature = "unsafe_no_mutex_lock"))]
|
|
|
- while self.locked.compare_and_swap(false, true, atomic::Ordering::SeqCst) {
|
|
|
- // ,___,
|
|
|
- // {O,o}
|
|
|
- // |)``)
|
|
|
- // SRSLY?
|
|
|
- sys::yield_now();
|
|
|
- }
|
|
|
-
|
|
|
- MutexGuard {
|
|
|
- mutex: self,
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
unsafe impl<T: Send> Send for Mutex<T> {}
|
|
|
unsafe impl<T: Send> Sync for Mutex<T> {}
|
|
|
|