Use AcquireSRWLockExclusive::is_available() instead of an extra lookup.

This commit is contained in:
Mara Bos 2020-09-20 14:49:04 +02:00
parent 8b2bdfd453
commit 93310efdbe
1 changed files with 6 additions and 17 deletions

View File

@ -23,7 +23,6 @@ use crate::cell::{Cell, UnsafeCell};
use crate::mem::{self, MaybeUninit}; use crate::mem::{self, MaybeUninit};
use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sys::c; use crate::sys::c;
use crate::sys::compat;
pub struct Mutex { pub struct Mutex {
// This is either directly an SRWLOCK (if supported), or a Box<Inner> otherwise. // This is either directly an SRWLOCK (if supported), or a Box<Inner> otherwise.
@ -40,8 +39,8 @@ struct Inner {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
enum Kind { enum Kind {
SRWLock = 1, SRWLock,
CriticalSection = 2, CriticalSection,
} }
#[inline] #[inline]
@ -130,21 +129,11 @@ impl Mutex {
} }
fn kind() -> Kind { fn kind() -> Kind {
static KIND: AtomicUsize = AtomicUsize::new(0); if c::AcquireSRWLockExclusive::is_available() {
Kind::SRWLock
let val = KIND.load(Ordering::SeqCst); } else {
if val == Kind::SRWLock as usize { Kind::CriticalSection
return Kind::SRWLock;
} else if val == Kind::CriticalSection as usize {
return Kind::CriticalSection;
} }
let ret = match compat::lookup("kernel32", "AcquireSRWLockExclusive") {
None => Kind::CriticalSection,
Some(..) => Kind::SRWLock,
};
KIND.store(ret as usize, Ordering::SeqCst);
ret
} }
pub struct ReentrantMutex { pub struct ReentrantMutex {