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_loop/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 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/trace_event/trace_event.h" |
18 #include "third_party/libevent/event.h" | 19 #include "third_party/libevent/event.h" |
19 | 20 |
20 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
21 #include "base/mac/scoped_nsautorelease_pool.h" | 22 #include "base/mac/scoped_nsautorelease_pool.h" |
22 #endif | 23 #endif |
23 | 24 |
24 // Lifecycle of struct event | 25 // Lifecycle of struct event |
25 // Libevent uses two main data structures: | 26 // Libevent uses two main data structures: |
26 // struct event_base (of which there is one per message pump), and | 27 // struct event_base (of which there is one per message pump), and |
27 // struct event (of which there is roughly one per socket). | 28 // struct event (of which there is roughly one per socket). |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 event_base_set(event_base_, timer_event.get()); | 268 event_base_set(event_base_, timer_event.get()); |
268 event_add(timer_event.get(), &poll_tv); | 269 event_add(timer_event.get(), &poll_tv); |
269 event_base_loop(event_base_, EVLOOP_ONCE); | 270 event_base_loop(event_base_, EVLOOP_ONCE); |
270 event_del(timer_event.get()); | 271 event_del(timer_event.get()); |
271 } else { | 272 } else { |
272 // It looks like delayed_work_time_ indicates a time in the past, so we | 273 // It looks like delayed_work_time_ indicates a time in the past, so we |
273 // need to call DoDelayedWork now. | 274 // need to call DoDelayedWork now. |
274 delayed_work_time_ = TimeTicks(); | 275 delayed_work_time_ = TimeTicks(); |
275 } | 276 } |
276 } | 277 } |
| 278 |
| 279 if (!keep_running_) |
| 280 break; |
277 } | 281 } |
278 } | 282 } |
279 | 283 |
280 void MessagePumpLibevent::Quit() { | 284 void MessagePumpLibevent::Quit() { |
281 DCHECK(in_run_) << "Quit was called outside of Run!"; | 285 DCHECK(in_run_) << "Quit was called outside of Run!"; |
282 // Tell both libevent and Run that they should break out of their loops. | 286 // Tell both libevent and Run that they should break out of their loops. |
283 keep_running_ = false; | 287 keep_running_ = false; |
284 ScheduleWork(); | 288 ScheduleWork(); |
285 } | 289 } |
286 | 290 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 return false; | 338 return false; |
335 return true; | 339 return true; |
336 } | 340 } |
337 | 341 |
338 // static | 342 // static |
339 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, | 343 void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, |
340 void* context) { | 344 void* context) { |
341 WeakPtr<FileDescriptorWatcher> controller = | 345 WeakPtr<FileDescriptorWatcher> controller = |
342 static_cast<FileDescriptorWatcher*>(context)->weak_factory_.GetWeakPtr(); | 346 static_cast<FileDescriptorWatcher*>(context)->weak_factory_.GetWeakPtr(); |
343 DCHECK(controller.get()); | 347 DCHECK(controller.get()); |
| 348 TRACE_EVENT1("toplevel", "MessagePumpLibevent::OnLibeventNotification", |
| 349 "fd", fd); |
344 | 350 |
345 MessagePumpLibevent* pump = controller->pump(); | 351 MessagePumpLibevent* pump = controller->pump(); |
346 pump->processed_io_events_ = true; | 352 pump->processed_io_events_ = true; |
347 | 353 |
348 if (flags & EV_WRITE) { | 354 if (flags & EV_WRITE) { |
349 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 355 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
350 } | 356 } |
351 // Check |controller| in case it's been deleted in | 357 // Check |controller| in case it's been deleted in |
352 // controller->OnFileCanWriteWithoutBlocking(). | 358 // controller->OnFileCanWriteWithoutBlocking(). |
353 if (controller.get() && flags & EV_READ) { | 359 if (controller.get() && flags & EV_READ) { |
(...skipping 10 matching lines...) Expand all Loading... |
364 // Remove and discard the wakeup byte. | 370 // Remove and discard the wakeup byte. |
365 char buf; | 371 char buf; |
366 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 372 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
367 DCHECK_EQ(nread, 1); | 373 DCHECK_EQ(nread, 1); |
368 that->processed_io_events_ = true; | 374 that->processed_io_events_ = true; |
369 // Tell libevent to break out of inner loop. | 375 // Tell libevent to break out of inner loop. |
370 event_base_loopbreak(that->event_base_); | 376 event_base_loopbreak(that->event_base_); |
371 } | 377 } |
372 | 378 |
373 } // namespace base | 379 } // namespace base |
OLD | NEW |