OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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. | |
4 | |
5 #ifndef SCOPED_MUTEX_LOCK_H_ | |
6 #define SCOPED_MUTEX_LOCK_H_ | |
7 | |
8 #include "experimental/conways_life/threading/pthread_ext.h" | |
9 | |
10 namespace threading { | |
11 // A small helper RAII class that implements a scoped pthread_mutex lock. | |
12 class ScopedMutexLock { | |
13 public: | |
14 explicit ScopedMutexLock(pthread_mutex_t* mutex) : mutex_(mutex) { | |
15 if (pthread_mutex_lock(mutex_) != PTHREAD_MUTEX_SUCCESS) { | |
16 mutex_ = NULL; | |
17 } | |
18 } | |
19 ~ScopedMutexLock() { | |
20 if (mutex_) | |
21 pthread_mutex_unlock(mutex_); | |
22 } | |
23 bool is_valid() const { | |
24 return mutex_ != NULL; | |
25 } | |
26 private: | |
27 pthread_mutex_t* mutex_; // Weak reference. | |
28 }; | |
29 } // namespace threading | |
30 | |
31 #endif // SCOPED_MUTEX_LOCK_H_ | |
32 | |
OLD | NEW |