Lines Matching refs:Mutex
33 // A simple mutex wrapper, supporting locks and read-write locks.
46 // problems when we have multiple versions of Mutex in each shared object.
70 // function that tries to acquire this mutex -- but that all happens
71 // before this mutex's constructor has run. (This can happen even if
72 // the mutex and the function that uses the mutex are in the same .cc
73 // file.) Basically, because Mutex does non-trivial work in its
77 // The solution used here is to pair the actual mutex primitive with a
78 // bool that is set to true when the mutex is dynamically initialized.
79 // (Before that it's false.) Then we modify all mutex routines to
81 // it to true (which happens after the Mutex constructor has run.)
85 // the mutex operations are a no-op, since we don't need locking then
93 // avoid trying to acquire a mutex in a global constructor, if you
102 // A related issue is code that could try to access the mutex
104 // the Mutex global destructor runs before some other global
105 // destructor, that tries to acquire the mutex). The way we
109 // weird to a Mutex's memory after it is destroyed, but for a
145 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
155 class Mutex {
160 // Create a Mutex that is not held by anybody. This constructor is
162 inline Mutex();
163 // This constructor should be used for global, static Mutex objects.
165 // safer for code that tries to acqiure this mutex in their global
167 inline Mutex(LinkerInitialized);
170 inline ~Mutex();
182 inline void ReaderUnlock(); // Release a read share of this Mutex
197 // Catch the error of writing Mutex when intending MutexLock.
198 Mutex(Mutex* /*ignored*/) {}
200 Mutex(const Mutex&);
201 void operator=(const Mutex&);
204 // Now the implementation of Mutex for various systems
217 Mutex::Mutex() : mutex_(0) { }
218 Mutex::Mutex(Mutex::LinkerInitialized) : mutex_(0) { }
219 Mutex::~Mutex() { assert(mutex_ == 0); }
220 void Mutex::Lock() { assert(--mutex_ == -1); }
221 void Mutex::Unlock() { assert(mutex_++ == -1); }
223 bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
225 void Mutex::ReaderLock() { assert(++mutex_ > 0); }
226 void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
230 Mutex::Mutex() : destroy_(true) {
234 Mutex::Mutex(LinkerInitialized) : destroy_(false) {
238 Mutex::~Mutex() { if (destroy_) DeleteCriticalSection(&mutex_); }
239 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); }
240 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); }
242 bool Mutex::TryLock() { return is_safe_ ?
245 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
246 void Mutex::ReaderUnlock() { Unlock(); }
254 Mutex::Mutex() : destroy_(true) {
258 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) {
262 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_rwlock_destroy); }
263 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); }
264 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
266 bool Mutex::TryLock() { return is_safe_ ?
269 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock); }
270 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
279 Mutex::Mutex() : destroy_(true) {
283 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) {
287 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_mutex_destroy); }
288 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); }
289 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock); }
291 bool Mutex::TryLock() { return is_safe_ ?
294 void Mutex::ReaderLock() { Lock(); }
295 void Mutex::ReaderUnlock() { Unlock(); }
306 explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
309 Mutex * const mu_;
318 explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
321 Mutex * const mu_;
329 explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
332 Mutex * const mu_;