Lines Matching full:mutex
32 // A simple mutex wrapper, supporting locks and read-write locks.
40 // problems when we have multiple versions of Mutex in each shared object.
64 // function that tries to acquire this mutex -- but that all happens
65 // before this mutex's constructor has run. (This can happen even if
66 // the mutex and the function that uses the mutex are in the same .cc
67 // file.) Basically, because Mutex does non-trivial work in its
71 // The solution used here is to pair the actual mutex primitive with a
72 // bool that is set to true when the mutex is dynamically initialized.
73 // (Before that it's false.) Then we modify all mutex routines to
75 // it to true (which happens after the Mutex constructor has run.)
79 // the mutex operations are a no-op, since we don't need locking then
87 // avoid trying to acquire a mutex in a global constructor, if you
96 // A related issue is code that could try to access the mutex
98 // the Mutex global destructor runs before some other global
99 // destructor, that tries to acquire the mutex). The way we
103 // weird to a Mutex's memory after it is destroyed, but for a
147 # error Need to implement mutex.h for your architecture, or #define NO_THREADS
157 class Mutex {
162 // Create a Mutex that is not held by anybody. This constructor is
164 inline Mutex();
165 // This constructor should be used for global, static Mutex objects.
167 // safer for code that tries to acqiure this mutex in their global
169 explicit inline Mutex(LinkerInitialized);
172 inline ~Mutex();
184 inline void ReaderUnlock(); // Release a read share of this Mutex
199 // Catch the error of writing Mutex when intending MutexLock.
200 explicit Mutex(Mutex* /*ignored*/) {} in Mutex() argument
202 Mutex(const Mutex&);
203 void operator=(const Mutex&);
206 // Now the implementation of Mutex for various systems
219 Mutex::Mutex() : mutex_(0) { } in Mutex() function
220 Mutex::Mutex(Mutex::LinkerInitialized) : mutex_(0) { } in Mutex() function
221 Mutex::~Mutex() { assert(mutex_ == 0); } in ~Mutex()
222 void Mutex::Lock() { assert(--mutex_ == -1); } in Lock()
223 void Mutex::Unlock() { assert(mutex_++ == -1); } in Unlock()
225 bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; } in TryLock()
227 void Mutex::ReaderLock() { assert(++mutex_ > 0); } in ReaderLock()
228 void Mutex::ReaderUnlock() { assert(mutex_-- > 0); } in ReaderUnlock()
232 Mutex::Mutex() : destroy_(true) { in Mutex() function
236 Mutex::Mutex(LinkerInitialized) : destroy_(false) { in Mutex() function
240 Mutex::~Mutex() { if (destroy_) DeleteCriticalSection(&mutex_); } in ~Mutex()
241 void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); } in Lock()
242 void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); } in Unlock()
244 bool Mutex::TryLock() { return is_safe_ ? in TryLock()
247 void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks in ReaderLock()
248 void Mutex::ReaderUnlock() { Unlock(); } in ReaderUnlock()
256 Mutex::Mutex() : destroy_(true) { in Mutex() function
260 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { in Mutex() function
264 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_rwlock_destroy); } in ~Mutex()
265 void Mutex::Lock() { SAFE_PTHREAD(pthread_rwlock_wrlock); } in Lock()
266 void Mutex::Unlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } in Unlock()
268 bool Mutex::TryLock() { return is_safe_ ? in TryLock()
271 void Mutex::ReaderLock() { SAFE_PTHREAD(pthread_rwlock_rdlock); } in ReaderLock()
272 void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock); } in ReaderUnlock()
281 Mutex::Mutex() : destroy_(true) { in Mutex() function
285 Mutex::Mutex(Mutex::LinkerInitialized) : destroy_(false) { in Mutex() function
289 Mutex::~Mutex() { if (destroy_) SAFE_PTHREAD(pthread_mutex_destroy); } in ~Mutex()
290 void Mutex::Lock() { SAFE_PTHREAD(pthread_mutex_lock); } in Lock()
291 void Mutex::Unlock() { SAFE_PTHREAD(pthread_mutex_unlock); } in Unlock()
293 bool Mutex::TryLock() { return is_safe_ ? in TryLock()
296 void Mutex::ReaderLock() { Lock(); } in ReaderLock()
297 void Mutex::ReaderUnlock() { Unlock(); } in ReaderUnlock()
308 explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); } in MutexLock()
311 Mutex * const mu_;
320 explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); } in ReaderMutexLock()
323 Mutex * const mu_;
331 explicit WriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); } in WriterMutexLock()
334 Mutex * const mu_;