mutex.rs 15.9 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};
50
/// use std::thread::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 0u..10 {
A
Alex Crichton 已提交
64
///     let (data, tx) = (data.clone(), tx.clone());
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 88 89 90 91 92
///
/// To recover from a poisoned mutex:
///
/// ```rust
/// use std::sync::{Arc, Mutex};
/// use std::thread::Thread;
///
/// let lock = Arc::new(Mutex::new(0u));
/// let lock2 = lock.clone();
///
A
Aaron Turon 已提交
93
/// let _ = Thread::scoped(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
}

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

F
Flavio Percoco 已提交
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
};

A
Alex Crichton 已提交
183 184
impl<T: Send> Mutex<T> {
    /// 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
    }
A
Alex Crichton 已提交
231
}
232

A
Alex Crichton 已提交
233
#[unsafe_destructor]
B
Brian Anderson 已提交
234
#[stable(feature = "rust1", since = "1.0.0")]
A
Alex Crichton 已提交
235 236 237 238 239 240 241 242 243
impl<T: Send> Drop for Mutex<T> {
    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 已提交
244 245 246
struct Dummy(UnsafeCell<()>);
unsafe impl Sync for Dummy {}
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });
247

A
Alex Crichton 已提交
248
impl StaticMutex {
249
    /// Acquires this lock, see `Mutex::lock`
250
    #[inline]
251
    #[unstable(feature = "std_misc",
252
               reason = "may be merged with Mutex in the future")]
253
    pub fn lock(&'static self) -> LockResult<MutexGuard<()>> {
A
Alex Crichton 已提交
254
        unsafe { self.lock.lock() }
A
Alex Crichton 已提交
255
        MutexGuard::new(self, &DUMMY.0)
A
Alex Crichton 已提交
256 257 258
    }

    /// Attempts to grab this lock, see `Mutex::try_lock`
259
    #[inline]
260
    #[unstable(feature = "std_misc",
261
               reason = "may be merged with Mutex in the future")]
262
    pub fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>> {
A
Alex Crichton 已提交
263
        if unsafe { self.lock.try_lock() } {
A
Alex Crichton 已提交
264
            Ok(try!(MutexGuard::new(self, &DUMMY.0)))
A
Alex Crichton 已提交
265
        } else {
266
            Err(TryLockError::WouldBlock)
A
Alex Crichton 已提交
267
        }
268 269 270 271 272 273 274 275 276 277 278 279
    }

    /// 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.
280
    #[unstable(feature = "std_misc",
281
               reason = "may be merged with Mutex in the future")]
A
Alex Crichton 已提交
282
    pub unsafe fn destroy(&'static self) {
283 284 285 286
        self.lock.destroy()
    }
}

A
Alex Crichton 已提交
287
impl<'mutex, T> MutexGuard<'mutex, T> {
288 289 290 291 292 293 294 295 296 297 298

    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 已提交
299
}
300

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

305
    fn deref<'a>(&'a self) -> &'a T {
306
        unsafe { &*self.__data.get() }
307
    }
A
Alex Crichton 已提交
308
}
B
Brian Anderson 已提交
309
#[stable(feature = "rust1", since = "1.0.0")]
310
impl<'mutex, T> DerefMut for MutexGuard<'mutex, T> {
A
Alex Crichton 已提交
311
    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
312
        unsafe { &mut *self.__data.get() }
A
Alex Crichton 已提交
313 314 315
    }
}

316
#[unsafe_destructor]
B
Brian Anderson 已提交
317
#[stable(feature = "rust1", since = "1.0.0")]
318
impl<'a, T> Drop for MutexGuard<'a, T> {
319
    #[inline]
320 321 322 323
    fn drop(&mut self) {
        unsafe {
            self.__lock.poison.done(&self.__poison);
            self.__lock.lock.unlock();
324 325 326 327
        }
    }
}

328 329
pub fn guard_lock<'a, T>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
    &guard.__lock.lock
330 331
}

332 333
pub fn guard_poison<'a, T>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag {
    &guard.__lock.poison
334 335 336 337
}

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

340
    use sync::mpsc::channel;
A
Alex Crichton 已提交
341
    use sync::{Arc, Mutex, StaticMutex, MUTEX_INIT, Condvar};
342
    use thread::Thread;
343

344 345 346 347 348
    struct Packet<T>(Arc<(Mutex<T>, Condvar)>);

    unsafe impl<T:'static+Send> Send for Packet<T> {}
    unsafe impl<T> Sync for Packet<T> {}

349 350
    #[test]
    fn smoke() {
A
Alex Crichton 已提交
351
        let m = Mutex::new(());
352 353
        drop(m.lock().unwrap());
        drop(m.lock().unwrap());
354 355 356 357
    }

    #[test]
    fn smoke_static() {
N
NODA, Kai 已提交
358
        static M: StaticMutex = MUTEX_INIT;
359
        unsafe {
360 361
            drop(M.lock().unwrap());
            drop(M.lock().unwrap());
N
NODA, Kai 已提交
362
            M.destroy();
363 364 365 366 367
        }
    }

    #[test]
    fn lots_and_lots() {
N
NODA, Kai 已提交
368
        static M: StaticMutex = MUTEX_INIT;
369
        static mut CNT: uint = 0;
N
NODA, Kai 已提交
370 371
        static J: uint = 1000;
        static K: uint = 3;
372 373

        fn inc() {
374
            for _ in 0..J {
375
                unsafe {
376
                    let _g = M.lock().unwrap();
377 378 379 380 381
                    CNT += 1;
                }
            }
        }

382
        let (tx, rx) = channel();
383
        for _ in 0..K {
384
            let tx2 = tx.clone();
A
Aaron Turon 已提交
385
            Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); });
