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
3 // found in the LICENSE file.
5 #include "mojo/public/utility/run_loop.h"
12 #include "mojo/public/utility/lib/thread_local.h"
13 #include "mojo/public/utility/run_loop_handler.h"
18 internal::ThreadLocalPointer<RunLoop> current_run_loop;
20 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0);
24 // State needed for one iteration of WaitMany().
25 struct RunLoop::WaitState {
26 WaitState() : deadline(MOJO_DEADLINE_INDEFINITE) {}
28 std::vector<Handle> handles;
29 std::vector<MojoWaitFlags> wait_flags;
30 MojoDeadline deadline;
33 struct RunLoop::RunState {
34 RunState() : should_quit(false) {}
39 RunLoop::RunLoop() : run_state_(NULL), next_handler_id_(0) {
41 current_run_loop.Set(this);
45 assert(current() == this);
46 current_run_loop.Set(NULL);
50 void RunLoop::SetUp() {
51 current_run_loop.Allocate();
55 void RunLoop::TearDown() {
57 current_run_loop.Free();
61 RunLoop* RunLoop::current() {
62 return current_run_loop.Get();
65 void RunLoop::AddHandler(RunLoopHandler* handler,
67 MojoWaitFlags wait_flags,
68 MojoDeadline deadline) {
69 assert(current() == this);
71 assert(handle.is_valid());
72 // Assume it's an error if someone tries to reregister an existing handle.
73 assert(0u == handler_data_.count(handle));
74 HandlerData handler_data;
75 handler_data.handler = handler;
76 handler_data.wait_flags = wait_flags;
77 handler_data.deadline = (deadline == MOJO_DEADLINE_INDEFINITE) ?
79 GetTimeTicksNow() + static_cast<MojoTimeTicks>(deadline);
80 handler_data.id = next_handler_id_++;
81 handler_data_[handle] = handler_data;
84 void RunLoop::RemoveHandler(const Handle& handle) {
85 assert(current() == this);
86 handler_data_.erase(handle);
89 bool RunLoop::HasHandler(const Handle& handle) const {
90 return handler_data_.find(handle) != handler_data_.end();
94 assert(current() == this);
95 // We don't currently support nesting.
97 RunState* old_state = run_state_;
99 run_state_ = &run_state;
100 while (!run_state.should_quit)
102 run_state_ = old_state;
105 void RunLoop::RunUntilIdle() {
106 assert(current() == this);
107 // We don't currently support nesting.
109 RunState* old_state = run_state_;
111 run_state_ = &run_state;
112 while (!run_state.should_quit) {
116 run_state_ = old_state;
119 void RunLoop::Quit() {
120 assert(current() == this);
122 run_state_->should_quit = true;
125 bool RunLoop::Wait(bool non_blocking) {
126 const WaitState wait_state = GetWaitState(non_blocking);
127 if (wait_state.handles.empty()) {
132 const MojoResult result =
133 WaitMany(wait_state.handles, wait_state.wait_flags, wait_state.deadline);
135 const size_t index = static_cast<size_t>(result);
136 assert(handler_data_.find(wait_state.handles[index]) !=
137 handler_data_.end());
138 handler_data_[wait_state.handles[index]].handler->OnHandleReady(
139 wait_state.handles[index]);
144 case MOJO_RESULT_INVALID_ARGUMENT:
145 case MOJO_RESULT_FAILED_PRECONDITION:
146 return RemoveFirstInvalidHandle(wait_state);
147 case MOJO_RESULT_DEADLINE_EXCEEDED:
148 return NotifyDeadlineExceeded();
155 bool RunLoop::NotifyDeadlineExceeded() {
156 bool notified = false;
158 // Make a copy in case someone tries to add/remove new handlers as part of
160 const HandleToHandlerData cloned_handlers(handler_data_);
161 const MojoTimeTicks now(GetTimeTicksNow());
162 for (HandleToHandlerData::const_iterator i = cloned_handlers.begin();
163 i != cloned_handlers.end(); ++i) {
164 // Since we're iterating over a clone of the handlers, verify the handler is
165 // still valid before notifying.
166 if (i->second.deadline != kInvalidTimeTicks &&
167 i->second.deadline < now &&
168 handler_data_.find(i->first) != handler_data_.end() &&
169 handler_data_[i->first].id == i->second.id) {
170 handler_data_.erase(i->first);
171 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED);
179 bool RunLoop::RemoveFirstInvalidHandle(const WaitState& wait_state) {
180 for (size_t i = 0; i < wait_state.handles.size(); ++i) {
181 const MojoResult result =
182 mojo::Wait(wait_state.handles[i], wait_state.wait_flags[i],
183 static_cast<MojoDeadline>(0));
184 if (result == MOJO_RESULT_INVALID_ARGUMENT ||
185 result == MOJO_RESULT_FAILED_PRECONDITION) {
186 // Remove the handle first, this way if OnHandleError() tries to remove
187 // the handle our iterator isn't invalidated.
188 assert(handler_data_.find(wait_state.handles[i]) != handler_data_.end());
189 RunLoopHandler* handler =
190 handler_data_[wait_state.handles[i]].handler;
191 handler_data_.erase(wait_state.handles[i]);
192 handler->OnHandleError(wait_state.handles[i], result);
195 assert(MOJO_RESULT_DEADLINE_EXCEEDED == result);
200 RunLoop::WaitState RunLoop::GetWaitState(bool non_blocking) const {
201 WaitState wait_state;
202 MojoTimeTicks min_time = kInvalidTimeTicks;
203 for (HandleToHandlerData::const_iterator i = handler_data_.begin();
204 i != handler_data_.end(); ++i) {
205 wait_state.handles.push_back(i->first);
206 wait_state.wait_flags.push_back(i->second.wait_flags);
207 if (!non_blocking && i->second.deadline != kInvalidTimeTicks &&
208 (min_time == kInvalidTimeTicks || i->second.deadline < min_time)) {
209 min_time = i->second.deadline;
213 wait_state.deadline = static_cast<MojoDeadline>(0);
214 } else if (min_time != kInvalidTimeTicks) {
215 const MojoTimeTicks now = GetTimeTicksNow();
217 wait_state.deadline = static_cast<MojoDeadline>(0);
219 wait_state.deadline = static_cast<MojoDeadline>(min_time - now);