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

Side by Side Diff: ui/accelerated_widget_mac/window_resize_helper_mac.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: erase Closure* Created 3 years, 8 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 | « third_party/WebKit/Source/platform/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc ('k') | 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 "ui/accelerated_widget_mac/window_resize_helper_mac.h" 5 #include "ui/accelerated_widget_mac/window_resize_helper_mac.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <list> 9 #include <list>
10 #include <utility>
10 11
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/callback.h" 13 #include "base/callback.h"
13 #include "base/macros.h" 14 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
15 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h" 17 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread_restrictions.h" 18 #include "base/threading/thread_restrictions.h"
18 19
19 namespace ui { 20 namespace ui {
20 namespace { 21 namespace {
21 22
22 class WrappedTask; 23 class WrappedTask;
23 class PumpableTaskRunner; 24 class PumpableTaskRunner;
24 typedef std::list<WrappedTask*> WrappedTaskQueue; 25 typedef std::list<WrappedTask*> WrappedTaskQueue;
25 typedef base::Callback<void(base::WaitableEvent*, base::TimeDelta)> 26 typedef base::Callback<void(base::WaitableEvent*, base::TimeDelta)>
26 EventTimedWaitCallback; 27 EventTimedWaitCallback;
27 28
28 // A wrapper for IPCs and tasks that we may potentially execute in 29 // A wrapper for IPCs and tasks that we may potentially execute in
29 // WaitForSingleTaskToRun. Because these tasks are sent to two places to run, 30 // WaitForSingleTaskToRun. Because these tasks are sent to two places to run,
30 // we to wrap them in this structure and track whether or not they have run 31 // we to wrap them in this structure and track whether or not they have run
31 // yet, to avoid running them twice. 32 // yet, to avoid running them twice.
32 class WrappedTask { 33 class WrappedTask {
33 public: 34 public:
34 WrappedTask(const base::Closure& closure, base::TimeDelta delay); 35 WrappedTask(base::Closure closure, base::TimeDelta delay);
35 ~WrappedTask(); 36 ~WrappedTask();
36 bool ShouldRunBefore(const WrappedTask& other); 37 bool ShouldRunBefore(const WrappedTask& other);
37 void Run(); 38 void Run();
38 void AddToTaskRunnerQueue(PumpableTaskRunner* pumpable_task_runner); 39 void AddToTaskRunnerQueue(PumpableTaskRunner* pumpable_task_runner);
39 void RemoveFromTaskRunnerQueue(); 40 void RemoveFromTaskRunnerQueue();
40 const base::TimeTicks& can_run_time() const { return can_run_time_; } 41 const base::TimeTicks& can_run_time() const { return can_run_time_; }
41 42
42 private: 43 private:
43 base::Closure closure_; 44 base::Closure closure_;
44 base::TimeTicks can_run_time_; 45 base::TimeTicks can_run_time_;
(...skipping 21 matching lines...) Expand all
66 // Enqueue WrappedTask and post it to |target_task_runner_|. 67 // Enqueue WrappedTask and post it to |target_task_runner_|.
67 bool EnqueueAndPostWrappedTask(const tracked_objects::Location& from_here, 68 bool EnqueueAndPostWrappedTask(const tracked_objects::Location& from_here,
68 WrappedTask* task, 69 WrappedTask* task,
69 base::TimeDelta delay); 70 base::TimeDelta delay);
70 71
71 // Wait at most |max_delay| to run an enqueued task. 72 // Wait at most |max_delay| to run an enqueued task.
72 bool WaitForSingleWrappedTaskToRun(const base::TimeDelta& max_delay); 73 bool WaitForSingleWrappedTaskToRun(const base::TimeDelta& max_delay);
73 74
74 // base::SingleThreadTaskRunner implementation: 75 // base::SingleThreadTaskRunner implementation:
75 bool PostDelayedTask(const tracked_objects::Location& from_here, 76 bool PostDelayedTask(const tracked_objects::Location& from_here,
76 const base::Closure& task, 77 base::Closure task,
77 base::TimeDelta delay) override; 78 base::TimeDelta delay) override;
78 79
79 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 80 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
80 const base::Closure& task, 81 base::Closure task,
81 base::TimeDelta delay) override; 82 base::TimeDelta delay) override;
82 83
83 bool RunsTasksOnCurrentThread() const override; 84 bool RunsTasksOnCurrentThread() const override;
84 85
85 private: 86 private:
86 friend class WrappedTask; 87 friend class WrappedTask;
87 88
88 ~PumpableTaskRunner() override; 89 ~PumpableTaskRunner() override;
89 90
90 // A queue of live messages. Must hold |task_queue_lock_| to access. Tasks 91 // A queue of live messages. Must hold |task_queue_lock_| to access. Tasks
(...skipping 15 matching lines...) Expand all
106 107
107 DISALLOW_COPY_AND_ASSIGN(PumpableTaskRunner); 108 DISALLOW_COPY_AND_ASSIGN(PumpableTaskRunner);
108 }; 109 };
109 110
110 base::LazyInstance<WindowResizeHelperMac>::Leaky g_window_resize_helper = 111 base::LazyInstance<WindowResizeHelperMac>::Leaky g_window_resize_helper =
111 LAZY_INSTANCE_INITIALIZER; 112 LAZY_INSTANCE_INITIALIZER;
112 113
113 //////////////////////////////////////////////////////////////////////////////// 114 ////////////////////////////////////////////////////////////////////////////////
114 // WrappedTask 115 // WrappedTask
115 116
116 WrappedTask::WrappedTask(const base::Closure& closure, base::TimeDelta delay) 117 WrappedTask::WrappedTask(base::Closure closure, base::TimeDelta delay)
117 : closure_(closure), 118 : closure_(std::move(closure)),
118 can_run_time_(base::TimeTicks::Now() + delay), 119 can_run_time_(base::TimeTicks::Now() + delay),
119 has_run_(false), 120 has_run_(false),
120 sequence_number_(0) {} 121 sequence_number_(0) {}
121 122
122 WrappedTask::~WrappedTask() { 123 WrappedTask::~WrappedTask() {
123 RemoveFromTaskRunnerQueue(); 124 RemoveFromTaskRunnerQueue();
124 } 125 }
125 126
126 bool WrappedTask::ShouldRunBefore(const WrappedTask& other) { 127 bool WrappedTask::ShouldRunBefore(const WrappedTask& other) {
127 if (can_run_time_ < other.can_run_time_) 128 if (can_run_time_ < other.can_run_time_)
128 return true; 129 return true;
129 if (can_run_time_ > other.can_run_time_) 130 if (can_run_time_ > other.can_run_time_)
130 return false; 131 return false;
131 if (sequence_number_ < other.sequence_number_) 132 if (sequence_number_ < other.sequence_number_)
132 return true; 133 return true;
133 if (sequence_number_ > other.sequence_number_) 134 if (sequence_number_ > other.sequence_number_)
134 return false; 135 return false;
135 // Sequence numbers are unique, so this should never happen. 136 // Sequence numbers are unique, so this should never happen.
136 NOTREACHED(); 137 NOTREACHED();
137 return false; 138 return false;
138 } 139 }
139 140
140 void WrappedTask::Run() { 141 void WrappedTask::Run() {
141 if (has_run_) 142 if (has_run_)
142 return; 143 return;
143 RemoveFromTaskRunnerQueue(); 144 RemoveFromTaskRunnerQueue();
144 has_run_ = true; 145 has_run_ = true;
145 closure_.Run(); 146 std::move(closure_).Run();
146 } 147 }
147 148
148 void WrappedTask::AddToTaskRunnerQueue( 149 void WrappedTask::AddToTaskRunnerQueue(
149 PumpableTaskRunner* pumpable_task_runner) { 150 PumpableTaskRunner* pumpable_task_runner) {
150 pumpable_task_runner_ = pumpable_task_runner; 151 pumpable_task_runner_ = pumpable_task_runner;
151 base::AutoLock lock(pumpable_task_runner_->task_queue_lock_); 152 base::AutoLock lock(pumpable_task_runner_->task_queue_lock_);
152 static uint64_t last_sequence_number = 0; 153 static uint64_t last_sequence_number = 0;
153 last_sequence_number += 1; 154 last_sequence_number += 1;
154 sequence_number_ = last_sequence_number; 155 sequence_number_ = last_sequence_number;
155 iterator_ = pumpable_task_runner_->task_queue_.insert( 156 iterator_ = pumpable_task_runner_->task_queue_.insert(
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 253
253 return target_task_runner_->PostDelayedTask( 254 return target_task_runner_->PostDelayedTask(
254 from_here, base::Bind(&WrappedTask::Run, base::Owned(task)), delay); 255 from_here, base::Bind(&WrappedTask::Run, base::Owned(task)), delay);
255 } 256 }
256 257
257 //////////////////////////////////////////////////////////////////////////////// 258 ////////////////////////////////////////////////////////////////////////////////
258 // PumpableTaskRunner, base::SingleThreadTaskRunner implementation: 259 // PumpableTaskRunner, base::SingleThreadTaskRunner implementation:
259 260
260 bool PumpableTaskRunner::PostDelayedTask( 261 bool PumpableTaskRunner::PostDelayedTask(
261 const tracked_objects::Location& from_here, 262 const tracked_objects::Location& from_here,
262 const base::Closure& task, 263 base::Closure task,
263 base::TimeDelta delay) { 264 base::TimeDelta delay) {
264 return EnqueueAndPostWrappedTask(from_here, new WrappedTask(task, delay), 265 return EnqueueAndPostWrappedTask(
265 delay); 266 from_here, new WrappedTask(std::move(task), delay), delay);
266 } 267 }
267 268
268 bool PumpableTaskRunner::PostNonNestableDelayedTask( 269 bool PumpableTaskRunner::PostNonNestableDelayedTask(
269 const tracked_objects::Location& from_here, 270 const tracked_objects::Location& from_here,
270 const base::Closure& task, 271 base::Closure task,
271 base::TimeDelta delay) { 272 base::TimeDelta delay) {
272 // The correctness of non-nestable events hasn't been proven for this 273 // The correctness of non-nestable events hasn't been proven for this
273 // structure. 274 // structure.
274 NOTREACHED(); 275 NOTREACHED();
275 return false; 276 return false;
276 } 277 }
277 278
278 bool PumpableTaskRunner::RunsTasksOnCurrentThread() const { 279 bool PumpableTaskRunner::RunsTasksOnCurrentThread() const {
279 return target_task_runner_->RunsTasksOnCurrentThread(); 280 return target_task_runner_->RunsTasksOnCurrentThread();
280 } 281 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 WindowResizeHelperMac::WindowResizeHelperMac() {} 318 WindowResizeHelperMac::WindowResizeHelperMac() {}
318 WindowResizeHelperMac::~WindowResizeHelperMac() {} 319 WindowResizeHelperMac::~WindowResizeHelperMac() {}
319 320
320 void WindowResizeHelperMac::EventTimedWait(base::WaitableEvent* event, 321 void WindowResizeHelperMac::EventTimedWait(base::WaitableEvent* event,
321 base::TimeDelta delay) { 322 base::TimeDelta delay) {
322 base::ThreadRestrictions::ScopedAllowWait allow_wait; 323 base::ThreadRestrictions::ScopedAllowWait allow_wait;
323 event->TimedWait(delay); 324 event->TimedWait(delay);
324 } 325 }
325 326
326 } // namespace ui 327 } // namespace ui
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698