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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(UNTIL_EMPTY); |
| 97 } |
| 98 |
| 99 void RunLoop::RunUntilIdle() { |
| 100 RunInternal(UNTIL_IDLE); |
| 101 } |
| 102 |
| 103 void RunLoop::RunInternal(RunMode run_mode) { |
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 bool did_work = DoDelayedWork(); |
102 Wait(false); | 110 if (run_state.should_quit) |
103 } | 111 break; |
104 run_state_ = old_state; | 112 did_work |= Wait(run_mode == UNTIL_IDLE); |
105 } | 113 if (run_state.should_quit) |
106 | 114 break; |
107 void RunLoop::RunUntilIdle() { | 115 if (!did_work && run_mode == UNTIL_IDLE) |
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 bool 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(); |
125 task.task.Run(); | 126 task.task.Run(); |
| 127 return true; |
126 } | 128 } |
| 129 return false; |
127 } | 130 } |
128 | 131 |
129 void RunLoop::Quit() { | 132 void RunLoop::Quit() { |
130 assert(current() == this); | 133 assert(current() == this); |
131 if (run_state_) | 134 if (run_state_) |
132 run_state_->should_quit = true; | 135 run_state_->should_quit = true; |
133 } | 136 } |
134 | 137 |
135 void RunLoop::PostDelayedTask(const Closure& task, MojoTimeTicks delay) { | 138 void RunLoop::PostDelayedTask(const Closure& task, MojoTimeTicks delay) { |
136 assert(current() == this); | 139 assert(current() == this); |
(...skipping 129 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 | 269 // 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 | 270 // want the soonest eligible task to be at the head of the queue, so |
268 // run_times further in the future are considered lesser. | 271 // run_times further in the future are considered lesser. |
269 return run_time > other.run_time; | 272 return run_time > other.run_time; |
270 } | 273 } |
271 | 274 |
272 return sequence_number > other.sequence_number; | 275 return sequence_number > other.sequence_number; |
273 } | 276 } |
274 | 277 |
275 } // namespace mojo | 278 } // namespace mojo |
OLD | NEW |