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