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

Side by Side Diff: mojo/public/cpp/utility/lib/run_loop.cc

Issue 588593002: mojo: Allow basic RunLoop to be quit from a posted task. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Follow review Created 6 years, 2 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 | mojo/public/cpp/utility/run_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "mojo/public/cpp/utility/run_loop.h" 5 #include "mojo/public/cpp/utility/run_loop.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <vector> 10 #include <vector>
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 void RunLoop::RemoveHandler(const Handle& handle) { 86 void RunLoop::RemoveHandler(const Handle& handle) {
87 assert(current() == this); 87 assert(current() == this);
88 handler_data_.erase(handle); 88 handler_data_.erase(handle);
89 } 89 }
90 90
91 bool RunLoop::HasHandler(const Handle& handle) const { 91 bool RunLoop::HasHandler(const Handle& handle) const {
92 return handler_data_.find(handle) != handler_data_.end(); 92 return handler_data_.find(handle) != handler_data_.end();
93 } 93 }
94 94
95 void RunLoop::Run() { 95 void RunLoop::Run() {
96 RunInternal(false);
97 }
98
99 void RunLoop::RunUntilIdle() {
100 RunInternal(true);
101 }
102
103 void RunLoop::RunInternal(bool until_idle) {
96 assert(current() == this); 104 assert(current() == this);
97 RunState* old_state = run_state_; 105 RunState* old_state = run_state_;
98 RunState run_state; 106 RunState run_state;
99 run_state_ = &run_state; 107 run_state_ = &run_state;
100 while (!run_state.should_quit) { 108 for (;;) {
101 DoDelayedWork(); 109 DoDelayedWork();
102 Wait(false); 110 if (run_state.should_quit)
103 } 111 break;
104 run_state_ = old_state; 112 bool did_notify = Wait(until_idle);
105 } 113 if (run_state.should_quit)
106 114 break;
107 void RunLoop::RunUntilIdle() { 115 if (!did_notify && until_idle && delayed_tasks_.empty())
jamesr 2014/09/24 04:23:10 RunUntilIdle does not mean run until the delayed t
108 assert(current() == this);
109 RunState* old_state = run_state_;
110 RunState run_state;
111 run_state_ = &run_state;
112 while (!run_state.should_quit) {
113 DoDelayedWork();
114 if (!Wait(true) && delayed_tasks_.empty())
115 break; 116 break;
116 } 117 }
117 run_state_ = old_state; 118 run_state_ = old_state;
118 } 119 }
119 120
120 void RunLoop::DoDelayedWork() { 121 void RunLoop::DoDelayedWork() {
121 MojoTimeTicks now = GetTimeTicksNow(); 122 MojoTimeTicks now = GetTimeTicksNow();
122 if (!delayed_tasks_.empty() && delayed_tasks_.top().run_time <= now) { 123 if (!delayed_tasks_.empty() && delayed_tasks_.top().run_time <= now) {
123 PendingTask task = delayed_tasks_.top(); 124 PendingTask task = delayed_tasks_.top();
124 delayed_tasks_.pop(); 125 delayed_tasks_.pop();
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 min_time = i->second.deadline; 234 min_time = i->second.deadline;
234 } 235 }
235 } 236 }
236 if (!delayed_tasks_.empty()) { 237 if (!delayed_tasks_.empty()) {
237 MojoTimeTicks delayed_min_time = delayed_tasks_.top().run_time; 238 MojoTimeTicks delayed_min_time = delayed_tasks_.top().run_time;
238 if (min_time == kInvalidTimeTicks) 239 if (min_time == kInvalidTimeTicks)
239 min_time = delayed_min_time; 240 min_time = delayed_min_time;
240 else 241 else
241 min_time = std::min(min_time, delayed_min_time); 242 min_time = std::min(min_time, delayed_min_time);
242 } 243 }
243 if (non_blocking) { 244 if (non_blocking && delayed_tasks_.empty()) {
244 wait_state.deadline = static_cast<MojoDeadline>(0); 245 wait_state.deadline = static_cast<MojoDeadline>(0);
245 } else if (min_time != kInvalidTimeTicks) { 246 } else if (min_time != kInvalidTimeTicks) {
246 const MojoTimeTicks now = GetTimeTicksNow(); 247 const MojoTimeTicks now = GetTimeTicksNow();
247 if (min_time < now) 248 if (min_time < now)
248 wait_state.deadline = static_cast<MojoDeadline>(0); 249 wait_state.deadline = static_cast<MojoDeadline>(0);
249 else 250 else
250 wait_state.deadline = static_cast<MojoDeadline>(min_time - now); 251 wait_state.deadline = static_cast<MojoDeadline>(min_time - now);
251 } 252 }
252 return wait_state; 253 return wait_state;
253 } 254 }
(...skipping 12 matching lines...) Expand all
266 // std::priority_queue<> puts the least element at the end of the queue. We 267 // std::priority_queue<> puts the least element at the end of the queue. We
267 // want the soonest eligible task to be at the head of the queue, so 268 // want the soonest eligible task to be at the head of the queue, so
268 // run_times further in the future are considered lesser. 269 // run_times further in the future are considered lesser.
269 return run_time > other.run_time; 270 return run_time > other.run_time;
270 } 271 }
271 272
272 return sequence_number > other.sequence_number; 273 return sequence_number > other.sequence_number;
273 } 274 }
274 275
275 } // namespace mojo 276 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/utility/run_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698