bloaty/third_party/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 #if !defined(_WIN32)
14 #ifndef _POSIX_C_SOURCE
15 #define _POSIX_C_SOURCE 200809L
16 #endif
17 #include <unistd.h>
18 #if defined(_POSIX_READER_WRITER_LOCKS) && _POSIX_READER_WRITER_LOCKS > 0
19 #define MUTEX_IS_PTHREAD_RWLOCK
20 #endif
21 #endif
22 
23 #if defined(MUTEX_IS_PTHREAD_RWLOCK)
24 #include <pthread.h>
25 #include <stdlib.h>
26 typedef pthread_rwlock_t MutexType;
27 #else
28 #include <mutex>
30 #endif
31 
32 namespace re2 {
33 
34 class Mutex {
35  public:
36  inline Mutex();
37  inline ~Mutex();
38  inline void Lock(); // Block if needed until free then acquire exclusively
39  inline void Unlock(); // Release a lock acquired via Lock()
40  // Note that on systems that don't support read-write locks, these may
41  // be implemented as synonyms to Lock() and Unlock(). So you can use
42  // these for efficiency, but don't use them anyplace where being able
43  // to do shared reads is necessary to avoid deadlock.
44  inline void ReaderLock(); // Block until free or shared then acquire a share
45  inline void ReaderUnlock(); // Release a read share of this Mutex
46  inline void WriterLock() { Lock(); } // Acquire an exclusive lock
47  inline void WriterUnlock() { Unlock(); } // Release a lock from WriterLock()
48 
49  private:
51 
52  // Catch the error of writing Mutex when intending MutexLock.
53  Mutex(Mutex *ignored);
54 
55  Mutex(const Mutex&) = delete;
56  Mutex& operator=(const Mutex&) = delete;
57 };
58 
59 #if defined(MUTEX_IS_PTHREAD_RWLOCK)
60 
61 #define SAFE_PTHREAD(fncall) \
62  do { \
63  if ((fncall) != 0) abort(); \
64  } while (0)
65 
66 Mutex::Mutex() { SAFE_PTHREAD(pthread_rwlock_init(&mutex_, NULL)); }
67 Mutex::~Mutex() { SAFE_PTHREAD(pthread_rwlock_destroy(&mutex_)); }
68 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock(&mutex_)); }
69 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
70 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock(&mutex_)); }
71 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
72 
73 #undef SAFE_PTHREAD
74 
75 #else
76 
79 void Mutex::Lock() { mutex_.lock(); }
80 void Mutex::Unlock() { mutex_.unlock(); }
81 void Mutex::ReaderLock() { Lock(); } // C++11 doesn't have std::shared_mutex.
83 
84 #endif
85 
86 // --------------------------------------------------------------------------
87 // Some helper classes
88 
89 // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
90 class MutexLock {
91  public:
92  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
93  ~MutexLock() { mu_->Unlock(); }
94  private:
95  Mutex * const mu_;
96 
97  MutexLock(const MutexLock&) = delete;
98  MutexLock& operator=(const MutexLock&) = delete;
99 };
100 
101 // ReaderMutexLock and WriterMutexLock do the same, for rwlocks
103  public:
104  explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
106  private:
107  Mutex * const mu_;
108 
109  ReaderMutexLock(const ReaderMutexLock&) = delete;
110  ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
111 };
112 
114  public:
115  explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
117  private:
118  Mutex * const mu_;
119 
120  WriterMutexLock(const WriterMutexLock&) = delete;
121  WriterMutexLock& operator=(const WriterMutexLock&) = delete;
122 };
123 
124 // Catch bug where variable name is omitted, e.g. MutexLock (&mu);
125 #define MutexLock(x) static_assert(false, "MutexLock declaration missing variable name")
126 #define ReaderMutexLock(x) static_assert(false, "ReaderMutexLock declaration missing variable name")
127 #define WriterMutexLock(x) static_assert(false, "WriterMutexLock declaration missing variable name")
128 
129 } // namespace re2
130 
131 #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: bloaty/third_party/re2/util/mutex.h:93
re2::WriterMutexLock
Definition: bloaty/third_party/re2/util/mutex.h:113
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: bloaty/third_party/re2/util/mutex.h:104
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
re2::Mutex::Lock
void Lock()
Definition: bloaty/third_party/re2/util/mutex.h:79
re2::Mutex::WriterUnlock
void WriterUnlock()
Definition: bloaty/third_party/re2/util/mutex.h:47
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: bloaty/third_party/re2/util/mutex.h:105
re2::ReaderMutexLock::mu_
Mutex *const mu_
Definition: bloaty/third_party/re2/util/mutex.h:107
re2::WriterMutexLock::WriterMutexLock
WriterMutexLock(Mutex *mu)
Definition: bloaty/third_party/re2/util/mutex.h:115
re2::MutexLock::MutexLock
MutexLock(Mutex *mu)
Definition: bloaty/third_party/re2/util/mutex.h:92
re2::Mutex::operator=
Mutex & operator=(const Mutex &)=delete
re2::WriterMutexLock::mu_
Mutex *const mu_
Definition: bloaty/third_party/re2/util/mutex.h:118
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: bloaty/third_party/re2/util/mutex.h:46
re2::ReaderMutexLock::operator=
ReaderMutexLock & operator=(const ReaderMutexLock &)=delete
re2::Mutex::ReaderLock
void ReaderLock()
Definition: bloaty/third_party/re2/util/mutex.h:81
re2::Mutex::Unlock
void Unlock()
Definition: bloaty/third_party/re2/util/mutex.h:80
re2::WriterMutexLock::~WriterMutexLock
~WriterMutexLock()
Definition: bloaty/third_party/re2/util/mutex.h:116


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