Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(538)

Unified Diff: base/memory/ref_counted_delete_on_task_runner.h

Issue 369703003: Reduce usage of MessageLoopProxy in base/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/ref_counted_delete_on_message_loop.h ('k') | base/observer_list_threadsafe.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/ref_counted_delete_on_task_runner.h
diff --git a/base/memory/ref_counted_delete_on_task_runner.h b/base/memory/ref_counted_delete_on_task_runner.h
new file mode 100644
index 0000000000000000000000000000000000000000..ece422ce1fb579bb347bf0a043e85753ceb05b24
--- /dev/null
+++ b/base/memory/ref_counted_delete_on_task_runner.h
@@ -0,0 +1,69 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_
+#define BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_
+
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/memory/ref_counted.h"
+#include "base/sequenced_task_runner.h"
+
+namespace base {
+
+// RefCountedDeleteOnTaskRunner is similar to RefCountedThreadSafe, and ensures
+// that the object will be deleted on a specified task runner.
+//
+// Sample usage:
+// class Foo : public RefCountedDeleteOnTaskRunner<Foo> {
+//
+// Foo(const scoped_refptr<SequencedTaskRunner>& task_runner)
+// : RefCountedDeleteOnTaskRunner<Foo>(task_runner) {
+// ...
+// }
+// ...
+// private:
+// friend class RefCountedDeleteOnTaskRunner<Foo>;
+// friend class DeleteHelper<Foo>;
+//
+// ~Foo();
+// };
+
+template <class T>
+class RefCountedDeleteOnTaskRunner : public subtle::RefCountedThreadSafeBase {
+ public:
+ RefCountedDeleteOnTaskRunner(
awong 2014/07/07 21:50:00 explicit?
+ const scoped_refptr<SequencedTaskRunner>& task_runner)
+ : task_runner_(task_runner) {
+ DCHECK(task_runner_.get());
+ }
+
+ void AddRef() const { subtle::RefCountedThreadSafeBase::AddRef(); }
+
+ void Release() const {
+ if (subtle::RefCountedThreadSafeBase::Release())
+ DestructOnTaskRunner();
+ }
+
+ protected:
+ friend class DeleteHelper<RefCountedDeleteOnTaskRunner>;
+ ~RefCountedDeleteOnTaskRunner() {}
+
+ void DestructOnTaskRunner() const {
+ const T* t = static_cast<const T*>(this);
+ if (task_runner_->RunsTasksOnCurrentThread())
+ delete t;
+ else
+ task_runner_->DeleteSoon(FROM_HERE, t);
+ }
+
+ scoped_refptr<SequencedTaskRunner> task_runner_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnTaskRunner);
+};
+
+} // namespace base
+
+#endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_
« no previous file with comments | « base/memory/ref_counted_delete_on_message_loop.h ('k') | base/observer_list_threadsafe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698