]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/mutex.rs
auto merge of #21132 : sfackler/rust/wait_timeout, r=alexcrichton
[rust.git] / src / libstd / sys / windows / 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 marker::Sync;
12 use cell::UnsafeCell;
13 use sys::sync as ffi;
14
15 pub struct Mutex { inner: UnsafeCell<ffi::SRWLOCK> }
16
17 pub const MUTEX_INIT: Mutex = Mutex {
18     inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
19 };
20
21 unsafe impl Sync for Mutex {}
22
23 #[inline]
24 pub unsafe fn raw(m: &Mutex) -> ffi::PSRWLOCK {
25     m.inner.get()
26 }
27
28 // So you might be asking why we're using SRWLock instead of CriticalSection?
29 //
30 // 1. SRWLock is several times faster than CriticalSection according to benchmarks performed on both
31 // Windows 8 and Windows 7.
32 //
33 // 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix implementation
34 // deadlocks so consistency is preferred. See #19962 for more details.
35 //
36 // 3. While CriticalSection is fair and SRWLock is not, the current Rust policy is there there are
37 // no guarantees of fairness.
38
39 impl Mutex {
40     #[inline]
41     pub unsafe fn new() -> Mutex { MUTEX_INIT }
42     #[inline]
43     pub unsafe fn lock(&self) {
44         ffi::AcquireSRWLockExclusive(self.inner.get())
45     }
46     #[inline]
47     pub unsafe fn try_lock(&self) -> bool {
48         ffi::TryAcquireSRWLockExclusive(self.inner.get()) != 0
49     }
50     #[inline]
51     pub unsafe fn unlock(&self) {
52         ffi::ReleaseSRWLockExclusive(self.inner.get())
53     }
54     #[inline]
55     pub unsafe fn destroy(&self) {
56         // ...
57     }
58 }