mutex.rs 16.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
use prelude::v1::*;
12

F
Flavio Percoco 已提交
13
use cell::UnsafeCell;
N
Nick Cameron 已提交
14
use marker;
15
use ops::{Deref, DerefMut};
16
use sync::poison::{self, TryLockError, TryLockResult, LockResult};
A
Alex Crichton 已提交
17
use sys_common::mutex as sys;
18 19 20

/// A mutual exclusion primitive useful for protecting shared data
///
A
Alex Crichton 已提交
21 22 23 24 25 26 27 28 29
/// This mutex will block threads waiting for the lock to become available. The
/// mutex can also be statically initialized or created via a `new`
/// constructor. Each mutex has a type parameter which represents the data that
/// it is protecting. The data can only be accessed through the RAII guards
/// returned from `lock` and `try_lock`, which guarantees that the data is only
/// ever accessed when the mutex is locked.
///
/// # Poisoning
///
30 31 32 33 34
/// The mutexes in this module implement a strategy called "poisoning" where a
/// mutex is considered poisoned whenever a thread panics while holding the
/// lock. Once a mutex is poisoned, all other tasks are unable to access the
/// data by default as it is likely tainted (some invariant is not being
/// upheld).
35
///
36 37 38 39 40 41 42 43 44 45 46
/// For a mutex, this means that the `lock` and `try_lock` methods return a
/// `Result` which indicates whether a mutex has been poisoned or not. Most
/// usage of a mutex will simply `unwrap()` these results, propagating panics
/// among threads to ensure that a possibly invalid invariant is not witnessed.
///
/// A poisoned mutex, however, does not prevent all access to the underlying
/// data. The `PoisonError` type has an `into_guard` method which will return
/// the guard that would have otherwise been returned on a successful lock. This
/// allows access to the data, despite the lock being poisoned.
///
/// # Examples
47
///
A
Alex Crichton 已提交
48 49
/// ```rust
/// use std::sync::{Arc, Mutex};
A
Aaron Turon 已提交
50
/// use std::thread;
51
/// use std::sync::mpsc::channel;
52
///
A
Alex Crichton 已提交
53
/// const N: uint = 10;
54
///
A
Alex Crichton 已提交
55 56 57 58 59 60 61 62
/// // Spawn a few threads to increment a shared variable (non-atomically), and
/// // let the main thread know once all increments are done.
/// //
/// // Here we're using an Arc to share memory among tasks, and the data inside
/// // the Arc is protected with a mutex.
/// let data = Arc::new(Mutex::new(0));
///
/// let (tx, rx) = channel();
63
/// for _ in 0..10 {
A
Alex Crichton 已提交
64
///     let (data, tx) = (data.clone(), tx.clone());
A
Aaron Turon 已提交
65
///     thread::spawn(move || {
A
Alex Crichton 已提交
66 67 68
///         // The shared static can only be accessed once the lock is held.
///         // Our non-atomic increment is safe because we're the only thread
///         // which can access the shared state when the lock is held.
69 70 71 72
///         //
///         // We unwrap() the return value to assert that we are not expecting
///         // tasks to ever fail while holding the lock.
///         let mut data = data.lock().unwrap();
A
Alex Crichton 已提交
73 74
///         *data += 1;
///         if *data == N {
75
///             tx.send(()).unwrap();
A
Alex Crichton 已提交
76 77
///         }
///         // the lock is unlocked here when `data` goes out of scope.
A
Aaron Turon 已提交
78
///     });
A
Alex Crichton 已提交
79 80
/// }
///
81
/// rx.recv().unwrap();
82
/// ```
83 84 85 86 87
///
/// To recover from a poisoned mutex:
///
/// ```rust
/// use std::sync::{Arc, Mutex};
A
Aaron Turon 已提交
88
/// use std::thread;
89
///
90
/// let lock = Arc::new(Mutex::new(0_u32));
91 92
/// let lock2 = lock.clone();
///
A
Aaron Turon 已提交
93
/// let _ = thread::spawn(move || -> () {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
///     // This thread will acquire the mutex first, unwrapping the result of
///     // `lock` because the lock has not been poisoned.
///     let _lock = lock2.lock().unwrap();
///
///     // This panic while holding the lock (`_guard` is in scope) will poison
///     // the mutex.
///     panic!();
/// }).join();
///
/// // The lock is poisoned by this point, but the returned result can be
/// // pattern matched on to return the underlying guard on both branches.
/// let mut guard = match lock.lock() {
///     Ok(guard) => guard,
///     Err(poisoned) => poisoned.into_guard(),
/// };
///
/// *guard += 1;
/// ```
B
Brian Anderson 已提交
112
#[stable(feature = "rust1", since = "1.0.0")]
A
Alex Crichton 已提交
113
pub struct Mutex<T> {
A
Alex Crichton 已提交
114
    // Note that this static mutex is in a *box*, not inlined into the struct
A
Alex Crichton 已提交
115 116 117 118 119
    // itself. Once a native mutex has been used once, its address can never
    // change (it can't be moved). This mutex type can be safely moved at any
    // time, so to ensure that the native mutex is used correctly we box the
    // inner lock to give it a constant address.
    inner: Box<StaticMutex>,
120
    data: UnsafeCell<T>,
121 122
}

