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_) |
| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 return true; | 331 return true; |
325 } | 332 } |
326 | 333 |
327 // static | 334 // static |
328 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, | 335 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, |
329 void* context) { | 336 void* context) { |
330 FileDescriptorWatcher* controller = | 337 FileDescriptorWatcher* controller = |
331 static_cast<FileDescriptorWatcher*>(context); | 338 static_cast<FileDescriptorWatcher*>(context); |
332 | 339 |
333 MessagePumpLibevent* pump = controller->pump(); | 340 MessagePumpLibevent* pump = controller->pump(); |
| 341 pump->processed_io_events_ = true; |
334 | 342 |
335 if (flags & EV_WRITE) { | 343 if (flags & EV_WRITE) { |
336 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 344 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
337 } | 345 } |
338 if (flags & EV_READ) { | 346 if (flags & EV_READ) { |
339 controller->OnFileCanReadWithoutBlocking(fd, pump); | 347 controller->OnFileCanReadWithoutBlocking(fd, pump); |
340 } | 348 } |
341 } | 349 } |
342 | 350 |
343 // Called if a byte is received on the wakeup pipe. | 351 // Called if a byte is received on the wakeup pipe. |
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 |