Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "public/platform/WebTaskRunner.h" | 5 #include "public/platform/WebTaskRunner.h" |
| 6 | 6 |
| 7 namespace blink { | 7 namespace blink { |
| 8 | 8 |
| 9 // This class holds a reference to a TaskHandle to keep it alive while a task is | |
| 10 // pending in a task queue, and clears the reference on the task disposal, so | |
| 11 // that it doesn't leave a circular reference like below: | |
| 12 // struct Foo : GarbageCollected<Foo> { | |
| 13 // void bar() {} | |
| 14 // RefPtr<TaskHandle> m_handle; | |
| 15 // }; | |
| 16 // | |
| 17 // foo->m_handle = taskRunner->postCancellableTask( | |
| 18 // BLINK_FROM_HERE, WTF::bind(&Foo::bar, wrapPersistent(foo))); | |
| 19 // | |
| 20 // There is a circular reference in the example above as: | |
| 21 // foo -> m_handle -> m_task -> Persistent<Foo> in WTF::bind. | |
| 22 // CancelOnTaskDestruction is needed to break the circle by clearing |m_task| | |
|
alex clarke (OOO till 29th)
2016/10/26 12:40:42
I wonder if we could do something in a follow on p
tzik
2016/10/27 06:38:53
Do you mean a "fast path" for closures that don't
alex clarke (OOO till 29th)
2016/10/27 09:46:37
I'm wondering about things like this:
foo->m_hand
| |
| 23 // when the wrapped WTF::Closure is deleted. | |
| 24 class TaskHandle::CancelOnTaskDestruction { | |
| 25 public: | |
| 26 explicit CancelOnTaskDestruction(RefPtr<TaskHandle> handle) | |
| 27 : m_handle(std::move(handle)) {} | |
| 28 | |
| 29 CancelOnTaskDestruction(CancelOnTaskDestruction&&) = default; | |
| 30 | |
| 31 void cancel() const { | |
| 32 if (m_handle) | |
| 33 m_handle->cancel(); | |
| 34 } | |
| 35 | |
| 36 ~CancelOnTaskDestruction() { cancel(); } | |
| 37 | |
| 38 private: | |
| 39 RefPtr<TaskHandle> m_handle; | |
|
Sami
2016/10/26 12:32:51
DISALLOW_COPY_AND_ASSIGN?
tzik
2016/10/27 06:38:53
Done.
| |
| 40 }; | |
| 41 | |
| 42 bool TaskHandle::isActive() const { | |
| 43 return static_cast<bool>(m_task); | |
| 44 } | |
| 45 | |
| 46 void TaskHandle::cancel() { | |
| 47 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
| 48 m_weakPtrFactory.revokeAll(); | |
| 49 } | |
| 50 | |
| 51 TaskHandle::~TaskHandle() {} | |
| 52 | |
| 53 TaskHandle::TaskHandle(std::unique_ptr<WTF::Closure> task) | |
| 54 : m_task(std::move(task)), m_weakPtrFactory(this) { | |
| 55 DCHECK(m_task); | |
| 56 } | |
| 57 | |
| 58 void TaskHandle::run(const CancelOnTaskDestruction& scoper) { | |
| 59 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
| 60 cancel(); | |
|
dcheng
2016/10/26 01:58:54
This call looks a bit interesting, because cancel(
tzik
2016/10/27 06:38:53
I think cancel() can be potentially reenterred thr
| |
| 61 (*task)(); | |
| 62 } | |
| 63 | |
| 64 WTF::WeakPtr<TaskHandle> TaskHandle::asWeakPtr() { | |
| 65 return m_weakPtrFactory.createWeakPtr(); | |
| 66 } | |
| 67 | |
| 9 void WebTaskRunner::postTask(const WebTraceLocation& location, | 68 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 10 std::unique_ptr<CrossThreadClosure> task) { | 69 std::unique_ptr<CrossThreadClosure> task) { |
| 11 toSingleThreadTaskRunner()->PostTask(location, | 70 toSingleThreadTaskRunner()->PostTask(location, |
| 12 convertToBaseCallback(std::move(task))); | 71 convertToBaseCallback(std::move(task))); |
| 13 } | 72 } |
| 14 | 73 |
| 15 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 74 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 16 std::unique_ptr<CrossThreadClosure> task, | 75 std::unique_ptr<CrossThreadClosure> task, |
| 17 long long delayMs) { | 76 long long delayMs) { |
| 18 toSingleThreadTaskRunner()->PostDelayedTask( | 77 toSingleThreadTaskRunner()->PostDelayedTask( |
| 19 location, convertToBaseCallback(std::move(task)), | 78 location, convertToBaseCallback(std::move(task)), |
| 20 base::TimeDelta::FromMilliseconds(delayMs)); | 79 base::TimeDelta::FromMilliseconds(delayMs)); |
| 21 } | 80 } |
| 22 | 81 |
| 23 void WebTaskRunner::postTask(const WebTraceLocation& location, | 82 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 24 std::unique_ptr<WTF::Closure> task) { | 83 std::unique_ptr<WTF::Closure> task) { |
| 25 toSingleThreadTaskRunner()->PostTask(location, | 84 toSingleThreadTaskRunner()->PostTask(location, |
| 26 convertToBaseCallback(std::move(task))); | 85 convertToBaseCallback(std::move(task))); |
| 27 } | 86 } |
| 28 | 87 |
| 29 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 88 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 30 std::unique_ptr<WTF::Closure> task, | 89 std::unique_ptr<WTF::Closure> task, |
| 31 long long delayMs) { | 90 long long delayMs) { |
| 32 toSingleThreadTaskRunner()->PostDelayedTask( | 91 toSingleThreadTaskRunner()->PostDelayedTask( |
| 33 location, convertToBaseCallback(std::move(task)), | 92 location, convertToBaseCallback(std::move(task)), |
| 34 base::TimeDelta::FromMilliseconds(delayMs)); | 93 base::TimeDelta::FromMilliseconds(delayMs)); |
| 35 } | 94 } |
| 36 | 95 |
| 96 RefPtr<TaskHandle> WebTaskRunner::postCancellableTask( | |
| 97 const WebTraceLocation& location, | |
| 98 std::unique_ptr<WTF::Closure> task) { | |
| 99 DCHECK(runsTasksOnCurrentThread()); | |
| 100 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 101 postTask(location, WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 102 TaskHandle::CancelOnTaskDestruction(handle))); | |
| 103 return handle; | |
| 104 } | |
| 105 | |
| 106 RefPtr<TaskHandle> WebTaskRunner::postDelayedCancellableTask( | |
| 107 const WebTraceLocation& location, | |
| 108 std::unique_ptr<WTF::Closure> task, | |
| 109 long long delayMs) { | |
| 110 DCHECK(runsTasksOnCurrentThread()); | |
| 111 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 112 postDelayedTask(location, | |
| 113 WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 114 TaskHandle::CancelOnTaskDestruction(handle)), | |
| 115 delayMs); | |
| 116 return handle; | |
| 117 } | |
| 118 | |
| 37 } // namespace blink | 119 } // namespace blink |
| OLD | NEW |