]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/mutex.rs
SGX target: implement synchronization primitives and threading
[rust.git] / src / libstd / sys_common / mutex.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use sys::mutex as imp;
12
13 /// An OS-based mutual exclusion lock.
14 ///
15 /// This is the thinnest cross-platform wrapper around OS mutexes. All usage of
16 /// this mutex is unsafe and it is recommended to instead use the safe wrapper
17 /// at the top level of the crate instead of this type.
18 pub struct Mutex(imp::Mutex);
19
20 unsafe impl Sync for Mutex {}
21
22 impl Mutex {
23     /// Creates a new mutex for use.
24     ///
25     /// Behavior is undefined if the mutex is moved after it is
26     /// first used with any of the functions below.
27     /// Also, until `init` is called, behavior is undefined if this
28     /// mutex is ever used reentrantly, i.e., `raw_lock` or `try_lock`
29     /// are called by the thread currently holding the lock.
30     #[unstable(feature = "sys_internals", issue = "0")] // FIXME: min_const_fn
31     pub const fn new() -> Mutex { Mutex(imp::Mutex::new()) }
32
33     /// Prepare the mutex for use.
34     ///
35     /// This should be called once the mutex is at a stable memory address.
36     /// If called, this must be the very first thing that happens to the mutex.
37     /// Calling it in parallel with or after any operation (including another
38     /// `init()`) is undefined behavior.
39     #[inline]
40     pub unsafe fn init(&mut self) { self.0.init() }
41
42     /// Locks the mutex blocking the current thread until it is available.
43     ///
44     /// Behavior is undefined if the mutex has been moved between this and any
45     /// previous function call.
46     #[inline]
47     pub unsafe fn raw_lock(&self) { self.0.lock() }
48
49     /// Calls raw_lock() and then returns an RAII guard to guarantee the mutex
50     /// will be unlocked.
51     #[inline]
52     pub unsafe fn lock(&self) -> MutexGuard {
53         self.raw_lock();
54         MutexGuard(&self.0)
55     }
56
57     /// Attempts to lock the mutex without blocking, returning whether it was
58     /// successfully acquired or not.
59     ///
60     /// Behavior is undefined if the mutex has been moved between this and any
61     /// previous function call.
62     #[inline]
63     pub unsafe fn try_lock(&self) -> bool { self.0.try_lock() }
64
65     /// Unlocks the mutex.
66     ///
67     /// Behavior is undefined if the current thread does not actually hold the
68     /// mutex.
69     ///
70     /// Consider switching from the pair of raw_lock() and raw_unlock() to
71     /// lock() whenever possible.
72     #[inline]
73     pub unsafe fn raw_unlock(&self) { self.0.unlock() }
74
75     /// Deallocates all resources associated with this mutex.
76     ///
77     /// Behavior is undefined if there are current or will be future users of
78     /// this mutex.
79     #[inline]
80     pub unsafe fn destroy(&self) { self.0.destroy() }
81 }
82
83 // not meant to be exported to the outside world, just the containing module
84 pub fn raw(mutex: &Mutex) -> &imp::Mutex { &mutex.0 }
85
86 #[must_use]
87 /// A simple RAII utility for the above Mutex without the poisoning semantics.
88 pub struct MutexGuard<'a>(&'a imp::Mutex);
89
90 impl<'a> Drop for MutexGuard<'a> {
91     #[inline]
92     fn drop(&mut self) {
93         unsafe { self.0.unlock(); }
94     }
95 }