Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
|
gab
2016/12/22 20:32:28
Note: preferable to tweak git cl upload --similari
fdoray
2016/12/23 12:49:30
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ | |
| 6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ | |
| 7 | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/sequenced_task_runner.h" | |
| 15 | |
| 16 namespace base { | |
| 17 | |
| 18 // RefCountedDeleteOnSequence is similar to RefCountedThreadSafe, and ensures | |
| 19 // that the object will be deleted on a specified sequence. | |
| 20 // | |
| 21 // Sample usage: | |
| 22 // class Foo : public RefCountedDeleteOnSequence<Foo> { | |
| 23 // | |
| 24 // Foo(scoped_refptr<SequencedTaskRunner> task_runner) | |
| 25 // : RefCountedDeleteOnSequence<Foo>(std::move(task_runner)) {} | |
| 26 // ... | |
| 27 // private: | |
| 28 // friend class RefCountedDeleteOnSequence<Foo>; | |
| 29 // friend class DeleteHelper<Foo>; | |
| 30 // | |
| 31 // ~Foo(); | |
| 32 // }; | |
| 33 template <class T> | |
| 34 class RefCountedDeleteOnSequence : public subtle::RefCountedThreadSafeBase { | |
| 35 public: | |
| 36 // A SequencedTaskRunner for the current sequence can be acquired by calling | |
| 37 // SequencedTaskRunnerHandle::Get(). | |
| 38 RefCountedDeleteOnSequence(scoped_refptr<SequencedTaskRunner> task_runner) | |
| 39 : task_runner_(std::move(task_runner)) { | |
| 40 DCHECK(task_runner_); | |
| 41 } | |
| 42 | |
| 43 void AddRef() const { subtle::RefCountedThreadSafeBase::AddRef(); } | |
| 44 | |
| 45 void Release() const { | |
| 46 if (subtle::RefCountedThreadSafeBase::Release()) | |
| 47 DestructOnSequence(); | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 friend class DeleteHelper<RefCountedDeleteOnSequence>; | |
| 52 ~RefCountedDeleteOnSequence() = default; | |
| 53 | |
| 54 private: | |
| 55 void DestructOnSequence() const { | |
| 56 const T* t = static_cast<const T*>(this); | |
| 57 if (task_runner_->RunsTasksOnCurrentThread()) | |
| 58 delete t; | |
| 59 else | |
| 60 task_runner_->DeleteSoon(FROM_HERE, t); | |
| 61 } | |
| 62 | |
| 63 const scoped_refptr<SequencedTaskRunner> task_runner_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnSequence); | |
| 66 }; | |
| 67 | |
| 68 } // namespace base | |
| 69 | |
| 70 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ | |
| OLD | NEW |