OLD | NEW |
1 // Copyright (c) 2006-2008 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 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_ | 5 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_ |
6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ | 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/message_pump.h" | 10 #include "base/message_pump.h" |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump); | 79 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump); |
80 | 80 |
81 bool is_persistent_; // false if this event is one-shot. | 81 bool is_persistent_; // false if this event is one-shot. |
82 event* event_; | 82 event* event_; |
83 MessagePumpLibevent* pump_; | 83 MessagePumpLibevent* pump_; |
84 Watcher* watcher_; | 84 Watcher* watcher_; |
85 | 85 |
86 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | 86 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); |
87 }; | 87 }; |
88 | 88 |
89 MessagePumpLibevent(); | |
90 virtual ~MessagePumpLibevent(); | |
91 | |
92 enum Mode { | 89 enum Mode { |
93 WATCH_READ = 1 << 0, | 90 WATCH_READ = 1 << 0, |
94 WATCH_WRITE = 1 << 1, | 91 WATCH_WRITE = 1 << 1, |
95 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE | 92 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE |
96 }; | 93 }; |
97 | 94 |
| 95 MessagePumpLibevent(); |
| 96 virtual ~MessagePumpLibevent(); |
| 97 |
98 // Have the current thread's message loop watch for a a situation in which | 98 // Have the current thread's message loop watch for a a situation in which |
99 // reading/writing to the FD can be performed without blocking. | 99 // reading/writing to the FD can be performed without blocking. |
100 // Callers must provide a preallocated FileDescriptorWatcher object which | 100 // Callers must provide a preallocated FileDescriptorWatcher object which |
101 // can later be used to manage the lifetime of this event. | 101 // can later be used to manage the lifetime of this event. |
102 // If a FileDescriptorWatcher is passed in which is already attached to | 102 // If a FileDescriptorWatcher is passed in which is already attached to |
103 // an event, then the effect is cumulative i.e. after the call |controller| | 103 // an event, then the effect is cumulative i.e. after the call |controller| |
104 // will watch both the previous event and the new one. | 104 // will watch both the previous event and the new one. |
105 // If an error occurs while calling this method in a cumulative fashion, the | 105 // If an error occurs while calling this method in a cumulative fashion, the |
106 // event previously attached to |controller| is aborted. | 106 // event previously attached to |controller| is aborted. |
107 // Returns true on success. | 107 // Returns true on success. |
(...skipping 13 matching lines...) Expand all Loading... |
121 virtual void ScheduleWork(); | 121 virtual void ScheduleWork(); |
122 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); | 122 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); |
123 | 123 |
124 private: | 124 private: |
125 void WillProcessIOEvent(); | 125 void WillProcessIOEvent(); |
126 void DidProcessIOEvent(); | 126 void DidProcessIOEvent(); |
127 | 127 |
128 // Risky part of constructor. Returns true on success. | 128 // Risky part of constructor. Returns true on success. |
129 bool Init(); | 129 bool Init(); |
130 | 130 |
| 131 // Called by libevent to tell us a registered FD can be read/written to. |
| 132 static void OnLibeventNotification(int fd, short flags, |
| 133 void* context); |
| 134 |
| 135 // Unix pipe used to implement ScheduleWork() |
| 136 // ... callback; called by libevent inside Run() when pipe is ready to read |
| 137 static void OnWakeup(int socket, short flags, void* context); |
| 138 |
131 // This flag is set to false when Run should return. | 139 // This flag is set to false when Run should return. |
132 bool keep_running_; | 140 bool keep_running_; |
133 | 141 |
134 // This flag is set when inside Run. | 142 // This flag is set when inside Run. |
135 bool in_run_; | 143 bool in_run_; |
136 | 144 |
137 // The time at which we should call DoDelayedWork. | 145 // The time at which we should call DoDelayedWork. |
138 TimeTicks delayed_work_time_; | 146 TimeTicks delayed_work_time_; |
139 | 147 |
140 // Libevent dispatcher. Watches all sockets registered with it, and sends | 148 // Libevent dispatcher. Watches all sockets registered with it, and sends |
141 // readiness callbacks when a socket is ready for I/O. | 149 // readiness callbacks when a socket is ready for I/O. |
142 event_base* event_base_; | 150 event_base* event_base_; |
143 | 151 |
144 // Called by libevent to tell us a registered FD can be read/written to. | |
145 static void OnLibeventNotification(int fd, short flags, | |
146 void* context); | |
147 | |
148 // Unix pipe used to implement ScheduleWork() | |
149 // ... callback; called by libevent inside Run() when pipe is ready to read | |
150 static void OnWakeup(int socket, short flags, void* context); | |
151 // ... write end; ScheduleWork() writes a single byte to it | 152 // ... write end; ScheduleWork() writes a single byte to it |
152 int wakeup_pipe_in_; | 153 int wakeup_pipe_in_; |
153 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep | 154 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep |
154 int wakeup_pipe_out_; | 155 int wakeup_pipe_out_; |
155 // ... libevent wrapper for read end | 156 // ... libevent wrapper for read end |
156 event* wakeup_event_; | 157 event* wakeup_event_; |
157 | 158 |
158 ObserverList<IOObserver> io_observers_; | 159 ObserverList<IOObserver> io_observers_; |
159 | 160 |
160 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); | 161 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); |
161 }; | 162 }; |
162 | 163 |
163 } // namespace base | 164 } // namespace base |
164 | 165 |
165 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ | 166 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ |
OLD | NEW |