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 REF_COUNT_H_ | |
6 #define REF_COUNT_H_ | |
7 | |
8 #include "threading/scoped_mutex_lock.h" | |
9 | |
10 namespace threading { | |
11 | |
12 // A thread-safe reference counter for class CompletionCallbackFactory. | |
13 class RefCount { | |
14 public: | |
15 RefCount() : ref_(0) { | |
16 pthread_mutex_init(&mutex_, NULL); | |
17 } | |
18 ~RefCount() { | |
19 pthread_mutex_destroy(&mutex_); | |
20 } | |
21 | |
22 int32_t AddRef() { | |
23 int32_t ret_val = 0; | |
24 threading::ScopedMutexLock scoped_mutex(&mutex_); | |
25 if (scoped_mutex.is_valid()) { | |
26 ret_val = ++ref_; | |
27 } | |
28 return ret_val; | |
29 } | |
30 | |
31 int32_t Release() { | |
32 int32_t ret_val = -1; | |
33 threading::ScopedMutexLock scoped_mutex(&mutex_); | |
34 if (scoped_mutex.is_valid()) { | |
35 ret_val = --ref_; | |
36 } | |
37 return ret_val; | |
38 } | |
39 | |
40 private: | |
41 int32_t ref_; | |
42 pthread_mutex_t mutex_; | |
43 }; | |
44 | |
45 } // namespace threading | |
46 #endif // REF_COUNT_H_ | |
OLD | NEW |