xref: /aosp_15_r20/external/gflags/src/mutex.h (revision 08ab5237c114d5c0eac1090c56f941d3f639d7d3)
1*08ab5237SOystein Eftevaag // Copyright (c) 2007, Google Inc.
2*08ab5237SOystein Eftevaag // All rights reserved.
3*08ab5237SOystein Eftevaag //
4*08ab5237SOystein Eftevaag // Redistribution and use in source and binary forms, with or without
5*08ab5237SOystein Eftevaag // modification, are permitted provided that the following conditions are
6*08ab5237SOystein Eftevaag // met:
7*08ab5237SOystein Eftevaag //
8*08ab5237SOystein Eftevaag //     * Redistributions of source code must retain the above copyright
9*08ab5237SOystein Eftevaag // notice, this list of conditions and the following disclaimer.
10*08ab5237SOystein Eftevaag //     * Redistributions in binary form must reproduce the above
11*08ab5237SOystein Eftevaag // copyright notice, this list of conditions and the following disclaimer
12*08ab5237SOystein Eftevaag // in the documentation and/or other materials provided with the
13*08ab5237SOystein Eftevaag // distribution.
14*08ab5237SOystein Eftevaag //     * Neither the name of Google Inc. nor the names of its
15*08ab5237SOystein Eftevaag // contributors may be used to endorse or promote products derived from
16*08ab5237SOystein Eftevaag // this software without specific prior written permission.
17*08ab5237SOystein Eftevaag //
18*08ab5237SOystein Eftevaag // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*08ab5237SOystein Eftevaag // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*08ab5237SOystein Eftevaag // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*08ab5237SOystein Eftevaag // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*08ab5237SOystein Eftevaag // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*08ab5237SOystein Eftevaag // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*08ab5237SOystein Eftevaag // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*08ab5237SOystein Eftevaag // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*08ab5237SOystein Eftevaag // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*08ab5237SOystein Eftevaag // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*08ab5237SOystein Eftevaag // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*08ab5237SOystein Eftevaag //
30*08ab5237SOystein Eftevaag // ---
31*08ab5237SOystein Eftevaag //
32*08ab5237SOystein Eftevaag // A simple mutex wrapper, supporting locks and read-write locks.
33*08ab5237SOystein Eftevaag // You should assume the locks are *not* re-entrant.
34*08ab5237SOystein Eftevaag //
35*08ab5237SOystein Eftevaag // This class is meant to be internal-only and should be wrapped by an
36*08ab5237SOystein Eftevaag // internal namespace.  Before you use this module, please give the
37*08ab5237SOystein Eftevaag // name of your internal namespace for this module.  Or, if you want
38*08ab5237SOystein Eftevaag // to expose it, you'll want to move it to the Google namespace.  We
39*08ab5237SOystein Eftevaag // cannot put this class in global namespace because there can be some
40*08ab5237SOystein Eftevaag // problems when we have multiple versions of Mutex in each shared object.
41*08ab5237SOystein Eftevaag //
42*08ab5237SOystein Eftevaag // NOTE: by default, we have #ifdef'ed out the TryLock() method.
43*08ab5237SOystein Eftevaag //       This is for two reasons:
44*08ab5237SOystein Eftevaag // 1) TryLock() under Windows is a bit annoying (it requires a
45*08ab5237SOystein Eftevaag //    #define to be defined very early).
46*08ab5237SOystein Eftevaag // 2) TryLock() is broken for NO_THREADS mode, at least in NDEBUG
47*08ab5237SOystein Eftevaag //    mode.
48*08ab5237SOystein Eftevaag // If you need TryLock(), and either these two caveats are not a
49*08ab5237SOystein Eftevaag // problem for you, or you're willing to work around them, then
50*08ab5237SOystein Eftevaag // feel free to #define GMUTEX_TRYLOCK, or to remove the #ifdefs
51*08ab5237SOystein Eftevaag // in the code below.
52*08ab5237SOystein Eftevaag //
53*08ab5237SOystein Eftevaag // CYGWIN NOTE: Cygwin support for rwlock seems to be buggy:
54*08ab5237SOystein Eftevaag //    http://www.cygwin.com/ml/cygwin/2008-12/msg00017.html
55*08ab5237SOystein Eftevaag // Because of that, we might as well use windows locks for
56*08ab5237SOystein Eftevaag // cygwin.  They seem to be more reliable than the cygwin pthreads layer.
57*08ab5237SOystein Eftevaag //
58*08ab5237SOystein Eftevaag // TRICKY IMPLEMENTATION NOTE:
59*08ab5237SOystein Eftevaag // This class is designed to be safe to use during
60*08ab5237SOystein Eftevaag // dynamic-initialization -- that is, by global constructors that are
61*08ab5237SOystein Eftevaag // run before main() starts.  The issue in this case is that
62*08ab5237SOystein Eftevaag // dynamic-initialization happens in an unpredictable order, and it
63*08ab5237SOystein Eftevaag // could be that someone else's dynamic initializer could call a
64*08ab5237SOystein Eftevaag // function that tries to acquire this mutex -- but that all happens
65*08ab5237SOystein Eftevaag // before this mutex's constructor has run.  (This can happen even if
66*08ab5237SOystein Eftevaag // the mutex and the function that uses the mutex are in the same .cc
67*08ab5237SOystein Eftevaag // file.)  Basically, because Mutex does non-trivial work in its
68*08ab5237SOystein Eftevaag // constructor, it's not, in the naive implementation, safe to use
69*08ab5237SOystein Eftevaag // before dynamic initialization has run on it.
70*08ab5237SOystein Eftevaag //
71*08ab5237SOystein Eftevaag // The solution used here is to pair the actual mutex primitive with a
72*08ab5237SOystein Eftevaag // bool that is set to true when the mutex is dynamically initialized.
73*08ab5237SOystein Eftevaag // (Before that it's false.)  Then we modify all mutex routines to
74*08ab5237SOystein Eftevaag // look at the bool, and not try to lock/unlock until the bool makes
75*08ab5237SOystein Eftevaag // it to true (which happens after the Mutex constructor has run.)
76*08ab5237SOystein Eftevaag //
77*08ab5237SOystein Eftevaag // This works because before main() starts -- particularly, during
78*08ab5237SOystein Eftevaag // dynamic initialization -- there are no threads, so a) it's ok that
79*08ab5237SOystein Eftevaag // the mutex operations are a no-op, since we don't need locking then
80*08ab5237SOystein Eftevaag // anyway; and b) we can be quite confident our bool won't change
81*08ab5237SOystein Eftevaag // state between a call to Lock() and a call to Unlock() (that would
82*08ab5237SOystein Eftevaag // require a global constructor in one translation unit to call Lock()
83*08ab5237SOystein Eftevaag // and another global constructor in another translation unit to call
84*08ab5237SOystein Eftevaag // Unlock() later, which is pretty perverse).
85*08ab5237SOystein Eftevaag //
86*08ab5237SOystein Eftevaag // That said, it's tricky, and can conceivably fail; it's safest to
87*08ab5237SOystein Eftevaag // avoid trying to acquire a mutex in a global constructor, if you
88*08ab5237SOystein Eftevaag // can.  One way it can fail is that a really smart compiler might
89*08ab5237SOystein Eftevaag // initialize the bool to true at static-initialization time (too
90*08ab5237SOystein Eftevaag // early) rather than at dynamic-initialization time.  To discourage
91*08ab5237SOystein Eftevaag // that, we set is_safe_ to true in code (not the constructor
92*08ab5237SOystein Eftevaag // colon-initializer) and set it to true via a function that always
93*08ab5237SOystein Eftevaag // evaluates to true, but that the compiler can't know always
94*08ab5237SOystein Eftevaag // evaluates to true.  This should be good enough.
95*08ab5237SOystein Eftevaag //
96*08ab5237SOystein Eftevaag // A related issue is code that could try to access the mutex
97*08ab5237SOystein Eftevaag // after it's been destroyed in the global destructors (because
98*08ab5237SOystein Eftevaag // the Mutex global destructor runs before some other global
99*08ab5237SOystein Eftevaag // destructor, that tries to acquire the mutex).  The way we
100*08ab5237SOystein Eftevaag // deal with this is by taking a constructor arg that global
101*08ab5237SOystein Eftevaag // mutexes should pass in, that causes the destructor to do no
102*08ab5237SOystein Eftevaag // work.  We still depend on the compiler not doing anything
103*08ab5237SOystein Eftevaag // weird to a Mutex's memory after it is destroyed, but for a
104*08ab5237SOystein Eftevaag // static global variable, that's pretty safe.
105*08ab5237SOystein Eftevaag 
106*08ab5237SOystein Eftevaag #ifndef GFLAGS_MUTEX_H_
107*08ab5237SOystein Eftevaag #define GFLAGS_MUTEX_H_
108*08ab5237SOystein Eftevaag 
109*08ab5237SOystein Eftevaag #include "gflags/gflags_declare.h"     // to figure out pthreads support
110*08ab5237SOystein Eftevaag 
111*08ab5237SOystein Eftevaag #if defined(NO_THREADS)
112*08ab5237SOystein Eftevaag   typedef int MutexType;        // to keep a lock-count
113*08ab5237SOystein Eftevaag #elif defined(OS_WINDOWS)
114*08ab5237SOystein Eftevaag # ifndef WIN32_LEAN_AND_MEAN
115*08ab5237SOystein Eftevaag #   define WIN32_LEAN_AND_MEAN  // We only need minimal includes
116*08ab5237SOystein Eftevaag # endif
117*08ab5237SOystein Eftevaag # ifndef NOMINMAX
118*08ab5237SOystein Eftevaag #   define NOMINMAX             // Don't want windows to override min()/max()
119*08ab5237SOystein Eftevaag # endif
120*08ab5237SOystein Eftevaag # ifdef GMUTEX_TRYLOCK
121*08ab5237SOystein Eftevaag   // We need Windows NT or later for TryEnterCriticalSection().  If you
122*08ab5237SOystein Eftevaag   // don't need that functionality, you can remove these _WIN32_WINNT
123*08ab5237SOystein Eftevaag   // lines, and change TryLock() to assert(0) or something.
124*08ab5237SOystein Eftevaag #   ifndef _WIN32_WINNT
125*08ab5237SOystein Eftevaag #     define _WIN32_WINNT 0x0400
126*08ab5237SOystein Eftevaag #   endif
127*08ab5237SOystein Eftevaag # endif
128*08ab5237SOystein Eftevaag # include <windows.h>
129*08ab5237SOystein Eftevaag   typedef CRITICAL_SECTION MutexType;
130*08ab5237SOystein Eftevaag #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
131*08ab5237SOystein Eftevaag   // Needed for pthread_rwlock_*.  If it causes problems, you could take it
132*08ab5237SOystein Eftevaag   // out, but then you'd have to unset HAVE_RWLOCK (at least on linux -- it
133*08ab5237SOystein Eftevaag   // *does* cause problems for FreeBSD, or MacOSX, but isn't needed
134*08ab5237SOystein Eftevaag   // for locking there.)
135*08ab5237SOystein Eftevaag # ifdef __linux__
136*08ab5237SOystein Eftevaag #   if _XOPEN_SOURCE < 500      // including not being defined at all
137*08ab5237SOystein Eftevaag #     undef _XOPEN_SOURCE
138*08ab5237SOystein Eftevaag #     define _XOPEN_SOURCE 500  // may be needed to get the rwlock calls
139*08ab5237SOystein Eftevaag #   endif
140*08ab5237SOystein Eftevaag # endif
141*08ab5237SOystein Eftevaag # include <pthread.h>
142*08ab5237SOystein Eftevaag   typedef pthread_rwlock_t MutexType;
143*08ab5237SOystein Eftevaag #elif defined(HAVE_PTHREAD)
144*08ab5237SOystein Eftevaag # include <pthread.h>
145*08ab5237SOystein Eftevaag   typedef pthread_mutex_t MutexType;
146*08ab5237SOystein Eftevaag #else
147*08ab5237SOystein Eftevaag # error Need to implement mutex.h for your architecture, or #define NO_THREADS
148*08ab5237SOystein Eftevaag #endif
149*08ab5237SOystein Eftevaag 
150*08ab5237SOystein Eftevaag #include <assert.h>
151*08ab5237SOystein Eftevaag #include <stdlib.h>      // for abort()
152*08ab5237SOystein Eftevaag 
153*08ab5237SOystein Eftevaag #define MUTEX_NAMESPACE gflags_mutex_namespace
154*08ab5237SOystein Eftevaag 
155*08ab5237SOystein Eftevaag namespace MUTEX_NAMESPACE {
156*08ab5237SOystein Eftevaag 
157*08ab5237SOystein Eftevaag class Mutex {
158*08ab5237SOystein Eftevaag  public:
159*08ab5237SOystein Eftevaag   // This is used for the single-arg constructor
160*08ab5237SOystein Eftevaag   enum LinkerInitialized { LINKER_INITIALIZED };
161*08ab5237SOystein Eftevaag 
162*08ab5237SOystein Eftevaag   // Create a Mutex that is not held by anybody.  This constructor is
163*08ab5237SOystein Eftevaag   // typically used for Mutexes allocated on the heap or the stack.
164*08ab5237SOystein Eftevaag   inline Mutex();
165*08ab5237SOystein Eftevaag   // This constructor should be used for global, static Mutex objects.
166*08ab5237SOystein Eftevaag   // It inhibits work being done by the destructor, which makes it
167*08ab5237SOystein Eftevaag   // safer for code that tries to acqiure this mutex in their global
168*08ab5237SOystein Eftevaag   // destructor.
169*08ab5237SOystein Eftevaag   explicit inline Mutex(LinkerInitialized);
170*08ab5237SOystein Eftevaag 
171*08ab5237SOystein Eftevaag   // Destructor
172*08ab5237SOystein Eftevaag   inline ~Mutex();
173*08ab5237SOystein Eftevaag 
174*08ab5237SOystein Eftevaag   inline void Lock();    // Block if needed until free then acquire exclusively
175*08ab5237SOystein Eftevaag   inline void Unlock();  // Release a lock acquired via Lock()
176*08ab5237SOystein Eftevaag #ifdef GMUTEX_TRYLOCK
177*08ab5237SOystein Eftevaag   inline bool TryLock(); // If free, Lock() and return true, else return false
178*08ab5237SOystein Eftevaag #endif
179*08ab5237SOystein Eftevaag   // Note that on systems that don't support read-write locks, these may
180*08ab5237SOystein Eftevaag   // be implemented as synonyms to Lock() and Unlock().  So you can use
181*08ab5237SOystein Eftevaag   // these for efficiency, but don't use them anyplace where being able
182*08ab5237SOystein Eftevaag   // to do shared reads is necessary to avoid deadlock.
183*08ab5237SOystein Eftevaag   inline void ReaderLock();   // Block until free or shared then acquire a share
184*08ab5237SOystein Eftevaag   inline void ReaderUnlock(); // Release a read share of this Mutex
WriterLock()185*08ab5237SOystein Eftevaag   inline void WriterLock() { Lock(); }     // Acquire an exclusive lock
WriterUnlock()186*08ab5237SOystein Eftevaag   inline void WriterUnlock() { Unlock(); } // Release a lock from WriterLock()
187*08ab5237SOystein Eftevaag 
188*08ab5237SOystein Eftevaag  private:
189*08ab5237SOystein Eftevaag   MutexType mutex_;
190*08ab5237SOystein Eftevaag   // We want to make sure that the compiler sets is_safe_ to true only
191*08ab5237SOystein Eftevaag   // when we tell it to, and never makes assumptions is_safe_ is
192*08ab5237SOystein Eftevaag   // always true.  volatile is the most reliable way to do that.
193*08ab5237SOystein Eftevaag   volatile bool is_safe_;
194*08ab5237SOystein Eftevaag   // This indicates which constructor was called.
195*08ab5237SOystein Eftevaag   bool destroy_;
196*08ab5237SOystein Eftevaag 
SetIsSafe()197*08ab5237SOystein Eftevaag   inline void SetIsSafe() { is_safe_ = true; }
198*08ab5237SOystein Eftevaag 
199*08ab5237SOystein Eftevaag   // Catch the error of writing Mutex when intending MutexLock.
Mutex(Mutex *)200*08ab5237SOystein Eftevaag   explicit Mutex(Mutex* /*ignored*/) {}
201*08ab5237SOystein Eftevaag   // Disallow "evil" constructors
202*08ab5237SOystein Eftevaag   Mutex(const Mutex&);
203*08ab5237SOystein Eftevaag   void operator=(const Mutex&);
204*08ab5237SOystein Eftevaag };
205*08ab5237SOystein Eftevaag 
206*08ab5237SOystein Eftevaag // Now the implementation of Mutex for various systems
207*08ab5237SOystein Eftevaag #if defined(NO_THREADS)
208*08ab5237SOystein Eftevaag 
209*08ab5237SOystein Eftevaag // When we don't have threads, we can be either reading or writing,
210*08ab5237SOystein Eftevaag // but not both.  We can have lots of readers at once (in no-threads
211*08ab5237SOystein Eftevaag // mode, that's most likely to happen in recursive function calls),
212*08ab5237SOystein Eftevaag // but only one writer.  We represent this by having mutex_ be -1 when
213*08ab5237SOystein Eftevaag // writing and a number > 0 when reading (and 0 when no lock is held).
214*08ab5237SOystein Eftevaag //
215*08ab5237SOystein Eftevaag // In debug mode, we assert these invariants, while in non-debug mode
216*08ab5237SOystein Eftevaag // we do nothing, for efficiency.  That's why everything is in an
217*08ab5237SOystein Eftevaag // assert.
218*08ab5237SOystein Eftevaag 
Mutex()219*08ab5237SOystein Eftevaag Mutex::Mutex() : mutex_(0) { }
Mutex(Mutex::LinkerInitialized)220*08ab5237SOystein Eftevaag Mutex::Mutex(Mutex::LinkerInitialized) : mutex_(0) { }
~Mutex()221*08ab5237SOystein Eftevaag Mutex::~Mutex()            { assert(mutex_ == 0); }
Lock()222*08ab5237SOystein Eftevaag void Mutex::Lock()         { assert(--mutex_ == -1); }
Unlock()223*08ab5237SOystein Eftevaag void Mutex::Unlock()       { assert(mutex_++ == -1); }
224*08ab5237SOystein Eftevaag #ifdef GMUTEX_TRYLOCK
TryLock()225*08ab5237SOystein Eftevaag bool Mutex::TryLock()      { if (mutex_) return false; Lock(); return true; }
226*08ab5237SOystein Eftevaag #endif
ReaderLock()227*08ab5237SOystein Eftevaag void Mutex::ReaderLock()   { assert(++mutex_ > 0); }
ReaderUnlock()228*08ab5237SOystein Eftevaag void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
229*08ab5237SOystein Eftevaag 
230*08ab5237SOystein Eftevaag #elif defined(OS_WINDOWS)
231*08ab5237SOystein Eftevaag 
Mutex()232*08ab5237SOystein Eftevaag Mutex::Mutex() : destroy_(true) {
233*08ab5237SOystein Eftevaag   InitializeCriticalSection(&mutex_);
234*08ab5237SOystein Eftevaag   SetIsSafe();
235*08ab5237SOystein Eftevaag }
Mutex(LinkerInitialized)236*08ab5237SOystein Eftevaag Mutex::Mutex(LinkerInitialized) : destroy_(false) {
237*08ab5237SOystein Eftevaag   InitializeCriticalSection(&mutex_);
238*08ab5237SOystein Eftevaag   SetIsSafe();
239*08ab5237SOystein Eftevaag }
~Mutex()240*08ab5237SOystein Eftevaag Mutex::~Mutex()            { if (destroy_) DeleteCriticalSection(&mutex_); }
Lock()241*08ab5237SOystein Eftevaag void Mutex::Lock()         { if (is_safe_) EnterCriticalSection(&mutex_); }
Unlock()242*08ab5237SOystein Eftevaag void Mutex::Unlock()       { if (is_safe_) LeaveCriticalSection(&mutex_); }
243*08ab5237SOystein Eftevaag #ifdef GMUTEX_TRYLOCK
TryLock()244*08ab5237SOystein Eftevaag bool Mutex::TryLock()      { return is_safe_ ?
245*08ab5237SOystein Eftevaag                                  TryEnterCriticalSection(&mutex_) != 0 : true; }
246*08ab5237SOystein Eftevaag #endif
ReaderLock()247*08ab5237SOystein Eftevaag void Mutex::ReaderLock()   { Lock(); }      // we don't have read-write locks
ReaderUnlock()248*08ab5237SOystein Eftevaag void Mutex::ReaderUnlock() { Unlock(); }
249*08ab5237SOystein Eftevaag 
250*08ab5237SOystein Eftevaag #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
251*08ab5237SOystein Eftevaag 
252*08ab5237SOystein Eftevaag #define SAFE_PTHREAD(fncall)  do {   /* run fncall if is_safe_ is true */  \
253*08ab5237SOystein Eftevaag   if (is_safe_ && fncall(&mutex_) != 0) abort();                           \
254*08ab5237SOystein Eftevaag } while (0)
255*08ab5237SOystein Eftevaag 
Mutex()256*08ab5237SOystein Eftevaag Mutex::Mutex() : destroy_(true) {
257*08ab5237SOystein Eftevaag   SetIsSafe();
258*08ab5237SOystein Eftevaag   if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) abort();
259*08ab5237SOystein Eftevaag }
Mutex(Mutex::LinkerInitialized)260*08ab5237SOystein Eftevaag Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) {
261*08ab5237SOystein Eftevaag   SetIsSafe();
262*08ab5237SOystein Eftevaag   if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) abort();
263*08ab5237SOystein Eftevaag }
~Mutex()264*08ab5237SOystein Eftevaag Mutex::~Mutex()       { if (destroy_) SAFE_PTHREAD(pthread_rwlock_destroy); }
Lock()265*08ab5237SOystein Eftevaag void Mutex::Lock()         { SAFE_PTHREAD(pthread_rwlock_wrlock); }
Unlock()266*08ab5237SOystein Eftevaag void Mutex::Unlock()       { SAFE_PTHREAD(pthread_rwlock_unlock); }
267*08ab5237SOystein Eftevaag #ifdef GMUTEX_TRYLOCK
TryLock()268*08ab5237SOystein Eftevaag bool Mutex::TryLock()      { return is_safe_ ?
269*08ab5237SOystein Eftevaag                                pthread_rwlock_trywrlock(&mutex_) == 0 : true; }
270*08ab5237SOystein Eftevaag #endif
ReaderLock()271*08ab5237SOystein Eftevaag void Mutex::ReaderLock()   { SAFE_PTHREAD(pthread_rwlock_rdlock); }
ReaderUnlock()272*08ab5237SOystein Eftevaag void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
273*08ab5237SOystein Eftevaag #undef SAFE_PTHREAD
274*08ab5237SOystein Eftevaag 
275*08ab5237SOystein Eftevaag #elif defined(HAVE_PTHREAD)
276*08ab5237SOystein Eftevaag 
277*08ab5237SOystein Eftevaag #define SAFE_PTHREAD(fncall)  do {   /* run fncall if is_safe_ is true */  \
278*08ab5237SOystein Eftevaag   if (is_safe_ && fncall(&mutex_) != 0) abort();                           \
279*08ab5237SOystein Eftevaag } while (0)
280*08ab5237SOystein Eftevaag 
Mutex()281*08ab5237SOystein Eftevaag Mutex::Mutex() : destroy_(true) {
282*08ab5237SOystein Eftevaag   SetIsSafe();
283*08ab5237SOystein Eftevaag   if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) abort();
284*08ab5237SOystein Eftevaag }
Mutex(Mutex::LinkerInitialized)285*08ab5237SOystein Eftevaag Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) {
286*08ab5237SOystein Eftevaag   SetIsSafe();
287*08ab5237SOystein Eftevaag   if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) abort();
288*08ab5237SOystein Eftevaag }
~Mutex()289*08ab5237SOystein Eftevaag Mutex::~Mutex()       { if (destroy_) SAFE_PTHREAD(pthread_mutex_destroy); }
Lock()290*08ab5237SOystein Eftevaag void Mutex::Lock()         { SAFE_PTHREAD(pthread_mutex_lock); }
Unlock()291*08ab5237SOystein Eftevaag void Mutex::Unlock()       { SAFE_PTHREAD(pthread_mutex_unlock); }
292*08ab5237SOystein Eftevaag #ifdef GMUTEX_TRYLOCK
TryLock()293*08ab5237SOystein Eftevaag bool Mutex::TryLock()      { return is_safe_ ?
294*08ab5237SOystein Eftevaag                                  pthread_mutex_trylock(&mutex_) == 0 : true; }
295*08ab5237SOystein Eftevaag #endif
ReaderLock()296*08ab5237SOystein Eftevaag void Mutex::ReaderLock()   { Lock(); }
ReaderUnlock()297*08ab5237SOystein Eftevaag void Mutex::ReaderUnlock() { Unlock(); }
298*08ab5237SOystein Eftevaag #undef SAFE_PTHREAD
299*08ab5237SOystein Eftevaag 
300*08ab5237SOystein Eftevaag #endif
301*08ab5237SOystein Eftevaag 
302*08ab5237SOystein Eftevaag // --------------------------------------------------------------------------
303*08ab5237SOystein Eftevaag // Some helper classes
304*08ab5237SOystein Eftevaag 
305*08ab5237SOystein Eftevaag // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
306*08ab5237SOystein Eftevaag class MutexLock {
307*08ab5237SOystein Eftevaag  public:
MutexLock(Mutex * mu)308*08ab5237SOystein Eftevaag   explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
~MutexLock()309*08ab5237SOystein Eftevaag   ~MutexLock() { mu_->Unlock(); }
310*08ab5237SOystein Eftevaag  private:
311*08ab5237SOystein Eftevaag   Mutex * const mu_;
312*08ab5237SOystein Eftevaag   // Disallow "evil" constructors
313*08ab5237SOystein Eftevaag   MutexLock(const MutexLock&);
314*08ab5237SOystein Eftevaag   void operator=(const MutexLock&);
315*08ab5237SOystein Eftevaag };
316*08ab5237SOystein Eftevaag 
317*08ab5237SOystein Eftevaag // ReaderMutexLock and WriterMutexLock do the same, for rwlocks
318*08ab5237SOystein Eftevaag class ReaderMutexLock {
319*08ab5237SOystein Eftevaag  public:
ReaderMutexLock(Mutex * mu)320*08ab5237SOystein Eftevaag   explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
~ReaderMutexLock()321*08ab5237SOystein Eftevaag   ~ReaderMutexLock() { mu_->ReaderUnlock(); }
322*08ab5237SOystein Eftevaag  private:
323*08ab5237SOystein Eftevaag   Mutex * const mu_;
324*08ab5237SOystein Eftevaag   // Disallow "evil" constructors
325*08ab5237SOystein Eftevaag   ReaderMutexLock(const ReaderMutexLock&);
326*08ab5237SOystein Eftevaag   void operator=(const ReaderMutexLock&);
327*08ab5237SOystein Eftevaag };
328*08ab5237SOystein Eftevaag 
329*08ab5237SOystein Eftevaag class WriterMutexLock {
330*08ab5237SOystein Eftevaag  public:
WriterMutexLock(Mutex * mu)331*08ab5237SOystein Eftevaag   explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
~WriterMutexLock()332*08ab5237SOystein Eftevaag   ~WriterMutexLock() { mu_->WriterUnlock(); }
333*08ab5237SOystein Eftevaag  private:
334*08ab5237SOystein Eftevaag   Mutex * const mu_;
335*08ab5237SOystein Eftevaag   // Disallow "evil" constructors
336*08ab5237SOystein Eftevaag   WriterMutexLock(const WriterMutexLock&);
337*08ab5237SOystein Eftevaag   void operator=(const WriterMutexLock&);
338*08ab5237SOystein Eftevaag };
339*08ab5237SOystein Eftevaag 
340*08ab5237SOystein Eftevaag // Catch bug where variable name is omitted, e.g. MutexLock (&mu);
341*08ab5237SOystein Eftevaag #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
342*08ab5237SOystein Eftevaag #define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name)
343*08ab5237SOystein Eftevaag #define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name)
344*08ab5237SOystein Eftevaag 
345*08ab5237SOystein Eftevaag }  // namespace MUTEX_NAMESPACE
346*08ab5237SOystein Eftevaag 
347*08ab5237SOystein Eftevaag 
348*08ab5237SOystein Eftevaag #endif  /* #define GFLAGS_MUTEX_H__ */
349