| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 // Internal implementation details for ref_counted.h. | |
| 6 | |
| 7 #ifndef MOJO_EDK_SYSTEM_REF_COUNTED_INTERNAL_H_ | |
| 8 #define MOJO_EDK_SYSTEM_REF_COUNTED_INTERNAL_H_ | |
| 9 | |
| 10 #include <assert.h> | |
| 11 | |
| 12 #include <atomic> | |
| 13 | |
| 14 #include "mojo/public/cpp/system/macros.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace system { | |
| 18 namespace internal { | |
| 19 | |
| 20 class RefCountedThreadSafeBase { | |
| 21 public: | |
| 22 void AddRef() const { | |
| 23 assert(!adoption_required_); | |
| 24 assert(!destruction_started_); | |
| 25 ref_count_.fetch_add(1u, std::memory_order_relaxed); | |
| 26 } | |
| 27 | |
| 28 void AssertHasOneRef() const { | |
| 29 assert(ref_count_.load(std::memory_order_acquire) == 1u); | |
| 30 } | |
| 31 | |
| 32 protected: | |
| 33 RefCountedThreadSafeBase(); | |
| 34 ~RefCountedThreadSafeBase(); | |
| 35 | |
| 36 // Returns true if the object should self-delete. | |
| 37 bool Release() const { | |
| 38 assert(!adoption_required_); | |
| 39 assert(!destruction_started_); | |
| 40 assert(ref_count_.load(std::memory_order_acquire) != 0u); | |
| 41 // TODO(vtl): We could add the following: | |
| 42 // if (ref_count_.load(std::memory_order_relaxed) == 1u) { | |
| 43 // #ifndef NDEBUG | |
| 44 // destruction_started_= true; | |
| 45 // #endif | |
| 46 // return true; | |
| 47 // } | |
| 48 // This would be correct. On ARM (an Nexus 4), in *single-threaded* tests, | |
| 49 // this seems to make the destruction case marginally faster (barely | |
| 50 // measurable), and while the non-destruction case remains about the same | |
| 51 // (possibly marginally slower, but my measurements aren't good enough to | |
| 52 // have any confidence in that). I should try multithreaded/multicore tests. | |
| 53 if (ref_count_.fetch_sub(1u, std::memory_order_release) == 1u) { | |
| 54 std::atomic_thread_fence(std::memory_order_acquire); | |
| 55 #ifndef NDEBUG | |
| 56 destruction_started_ = true; | |
| 57 #endif | |
| 58 return true; | |
| 59 } | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 #ifndef NDEBUG | |
| 64 void Adopt() { | |
| 65 assert(adoption_required_); | |
| 66 adoption_required_ = false; | |
| 67 } | |
| 68 #endif | |
| 69 | |
| 70 private: | |
| 71 mutable std::atomic_uint_fast32_t ref_count_; | |
| 72 | |
| 73 #ifndef NDEBUG | |
| 74 mutable bool adoption_required_; | |
| 75 mutable bool destruction_started_; | |
| 76 #endif | |
| 77 | |
| 78 MOJO_DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); | |
| 79 }; | |
| 80 | |
| 81 inline RefCountedThreadSafeBase::RefCountedThreadSafeBase() | |
| 82 : ref_count_(1u) | |
| 83 #ifndef NDEBUG | |
| 84 , | |
| 85 adoption_required_(true), | |
| 86 destruction_started_(false) | |
| 87 #endif | |
| 88 { | |
| 89 } | |
| 90 | |
| 91 inline RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { | |
| 92 assert(!adoption_required_); | |
| 93 // Should only be destroyed as a result of |Release()|. | |
| 94 assert(destruction_started_); | |
| 95 } | |
| 96 | |
| 97 } // namespace internal | |
| 98 } // namespace system | |
| 99 } // namespace mojo | |
| 100 | |
| 101 #endif // MOJO_EDK_SYSTEM_REF_COUNTED_INTERNAL_H_ | |
| OLD | NEW |