Lines Matching refs:Mutex
33 // A simple mutex wrapper, supporting locks and read-write locks.
67 // function that tries to acquire this mutex -- but that all happens
68 // before this mutex's constructor has run. (This can happen even if
69 // the mutex and the function that uses the mutex are in the same .cc
70 // file.) Basically, because Mutex does non-trivial work in its
74 // The solution used here is to pair the actual mutex primitive with a
75 // bool that is set to true when the mutex is dynamically initialized.
76 // (Before that it's false.) Then we modify all mutex routines to
78 // it to true (which happens after the Mutex constructor has run.)
82 // the mutex operations are a no-op, since we don't need locking then
90 // avoid trying to acquire a mutex in a global constructor, if you
132 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
135 class Mutex {
137 // Create a Mutex that is not held by anybody. This constructor is
139 // See below for a recommendation for constructing global Mutex
141 inline Mutex();
144 inline ~Mutex();
156 inline void ReaderUnlock(); // Release a read share of this Mutex
169 // Catch the error of writing Mutex when intending MutexLock.
170 Mutex(Mutex* /*ignored*/) {}
172 Mutex(const Mutex&);
173 void operator=(const Mutex&);
176 // Now the implementation of Mutex for various systems
190 Mutex::Mutex() : mutex_(0) { }
191 Mutex::~Mutex() { assert(mutex_ == 0); }
192 void Mutex::Lock() { assert(--mutex_ == -1); }
193 void Mutex::Unlock() { assert(mutex_++ == -1); }
195 bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
197 void Mutex::ReaderLock() { assert(++mutex_ > 0); }
198 void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
202 Mutex::Mutex() { InitializeCriticalSection(&mutex_); SetIsSafe(); }
203 Mutex::~Mutex() { DeleteCriticalSection(&mutex_); }
204 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); }
205 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); }
207 bool Mutex::TryLock() { return is_safe_ ?
210 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
211 void Mutex::ReaderUnlock() { Unlock(); }
220 Mutex::Mutex() {
224 Mutex::~Mutex() { SAFE_PTHREAD(pthread_rwlock_destroy); }
225 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); }
226 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
228 bool Mutex::TryLock() { return is_safe_ ?
232 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock); }
233 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); }
243 Mutex::Mutex() {
247 Mutex::~Mutex() { SAFE_PTHREAD(pthread_mutex_destroy); }
248 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); }
249 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock); }
251 bool Mutex::TryLock() { return is_safe_ ?
254 void Mutex::ReaderLock() { Lock(); }
255 void Mutex::ReaderUnlock() { Unlock(); }
266 explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
269 Mutex * const mu_;
278 explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
281 Mutex * const mu_;
289 explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
292 Mutex * const mu_;