OLD | NEW |
---|---|
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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 assert(current() == this); | 96 assert(current() == this); |
97 RunState* old_state = run_state_; | 97 RunState* old_state = run_state_; |
98 RunState run_state; | 98 RunState run_state; |
99 run_state_ = &run_state; | 99 run_state_ = &run_state; |
100 while (!run_state.should_quit) { | 100 while (!run_state.should_quit) { |
101 DoDelayedWork(); | 101 DoDelayedWork(); |
102 Wait(false); | 102 if (!run_state.should_quit) |
103 Wait(false); | |
jamesr
2014/09/22 22:30:18
hmm, i think this would be cleaner if it was struc
| |
103 } | 104 } |
104 run_state_ = old_state; | 105 run_state_ = old_state; |
105 } | 106 } |
106 | 107 |
107 void RunLoop::RunUntilIdle() { | 108 void RunLoop::RunUntilIdle() { |
108 assert(current() == this); | 109 assert(current() == this); |
109 RunState* old_state = run_state_; | 110 RunState* old_state = run_state_; |
110 RunState run_state; | 111 RunState run_state; |
111 run_state_ = &run_state; | 112 run_state_ = &run_state; |
112 while (!run_state.should_quit) { | 113 while (!run_state.should_quit) { |
113 DoDelayedWork(); | 114 DoDelayedWork(); |
114 if (!Wait(true) && delayed_tasks_.empty()) | 115 if (!run_state.should_quit && !Wait(true) && delayed_tasks_.empty()) |
jamesr
2014/09/22 22:30:18
this is getting too hard to follow IMO, especially
qsr
2014/09/23 10:40:15
That would be incorrect though. This must continue
| |
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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
OLD | NEW |