LLVM: include/llvm/Support/RWMutex.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_SUPPORT_RWMUTEX_H

14#define LLVM_SUPPORT_RWMUTEX_H

15

16#include "llvm/Config/llvm-config.h"

18#include

19#include

20#include <shared_mutex>

21

22#if defined(__APPLE__)

23#define LLVM_USE_RW_MUTEX_IMPL

24#endif

25

26namespace llvm {

27namespace sys {

28

29#if defined(LLVM_USE_RW_MUTEX_IMPL)

30

31class RWMutexImpl {

32

33

34public:

35

36

37 explicit RWMutexImpl();

38

39

40

41

42 RWMutexImpl(const RWMutexImpl &original) = delete;

43 RWMutexImpl &operator=(const RWMutexImpl &) = delete;

44

45

46

47

48 ~RWMutexImpl();

49

50

51

52

53public:

54

55

56

57

58

59 bool lock_shared();

60

61

62

63

64 bool unlock_shared();

65

66

67

68 bool try_lock_shared();

69

70

71

72

73

74

75 bool lock();

76

77

78

79

80 bool unlock();

81

82

83

84 bool try_lock();

85

86

87

88

89private:

90#if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0

91 void *data_ = nullptr;

92#endif

93};

94#endif

95

96

97

98

100#if !defined(LLVM_USE_RW_MUTEX_IMPL)

101 std::shared_mutex impl;

102#else

103 RWMutexImpl impl;

104#endif

105 unsigned readers = 0;

106 unsigned writers = 0;

107

108public:

111 impl.lock_shared();

112 return true;

113 }

114

115

116

117 ++readers;

118 return true;

119 }

120

123 impl.unlock_shared();

124 return true;

125 }

126

127

128

129 assert(readers > 0 && "Reader lock not acquired before release!");

130 --readers;

131 return true;

132 }

133

135

138 impl.lock();

139 return true;

140 }

141

142

143

144 assert(writers == 0 && "Writer lock already acquired!");

145 ++writers;

146 return true;

147 }

148

151 impl.unlock();

152 return true;

153 }

154

155

156

157 assert(writers == 1 && "Writer lock not acquired before release!");

158 --writers;

159 return true;

160 }

161

162 bool try_lock() { return impl.try_lock(); }

163};

164

166

167

168#if !defined(LLVM_USE_RW_MUTEX_IMPL)

169template <bool mt_only>

171#else

174

176 mutex.lock_shared();

177 }

178

180};

181#endif

183

184

185#if !defined(LLVM_USE_RW_MUTEX_IMPL)

186template <bool mt_only>

188#else

191

193 mutex.lock();

194 }

195

197};

198#endif

200

201}

202}

203

204#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

SmartMutex - An R/W mutex with a compile time constant parameter that indicates whether this mutex sh...

Definition RWMutex.h:99

bool unlock_shared()

Definition RWMutex.h:121

bool try_lock()

Definition RWMutex.h:162

bool lock_shared()

Definition RWMutex.h:109

bool lock()

Definition RWMutex.h:136

bool try_lock_shared()

Definition RWMutex.h:134

bool unlock()

Definition RWMutex.h:149

std::lock_guard< SmartRWMutex< mt_only > > SmartScopedWriter

ScopedWriter - RAII acquisition of a writer lock.

Definition RWMutex.h:187

SmartRWMutex< false > RWMutex

Definition RWMutex.h:165

const std::shared_lock< SmartRWMutex< mt_only > > SmartScopedReader

ScopedReader - RAII acquisition of a reader lock.

Definition RWMutex.h:170

SmartScopedWriter< false > ScopedWriter

Definition RWMutex.h:199

SmartScopedReader< false > ScopedReader

Definition RWMutex.h:182

This is an optimization pass for GlobalISel generic memory operations.

constexpr bool llvm_is_multithreaded()

Returns true if LLVM is compiled with support for multi-threading, and false otherwise.