123
unsafe impl<T: Send> Send for Mutex<T> { }
F
Flavio Percoco 已提交
124

125
unsafe impl<T: Send> Sync for Mutex<T> { }
F
Flavio Percoco 已提交
126

127 128 129 130 131 132 133 134 135 136
/// The static mutex type is provided to allow for static allocation of mutexes.
///
/// Note that this is a separate type because using a Mutex correctly means that
/// it needs to have a destructor run. In Rust, statics are not allowed to have
/// destructors. As a result, a `StaticMutex` has one extra method when compared
/// to a `Mutex`, a `destroy` method. This method is unsafe to call, and
/// documentation can be found directly on the method.
///
/// # Example
///
A
Alex Crichton 已提交
137 138
/// ```rust
/// use std::sync::{StaticMutex, MUTEX_INIT};
139
///
A
Alex Crichton 已提交
140
/// static LOCK: StaticMutex = MUTEX_INIT;
141
///
A
Alex Crichton 已提交
142
/// {
143
///     let _g = LOCK.lock().unwrap();
144 145 146 147
///     // do some productive work
/// }
/// // lock is unlocked here.
/// ```
148
#[unstable(feature = "std_misc",
149
           reason = "may be merged with Mutex in the future")]
150
pub struct StaticMutex {
A
Alex Crichton 已提交
151
    lock: sys::Mutex,
152
    poison: poison::Flag,
153 154
}

F
Flavio Percoco 已提交
155 156
unsafe impl Sync for StaticMutex {}

157 158
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
/// dropped (falls out of scope), the lock will be unlocked.
A
Alex Crichton 已提交
159 160 161
///
/// The data protected by the mutex can be access through this guard via its
/// Deref and DerefMut implementations
162
#[must_use]
B
Brian Anderson 已提交
163
#[stable(feature = "rust1", since = "1.0.0")]
A
Alex Crichton 已提交
164 165 166
pub struct MutexGuard<'a, T: 'a> {
    // funny underscores due to how Deref/DerefMut currently work (they
    // disregard field privacy).
167 168 169
    __lock: &'a StaticMutex,
    __data: &'a UnsafeCell<T>,
    __poison: poison::Guard,
170 171
}

172 173
impl<'a, T> !marker::Send for MutexGuard<'a, T> {}

174 175
/// Static initialization of a mutex. This constant can be used to initialize
/// other mutex constants.
176
#[unstable(feature = "std_misc",
177
           reason = "may be merged with Mutex in the future")]
A
Alex Crichton 已提交
178
pub const MUTEX_INIT: StaticMutex = StaticMutex {
A
Alex Crichton 已提交
179
    lock: sys::MUTEX_INIT,
180
    poison: poison::FLAG_INIT,
181 182
};

183
impl<T: Send> Mutex<T> {
A
Alex Crichton 已提交
184
    /// Creates a new mutex in an unlocked state ready for use.
B
Brian Anderson 已提交
185
    #[stable(feature = "rust1", since = "1.0.0")]
A
Alex Crichton 已提交
186 187 188
    pub fn new(t: T) -> Mutex<T> {
        Mutex {
            inner: box MUTEX_INIT,
189
            data: UnsafeCell::new(t),
A
Alex Crichton 已提交
190 191 192 193 194 195 196 197 198 199
        }
    }

    /// Acquires a mutex, blocking the current task until it is able to do so.
    ///
    /// This function will block the local task until it is available to acquire
    /// the mutex. Upon returning, the task is the only task with the mutex
    /// held. An RAII guard is returned to allow scoped unlock of the lock. When
    /// the guard goes out of scope, the mutex will be unlocked.
    ///
200
    /// # Failure
A
Alex Crichton 已提交
201 202
    ///
    /// If another user of this mutex panicked while holding the mutex, then
203
    /// this call will return an error once the mutex is acquired.
B
Brian Anderson 已提交
204
    #[stable(feature = "rust1", since = "1.0.0")]
205 206
    pub fn lock(&self) -> LockResult<MutexGuard<T>> {
        unsafe { self.inner.lock.lock() }
207
        MutexGuard::new(&*self.inner, &self.data)
A
Alex Crichton 已提交
208 209 210 211 212 213 214 215 216 217
    }

