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.
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.
14 use sys_common::mutex;
16 pub struct Mutex { inner: UnsafeCell<ffi::pthread_mutex_t> }
19 pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t {
23 pub const MUTEX_INIT: Mutex = Mutex {
24 inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
27 unsafe impl Sync for Mutex {}
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
37 pub unsafe fn lock(&self) {
38 let r = ffi::pthread_mutex_lock(self.inner.get());
39 debug_assert_eq!(r, 0);
42 pub unsafe fn unlock(&self) {
43 let r = ffi::pthread_mutex_unlock(self.inner.get());
44 debug_assert_eq!(r, 0);
47 pub unsafe fn try_lock(&self) -> bool {
48 ffi::pthread_mutex_trylock(self.inner.get()) == 0
51 pub unsafe fn destroy(&self) {
52 let r = ffi::pthread_mutex_destroy(self.inner.get());
53 debug_assert_eq!(r, 0);