| OLD | NEW |
| 1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2007, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 # include <pthread.h> | 131 # include <pthread.h> |
| 132 typedef pthread_rwlock_t MutexType; | 132 typedef pthread_rwlock_t MutexType; |
| 133 #elif defined(HAVE_PTHREAD) | 133 #elif defined(HAVE_PTHREAD) |
| 134 # include <pthread.h> | 134 # include <pthread.h> |
| 135 typedef pthread_mutex_t MutexType; | 135 typedef pthread_mutex_t MutexType; |
| 136 #else | 136 #else |
| 137 # error Need to implement mutex.h for your architecture, or #define NO_THREADS | 137 # error Need to implement mutex.h for your architecture, or #define NO_THREADS |
| 138 #endif | 138 #endif |
| 139 | 139 |
| 140 #include <assert.h> | 140 #include <assert.h> |
| 141 #include <stdlib.h> // for abort() | 141 #include "base/abort.h" |
| 142 | 142 |
| 143 #define MUTEX_NAMESPACE perftools_mutex_namespace | 143 #define MUTEX_NAMESPACE perftools_mutex_namespace |
| 144 | 144 |
| 145 namespace MUTEX_NAMESPACE { | 145 namespace MUTEX_NAMESPACE { |
| 146 | 146 |
| 147 class Mutex { | 147 class Mutex { |
| 148 public: | 148 public: |
| 149 // This is used for the single-arg constructor | 149 // This is used for the single-arg constructor |
| 150 enum LinkerInitialized { LINKER_INITIALIZED }; | 150 enum LinkerInitialized { LINKER_INITIALIZED }; |
| 151 | 151 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); } | 227 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); } |
| 228 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); } | 228 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); } |
| 229 bool Mutex::TryLock() { return is_safe_ ? | 229 bool Mutex::TryLock() { return is_safe_ ? |
| 230 TryEnterCriticalSection(&mutex_) != 0 : true; } | 230 TryEnterCriticalSection(&mutex_) != 0 : true; } |
| 231 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks | 231 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks |
| 232 void Mutex::ReaderUnlock() { Unlock(); } | 232 void Mutex::ReaderUnlock() { Unlock(); } |
| 233 | 233 |
| 234 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK) | 234 #elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK) |
| 235 | 235 |
| 236 #define SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \ | 236 #define SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \ |
| 237 if (is_safe_ && fncall(&mutex_) != 0) abort(); \ | 237 if (is_safe_ && fncall(&mutex_) != 0) tcmalloc::Abort(); \ |
| 238 } while (0) | 238 } while (0) |
| 239 | 239 |
| 240 Mutex::Mutex() : destroy_(true) { | 240 Mutex::Mutex() : destroy_(true) { |
| 241 SetIsSafe(); | 241 SetIsSafe(); |
| 242 if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) abort(); | 242 if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) tcmalloc::Abort(); |
| 243 } | 243 } |
| 244 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { | 244 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { |
| 245 SetIsSafe(); | 245 SetIsSafe(); |
| 246 if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) abort(); | 246 if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) tcmalloc::Abort(); |
| 247 } | 247 } |
| 248 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_rwlock_destroy); } | 248 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_rwlock_destroy); } |
| 249 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); } | 249 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); } |
| 250 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } | 250 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } |
| 251 bool Mutex::TryLock() { return is_safe_ ? | 251 bool Mutex::TryLock() { return is_safe_ ? |
| 252 pthread_rwlock_trywrlock(&mutex_) == 0 : true; } | 252 pthread_rwlock_trywrlock(&mutex_) == 0 : true; } |
| 253 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock); } | 253 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock); } |
| 254 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } | 254 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } |
| 255 #undef SAFE_PTHREAD | 255 #undef SAFE_PTHREAD |
| 256 | 256 |
| 257 #elif defined(HAVE_PTHREAD) | 257 #elif defined(HAVE_PTHREAD) |
| 258 | 258 |
| 259 #define SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \ | 259 #define SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \ |
| 260 if (is_safe_ && fncall(&mutex_) != 0) abort(); \ | 260 if (is_safe_ && fncall(&mutex_) != 0) tcmalloc::Abort(); \ |
| 261 } while (0) | 261 } while (0) |
| 262 | 262 |
| 263 Mutex::Mutex() : destroy_(true) { | 263 Mutex::Mutex() : destroy_(true) { |
| 264 SetIsSafe(); | 264 SetIsSafe(); |
| 265 if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) abort(); | 265 if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) tcmalloc::Abort(); |
| 266 } | 266 } |
| 267 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { | 267 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { |
| 268 SetIsSafe(); | 268 SetIsSafe(); |
| 269 if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) abort(); | 269 if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) tcmalloc::Abort(); |
| 270 } | 270 } |
| 271 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_mutex_destroy); } | 271 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_mutex_destroy); } |
| 272 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); } | 272 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); } |
| 273 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock); } | 273 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock); } |
| 274 bool Mutex::TryLock() { return is_safe_ ? | 274 bool Mutex::TryLock() { return is_safe_ ? |
| 275 pthread_mutex_trylock(&mutex_) == 0 : true; } | 275 pthread_mutex_trylock(&mutex_) == 0 : true; } |
| 276 void Mutex::ReaderLock() { Lock(); } | 276 void Mutex::ReaderLock() { Lock(); } |
| 277 void Mutex::ReaderUnlock() { Unlock(); } | 277 void Mutex::ReaderUnlock() { Unlock(); } |
| 278 #undef SAFE_PTHREAD | 278 #undef SAFE_PTHREAD |
| 279 | 279 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 #define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name) | 322 #define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name) |
| 323 #define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name) | 323 #define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name) |
| 324 | 324 |
| 325 } // namespace MUTEX_NAMESPACE | 325 } // namespace MUTEX_NAMESPACE |
| 326 | 326 |
| 327 using namespace MUTEX_NAMESPACE; | 327 using namespace MUTEX_NAMESPACE; |
| 328 | 328 |
| 329 #undef MUTEX_NAMESPACE | 329 #undef MUTEX_NAMESPACE |
| 330 | 330 |
| 331 #endif /* #define GOOGLE_SIMPLE_MUTEX_H_ */ | 331 #endif /* #define GOOGLE_SIMPLE_MUTEX_H_ */ |
| OLD | NEW |