    /// Attempts to acquire this lock.
    ///
    /// If the lock could not be acquired at this time, then `None` is returned.
    /// Otherwise, an RAII guard is returned. The lock will be unlocked when the
    /// guard is dropped.
    ///
    /// This function does not block.
    ///
218
    /// # Failure
A
Alex Crichton 已提交
219 220
    ///
    /// If another user of this mutex panicked while holding the mutex, then
221
    /// this call will return failure if the mutex would otherwise be
A
Alex Crichton 已提交
222
    /// acquired.
B
Brian Anderson 已提交
223
    #[stable(feature = "rust1", since = "1.0.0")]
224 225
    pub fn try_lock(&self) -> TryLockResult<MutexGuard<T>> {
        if unsafe { self.inner.lock.try_lock() } {
226
            Ok(try!(MutexGuard::new(&*self.inner, &self.data)))
227 228
        } else {
            Err(TryLockError::WouldBlock)
A
Alex Crichton 已提交
229
        }
230
    }
231 232 233 234 235 236 237 238 239 240 241

    /// Determine whether the lock is poisoned.
    ///
    /// If another thread is active, the lock can still become poisoned at any
    /// time.  You should not trust a `false` value for program correctness
    /// without additional synchronization.
    #[inline]
    #[unstable(feature = "std_misc")]
    pub fn is_poisoned(&self) -> bool {
        self.inner.poison.get()
    }
A
Alex Crichton 已提交
242
}
243

A
Alex Crichton 已提交
244
#[unsafe_destructor]
B
Brian Anderson 已提交
245
#[stable(feature = "rust1", since = "1.0.0")]
246
impl<T: Send> Drop for Mutex<T> {
A
Alex Crichton 已提交
247 248 249 250 251 252 253 254
    fn drop(&mut self) {
        // This is actually safe b/c we know that there is no further usage of
        // this mutex (it's up to the user to arrange for a mutex to get
        // dropped, that's not our job)
        unsafe { self.inner.lock.destroy() }
    }
}

A
Alex Crichton 已提交
255 256 257
struct Dummy(UnsafeCell<()>);
unsafe impl Sync for Dummy {}
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });
258

A
Alex Crichton 已提交
259
impl StaticMutex {
260
    /// Acquires this lock, see `Mutex::lock`
261
    #[inline]
262
    #[unstable(feature = "std_misc",
263
               reason = "may be merged with Mutex in the future")]
264
    pub fn lock(&'static self) -> LockResult<MutexGuard<()>> {
A
Alex Crichton 已提交
265
        unsafe { self.lock.lock() }
A
Alex Crichton 已提交
266
        MutexGuard::new(self, &DUMMY.0)
A
Alex Crichton 已提交
267 268 269
    }

    /// Attempts to grab this lock, see `Mutex::try_lock`
270
    #[inline]
271
    #[unstable(feature = "std_misc",
272
               reason = "may be merged with Mutex in the future")]
273
    pub fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>> {
A
Alex Crichton 已提交
274
        if unsafe { self.lock.try_lock() } {
A
Alex Crichton 已提交
275
            Ok(try!(MutexGuard::new(self, &DUMMY.0)))
A
Alex Crichton 已提交
276
        } else {
277
            Err(TryLockError::WouldBlock)
A
Alex Crichton 已提交
278
        }
279 280 281 282 283 284 285 286 287 288 289 290
    }

    /// Deallocates resources associated with this static mutex.
    ///
    /// This method is unsafe because it provides no guarantees that there are
    /// no active users of this mutex, and safety is not guaranteed if there are
    /// active users of this mutex.
    ///
    /// This method is required to ensure that there are no memory leaks on
    /// *all* platforms. It may be the case that some platforms do not leak
    /// memory if this method is not called, but this is not guaranteed to be
    /// true on all platforms.
291
    #[unstable(feature = "std_misc",
292
               reason = "may be merged with Mutex in the future")]
A
Alex Crichton 已提交
293
    pub unsafe fn destroy(&'static self) {
294 295 296 297
        self.lock.destroy()
    }
}

A
Alex Crichton 已提交
298
impl<'mutex, T> MutexGuard<'mutex, T> {
299 300 301 302 303 304 305 306 307 308 309

    fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>)
           -> LockResult<MutexGuard<'mutex, T>> {
        poison::map_result(lock.poison.borrow(), |guard| {
            MutexGuard {
                __lock: lock,
                __data: data,
                __poison: guard,
            }
        })
    }
A
Alex Crichton 已提交
310
}
311

B
Brian Anderson 已提交
312
#[stable(feature = "rust1", since = "1.0.0")]
313 314 315
impl<'mutex, T> Deref for MutexGuard<'mutex, T> {
    type Target = T;

316
    fn deref<'a>(&'a self) -> &'a T {
317
        unsafe { &*self.__data.get() }
318
    }
A
Alex Crichton 已提交
319
}
B
Brian Anderson 已提交
320
#[stable(feature = "rust1", since = "1.0.0")]
321
impl<'mutex, T> DerefMut for MutexGuard<'mutex, T> {
A
Alex Crichton 已提交
322
    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
323
        unsafe { &mut *self.__data.get() }
A
Alex Crichton 已提交
324 325 326
    }
}

327
#[unsafe_destructor]
B
Brian Anderson 已提交
328
#[stable(feature = "rust1", since = "1.0.0")]
329
impl<'a, T> Drop for MutexGuard<'a, T> {
330
    #[inline]
331 332 333 334
    fn drop(&mut self) {
        unsafe {
            self.__lock.poison.done(&self.__poison);
            self.__lock.lock.unlock();
335 336 337 338
        }
    }
}

339 340
pub fn guard_lock<'a, T>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
    &guard.__lock.lock
341 342
}

343 344
pub fn guard_poison<'a, T>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag {
    &guard.__lock.poison
345 346 347 348
}

#[cfg(test)]
mod test {
349
    use prelude::v1::*;
A
Alex Crichton 已提交
350

351
    use sync::mpsc::channel;
A
Alex Crichton 已提交
352
    use sync::{Arc, Mutex, StaticMutex, MUTEX_INIT, Condvar};
A
Aaron Turon 已提交
353
    use thread;
354

355 356
    struct Packet<T>(Arc<(Mutex<T>, Condvar)>);

357
    unsafe impl<T: Send> Send for Packet<T> {}
358 359
    unsafe impl<T> Sync for Packet<T> {}

360 361
    #[test]
    fn smoke() {
A
Alex Crichton 已提交
362
        let m = Mutex::new(());
363 364
        drop(m.lock().unwrap());
        drop(m.lock().unwrap());
365 366 367 368
    }

    #[test]
    fn smoke_static() {
N
NODA, Kai 已提交
369
        static M: StaticMutex = MUTEX_INIT;
370
        unsafe {
371 372
            drop(M.lock().unwrap());
            drop(M.lock().unwrap());
N
NODA, Kai 已提交
373
            M.destroy();
374 375 376 377 378
        }
    }

    #[test]
    fn lots_and_lots() {
N
NODA, Kai 已提交
379
        static M: StaticMutex = MUTEX_INIT;
380
        static mut CNT: uint = 0;
N
NODA, Kai 已提交
381 382
        static J: uint = 1000;
        static K: uint = 3;
383 384

        fn inc() {
385
            for _ in 0..J {
386
                unsafe {
387
                    let _g = M.lock().unwrap();
388 389 390 391 392
                    CNT += 1;
                }
            }
        }

393
        let (tx, rx) = channel();
394
        for _ in 0..K {
395
            let tx2 = tx.clone();
A
Aaron Turon 已提交
396
            thread::spawn(move|| { inc(); tx2.send(()).unwrap(); });
397
            let tx2 = tx.clone();
A
Aaron Turon 已提交
398
            thread::spawn(move|| { inc(); tx2.send(()).unwrap(); });
399 400
        }

401
        drop(tx);
402
        for _ in 0..2 * K {
403
            rx.recv().unwrap();
404
        }
N
NODA, Kai 已提交
405
        assert_eq!(unsafe {CNT}, J * K * 2);
406
        unsafe {
N
NODA, Kai 已提交
407
            M.destroy();
408 409 410 411
        }
    }

    #[test]
A
Alex Crichton 已提交
412 413
    fn try_lock() {
        let m = Mutex::new(());
414
        *m.try_lock().unwrap() = ();
415
    }
A
Alex Crichton 已提交
416 417 418

