]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mutex.rs
Add verbose option to rustdoc in order to fix problem with --version
[rust.git] / src / libstd / sys / unix / 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 cell::UnsafeCell;
12 use kinds::Sync;
13 use sys::sync as ffi;
14 use sys_common::mutex;
15
16 pub struct Mutex { inner: UnsafeCell<ffi::pthread_mutex_t> }
17
18 #[inline]
19 pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t {
20     m.inner.get()
21 }
22
23 pub const MUTEX_INIT: Mutex = Mutex {
24     inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
25 };
26
27 unsafe impl Sync for Mutex {}
28
29 impl Mutex {
30     #[inline]
31     pub unsafe fn new() -> Mutex {
32         // Might be moved and address is changing it is better to avoid
33         // initialization of potentially opaque OS data before it landed
34         MUTEX_INIT
35     }
36     #[inline]
37     pub unsafe fn lock(&self) {
38         let r = ffi::pthread_mutex_lock(self.inner.get());
39         debug_assert_eq!(r, 0);
40     }
41     #[inline]
42     pub unsafe fn unlock(&self) {
43         let r = ffi::pthread_mutex_unlock(self.inner.get());
44         debug_assert_eq!(r, 0);
45     }
46     #[inline]
47     pub unsafe fn try_lock(&self) -> bool {
48         ffi::pthread_mutex_trylock(self.inner.get()) == 0
49     }
50     #[inline]
51     pub unsafe fn destroy(&self) {
52         let r = ffi::pthread_mutex_destroy(self.inner.get());
53         debug_assert_eq!(r, 0);
54     }
55 }