102 #ifndef GOOGLE_MUTEX_H_
103 #define GOOGLE_MUTEX_H_
107 #if defined(NO_THREADS)
108 typedef int MutexType;
109 #elif defined(_WIN32) || defined(__CYGWIN__)
110 # ifndef WIN32_LEAN_AND_MEAN
111 # define WIN32_LEAN_AND_MEAN // We only need minimal includes
113 # ifdef GMUTEX_TRYLOCK
117 # ifndef _WIN32_WINNT
118 # define _WIN32_WINNT 0x0400
129 # include <windows.h>
130 typedef CRITICAL_SECTION MutexType;
131 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
137 # ifndef _XOPEN_SOURCE // Some other header might have already set it for us.
138 # define _XOPEN_SOURCE 500 // may be needed to get the rwlock calls
141 # include <pthread.h>
142 typedef pthread_rwlock_t MutexType;
143 #elif defined(HAVE_PTHREAD)
144 # include <pthread.h>
145 typedef pthread_mutex_t MutexType;
147 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
155 #define MUTEX_NAMESPACE glog_internal_namespace_
172 #ifdef GMUTEX_TRYLOCK
173 inline bool TryLock();
204 #if defined(NO_THREADS)
220 #ifdef GMUTEX_TRYLOCK
221 bool Mutex::TryLock() {
if (
mutex_)
return false;
Lock();
return true; }
226 #elif defined(_WIN32) || defined(__CYGWIN__)
232 #ifdef GMUTEX_TRYLOCK
233 bool Mutex::TryLock() {
return is_safe_ ?
234 TryEnterCriticalSection(&
mutex_) != 0 :
true; }
239 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
241 #define SAFE_PTHREAD(fncall) do { \
242 if (is_safe_ && fncall(&mutex_) != 0) abort(); \
250 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); }
251 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
252 #ifdef GMUTEX_TRYLOCK
253 bool Mutex::TryLock() {
return is_safe_ ?
254 pthread_rwlock_trywrlock(&
mutex_) == 0 :
261 #elif defined(HAVE_PTHREAD)
263 #define SAFE_PTHREAD(fncall) do { \
264 if (is_safe_ && fncall(&mutex_) != 0) abort(); \
272 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); }
274 #ifdef GMUTEX_TRYLOCK
275 bool Mutex::TryLock() {
return is_safe_ ?
276 pthread_mutex_trylock(&
mutex_) == 0 :
true; }
323 #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
324 #define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name)
325 #define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name)
331 #undef MUTEX_NAMESPACE