| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_loop/message_pump_libevent.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #if defined(OS_MACOSX) | |
| 15 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 16 #endif | |
| 17 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/time.h" | 17 #include "base/time.h" |
| 21 #include "third_party/libevent/event.h" | 18 #include "third_party/libevent/event.h" |
| 22 | 19 |
| 23 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
| 24 #include "base/mac/scoped_nsautorelease_pool.h" | 21 #include "base/mac/scoped_nsautorelease_pool.h" |
| 25 #endif | 22 #endif |
| 26 | 23 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 211 |
| 215 // Tell libevent to break out of inner loop. | 212 // Tell libevent to break out of inner loop. |
| 216 static void timer_callback(int fd, short events, void *context) | 213 static void timer_callback(int fd, short events, void *context) |
| 217 { | 214 { |
| 218 event_base_loopbreak((struct event_base *)context); | 215 event_base_loopbreak((struct event_base *)context); |
| 219 } | 216 } |
| 220 | 217 |
| 221 // Reentrant! | 218 // Reentrant! |
| 222 void MessagePumpLibevent::Run(Delegate* delegate) { | 219 void MessagePumpLibevent::Run(Delegate* delegate) { |
| 223 DCHECK(keep_running_) << "Quit must have been called outside of Run!"; | 220 DCHECK(keep_running_) << "Quit must have been called outside of Run!"; |
| 224 base::AutoReset<bool> auto_reset_in_run(&in_run_, true); | 221 AutoReset<bool> auto_reset_in_run(&in_run_, true); |
| 225 | 222 |
| 226 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. | 223 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. |
| 227 // Instead, make our own timer and reuse it on each call to event_base_loop(). | 224 // Instead, make our own timer and reuse it on each call to event_base_loop(). |
| 228 scoped_ptr<event> timer_event(new event); | 225 scoped_ptr<event> timer_event(new event); |
| 229 | 226 |
| 230 for (;;) { | 227 for (;;) { |
| 231 #if defined(OS_MACOSX) | 228 #if defined(OS_MACOSX) |
| 232 mac::ScopedNSAutoreleasePool autorelease_pool; | 229 mac::ScopedNSAutoreleasePool autorelease_pool; |
| 233 #endif | 230 #endif |
| 234 | 231 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 event_base_set(event_base_, wakeup_event_); | 333 event_base_set(event_base_, wakeup_event_); |
| 337 | 334 |
| 338 if (event_add(wakeup_event_, 0)) | 335 if (event_add(wakeup_event_, 0)) |
| 339 return false; | 336 return false; |
| 340 return true; | 337 return true; |
| 341 } | 338 } |
| 342 | 339 |
| 343 // static | 340 // static |
| 344 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, | 341 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, |
| 345 void* context) { | 342 void* context) { |
| 346 base::WeakPtr<FileDescriptorWatcher> controller = | 343 WeakPtr<FileDescriptorWatcher> controller = |
| 347 static_cast<FileDescriptorWatcher*>(context)->weak_factory_.GetWeakPtr(); | 344 static_cast<FileDescriptorWatcher*>(context)->weak_factory_.GetWeakPtr(); |
| 348 DCHECK(controller.get()); | 345 DCHECK(controller.get()); |
| 349 | 346 |
| 350 MessagePumpLibevent* pump = controller->pump(); | 347 MessagePumpLibevent* pump = controller->pump(); |
| 351 pump->processed_io_events_ = true; | 348 pump->processed_io_events_ = true; |
| 352 | 349 |
| 353 if (flags & EV_WRITE) { | 350 if (flags & EV_WRITE) { |
| 354 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 351 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
| 355 } | 352 } |
| 356 // Check |controller| in case it's been deleted in | 353 // Check |controller| in case it's been deleted in |
| 357 // controller->OnFileCanWriteWithoutBlocking(). | 354 // controller->OnFileCanWriteWithoutBlocking(). |
| 358 if (controller.get() && flags & EV_READ) { | 355 if (controller.get() && flags & EV_READ) { |
| 359 controller->OnFileCanReadWithoutBlocking(fd, pump); | 356 controller->OnFileCanReadWithoutBlocking(fd, pump); |
| 360 } | 357 } |
| 361 } | 358 } |
| 362 | 359 |
| 363 // Called if a byte is received on the wakeup pipe. | 360 // Called if a byte is received on the wakeup pipe. |
| 364 // static | 361 // static |
| 365 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { | 362 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { |
| 366 base::MessagePumpLibevent* that = | 363 MessagePumpLibevent* that = static_cast<MessagePumpLibevent*>(context); |
| 367 static_cast<base::MessagePumpLibevent*>(context); | |
| 368 DCHECK(that->wakeup_pipe_out_ == socket); | 364 DCHECK(that->wakeup_pipe_out_ == socket); |
| 369 | 365 |
| 370 // Remove and discard the wakeup byte. | 366 // Remove and discard the wakeup byte. |
| 371 char buf; | 367 char buf; |
| 372 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 368 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
| 373 DCHECK_EQ(nread, 1); | 369 DCHECK_EQ(nread, 1); |
| 374 that->processed_io_events_ = true; | 370 that->processed_io_events_ = true; |
| 375 // Tell libevent to break out of inner loop. | 371 // Tell libevent to break out of inner loop. |
| 376 event_base_loopbreak(that->event_base_); | 372 event_base_loopbreak(that->event_base_); |
| 377 } | 373 } |
| 378 | 374 |
| 379 } // namespace base | 375 } // namespace base |
| OLD | NEW |