OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SCOPED_MUTEX_LOCK_H_ | 5 #ifndef GTEST_PPAPI_SCOPED_MUTEX_LOCK_H_ |
6 #define SCOPED_MUTEX_LOCK_H_ | 6 #define GTEST_PPAPI_SCOPED_MUTEX_LOCK_H_ |
7 | 7 |
8 #include "c_salt/threading/pthread_ext.h" | 8 #include "gtest_ppapi/pthread_ext.h" |
9 | 9 |
10 namespace threading { | |
11 // A small helper RAII class that implements a scoped pthread_mutex lock. | 10 // A small helper RAII class that implements a scoped pthread_mutex lock. |
12 class ScopedMutexLock { | 11 class ScopedMutexLock { |
13 public: | 12 public: |
14 explicit ScopedMutexLock(pthread_mutex_t* mutex) : mutex_(mutex) { | 13 explicit ScopedMutexLock(pthread_mutex_t* mutex) : mutex_(mutex) { |
15 if (pthread_mutex_lock(mutex_) != PTHREAD_MUTEX_SUCCESS) { | 14 if (pthread_mutex_lock(mutex_) != PTHREAD_MUTEX_SUCCESS) { |
16 mutex_ = NULL; | 15 mutex_ = NULL; |
17 } | 16 } |
18 } | 17 } |
19 ~ScopedMutexLock() { | 18 ~ScopedMutexLock() { |
20 if (mutex_) | 19 if (mutex_) |
21 pthread_mutex_unlock(mutex_); | 20 pthread_mutex_unlock(mutex_); |
22 } | 21 } |
23 bool is_valid() const { | 22 bool is_valid() const { |
24 return mutex_ != NULL; | 23 return mutex_ != NULL; |
25 } | 24 } |
26 private: | 25 private: |
27 pthread_mutex_t* mutex_; // Weak reference. | 26 pthread_mutex_t* mutex_; // Weak reference. |
28 }; | 27 }; |
29 } // namespace threading | |
30 | 28 |
31 #endif // SCOPED_MUTEX_LOCK_H_ | 29 #endif // GTEST_PPAPI_SCOPED_MUTEX_LOCK_H_ |
32 | |
OLD | NEW |