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 /*
6 * A simple mutex wrapper, supporting locks and read-write locks.
7 * You should assume the locks are *not* re-entrant.
8 */
9
10 #ifndef RE2_UTIL_MUTEX_H_
11 #define RE2_UTIL_MUTEX_H_
12
13 #include <stdlib.h>
14
15 namespace re2 {
16
17 #define NO_THREADS 1
18 #define HAVE_PTHREAD 0
19 #define HAVE_RWLOCK 0
20
21 #if defined(NO_THREADS)
22 typedef int MutexType; // to keep a lock-count
23 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
24 // Needed for pthread_rwlock_*. If it causes problems, you could take it
25 // out, but then you'd have to unset HAVE_RWLOCK (at least on linux -- it
26 // *does* cause problems for FreeBSD, or MacOSX, but isn't needed
27 // for locking there.)
28 # ifdef __linux__
29 # undef _XOPEN_SOURCE
30 # define _XOPEN_SOURCE 500 // may be needed to get the rwlock calls
31 # endif
32 # include <pthread.h>
33 typedef pthread_rwlock_t MutexType;
34 #elif defined(HAVE_PTHREAD)
35 # include <pthread.h>
36 typedef pthread_mutex_t MutexType;
37 #elif defined(WIN32)
38 # define WIN32_LEAN_AND_MEAN // We only need minimal includes
39 # ifdef GMUTEX_TRYLOCK
40 // We need Windows NT or later for TryEnterCriticalSection(). If you
41 // don't need that functionality, you can remove these _WIN32_WINNT
42 // lines, and change TryLock() to assert(0) or something.
43 # ifndef _WIN32_WINNT
44 # define _WIN32_WINNT 0x0400
45 # endif
46 # endif
47 # include <windows.h>
48 typedef CRITICAL_SECTION MutexType;
49 #else
50 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
51 #endif
52
53 class Mutex {
54 public:
55 // Create a Mutex that is not held by anybody.
56 inline Mutex();
57
58 // Destructor
59 inline ~Mutex();
60
61 inline void Lock(); // Block if needed until free then acquire exclusively
62 inline void Unlock(); // Release a lock acquired via Lock()
63 inline bool TryLock(); // If free, Lock() and return true, else return false
64 // Note that on systems that don't support read-write locks, these may
65 // be implemented as synonyms to Lock() and Unlock(). So you can use
66 // these for efficiency, but don't use them anyplace where being able
67 // to do shared reads is necessary to avoid deadlock.
68 inline void ReaderLock(); // Block until free or shared then acquire a share
69 inline void ReaderUnlock(); // Release a read share of this Mutex
WriterLock()70 inline void WriterLock() { Lock(); } // Acquire an exclusive lock
WriterUnlock()71 inline void WriterUnlock() { Unlock(); } // Release a lock from WriterLock()
AssertHeld()72 inline void AssertHeld() { }
73
74 private:
75 MutexType mutex_;
76
77 // Catch the error of writing Mutex when intending MutexLock.
78 Mutex(Mutex *ignored);
79 // Disallow "evil" constructors
80 Mutex(const Mutex&);
81 void operator=(const Mutex&);
82 };
83
84 // Now the implementation of Mutex for various systems
85 #if defined(NO_THREADS)
86
87 // When we don't have threads, we can be either reading or writing,
88 // but not both. We can have lots of readers at once (in no-threads
89 // mode, that's most likely to happen in recursive function calls),
90 // but only one writer. We represent this by having mutex_ be -1 when
91 // writing and a number > 0 when reading (and 0 when no lock is held).
92 //
93 // In debug mode, we assert these invariants, while in non-debug mode
94 // we do nothing, for efficiency. That's why everything is in an
95 // assert.
96 #include <assert.h>
97
Mutex()98 Mutex::Mutex() : mutex_(0) { }
~Mutex()99 Mutex::~Mutex() { assert(mutex_ == 0); }
Lock()100 void Mutex::Lock() { assert(--mutex_ == -1); }
Unlock()101 void Mutex::Unlock() { assert(mutex_++ == -1); }
TryLock()102 bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
ReaderLock()103 void Mutex::ReaderLock() { assert(++mutex_ > 0); }
ReaderUnlock()104 void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
105
106 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
107
108 #define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0)
109
Mutex()110 Mutex::Mutex() { SAFE_PTHREAD(pthread_rwlock_init(&mutex_, NULL)); }
~Mutex()111 Mutex::~Mutex() { SAFE_PTHREAD(pthread_rwlock_destroy(&mutex_)); }
Lock()112 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock(&mutex_)); }
Unlock()113 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
TryLock()114 bool Mutex::TryLock() { return pthread_rwlock_trywrlock(&mutex_) == 0; }
ReaderLock()115 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock(&mutex_)); }
ReaderUnlock()116 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
117
118 #undef SAFE_PTHREAD
119
120 #elif defined(HAVE_PTHREAD)
121
122 #define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0)
123
Mutex()124 Mutex::Mutex() { SAFE_PTHREAD(pthread_mutex_init(&mutex_, NULL)); }
~Mutex()125 Mutex::~Mutex() { SAFE_PTHREAD(pthread_mutex_destroy(&mutex_)); }
Lock()126 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock(&mutex_)); }
Unlock()127 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock(&mutex_)); }
TryLock()128 bool Mutex::TryLock() { return pthread_mutex_trylock(&mutex_) == 0; }
ReaderLock()129 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
ReaderUnlock()130 void Mutex::ReaderUnlock() { Unlock(); }
131 #undef SAFE_PTHREAD
132
133 #elif defined(WIN32)
134
Mutex()135 Mutex::Mutex() { InitializeCriticalSection(&mutex_); }
~Mutex()136 Mutex::~Mutex() { DeleteCriticalSection(&mutex_); }
Lock()137 void Mutex::Lock() { EnterCriticalSection(&mutex_); }
Unlock()138 void Mutex::Unlock() { LeaveCriticalSection(&mutex_); }
TryLock()139 bool Mutex::TryLock() { return TryEnterCriticalSection(&mutex_) != 0; }
ReaderLock()140 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
ReaderUnlock()141 void Mutex::ReaderUnlock() { Unlock(); }
142
143 #endif
144
145
146 // --------------------------------------------------------------------------
147 // Some helper classes
148
149 // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
150 class MutexLock {
151 public:
MutexLock(Mutex * mu)152 explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
~MutexLock()153 ~MutexLock() { mu_->Unlock(); }
154 private:
155 Mutex * const mu_;
156 // Disallow "evil" constructors
157 MutexLock(const MutexLock&);
158 void operator=(const MutexLock&);
159 };
160
161 // ReaderMutexLock and WriterMutexLock do the same, for rwlocks
162 class ReaderMutexLock {
163 public:
ReaderMutexLock(Mutex * mu)164 explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
~ReaderMutexLock()165 ~ReaderMutexLock() { mu_->ReaderUnlock(); }
166 private:
167 Mutex * const mu_;
168 // Disallow "evil" constructors
169 ReaderMutexLock(const ReaderMutexLock&);
170 void operator=(const ReaderMutexLock&);
171 };
172
173 class WriterMutexLock {
174 public:
WriterMutexLock(Mutex * mu)175 explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
~WriterMutexLock()176 ~WriterMutexLock() { mu_->WriterUnlock(); }
177 private:
178 Mutex * const mu_;
179 // Disallow "evil" constructors
180 WriterMutexLock(const WriterMutexLock&);
181 void operator=(const WriterMutexLock&);
182 };
183
184 // Catch bug where variable name is omitted, e.g. MutexLock (&mu);
185 #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
186 #define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name)
187 #define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name)
188
189 // Provide safe way to declare and use global, linker-initialized mutex. Sigh.
190 #ifdef HAVE_PTHREAD
191
192 #define GLOBAL_MUTEX(name) \
193 static pthread_mutex_t (name) = PTHREAD_MUTEX_INITIALIZER
194 #define GLOBAL_MUTEX_LOCK(name) \
195 pthread_mutex_lock(&(name))
196 #define GLOBAL_MUTEX_UNLOCK(name) \
197 pthread_mutex_unlock(&(name))
198
199 #else
200
201 #define GLOBAL_MUTEX(name) \
202 static Mutex name
203 #define GLOBAL_MUTEX_LOCK(name) \
204 name.Lock()
205 #define GLOBAL_MUTEX_UNLOCK(name) \
206 name.Unlock()
207
208 #endif
209
210 } // namespace re2
211
212 #endif /* #define RE2_UTIL_MUTEX_H_ */
213