Lines Matching refs:Mutex
31 // A simple mutex wrapper, supporting locks and read-write locks.
39 // problems when we have multiple versions of Mutex in each shared object.
63 // function that tries to acquire this mutex -- but that all happens
64 // before this mutex's constructor has run. (This can happen even if
65 // the mutex and the function that uses the mutex are in the same .cc
66 // file.) Basically, because Mutex does non-trivial work in its
70 // The solution used here is to pair the actual mutex primitive with a
71 // bool that is set to true when the mutex is dynamically initialized.
72 // (Before that it's false.) Then we modify all mutex routines to
74 // it to true (which happens after the Mutex constructor has run.)
78 // the mutex operations are a no-op, since we don't need locking then
86 // avoid trying to acquire a mutex in a global constructor, if you
136 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
147 class Mutex {
149 // Create a Mutex that is not held by anybody. This constructor is
151 // See below for a recommendation for constructing global Mutex
153 inline Mutex();
156 inline ~Mutex();
168 inline void ReaderUnlock(); // Release a read share of this Mutex
184 // Catch the error of writing Mutex when intending MutexLock.
185 Mutex(Mutex* /*ignored*/) {}
187 Mutex(const Mutex&);
188 void operator=(const Mutex&);
191 // Now the implementation of Mutex for various systems
204 Mutex::Mutex() : mutex_(0) { }
205 Mutex::~Mutex() { assert(mutex_ == 0); }
206 void Mutex::Lock() { assert(--mutex_ == -1); }
207 void Mutex::Unlock() { assert(mutex_++ == -1); }
209 bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
211 void Mutex::ReaderLock() { assert(++mutex_ > 0); }
212 void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
216 Mutex::Mutex() { InitializeCriticalSection(&mutex_); SetIsSafe(); }
217 Mutex::~Mutex() { DeleteCriticalSection(&mutex_); }
218 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); }
219 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); }
221 bool Mutex::TryLock() { return is_safe_ ?
224 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
225 void Mutex::ReaderUnlock() { Unlock(); }
233 Mutex::Mutex() {
237 Mutex::~Mutex() { CERES_SAFE_PTHREAD(pthread_rwlock_destroy); }
238 void Mutex::Lock() { CERES_SAFE_PTHREAD(pthread_rwlock_wrlock); }
239 void Mutex::Unlock() { CERES_SAFE_PTHREAD(pthread_rwlock_unlock); }
241 bool Mutex::TryLock() { return is_safe_ ?
245 void Mutex::ReaderLock() { CERES_SAFE_PTHREAD(pthread_rwlock_rdlock); }
246 void Mutex::ReaderUnlock() { CERES_SAFE_PTHREAD(pthread_rwlock_unlock); }
255 Mutex::Mutex() {
259 Mutex::~Mutex() { CERES_SAFE_PTHREAD(pthread_mutex_destroy); }
260 void Mutex::Lock() { CERES_SAFE_PTHREAD(pthread_mutex_lock); }
261 void Mutex::Unlock() { CERES_SAFE_PTHREAD(pthread_mutex_unlock); }
263 bool Mutex::TryLock() { return is_safe_ ?
266 void Mutex::ReaderLock() { Lock(); }
267 void Mutex::ReaderUnlock() { Unlock(); }
276 // similar mutex.h files included in the same translation unit. This is a
286 explicit CeresMutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
289 Mutex * const mu_;
298 explicit CeresReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
301 Mutex * const mu_;
309 explicit CeresWriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
312 Mutex * const mu_;