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.
15 pub struct Mutex { inner: UnsafeCell<ffi::SRWLOCK> }
17 pub const MUTEX_INIT: Mutex = Mutex {
18 inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
21 unsafe impl Sync for Mutex {}
24 pub unsafe fn raw(m: &Mutex) -> ffi::PSRWLOCK {
28 // So you might be asking why we're using SRWLock instead of CriticalSection?
30 // 1. SRWLock is several times faster than CriticalSection according to benchmarks performed on both
31 // Windows 8 and Windows 7.
33 // 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix implementation
34 // deadlocks so consistency is preferred. See #19962 for more details.
36 // 3. While CriticalSection is fair and SRWLock is not, the current Rust policy is there there are
37 // no guarantees of fairness.
41 pub unsafe fn new() -> Mutex { MUTEX_INIT }
43 pub unsafe fn lock(&self) {
44 ffi::AcquireSRWLockExclusive(self.inner.get())
47 pub unsafe fn try_lock(&self) -> bool {
48 ffi::TryAcquireSRWLockExclusive(self.inner.get()) != 0
51 pub unsafe fn unlock(&self) {
52 ffi::ReleaseSRWLockExclusive(self.inner.get())
55 pub unsafe fn destroy(&self) {