    #[test]
    fn test_mutex_arc_condvar() {
419 420
        let packet = Packet(Arc::new((Mutex::new(false), Condvar::new())));
        let packet2 = Packet(packet.0.clone());
A
Alex Crichton 已提交
421
        let (tx, rx) = channel();
A
Aaron Turon 已提交
422
        let _t = thread::spawn(move|| {
A
Alex Crichton 已提交
423
            // wait until parent gets in
424
            rx.recv().unwrap();
425
            let &(ref lock, ref cvar) = &*packet2.0;
426
            let mut lock = lock.lock().unwrap();
A
Alex Crichton 已提交
427 428 429 430
            *lock = true;
            cvar.notify_one();
        });

431
        let &(ref lock, ref cvar) = &*packet.0;
432
        let mut lock = lock.lock().unwrap();
433
        tx.send(()).unwrap();
A
Alex Crichton 已提交
434 435
        assert!(!*lock);
        while !*lock {
436
            lock = cvar.wait(lock).unwrap();
A
Alex Crichton 已提交
437 438 439 440 441
        }
    }

    #[test]
    fn test_arc_condvar_poison() {
T
Tobias Bucher 已提交
442
        let packet = Packet(Arc::new((Mutex::new(1), Condvar::new())));
443
        let packet2 = Packet(packet.0.clone());
A
Alex Crichton 已提交
444 445
        let (tx, rx) = channel();

A
Aaron Turon 已提交
446
        let _t = thread::spawn(move || -> () {
447
            rx.recv().unwrap();
448
            let &(ref lock, ref cvar) = &*packet2.0;
449
            let _g = lock.lock().unwrap();
A
Alex Crichton 已提交
450 451 452 453 454
            cvar.notify_one();
            // Parent should fail when it wakes up.
            panic!();
        });

455
        let &(ref lock, ref cvar) = &*packet.0;
456
        let mut lock = lock.lock().unwrap();
457
        tx.send(()).unwrap();
A
Alex Crichton 已提交
458
        while *lock == 1 {
459 460 461 462 463 464 465
            match cvar.wait(lock) {
                Ok(l) => {
                    lock = l;
                    assert_eq!(*lock, 1);
                }
                Err(..) => break,
            }
A
Alex Crichton 已提交
466 467 468 469 470
        }
    }

    #[test]
    fn test_mutex_arc_poison() {
T
Tobias Bucher 已提交
471
        let arc = Arc::new(Mutex::new(1));
472
        assert!(!arc.is_poisoned());
A
Alex Crichton 已提交
473
        let arc2 = arc.clone();
A
Aaron Turon 已提交
474
        let _ = thread::spawn(move|| {
475
            let lock = arc2.lock().unwrap();
A
Alex Crichton 已提交
476
            assert_eq!(*lock, 2);
A
Aaron Turon 已提交
477
        }).join();
478
        assert!(arc.lock().is_err());
479
        assert!(arc.is_poisoned());
A
Alex Crichton 已提交
480 481 482 483 484 485
    }

    #[test]
    fn test_mutex_arc_nested() {
        // Tests nested mutexes and access
        // to underlying data.
T
Tobias Bucher 已提交
486
        let arc = Arc::new(Mutex::new(1));
A
Alex Crichton 已提交
487 488
        let arc2 = Arc::new(Mutex::new(arc));
        let (tx, rx) = channel();
A
Aaron Turon 已提交
489
        let _t = thread::spawn(move|| {
490
            let lock = arc2.lock().unwrap();
491
            let lock2 = lock.lock().unwrap();
A
Alex Crichton 已提交
492
            assert_eq!(*lock2, 1);
493
            tx.send(()).unwrap();
A
Alex Crichton 已提交
494
        });
495
        rx.recv().unwrap();
A
Alex Crichton 已提交
496 497 498 499
    }

    #[test]
    fn test_mutex_arc_access_in_unwind() {
T
Tobias Bucher 已提交
500
        let arc = Arc::new(Mutex::new(1));
A
Alex Crichton 已提交
501
        let arc2 = arc.clone();
A
Aaron Turon 已提交
502
        let _ = thread::spawn(move|| -> () {
A
Alex Crichton 已提交
503 504 505 506 507
            struct Unwinder {
                i: Arc<Mutex<int>>,
            }
            impl Drop for Unwinder {
                fn drop(&mut self) {
508
                    *self.i.lock().unwrap() += 1;
A
Alex Crichton 已提交
509 510 511 512
                }
            }
            let _u = Unwinder { i: arc2 };
            panic!();
A
Aaron Turon 已提交
513
        }).join();
514
        let lock = arc.lock().unwrap();
A
Alex Crichton 已提交
515 516
        assert_eq!(*lock, 2);
    }
517
}