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 | 5 |
6 #ifndef LIBRARIES_UTILS_REF_OBJECT | 6 #ifndef LIBRARIES_UTILS_REF_OBJECT |
7 #define LIBRARIES_UTILS_REF_OBJECT | 7 #define LIBRARIES_UTILS_REF_OBJECT |
8 | 8 |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include "pthread.h" | 10 #include "pthread.h" |
11 | 11 |
12 class RefObject { | 12 class RefObject { |
13 public: | 13 public: |
14 RefObject() { | 14 RefObject() { |
15 ref_count_ = 1; | 15 ref_count_ = 1; |
16 pthread_mutex_init(&lock_, NULL); | 16 pthread_mutex_init(&lock_, NULL); |
17 } | 17 } |
18 | 18 |
19 int RefCount() const { return ref_count_; } | 19 int RefCount() const { return ref_count_; } |
20 | 20 |
21 void Acquire() { | 21 void Acquire() { |
22 ref_count_++; | 22 ref_count_++; |
23 } | 23 } |
24 void Release() { | 24 bool Release() { |
25 if (--ref_count_ == 0) { | 25 if (--ref_count_ == 0) { |
26 delete this; | 26 delete this; |
| 27 return false; |
27 } | 28 } |
| 29 return true; |
28 } | 30 } |
29 | 31 |
30 protected: | 32 protected: |
31 virtual ~RefObject() { | 33 virtual ~RefObject() { |
32 pthread_mutex_destroy(&lock_); | 34 pthread_mutex_destroy(&lock_); |
33 } | 35 } |
34 | 36 |
35 pthread_mutex_t lock_; | 37 pthread_mutex_t lock_; |
36 | 38 |
37 private: | 39 private: |
38 int ref_count_; | 40 int ref_count_; |
39 }; | 41 }; |
40 | 42 |
41 #endif // LIBRARIES_UTILS_REF_OBJECT | 43 #endif // LIBRARIES_UTILS_REF_OBJECT |
OLD | NEW |