1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2013 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.

use core::prelude::*;

use core::ty::Unsafe;
use mutex;

/// An OS mutex over some data.
///
/// This is not a safe primitive to use, it is unaware of the libgreen
/// scheduler, as well as being easily susceptible to misuse due to the usage of
/// the inner NativeMutex.
///
/// > **Note**: This type is not recommended for general use. The mutex provided
/// >           as part of `libsync` should almost always be favored.
pub struct Exclusive<T> {
    lock: mutex::NativeMutex,
    data: Unsafe<T>,
}

/// An RAII guard returned via `lock`
pub struct ExclusiveGuard<'a, T> {
    // FIXME #12808: strange name to try to avoid interfering with
    // field accesses of the contained type via Deref
    _data: &'a mut T,
    _guard: mutex::LockGuard<'a>,
}

impl<T: Send> Exclusive<T> {
    /// Creates a new `Exclusive` which will protect the data provided.
    pub fn new(user_data: T) -> Exclusive<T> {
        Exclusive {
            lock: unsafe { mutex::NativeMutex::new() },
            data: Unsafe::new(user_data),
        }
    }

    /// Acquires this lock, returning a guard which the data is accessed through
    /// and from which that lock will be unlocked.
    ///
    /// This method is unsafe due to many of the same reasons that the
    /// NativeMutex itself is unsafe.
    pub unsafe fn lock<'a>(&'a self) -> ExclusiveGuard<'a, T> {
        let guard = self.lock.lock();
        let data = &mut *self.data.get();

        ExclusiveGuard {
            _data: data,
            _guard: guard,
        }
    }
}

impl<'a, T: Send> ExclusiveGuard<'a, T> {
    // The unsafety here should be ok because our loan guarantees that the lock
    // itself is not moving
    pub fn signal(&self) {
        unsafe { self._guard.signal() }
    }
    pub fn wait(&self) {
        unsafe { self._guard.wait() }
    }
}

impl<'a, T: Send> Deref<T> for ExclusiveGuard<'a, T> {
    fn deref<'a>(&'a self) -> &'a T { &*self._data }
}
impl<'a, T: Send> DerefMut<T> for ExclusiveGuard<'a, T> {
    fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut *self._data }
}

#[cfg(test)]
mod tests {
    use std::prelude::*;
    use alloc::arc::Arc;
    use super::Exclusive;
    use std::task;

    #[test]
    fn exclusive_new_arc() {
        unsafe {
            let mut futures = Vec::new();

            let num_tasks = 10;
            let count = 10;

            let total = Arc::new(Exclusive::new(box 0));

            for _ in range(0u, num_tasks) {
                let total = total.clone();
                let (tx, rx) = channel();
                futures.push(rx);

                task::spawn(proc() {
                    for _ in range(0u, count) {
                        **total.lock() += 1;
                    }
                    tx.send(());
                });
            };

            for f in futures.mut_iter() { f.recv() }

            assert_eq!(**total.lock(), num_tasks * count);
        }
    }
}