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