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 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_
6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_pump.h"
12 #include "base/observer_list.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time.h"
16 // Declare structs we need from libevent.h rather than including it
22 // Class to monitor sockets and issue callbacks when sockets are ready for I/O
23 // TODO(dkegel): add support for background file IO somehow
24 class BASE_EXPORT MessagePumpLibevent
: public MessagePump
{
30 // An IOObserver is an object that receives IO notifications from the
33 // NOTE: An IOObserver implementation should be extremely fast!
34 virtual void WillProcessIOEvent() = 0;
35 virtual void DidProcessIOEvent() = 0;
38 virtual ~IOObserver() {}
41 class FileDescriptorWatcher
;
43 // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
44 // of a file descriptor.
47 // Called from MessageLoop::Run when an FD can be read from/written to
49 virtual void OnFileCanReadWithoutBlocking(int fd
) = 0;
50 virtual void OnFileCanWriteWithoutBlocking(int fd
) = 0;
56 // Object returned by WatchFileDescriptor to manage further watching.
57 class FileDescriptorWatcher
{
59 FileDescriptorWatcher();
60 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor.
62 // NOTE: These methods aren't called StartWatching()/StopWatching() to
63 // avoid confusion with the win32 ObjectWatcher class.
65 // Stop watching the FD, always safe to call. No-op if there's nothing
67 bool StopWatchingFileDescriptor();
70 friend class MessagePumpLibevent
;
71 friend class MessagePumpLibeventTest
;
73 // Called by MessagePumpLibevent, ownership of |e| is transferred to this
77 // Used by MessagePumpLibevent to take ownership of event_.
78 event
* ReleaseEvent();
80 void set_pump(MessagePumpLibevent
* pump
) { pump_
= pump
; }
81 MessagePumpLibevent
* pump() { return pump_
; }
83 void set_watcher(Watcher
* watcher
) { watcher_
= watcher
; }
85 void OnFileCanReadWithoutBlocking(int fd
, MessagePumpLibevent
* pump
);
86 void OnFileCanWriteWithoutBlocking(int fd
, MessagePumpLibevent
* pump
);
89 MessagePumpLibevent
* pump_
;
91 base::WeakPtrFactory
<FileDescriptorWatcher
> weak_factory_
;
93 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher
);
99 WATCH_READ_WRITE
= WATCH_READ
| WATCH_WRITE
102 MessagePumpLibevent();
104 // Have the current thread's message loop watch for a a situation in which
105 // reading/writing to the FD can be performed without blocking.
106 // Callers must provide a preallocated FileDescriptorWatcher object which
107 // can later be used to manage the lifetime of this event.
108 // If a FileDescriptorWatcher is passed in which is already attached to
109 // an event, then the effect is cumulative i.e. after the call |controller|
110 // will watch both the previous event and the new one.
111 // If an error occurs while calling this method in a cumulative fashion, the
112 // event previously attached to |controller| is aborted.
113 // Returns true on success.
114 // Must be called on the same thread the message_pump is running on.
115 // TODO(dkegel): switch to edge-triggered readiness notification
116 bool WatchFileDescriptor(int fd
,
119 FileDescriptorWatcher
*controller
,
122 void AddIOObserver(IOObserver
* obs
);
123 void RemoveIOObserver(IOObserver
* obs
);
125 // MessagePump methods:
126 virtual void Run(Delegate
* delegate
) OVERRIDE
;
127 virtual void Quit() OVERRIDE
;
128 virtual void ScheduleWork() OVERRIDE
;
129 virtual void ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) OVERRIDE
;
132 virtual ~MessagePumpLibevent();
135 friend class MessagePumpLibeventTest
;
137 void WillProcessIOEvent();
138 void DidProcessIOEvent();
140 // Risky part of constructor. Returns true on success.
143 // Called by libevent to tell us a registered FD can be read/written to.
144 static void OnLibeventNotification(int fd
, short flags
,
147 // Unix pipe used to implement ScheduleWork()
148 // ... callback; called by libevent inside Run() when pipe is ready to read
149 static void OnWakeup(int socket
, short flags
, void* context
);
151 // This flag is set to false when Run should return.
154 // This flag is set when inside Run.
157 // This flag is set if libevent has processed I/O events.
158 bool processed_io_events_
;
160 // The time at which we should call DoDelayedWork.
161 TimeTicks delayed_work_time_
;
163 // Libevent dispatcher. Watches all sockets registered with it, and sends
164 // readiness callbacks when a socket is ready for I/O.
165 event_base
* event_base_
;
167 // ... write end; ScheduleWork() writes a single byte to it
169 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep
170 int wakeup_pipe_out_
;
171 // ... libevent wrapper for read end
172 event
* wakeup_event_
;
174 ObserverList
<IOObserver
> io_observers_
;
175 ThreadChecker watch_file_descriptor_caller_checker_
;
176 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent
);
181 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_