| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include "base/logging.h" |
| 9 | |
| 10 #include "base/macros.h" | 9 #include "base/macros.h" |
| 11 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 11 #include "mojo/public/cpp/system/macros.h" |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 namespace internal { | 14 namespace internal { |
| 16 | 15 |
| 17 // Used to allocate an instance of T that can be shared via reference counting. | 16 // Used to allocate an instance of T that can be shared via reference counting. |
| 18 // | 17 // |
| 19 // A default constructed SharedData does not have a Holder until it is set, | 18 // A default constructed SharedData does not have a Holder until it is set, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 return holder_->value; | 75 return holder_->value; |
| 77 } | 76 } |
| 78 | 77 |
| 79 private: | 78 private: |
| 80 class Holder { | 79 class Holder { |
| 81 public: | 80 public: |
| 82 Holder() : value(), ref_count_(1) {} | 81 Holder() : value(), ref_count_(1) {} |
| 83 Holder(const T& value) : value(value), ref_count_(1) {} | 82 Holder(const T& value) : value(value), ref_count_(1) {} |
| 84 | 83 |
| 85 void Retain() { | 84 void Retain() { |
| 86 assert(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 87 ++ref_count_; | 86 ++ref_count_; |
| 88 } | 87 } |
| 89 void Release() { | 88 void Release() { |
| 90 assert(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 if (--ref_count_ == 0) | 90 if (--ref_count_ == 0) |
| 92 delete this; | 91 delete this; |
| 93 } | 92 } |
| 94 | 93 |
| 95 T value; | 94 T value; |
| 96 | 95 |
| 97 private: | 96 private: |
| 98 int ref_count_; | 97 int ref_count_; |
| 99 base::ThreadChecker thread_checker_; | 98 base::ThreadChecker thread_checker_; |
| 100 DISALLOW_COPY_AND_ASSIGN(Holder); | 99 DISALLOW_COPY_AND_ASSIGN(Holder); |
| 101 }; | 100 }; |
| 102 | 101 |
| 103 void LazyInit() const { | 102 void LazyInit() const { |
| 104 if (!holder_) | 103 if (!holder_) |
| 105 holder_ = new Holder(); | 104 holder_ = new Holder(); |
| 106 } | 105 } |
| 107 | 106 |
| 108 mutable Holder* holder_; | 107 mutable Holder* holder_; |
| 109 }; | 108 }; |
| 110 | 109 |
| 111 } // namespace internal | 110 } // namespace internal |
| 112 } // namespace mojo | 111 } // namespace mojo |
| 113 | 112 |
| 114 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ | 113 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_ |
| OLD | NEW |