re2/util/mutex.h
Go to the documentation of this file.
1 // Copyright 2007 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 #ifndef UTIL_MUTEX_H_
6 #define UTIL_MUTEX_H_
7 
8 /*
9  * A simple mutex wrapper, supporting locks and read-write locks.
10  * You should assume the locks are *not* re-entrant.
11  */
12 
13 #ifdef _WIN32
14 // Requires Windows Vista or Windows Server 2008 at minimum.
15 #include <windows.h>
16 #if defined(WINVER) && WINVER >= 0x0600
17 #define MUTEX_IS_WIN32_SRWLOCK
18 #endif
19 #else
20 #ifndef _POSIX_C_SOURCE
21 #define _POSIX_C_SOURCE 200809L
22 #endif
23 #include <unistd.h>
24 #if defined(_POSIX_READER_WRITER_LOCKS) && _POSIX_READER_WRITER_LOCKS > 0
25 #define MUTEX_IS_PTHREAD_RWLOCK
26 #endif
27 #endif
28 
29 #if defined(MUTEX_IS_WIN32_SRWLOCK)
30 typedef SRWLOCK MutexType;
31 #elif defined(MUTEX_IS_PTHREAD_RWLOCK)
32 #include <pthread.h>
33 #include <stdlib.h>
34 typedef pthread_rwlock_t MutexType;
35 #else
36 #include <mutex>
38 #endif
39 
40 namespace re2 {
41 
42 class Mutex {
43  public:
44  inline Mutex();
45  inline ~Mutex();
46  inline void Lock(); // Block if needed until free then acquire exclusively
47  inline void Unlock(); // Release a lock acquired via Lock()
48  // Note that on systems that don't support read-write locks, these may
49  // be implemented as synonyms to Lock() and Unlock(). So you can use
50  // these for efficiency, but don't use them anyplace where being able
51  // to do shared reads is necessary to avoid deadlock.
52  inline void ReaderLock(); // Block until free or shared then acquire a share
53  inline void ReaderUnlock(); // Release a read share of this Mutex
54  inline void WriterLock() { Lock(); } // Acquire an exclusive lock
55  inline void WriterUnlock() { Unlock(); } // Release a lock from WriterLock()
56 
57  private:
59 
60  // Catch the error of writing Mutex when intending MutexLock.
61  Mutex(Mutex *ignored);
62 
63  Mutex(const Mutex&) = delete;
64  Mutex& operator=(const Mutex&) = delete;
65 };
66 
67 #if defined(MUTEX_IS_WIN32_SRWLOCK)
68 
69 Mutex::Mutex() : mutex_(SRWLOCK_INIT) { }
70 Mutex::~Mutex() { }
71 void Mutex::Lock() { AcquireSRWLockExclusive(&mutex_); }
72 void Mutex::Unlock() { ReleaseSRWLockExclusive(&mutex_); }
73 void Mutex::ReaderLock() { AcquireSRWLockShared(&mutex_); }
74 void Mutex::ReaderUnlock() { ReleaseSRWLockShared(&mutex_); }
75 
76 #elif defined(MUTEX_IS_PTHREAD_RWLOCK)
77 
78 #define SAFE_PTHREAD(fncall) \
79  do { \
80  if ((fncall) != 0) abort(); \
81  } while (0)
82 
83 Mutex::Mutex() { SAFE_PTHREAD(pthread_rwlock_init(&mutex_, NULL)); }
84 Mutex::~Mutex() { SAFE_PTHREAD(pthread_rwlock_destroy(&mutex_)); }
85 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock(&mutex_)); }
86 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
87 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock(&mutex_)); }
88 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
89 
90 #undef SAFE_PTHREAD
91 
92 #else
93 
94 Mutex::Mutex() { }
95 Mutex::~Mutex() { }
96 void Mutex::Lock() { mutex_.lock(); }
97 void Mutex::Unlock() { mutex_.unlock(); }
98 void Mutex::ReaderLock() { Lock(); } // C++11 doesn't have std::shared_mutex.
99 void Mutex::ReaderUnlock() { Unlock(); }
100 
101 #endif
102 
103 // --------------------------------------------------------------------------
104 // Some helper classes
105 
106 // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
107 class MutexLock {
108  public:
109  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
111  private:
112  Mutex * const mu_;
113 
114  MutexLock(const MutexLock&) = delete;
115  MutexLock& operator=(const MutexLock&) = delete;
116 };
117 
118 // ReaderMutexLock and WriterMutexLock do the same, for rwlocks
119 class ReaderMutexLock {
120  public:
121  explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
123  private:
124  Mutex * const mu_;
125 
126  ReaderMutexLock(const ReaderMutexLock&) = delete;
127  ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
128 };
129 
130 class WriterMutexLock {
131  public:
132  explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
134  private:
135  Mutex * const mu_;
136 
137  WriterMutexLock(const WriterMutexLock&) = delete;
138  WriterMutexLock& operator=(const WriterMutexLock&) = delete;
139 };
140 
141 // Catch bug where variable name is omitted, e.g. MutexLock (&mu);
142 #define MutexLock(x) static_assert(false, "MutexLock declaration missing variable name")
143 #define ReaderMutexLock(x) static_assert(false, "ReaderMutexLock declaration missing variable name")
144 #define WriterMutexLock(x) static_assert(false, "WriterMutexLock declaration missing variable name")
145 
146 } // namespace re2
147 
148 #endif // UTIL_MUTEX_H_
re2::Mutex
Definition: bloaty/third_party/re2/util/mutex.h:34
re2::MutexLock
Definition: bloaty/third_party/re2/util/mutex.h:90
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
re2::Mutex::Mutex
Mutex()
Definition: bloaty/third_party/re2/util/mutex.h:77
re2::MutexLock::~MutexLock
~MutexLock()
Definition: re2/util/mutex.h:110
re2::WriterMutexLock
Definition: bloaty/third_party/re2/util/mutex.h:113
ReaderMutexLock
#define ReaderMutexLock(x)
Definition: re2/util/mutex.h:143
re2::WriterMutexLock::operator=
WriterMutexLock & operator=(const WriterMutexLock &)=delete
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::ReaderMutexLock::ReaderMutexLock
ReaderMutexLock(Mutex *mu)
Definition: re2/util/mutex.h:121
re2::MutexLock::mu_
Mutex *const mu_
Definition: bloaty/third_party/re2/util/mutex.h:95
re2::Mutex::~Mutex
~Mutex()
Definition: bloaty/third_party/re2/util/mutex.h:78
MutexLock
#define MutexLock(x)
Definition: re2/util/mutex.h:142
re2::Mutex::Lock
void Lock()
Definition: bloaty/third_party/re2/util/mutex.h:79
re2::Mutex::WriterUnlock
void WriterUnlock()
Definition: re2/util/mutex.h:55
WriterMutexLock
#define WriterMutexLock(x)
Definition: re2/util/mutex.h:144
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
re2::Mutex::mutex_
MutexType mutex_
Definition: bloaty/third_party/re2/util/mutex.h:50
re2::ReaderMutexLock
Definition: bloaty/third_party/re2/util/mutex.h:102
re2::ReaderMutexLock::~ReaderMutexLock
~ReaderMutexLock()
Definition: re2/util/mutex.h:122
re2::ReaderMutexLock::mu_
Mutex *const mu_
Definition: bloaty/third_party/re2/util/mutex.h:107
re2::WriterMutexLock::WriterMutexLock
WriterMutexLock(Mutex *mu)
Definition: re2/util/mutex.h:132
re2::MutexLock::MutexLock
MutexLock(Mutex *mu)
Definition: re2/util/mutex.h:109
re2::Mutex::operator=
Mutex & operator=(const Mutex &)=delete
re2::WriterMutexLock::mu_
Mutex *const mu_
Definition: bloaty/third_party/re2/util/mutex.h:118
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
re2::MutexLock::operator=
MutexLock & operator=(const MutexLock &)=delete
re2::Mutex::ReaderUnlock
void ReaderUnlock()
Definition: bloaty/third_party/re2/util/mutex.h:82
MutexType
std::mutex MutexType
Definition: bloaty/third_party/re2/util/mutex.h:29
re2::Mutex::WriterLock
void WriterLock()
Definition: re2/util/mutex.h:54
re2::ReaderMutexLock::operator=
ReaderMutexLock & operator=(const ReaderMutexLock &)=delete
SRWLOCK
RTL_SRWLOCK SRWLOCK
Definition: win.h:174
re2::Mutex::ReaderLock
void ReaderLock()
Definition: bloaty/third_party/re2/util/mutex.h:81
mutex_
internal::WrappedMutex mutex_
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.cc:569
re2::Mutex::Unlock
void Unlock()
Definition: bloaty/third_party/re2/util/mutex.h:80
MutexType
std::mutex MutexType
Definition: re2/util/mutex.h:37
re2::WriterMutexLock::~WriterMutexLock
~WriterMutexLock()
Definition: re2/util/mutex.h:133


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:31