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

Side by Side Diff: third_party/WebKit/Source/platform/WebTaskRunner.cpp

Issue 2626883003: Make WebTaskRunner::postTask thread safe (Closed)
Patch Set: dcheng review Created 3 years, 11 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 #include "platform/WebTaskRunner.h" 5 #include "platform/WebTaskRunner.h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/single_thread_task_runner.h" 8 #include "base/single_thread_task_runner.h"
9 9
10 namespace base { 10 namespace base {
(...skipping 11 matching lines...) Expand all
22 const WTF::WeakPtr<blink::TaskHandle::Runner>&, 22 const WTF::WeakPtr<blink::TaskHandle::Runner>&,
23 const blink::TaskHandle& handle) { 23 const blink::TaskHandle& handle) {
24 return !handle.isActive(); 24 return !handle.isActive();
25 } 25 }
26 }; 26 };
27 27
28 } // namespace base 28 } // namespace base
29 29
30 namespace blink { 30 namespace blink {
31 31
32 namespace {
33
34 void runCrossThreadClosure(std::unique_ptr<CrossThreadClosure> task) {
35 (*task)();
36 }
37
38 } // namespace
39
32 class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> { 40 class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> {
33 public: 41 public:
34 explicit Runner(std::unique_ptr<WTF::Closure> task) 42 explicit Runner(std::unique_ptr<WTF::Closure> task)
35 : m_task(std::move(task)), m_weakPtrFactory(this) {} 43 : m_task(std::move(task)), m_weakPtrFactory(this) {}
36 44
37 WTF::WeakPtr<Runner> asWeakPtr() { return m_weakPtrFactory.createWeakPtr(); } 45 WTF::WeakPtr<Runner> asWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
38 46
39 bool isActive() const { return m_task && !m_task->isCancelled(); } 47 bool isActive() const { return m_task && !m_task->isCancelled(); }
40 48
41 void cancel() { 49 void cancel() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 TaskHandle& TaskHandle::operator=(TaskHandle&& other) { 104 TaskHandle& TaskHandle::operator=(TaskHandle&& other) {
97 TaskHandle tmp(std::move(other)); 105 TaskHandle tmp(std::move(other));
98 m_runner.swap(tmp.m_runner); 106 m_runner.swap(tmp.m_runner);
99 return *this; 107 return *this;
100 } 108 }
101 109
102 TaskHandle::TaskHandle(RefPtr<Runner> runner) : m_runner(std::move(runner)) { 110 TaskHandle::TaskHandle(RefPtr<Runner> runner) : m_runner(std::move(runner)) {
103 DCHECK(m_runner); 111 DCHECK(m_runner);
104 } 112 }
105 113
114 // Use a custom function for base::Bind instead of convertToBaseCallback to
115 // avoid copying the closure later in the call chain. Copying the bound state
116 // can lead to data races with ref counted objects like StringImpl. See
117 // crbug.com/679915 for more details.
Wez 2017/01/11 21:21:49 Wouldn't it be better to "fix" convertToBaseCallba
106 void WebTaskRunner::postTask(const WebTraceLocation& location, 118 void WebTaskRunner::postTask(const WebTraceLocation& location,
107 std::unique_ptr<CrossThreadClosure> task) { 119 std::unique_ptr<CrossThreadClosure> task) {
108 toSingleThreadTaskRunner()->PostTask(location, 120 toSingleThreadTaskRunner()->PostTask(
109 convertToBaseCallback(std::move(task))); 121 location, base::Bind(&runCrossThreadClosure, base::Passed(&task)));
110 } 122 }
111 123
112 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, 124 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location,
113 std::unique_ptr<CrossThreadClosure> task, 125 std::unique_ptr<CrossThreadClosure> task,
114 long long delayMs) { 126 long long delayMs) {
115 toSingleThreadTaskRunner()->PostDelayedTask( 127 toSingleThreadTaskRunner()->PostDelayedTask(
116 location, convertToBaseCallback(std::move(task)), 128 location, base::Bind(&runCrossThreadClosure, base::Passed(&task)),
117 base::TimeDelta::FromMilliseconds(delayMs)); 129 base::TimeDelta::FromMilliseconds(delayMs));
118 } 130 }
119 131
120 void WebTaskRunner::postTask(const WebTraceLocation& location, 132 void WebTaskRunner::postTask(const WebTraceLocation& location,
121 std::unique_ptr<WTF::Closure> task) { 133 std::unique_ptr<WTF::Closure> task) {
122 toSingleThreadTaskRunner()->PostTask(location, 134 toSingleThreadTaskRunner()->PostTask(location,
123 convertToBaseCallback(std::move(task))); 135 convertToBaseCallback(std::move(task)));
124 } 136 }
125 137
126 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, 138 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location,
(...skipping 24 matching lines...) Expand all
151 adoptRef(new TaskHandle::Runner(std::move(task))); 163 adoptRef(new TaskHandle::Runner(std::move(task)));
152 postDelayedTask(location, WTF::bind(&TaskHandle::Runner::run, 164 postDelayedTask(location, WTF::bind(&TaskHandle::Runner::run,
153 runner->asWeakPtr(), TaskHandle(runner)), 165 runner->asWeakPtr(), TaskHandle(runner)),
154 delayMs); 166 delayMs);
155 return TaskHandle(runner); 167 return TaskHandle(runner);
156 } 168 }
157 169
158 WebTaskRunner::~WebTaskRunner() = default; 170 WebTaskRunner::~WebTaskRunner() = default;
159 171
160 } // namespace blink 172 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698