Skip to content

Commit

Permalink
Use AcquireSRWLockExclusive::is_available() instead of an extra lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Oct 1, 2020
1 parent 8b2bdfd commit 93310ef
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions library/std/src/sys/windows/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::cell::{Cell, UnsafeCell};
use crate::mem::{self, MaybeUninit};
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sys::c;
use crate::sys::compat;

pub struct Mutex {
// This is either directly an SRWLOCK (if supported), or a Box<Inner> otherwise.
Expand All @@ -40,8 +39,8 @@ struct Inner {

#[derive(Clone, Copy)]
enum Kind {
SRWLock = 1,
CriticalSection = 2,
SRWLock,
CriticalSection,
}

#[inline]
Expand Down Expand Up @@ -130,21 +129,11 @@ impl Mutex {
}

fn kind() -> Kind {
static KIND: AtomicUsize = AtomicUsize::new(0);

let val = KIND.load(Ordering::SeqCst);
if val == Kind::SRWLock as usize {
return Kind::SRWLock;
} else if val == Kind::CriticalSection as usize {
return Kind::CriticalSection;
if c::AcquireSRWLockExclusive::is_available() {
Kind::SRWLock
} else {
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 {
Expand Down

0 comments on commit 93310ef

Please sign in to comment.