1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_UTILITY_MUTEX_H_
6 #define MOJO_PUBLIC_UTILITY_MUTEX_H_
9 #error "Not implemented: See crbug.com/342893."
14 #include "mojo/public/system/macros.h"
19 // Note: Make a C++ constant for |PTHREAD_MUTEX_INITIALIZER|. (We can't directly
20 // use the C macro in an initializer list, since it might expand to |{ ... }|.)
22 const pthread_mutex_t kPthreadMutexInitializer = PTHREAD_MUTEX_INITIALIZER;
29 Mutex() : mutex_(internal::kPthreadMutexInitializer) {}
30 ~Mutex() { pthread_mutex_destroy(&mutex_); }
32 void Lock() { pthread_mutex_lock(&mutex_); }
33 void Unlock() { pthread_mutex_unlock(&mutex_); }
34 bool TryLock() { return pthread_mutex_trylock(&mutex_) == 0; }
49 pthread_mutex_t mutex_;
51 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex);
56 explicit MutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
57 ~MutexLock() { mutex_->Unlock(); }
62 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLock);
65 // Catch bug where variable name is omitted (e.g., |MutexLock (&mu)|).
66 #define MutexLock(x) MOJO_COMPILE_ASSERT(0, mutex_lock_missing_variable_name);
70 #endif // MOJO_PUBLIC_UTILITY_MUTEX_H_