Chromium Code Reviews| 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 if (!keep_running_) | |
|
Mark Mentovai
2011/06/30 22:07:16
I was wondering about this. I should have spoken u
| |
| 234 break; | |
| 235 | |
| 229 did_work |= delegate->DoDelayedWork(&delayed_work_time_); | 236 did_work |= delegate->DoDelayedWork(&delayed_work_time_); |
| 230 if (!keep_running_) | 237 if (!keep_running_) |
| 231 break; | 238 break; |
| 232 | 239 |
| 233 if (did_work) | 240 if (did_work) |
| 234 continue; | 241 continue; |
| 235 | 242 |
| 236 did_work = delegate->DoIdleWork(); | 243 did_work = delegate->DoIdleWork(); |
| 237 if (!keep_running_) | 244 if (!keep_running_) |
| 238 break; | 245 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. | 295 // record of how long to sleep when we do sleep. |
| 289 delayed_work_time_ = delayed_work_time; | 296 delayed_work_time_ = delayed_work_time; |
| 290 } | 297 } |
| 291 | 298 |
| 292 void MessagePumpLibevent::WillProcessIOEvent() { | 299 void MessagePumpLibevent::WillProcessIOEvent() { |
| 293 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 300 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 294 } | 301 } |
| 295 | 302 |
| 296 void MessagePumpLibevent::DidProcessIOEvent() { | 303 void MessagePumpLibevent::DidProcessIOEvent() { |
| 297 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 304 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 305 processed_io_events_ = true; | |
| 298 } | 306 } |
| 299 | 307 |
| 300 bool MessagePumpLibevent::Init() { | 308 bool MessagePumpLibevent::Init() { |
| 301 int fds[2]; | 309 int fds[2]; |
| 302 if (pipe(fds)) { | 310 if (pipe(fds)) { |
| 303 DLOG(ERROR) << "pipe() failed, errno: " << errno; | 311 DLOG(ERROR) << "pipe() failed, errno: " << errno; |
| 304 return false; | 312 return false; |
| 305 } | 313 } |
| 306 if (SetNonBlocking(fds[0])) { | 314 if (SetNonBlocking(fds[0])) { |
| 307 DLOG(ERROR) << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 315 DLOG(ERROR) << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 // static | 352 // static |
| 345 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { | 353 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { |
| 346 base::MessagePumpLibevent* that = | 354 base::MessagePumpLibevent* that = |
| 347 static_cast<base::MessagePumpLibevent*>(context); | 355 static_cast<base::MessagePumpLibevent*>(context); |
| 348 DCHECK(that->wakeup_pipe_out_ == socket); | 356 DCHECK(that->wakeup_pipe_out_ == socket); |
| 349 | 357 |
| 350 // Remove and discard the wakeup byte. | 358 // Remove and discard the wakeup byte. |
| 351 char buf; | 359 char buf; |
| 352 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 360 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
| 353 DCHECK_EQ(nread, 1); | 361 DCHECK_EQ(nread, 1); |
| 362 that->processed_io_events_ = true; | |
| 354 // Tell libevent to break out of inner loop. | 363 // Tell libevent to break out of inner loop. |
| 355 event_base_loopbreak(that->event_base_); | 364 event_base_loopbreak(that->event_base_); |
| 356 } | 365 } |
| 357 | 366 |
| 358 } // namespace base | 367 } // namespace base |
| OLD | NEW |