386
            let tx2 = tx.clone();
A
Aaron Turon 已提交
387
            Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); });
388 389
        }

390
        drop(tx);
391
        for _ in 0..2 * K {
392
            rx.recv().unwrap();
393
        }
N
NODA, Kai 已提交
394
        assert_eq!(unsafe {CNT}, J * K * 2);
395
        unsafe {
N
NODA, Kai 已提交
396
            M.destroy();
397 398 399 400
        }
    }

    #[test]
A
Alex Crichton 已提交
401 402
    fn try_lock() {
        let m = Mutex::new(());
403
        *m.try_lock().unwrap() = ();
404
    }
A
Alex Crichton 已提交
405 406 407

    #[test]
    fn test_mutex_arc_condvar() {
408 409
        let packet = Packet(Arc::new((Mutex::new(false), Condvar::new())));
        let packet2 = Packet(packet.0.clone());
A
Alex Crichton 已提交
410
        let (tx, rx) = channel();
411
        let _t = Thread::spawn(move|| {
A
Alex Crichton 已提交
412
            // wait until parent gets in
413
            rx.recv().unwrap();
414
            let &(ref lock, ref cvar) = &*packet2.0;
415
            let mut lock = lock.lock().unwrap();
A
Alex Crichton 已提交
416 417 418 419
            *lock = true;
            cvar.notify_one();
        });

420
        let &(ref lock, ref cvar) = &*packet.0;
421
        let mut lock = lock.lock().unwrap();
422
        tx.send(()).unwrap();
A
Alex Crichton 已提交
423 424
        assert!(!*lock);
        while !*lock {
425
            lock = cvar.wait(lock).unwrap();
A
Alex Crichton 已提交
426 427 428 429 430
        }
    }

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

435
        let _t = Thread::spawn(move || -> () {
436
            rx.recv().unwrap();
437
            let &(ref lock, ref cvar) = &*packet2.0;
438
            let _g = lock.lock().unwrap();
A
Alex Crichton 已提交
439 440 441 442 443
            cvar.notify_one();
            // Parent should fail when it wakes up.
            panic!();
        });

444
        let &(ref lock, ref cvar) = &*packet.0;
445
        let mut lock = lock.lock().unwrap();
446
        tx.send(()).unwrap();
A
Alex Crichton 已提交
447
        while *lock == 1 {
448 449 450 451 452 453 454
            match cvar.wait(lock) {
                Ok(l) => {
                    lock = l;
                    assert_eq!(*lock, 1);
                }
                Err(..) => break,
            }
A
Alex Crichton 已提交
455 456 457 458 459
        }
    }

    #[test]
    fn test_mutex_arc_poison() {
T
Tobias Bucher 已提交
460
        let arc = Arc::new(Mutex::new(1));
A
Alex Crichton 已提交
461
        let arc2 = arc.clone();
A
Aaron Turon 已提交
462
        let _ = Thread::scoped(move|| {
463
            let lock = arc2.lock().unwrap();
A
Alex Crichton 已提交
464
            assert_eq!(*lock, 2);
A
Aaron Turon 已提交
465
        }).join();
466
        assert!(arc.lock().is_err());
A
Alex Crichton 已提交
467 468 469 470 471 472
    }

    #[test]
    fn test_mutex_arc_nested() {
        // Tests nested mutexes and access
        // to underlying data.
T
Tobias Bucher 已提交
473
        let arc = Arc::new(Mutex::new(1));
A
Alex Crichton 已提交
474 475
        let arc2 = Arc::new(Mutex::new(arc));
        let (tx, rx) = channel();
476
        let _t = Thread::spawn(move|| {
477
            let lock = arc2.lock().unwrap();
478
            let lock2 = lock.lock().unwrap();
A
Alex Crichton 已提交
479
            assert_eq!(*lock2, 1);
480
            tx.send(()).unwrap();
A
Alex Crichton 已提交
481
        });
482
        rx.recv().unwrap();
A
Alex Crichton 已提交
483 484 485 486
    }

    #[test]
    fn test_mutex_arc_access_in_unwind() {
T
Tobias Bucher 已提交
487
        let arc = Arc::new(Mutex::new(1));
A
Alex Crichton 已提交
488
        let arc2 = arc.clone();
A
Aaron Turon 已提交
489
        let _ = Thread::scoped(move|| -> () {
A
Alex Crichton 已提交
490 491 492 493 494
            struct Unwinder {
                i: Arc<Mutex<int>>,
            }
            impl Drop for Unwinder {
                fn drop(&mut self) {
495
                    *self.i.lock().unwrap() += 1;
A
Alex Crichton 已提交
496 497 498 499
                }
            }
            let _u = Unwinder { i: arc2 };
            panic!();
A
Aaron Turon 已提交
500
        }).join();
501
        let lock = arc.lock().unwrap();
A
Alex Crichton 已提交
502 503
        assert_eq!(*lock, 2);
    }
504
}