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
3 // found in the LICENSE file.
5 #include "base/message_pump_libevent.h"
11 #include "base/auto_reset.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #if defined(OS_MACOSX)
15 #include "base/mac/scoped_nsautorelease_pool.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/posix/eintr_wrapper.h"
20 #include "base/time.h"
21 #include "third_party/libevent/event.h"
23 #if defined(OS_MACOSX)
24 #include "base/mac/scoped_nsautorelease_pool.h"
27 // Lifecycle of struct event
28 // Libevent uses two main data structures:
29 // struct event_base (of which there is one per message pump), and
30 // struct event (of which there is roughly one per socket).
31 // The socket's struct event is created in
32 // MessagePumpLibevent::WatchFileDescriptor(),
33 // is owned by the FileDescriptorWatcher, and is destroyed in
34 // StopWatchingFileDescriptor().
35 // It is moved into and out of lists in struct event_base by
36 // the libevent functions event_add() and event_del().
39 // At the moment bad things happen if a FileDescriptorWatcher
40 // is active after its MessagePumpLibevent has been destroyed.
41 // See MessageLoopTest.FileDescriptorWatcherOutlivesMessageLoop
42 // Not clear yet whether that situation occurs in practice,
43 // but if it does, we need to fix it.
47 // Return 0 on success
48 // Too small a function to bother putting in a library?
49 static int SetNonBlocking(int fd
) {
50 int flags
= fcntl(fd
, F_GETFL
, 0);
53 return fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
);
56 MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher()
60 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
63 MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() {
65 StopWatchingFileDescriptor();
69 bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() {
70 event
* e
= ReleaseEvent();
74 // event_del() is a no-op if the event isn't active.
75 int rv
= event_del(e
);
82 void MessagePumpLibevent::FileDescriptorWatcher::Init(event
*e
) {
89 event
*MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() {
90 struct event
*e
= event_
;
95 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking(
96 int fd
, MessagePumpLibevent
* pump
) {
97 // Since OnFileCanWriteWithoutBlocking() gets called first, it can stop
98 // watching the file descriptor.
101 pump
->WillProcessIOEvent();
102 watcher_
->OnFileCanReadWithoutBlocking(fd
);
103 pump
->DidProcessIOEvent();
106 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking(
107 int fd
, MessagePumpLibevent
* pump
) {
109 pump
->WillProcessIOEvent();
110 watcher_
->OnFileCanWriteWithoutBlocking(fd
);
111 pump
->DidProcessIOEvent();
114 MessagePumpLibevent::MessagePumpLibevent()
115 : keep_running_(true),
117 processed_io_events_(false),
118 event_base_(event_base_new()),
120 wakeup_pipe_out_(-1) {
125 MessagePumpLibevent::~MessagePumpLibevent() {
126 DCHECK(wakeup_event_
);
128 event_del(wakeup_event_
);
129 delete wakeup_event_
;
130 if (wakeup_pipe_in_
>= 0) {
131 if (HANDLE_EINTR(close(wakeup_pipe_in_
)) < 0)
132 DPLOG(ERROR
) << "close";
134 if (wakeup_pipe_out_
>= 0) {
135 if (HANDLE_EINTR(close(wakeup_pipe_out_
)) < 0)
136 DPLOG(ERROR
) << "close";
138 event_base_free(event_base_
);
141 bool MessagePumpLibevent::WatchFileDescriptor(int fd
,
144 FileDescriptorWatcher
*controller
,
149 DCHECK(mode
== WATCH_READ
|| mode
== WATCH_WRITE
|| mode
== WATCH_READ_WRITE
);
150 // WatchFileDescriptor should be called on the pump thread. It is not
151 // threadsafe, and your watcher may never be registered.
152 DCHECK(watch_file_descriptor_caller_checker_
.CalledOnValidThread());
154 int event_mask
= persistent
? EV_PERSIST
: 0;
155 if (mode
& WATCH_READ
) {
156 event_mask
|= EV_READ
;
158 if (mode
& WATCH_WRITE
) {
159 event_mask
|= EV_WRITE
;
162 scoped_ptr
<event
> evt(controller
->ReleaseEvent());
163 if (evt
.get() == NULL
) {
164 // Ownership is transferred to the controller.
165 evt
.reset(new event
);
167 // Make sure we don't pick up any funky internal libevent masks.
168 int old_interest_mask
= evt
.get()->ev_events
&
169 (EV_READ
| EV_WRITE
| EV_PERSIST
);
171 // Combine old/new event masks.
172 event_mask
|= old_interest_mask
;
174 // Must disarm the event before we can reuse it.
175 event_del(evt
.get());
177 // It's illegal to use this function to listen on 2 separate fds with the
178 // same |controller|.
179 if (EVENT_FD(evt
.get()) != fd
) {
180 NOTREACHED() << "FDs don't match" << EVENT_FD(evt
.get()) << "!=" << fd
;
185 // Set current interest mask and message pump for this event.
186 event_set(evt
.get(), fd
, event_mask
, OnLibeventNotification
, controller
);
188 // Tell libevent which message pump this socket will belong to when we add it.
189 if (event_base_set(event_base_
, evt
.get())) {
193 // Add this socket to the list of monitored sockets.
194 if (event_add(evt
.get(), NULL
)) {
198 // Transfer ownership of evt to controller.
199 controller
->Init(evt
.release());
201 controller
->set_watcher(delegate
);
202 controller
->set_pump(this);
207 void MessagePumpLibevent::AddIOObserver(IOObserver
*obs
) {
208 io_observers_
.AddObserver(obs
);
211 void MessagePumpLibevent::RemoveIOObserver(IOObserver
*obs
) {
212 io_observers_
.RemoveObserver(obs
);
215 // Tell libevent to break out of inner loop.
216 static void timer_callback(int fd
, short events
, void *context
)
218 event_base_loopbreak((struct event_base
*)context
);
222 void MessagePumpLibevent::Run(Delegate
* delegate
) {
223 DCHECK(keep_running_
) << "Quit must have been called outside of Run!";
224 base::AutoReset
<bool> auto_reset_in_run(&in_run_
, true);
226 // 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().
228 scoped_ptr
<event
> timer_event(new event
);
231 #if defined(OS_MACOSX)
232 mac::ScopedNSAutoreleasePool autorelease_pool
;
235 bool did_work
= delegate
->DoWork();
239 event_base_loop(event_base_
, EVLOOP_NONBLOCK
);
240 did_work
|= processed_io_events_
;
241 processed_io_events_
= false;
245 did_work
|= delegate
->DoDelayedWork(&delayed_work_time_
);
252 did_work
= delegate
->DoIdleWork();
259 // EVLOOP_ONCE tells libevent to only block once,
260 // but to service all pending events when it wakes up.
261 if (delayed_work_time_
.is_null()) {
262 event_base_loop(event_base_
, EVLOOP_ONCE
);
264 TimeDelta delay
= delayed_work_time_
- TimeTicks::Now();
265 if (delay
> TimeDelta()) {
266 struct timeval poll_tv
;
267 poll_tv
.tv_sec
= delay
.InSeconds();
268 poll_tv
.tv_usec
= delay
.InMicroseconds() % Time::kMicrosecondsPerSecond
;
269 event_set(timer_event
.get(), -1, 0, timer_callback
, event_base_
);
270 event_base_set(event_base_
, timer_event
.get());
271 event_add(timer_event
.get(), &poll_tv
);
272 event_base_loop(event_base_
, EVLOOP_ONCE
);
273 event_del(timer_event
.get());
275 // It looks like delayed_work_time_ indicates a time in the past, so we
276 // need to call DoDelayedWork now.
277 delayed_work_time_
= TimeTicks();
282 keep_running_
= true;
285 void MessagePumpLibevent::Quit() {
287 // Tell both libevent and Run that they should break out of their loops.
288 keep_running_
= false;
292 void MessagePumpLibevent::ScheduleWork() {
293 // Tell libevent (in a threadsafe way) that it should break out of its loop.
295 int nwrite
= HANDLE_EINTR(write(wakeup_pipe_in_
, &buf
, 1));
296 DCHECK(nwrite
== 1 || errno
== EAGAIN
)
297 << "[nwrite:" << nwrite
<< "] [errno:" << errno
<< "]";
300 void MessagePumpLibevent::ScheduleDelayedWork(
301 const TimeTicks
& delayed_work_time
) {
302 // We know that we can't be blocked on Wait right now since this method can
303 // only be called on the same thread as Run, so we only need to update our
304 // record of how long to sleep when we do sleep.
305 delayed_work_time_
= delayed_work_time
;
308 void MessagePumpLibevent::WillProcessIOEvent() {
309 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, WillProcessIOEvent());
312 void MessagePumpLibevent::DidProcessIOEvent() {
313 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, DidProcessIOEvent());
316 bool MessagePumpLibevent::Init() {
319 DLOG(ERROR
) << "pipe() failed, errno: " << errno
;
322 if (SetNonBlocking(fds
[0])) {
323 DLOG(ERROR
) << "SetNonBlocking for pipe fd[0] failed, errno: " << errno
;
326 if (SetNonBlocking(fds
[1])) {
327 DLOG(ERROR
) << "SetNonBlocking for pipe fd[1] failed, errno: " << errno
;
330 wakeup_pipe_out_
= fds
[0];
331 wakeup_pipe_in_
= fds
[1];
333 wakeup_event_
= new event
;
334 event_set(wakeup_event_
, wakeup_pipe_out_
, EV_READ
| EV_PERSIST
,
336 event_base_set(event_base_
, wakeup_event_
);
338 if (event_add(wakeup_event_
, 0))
344 void MessagePumpLibevent::OnLibeventNotification(int fd
, short flags
,
346 base::WeakPtr
<FileDescriptorWatcher
> controller
=
347 static_cast<FileDescriptorWatcher
*>(context
)->weak_factory_
.GetWeakPtr();
348 DCHECK(controller
.get());
350 MessagePumpLibevent
* pump
= controller
->pump();
351 pump
->processed_io_events_
= true;
353 if (flags
& EV_WRITE
) {
354 controller
->OnFileCanWriteWithoutBlocking(fd
, pump
);
356 // Check |controller| in case it's been deleted in
357 // controller->OnFileCanWriteWithoutBlocking().
358 if (controller
.get() && flags
& EV_READ
) {
359 controller
->OnFileCanReadWithoutBlocking(fd
, pump
);
363 // Called if a byte is received on the wakeup pipe.
365 void MessagePumpLibevent::OnWakeup(int socket
, short flags
, void* context
) {
366 base::MessagePumpLibevent
* that
=
367 static_cast<base::MessagePumpLibevent
*>(context
);
368 DCHECK(that
->wakeup_pipe_out_
== socket
);
370 // Remove and discard the wakeup byte.
372 int nread
= HANDLE_EINTR(read(socket
, &buf
, 1));
374 that
->processed_io_events_
= true;
375 // Tell libevent to break out of inner loop.
376 event_base_loopbreak(that
->event_base_
);