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" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/mac/scoped_nsautorelease_pool.h" | |
15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
16 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
17 #include "base/time.h" | 16 #include "base/time.h" |
18 #if defined(USE_SYSTEM_LIBEVENT) | 17 #if defined(USE_SYSTEM_LIBEVENT) |
19 #include <event.h> | 18 #include <event.h> |
20 #else | 19 #else |
21 #include "third_party/libevent/event.h" | 20 #include "third_party/libevent/event.h" |
22 #endif | 21 #endif |
23 | 22 |
| 23 #if defined(OS_MACOSX) |
| 24 #include "base/mac/scoped_nsautorelease_pool.h" |
| 25 #endif |
| 26 |
24 // Lifecycle of struct event | 27 // Lifecycle of struct event |
25 // Libevent uses two main data structures: | 28 // Libevent uses two main data structures: |
26 // struct event_base (of which there is one per message pump), and | 29 // struct event_base (of which there is one per message pump), and |
27 // struct event (of which there is roughly one per socket). | 30 // struct event (of which there is roughly one per socket). |
28 // The socket's struct event is created in | 31 // The socket's struct event is created in |
29 // MessagePumpLibevent::WatchFileDescriptor(), | 32 // MessagePumpLibevent::WatchFileDescriptor(), |
30 // is owned by the FileDescriptorWatcher, and is destroyed in | 33 // is owned by the FileDescriptorWatcher, and is destroyed in |
31 // StopWatchingFileDescriptor(). | 34 // StopWatchingFileDescriptor(). |
32 // It is moved into and out of lists in struct event_base by | 35 // It is moved into and out of lists in struct event_base by |
33 // the libevent functions event_add() and event_del(). | 36 // the libevent functions event_add() and event_del(). |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 // Reentrant! | 224 // Reentrant! |
222 void MessagePumpLibevent::Run(Delegate* delegate) { | 225 void MessagePumpLibevent::Run(Delegate* delegate) { |
223 DCHECK(keep_running_) << "Quit must have been called outside of Run!"; | 226 DCHECK(keep_running_) << "Quit must have been called outside of Run!"; |
224 AutoReset<bool> auto_reset_in_run(&in_run_, true); | 227 AutoReset<bool> auto_reset_in_run(&in_run_, true); |
225 | 228 |
226 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. | 229 // 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(). | 230 // Instead, make our own timer and reuse it on each call to event_base_loop(). |
228 scoped_ptr<event> timer_event(new event); | 231 scoped_ptr<event> timer_event(new event); |
229 | 232 |
230 for (;;) { | 233 for (;;) { |
| 234 #if defined(OS_MACOSX) |
231 mac::ScopedNSAutoreleasePool autorelease_pool; | 235 mac::ScopedNSAutoreleasePool autorelease_pool; |
| 236 #endif |
232 | 237 |
233 bool did_work = delegate->DoWork(); | 238 bool did_work = delegate->DoWork(); |
234 if (!keep_running_) | 239 if (!keep_running_) |
235 break; | 240 break; |
236 | 241 |
237 event_base_loop(event_base_, EVLOOP_NONBLOCK); | 242 event_base_loop(event_base_, EVLOOP_NONBLOCK); |
238 did_work |= processed_io_events_; | 243 did_work |= processed_io_events_; |
239 processed_io_events_ = false; | 244 processed_io_events_ = false; |
240 if (!keep_running_) | 245 if (!keep_running_) |
241 break; | 246 break; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 // Remove and discard the wakeup byte. | 373 // Remove and discard the wakeup byte. |
369 char buf; | 374 char buf; |
370 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 375 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
371 DCHECK_EQ(nread, 1); | 376 DCHECK_EQ(nread, 1); |
372 that->processed_io_events_ = true; | 377 that->processed_io_events_ = true; |
373 // Tell libevent to break out of inner loop. | 378 // Tell libevent to break out of inner loop. |
374 event_base_loopbreak(that->event_base_); | 379 event_base_loopbreak(that->event_base_); |
375 } | 380 } |
376 | 381 |
377 } // namespace base | 382 } // namespace base |
OLD | NEW |