OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_pump_libevent.h" | 5 #include "base/message_pump_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( | 100 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( |
101 int fd, MessagePumpLibevent* pump) { | 101 int fd, MessagePumpLibevent* pump) { |
102 pump->WillProcessIOEvent(); | 102 pump->WillProcessIOEvent(); |
103 watcher_->OnFileCanWriteWithoutBlocking(fd); | 103 watcher_->OnFileCanWriteWithoutBlocking(fd); |
104 pump->DidProcessIOEvent(); | 104 pump->DidProcessIOEvent(); |
105 } | 105 } |
106 | 106 |
107 MessagePumpLibevent::MessagePumpLibevent() | 107 MessagePumpLibevent::MessagePumpLibevent() |
108 : keep_running_(true), | 108 : keep_running_(true), |
109 in_run_(false), | 109 in_run_(false), |
| 110 processed_io_events_(false), |
110 event_base_(event_base_new()), | 111 event_base_(event_base_new()), |
111 wakeup_pipe_in_(-1), | 112 wakeup_pipe_in_(-1), |
112 wakeup_pipe_out_(-1) { | 113 wakeup_pipe_out_(-1) { |
113 if (!Init()) | 114 if (!Init()) |
114 NOTREACHED(); | 115 NOTREACHED(); |
115 } | 116 } |
116 | 117 |
117 MessagePumpLibevent::~MessagePumpLibevent() { | 118 MessagePumpLibevent::~MessagePumpLibevent() { |
118 DCHECK(wakeup_event_); | 119 DCHECK(wakeup_event_); |
119 DCHECK(event_base_); | 120 DCHECK(event_base_); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // Instead, make our own timer and reuse it on each call to event_base_loop(). | 220 // Instead, make our own timer and reuse it on each call to event_base_loop(). |
220 scoped_ptr<event> timer_event(new event); | 221 scoped_ptr<event> timer_event(new event); |
221 | 222 |
222 for (;;) { | 223 for (;;) { |
223 mac::ScopedNSAutoreleasePool autorelease_pool; | 224 mac::ScopedNSAutoreleasePool autorelease_pool; |
224 | 225 |
225 bool did_work = delegate->DoWork(); | 226 bool did_work = delegate->DoWork(); |
226 if (!keep_running_) | 227 if (!keep_running_) |
227 break; | 228 break; |
228 | 229 |
| 230 event_base_loop(event_base_, EVLOOP_NONBLOCK); |
| 231 did_work |= processed_io_events_; |
| 232 processed_io_events_ = false; |
| 233 |
229 did_work |= delegate->DoDelayedWork(&delayed_work_time_); | 234 did_work |= delegate->DoDelayedWork(&delayed_work_time_); |
230 if (!keep_running_) | 235 if (!keep_running_) |
231 break; | 236 break; |
232 | 237 |
233 if (did_work) | 238 if (did_work) |
234 continue; | 239 continue; |
235 | 240 |
236 did_work = delegate->DoIdleWork(); | 241 did_work = delegate->DoIdleWork(); |
237 if (!keep_running_) | 242 if (!keep_running_) |
238 break; | 243 break; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // record of how long to sleep when we do sleep. | 293 // record of how long to sleep when we do sleep. |
289 delayed_work_time_ = delayed_work_time; | 294 delayed_work_time_ = delayed_work_time; |
290 } | 295 } |
291 | 296 |
292 void MessagePumpLibevent::WillProcessIOEvent() { | 297 void MessagePumpLibevent::WillProcessIOEvent() { |
293 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 298 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
294 } | 299 } |
295 | 300 |
296 void MessagePumpLibevent::DidProcessIOEvent() { | 301 void MessagePumpLibevent::DidProcessIOEvent() { |
297 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 302 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 303 processed_io_events_ = true; |
298 } | 304 } |
299 | 305 |
300 bool MessagePumpLibevent::Init() { | 306 bool MessagePumpLibevent::Init() { |
301 int fds[2]; | 307 int fds[2]; |
302 if (pipe(fds)) { | 308 if (pipe(fds)) { |
303 DLOG(ERROR) << "pipe() failed, errno: " << errno; | 309 DLOG(ERROR) << "pipe() failed, errno: " << errno; |
304 return false; | 310 return false; |
305 } | 311 } |
306 if (SetNonBlocking(fds[0])) { | 312 if (SetNonBlocking(fds[0])) { |
307 DLOG(ERROR) << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 313 DLOG(ERROR) << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 355 |
350 // Remove and discard the wakeup byte. | 356 // Remove and discard the wakeup byte. |
351 char buf; | 357 char buf; |
352 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 358 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
353 DCHECK_EQ(nread, 1); | 359 DCHECK_EQ(nread, 1); |
354 // Tell libevent to break out of inner loop. | 360 // Tell libevent to break out of inner loop. |
355 event_base_loopbreak(that->event_base_); | 361 event_base_loopbreak(that->event_base_); |
356 } | 362 } |
357 | 363 |
358 } // namespace base | 364 } // namespace base |
OLD | NEW |