2014-01-27 22:41:10 -08:00
|
|
|
// 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.
|
|
|
|
|
2014-11-14 14:33:51 -08:00
|
|
|
//! A simple native mutex implementation. Warning: this API is likely
|
|
|
|
//! to change soon.
|
2014-01-27 22:41:10 -08:00
|
|
|
|
2014-11-14 14:33:51 -08:00
|
|
|
#![allow(dead_code)]
|
2014-01-27 22:41:10 -08:00
|
|
|
|
2014-06-07 11:13:26 -07:00
|
|
|
use core::prelude::*;
|
2014-07-10 14:19:17 -07:00
|
|
|
use alloc::boxed::Box;
|
2014-06-07 11:13:26 -07:00
|
|
|
use rustrt::mutex;
|
2014-01-27 22:41:10 -08:00
|
|
|
|
2014-10-06 16:14:30 -07:00
|
|
|
pub const LOCKED: uint = 1 << 0;
|
2014-11-14 14:33:51 -08:00
|
|
|
pub const BLOCKED: uint = 1 << 1;
|
2014-01-27 22:41:10 -08:00
|
|
|
|
|
|
|
/// A mutual exclusion primitive useful for protecting shared data
|
|
|
|
///
|
|
|
|
/// This mutex will properly block tasks waiting for the lock to become
|
|
|
|
/// available. The mutex can also be statically initialized or created via a
|
|
|
|
/// `new` constructor.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-11-23 12:52:37 -08:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// use std::sync::mutex::Mutex;
|
2014-01-27 22:41:10 -08:00
|
|
|
///
|
2014-03-22 00:47:43 -07:00
|
|
|
/// let m = Mutex::new();
|
2014-01-27 22:41:10 -08:00
|
|
|
/// let guard = m.lock();
|
|
|
|
/// // do some work
|
|
|
|
/// drop(guard); // unlock the lock
|
|
|
|
/// ```
|
|
|
|
pub struct Mutex {
|
2014-06-12 11:40:13 -07:00
|
|
|
// Note that this static mutex is in a *box*, not inlined into the struct
|
|
|
|
// itself. This is done for memory safety reasons with the usage of a
|
|
|
|
// StaticNativeMutex inside the static mutex above. 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.
|
|
|
|
lock: Box<StaticMutex>,
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
///
|
2014-11-23 12:52:37 -08:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// use std::sync::mutex::{StaticMutex, MUTEX_INIT};
|
2014-01-27 22:41:10 -08:00
|
|
|
///
|
2014-10-10 21:59:10 -07:00
|
|
|
/// static LOCK: StaticMutex = MUTEX_INIT;
|
2014-01-27 22:41:10 -08:00
|
|
|
///
|
2014-10-10 21:59:10 -07:00
|
|
|
/// {
|
2014-01-27 22:41:10 -08:00
|
|
|
/// let _g = LOCK.lock();
|
|
|
|
/// // do some productive work
|
|
|
|
/// }
|
|
|
|
/// // lock is unlocked here.
|
|
|
|
/// ```
|
|
|
|
pub struct StaticMutex {
|
2014-03-27 15:10:45 -07:00
|
|
|
lock: mutex::StaticNativeMutex,
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
|
|
|
|
/// dropped (falls out of scope), the lock will be unlocked.
|
2014-02-15 12:53:23 +11:00
|
|
|
#[must_use]
|
2014-01-27 22:41:10 -08:00
|
|
|
pub struct Guard<'a> {
|
2014-11-14 14:33:51 -08:00
|
|
|
guard: mutex::LockGuard<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lift_guard(guard: mutex::LockGuard) -> Guard {
|
|
|
|
Guard { guard: guard }
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Static initialization of a mutex. This constant can be used to initialize
|
|
|
|
/// other mutex constants.
|
2014-10-06 16:14:30 -07:00
|
|
|
pub const MUTEX_INIT: StaticMutex = StaticMutex {
|
2014-11-14 14:33:51 -08:00
|
|
|
lock: mutex::NATIVE_MUTEX_INIT
|
2014-01-27 22:41:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
impl StaticMutex {
|
|
|
|
/// Attempts to grab this lock, see `Mutex::try_lock`
|
2014-03-22 00:47:43 -07:00
|
|
|
pub fn try_lock<'a>(&'a self) -> Option<Guard<'a>> {
|
2014-11-14 14:33:51 -08:00
|
|
|
unsafe { self.lock.trylock().map(lift_guard) }
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires this lock, see `Mutex::lock`
|
2014-03-22 00:47:43 -07:00
|
|
|
pub fn lock<'a>(&'a self) -> Guard<'a> {
|
2014-11-14 14:33:51 -08:00
|
|
|
lift_guard(unsafe { self.lock.lock() })
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2014-03-22 00:47:43 -07:00
|
|
|
pub unsafe fn destroy(&self) {
|
2014-01-27 22:41:10 -08:00
|
|
|
self.lock.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mutex {
|
|
|
|
/// Creates a new mutex in an unlocked state ready for use.
|
|
|
|
pub fn new() -> Mutex {
|
|
|
|
Mutex {
|
2014-06-12 11:40:13 -07:00
|
|
|
lock: box StaticMutex {
|
2014-02-15 11:18:49 +11:00
|
|
|
lock: unsafe { mutex::StaticNativeMutex::new() },
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2014-03-22 00:47:43 -07:00
|
|
|
pub fn try_lock<'a>(&'a self) -> Option<Guard<'a>> {
|
2014-01-27 22:41:10 -08:00
|
|
|
self.lock.try_lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires a mutex, blocking the current task until it is able to do so.
|
|
|
|
///
|
2014-02-17 22:53:45 +11:00
|
|
|
/// This function will block the local task until it is available to acquire
|
2014-01-27 22:41:10 -08:00
|
|
|
/// 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.
|
2014-03-22 00:47:43 -07:00
|
|
|
pub fn lock<'a>(&'a self) -> Guard<'a> { self.lock.lock() }
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Mutex {
|
|
|
|
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.lock.destroy() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-11-23 12:52:37 -08:00
|
|
|
use prelude::*;
|
2014-01-27 22:41:10 -08:00
|
|
|
use super::{Mutex, StaticMutex, MUTEX_INIT};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke() {
|
2014-03-22 00:47:43 -07:00
|
|
|
let m = Mutex::new();
|
2014-01-27 22:41:10 -08:00
|
|
|
drop(m.lock());
|
|
|
|
drop(m.lock());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_static() {
|
2014-10-05 18:11:17 +08:00
|
|
|
static M: StaticMutex = MUTEX_INIT;
|
2014-01-27 22:41:10 -08:00
|
|
|
unsafe {
|
2014-10-05 18:11:17 +08:00
|
|
|
drop(M.lock());
|
|
|
|
drop(M.lock());
|
|
|
|
M.destroy();
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lots_and_lots() {
|
2014-10-05 18:11:17 +08:00
|
|
|
static M: StaticMutex = MUTEX_INIT;
|
2014-01-27 22:41:10 -08:00
|
|
|
static mut CNT: uint = 0;
|
2014-10-05 18:11:17 +08:00
|
|
|
static J: uint = 1000;
|
|
|
|
static K: uint = 3;
|
2014-01-27 22:41:10 -08:00
|
|
|
|
|
|
|
fn inc() {
|
2014-10-05 18:11:17 +08:00
|
|
|
for _ in range(0, J) {
|
2014-01-27 22:41:10 -08:00
|
|
|
unsafe {
|
2014-10-05 18:11:17 +08:00
|
|
|
let _g = M.lock();
|
2014-01-27 22:41:10 -08:00
|
|
|
CNT += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-10-05 18:11:17 +08:00
|
|
|
for _ in range(0, K) {
|
2014-03-09 14:58:32 -07:00
|
|
|
let tx2 = tx.clone();
|
2014-10-14 23:05:01 -07:00
|
|
|
spawn(proc() { inc(); tx2.send(()); });
|
2014-03-09 14:58:32 -07:00
|
|
|
let tx2 = tx.clone();
|
|
|
|
spawn(proc() { inc(); tx2.send(()); });
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
drop(tx);
|
2014-10-05 18:11:17 +08:00
|
|
|
for _ in range(0, 2 * K) {
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
2014-10-05 18:11:17 +08:00
|
|
|
assert_eq!(unsafe {CNT}, J * K * 2);
|
2014-01-27 22:41:10 -08:00
|
|
|
unsafe {
|
2014-10-05 18:11:17 +08:00
|
|
|
M.destroy();
|
2014-01-27 22:41:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trylock() {
|
2014-03-22 00:47:43 -07:00
|
|
|
let m = Mutex::new();
|
2014-01-27 22:41:10 -08:00
|
|
|
assert!(m.try_lock().is_some());
|
|
|
|
}
|
|
|
|
}
|