| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ |
| 6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ |
| 7 |
| 8 #include <utility> |
| 7 | 9 |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 12 #include "base/single_thread_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 13 | 15 |
| 14 namespace base { | 16 namespace base { |
| 15 | 17 |
| 16 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures | 18 // RefCountedDeleteOnSequence is similar to RefCountedThreadSafe, and ensures |
| 17 // that the object will be deleted on a specified message loop. | 19 // that the object will be deleted on a specified sequence. |
| 18 // | 20 // |
| 19 // Sample usage: | 21 // Sample usage: |
| 20 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { | 22 // class Foo : public RefCountedDeleteOnSequence<Foo> { |
| 21 // | 23 // |
| 22 // Foo(scoped_refptr<SingleThreadTaskRunner> loop) | 24 // Foo(scoped_refptr<SequencedTaskRunner> task_runner) |
| 23 // : RefCountedDeleteOnMessageLoop<Foo>(std::move(loop)) {} | 25 // : RefCountedDeleteOnSequence<Foo>(std::move(task_runner)) {} |
| 24 // ... | 26 // ... |
| 25 // private: | 27 // private: |
| 26 // friend class RefCountedDeleteOnMessageLoop<Foo>; | 28 // friend class RefCountedDeleteOnSequence<Foo>; |
| 27 // friend class DeleteHelper<Foo>; | 29 // friend class DeleteHelper<Foo>; |
| 28 // | 30 // |
| 29 // ~Foo(); | 31 // ~Foo(); |
| 30 // }; | 32 // }; |
| 31 | |
| 32 // TODO(skyostil): Rename this to RefCountedDeleteOnTaskRunner. | |
| 33 template <class T> | 33 template <class T> |
| 34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { | 34 class RefCountedDeleteOnSequence : public subtle::RefCountedThreadSafeBase { |
| 35 public: | 35 public: |
| 36 // A SingleThreadTaskRunner for the current thread can be acquired by calling | 36 // A SequencedTaskRunner for the current sequence can be acquired by calling |
| 37 // ThreadTaskRunnerHandle::Get(). | 37 // SequencedTaskRunnerHandle::Get(). |
| 38 RefCountedDeleteOnMessageLoop( | 38 RefCountedDeleteOnSequence(scoped_refptr<SequencedTaskRunner> task_runner) |
| 39 scoped_refptr<SingleThreadTaskRunner> task_runner) | |
| 40 : task_runner_(std::move(task_runner)) { | 39 : task_runner_(std::move(task_runner)) { |
| 41 DCHECK(task_runner_); | 40 DCHECK(task_runner_); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void AddRef() const { | 43 void AddRef() const { subtle::RefCountedThreadSafeBase::AddRef(); } |
| 45 subtle::RefCountedThreadSafeBase::AddRef(); | |
| 46 } | |
| 47 | 44 |
| 48 void Release() const { | 45 void Release() const { |
| 49 if (subtle::RefCountedThreadSafeBase::Release()) | 46 if (subtle::RefCountedThreadSafeBase::Release()) |
| 50 DestructOnMessageLoop(); | 47 DestructOnSequence(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 protected: | 50 protected: |
| 54 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; | 51 friend class DeleteHelper<RefCountedDeleteOnSequence>; |
| 55 ~RefCountedDeleteOnMessageLoop() {} | 52 ~RefCountedDeleteOnSequence() = default; |
| 56 | 53 |
| 57 void DestructOnMessageLoop() const { | 54 private: |
| 55 void DestructOnSequence() const { |
| 58 const T* t = static_cast<const T*>(this); | 56 const T* t = static_cast<const T*>(this); |
| 59 if (task_runner_->BelongsToCurrentThread()) | 57 if (task_runner_->RunsTasksOnCurrentThread()) |
| 60 delete t; | 58 delete t; |
| 61 else | 59 else |
| 62 task_runner_->DeleteSoon(FROM_HERE, t); | 60 task_runner_->DeleteSoon(FROM_HERE, t); |
| 63 } | 61 } |
| 64 | 62 |
| 65 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 63 const scoped_refptr<SequencedTaskRunner> task_runner_; |
| 66 | 64 |
| 67 private: | 65 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnSequence); |
| 68 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); | |
| 69 }; | 66 }; |
| 70 | 67 |
| 71 } // namespace base | 68 } // namespace base |
| 72 | 69 |
| 73 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 70 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_SEQUENCE_H_ |
| OLD | NEW |