File: RWMutex.inc

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (128 lines) | stat: -rw-r--r-- 4,208 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//= llvm/Support/Win32/Mutex.inc - Win32 Reader/Writer Mutual Exclusion Lock  =//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the Win32 specific (non-pthread) RWMutex class.
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only generic Win32 code that
//===          is guaranteed to work on *all* Win32 variants.
//===----------------------------------------------------------------------===//

#include "WindowsSupport.h"

namespace llvm {

// Windows has slim read-writer lock support on Vista and higher, so we
// will attempt to load the APIs.  If they exist, we will use them, and
// if not, we will fall back on critical sections.  When we drop support
// for XP, we can stop lazy-loading these APIs and just use them directly.
#if defined(__MINGW32__)
  // Taken from WinNT.h
  typedef struct _RTL_SRWLOCK {
    PVOID Ptr;
  } RTL_SRWLOCK, *PRTL_SRWLOCK;

  // Taken from WinBase.h
  typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
#endif

static VOID (WINAPI *fpInitializeSRWLock)(PSRWLOCK lock) = NULL;
static VOID (WINAPI *fpAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
static VOID (WINAPI *fpAcquireSRWLockShared)(PSRWLOCK lock) = NULL;
static VOID (WINAPI *fpReleaseSRWLockExclusive)(PSRWLOCK lock) = NULL;
static VOID (WINAPI *fpReleaseSRWLockShared)(PSRWLOCK lock) = NULL;

static bool sHasSRW = false;

static bool loadSRW() {
  static bool sChecked = false;
  if (!sChecked) {
    sChecked = true;

    if (HMODULE hLib = ::GetModuleHandleW(L"Kernel32.dll")) {
      fpInitializeSRWLock =
        (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                               "InitializeSRWLock");
      fpAcquireSRWLockExclusive =
        (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                               "AcquireSRWLockExclusive");
      fpAcquireSRWLockShared =
        (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                               "AcquireSRWLockShared");
      fpReleaseSRWLockExclusive =
        (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                               "ReleaseSRWLockExclusive");
      fpReleaseSRWLockShared =
        (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                               "ReleaseSRWLockShared");

      if (fpInitializeSRWLock != NULL) {
        sHasSRW = true;
      }
    }
  }
  return sHasSRW;
}

sys::RWMutexImpl::RWMutexImpl() {
  if (loadSRW()) {
    data_ = safe_calloc(1, sizeof(SRWLOCK));
    fpInitializeSRWLock(static_cast<PSRWLOCK>(data_));
  } else {
    data_ = safe_calloc(1, sizeof(CRITICAL_SECTION));
    InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  }
}

sys::RWMutexImpl::~RWMutexImpl() {
  if (!sHasSRW)
    DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  // Nothing to do in the case of slim reader/writers except free the memory.
  free(data_);
}

bool sys::RWMutexImpl::reader_acquire() {
  if (sHasSRW) {
    fpAcquireSRWLockShared(static_cast<PSRWLOCK>(data_));
  } else {
    EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  }
  return true;
}

bool sys::RWMutexImpl::reader_release() {
  if (sHasSRW) {
    fpReleaseSRWLockShared(static_cast<PSRWLOCK>(data_));
  } else {
    LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  }
  return true;
}

bool sys::RWMutexImpl::writer_acquire() {
  if (sHasSRW) {
    fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(data_));
  } else {
    EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  }
  return true;
}

bool sys::RWMutexImpl::writer_release() {
  if (sHasSRW) {
    fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(data_));
  } else {
    LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
  }
  return true;
}


}