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 <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 event_ = e; | 80 event_ = e; |
81 } | 81 } |
82 | 82 |
83 event *MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { | 83 event *MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { |
84 struct event *e = event_; | 84 struct event *e = event_; |
85 event_ = NULL; | 85 event_ = NULL; |
86 return e; | 86 return e; |
87 } | 87 } |
88 | 88 |
89 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( | 89 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( |
90 int fd, MessagePumpLibevent* pump) { | 90 int fd, |
| 91 MessagePumpLibevent*) { |
91 // Since OnFileCanWriteWithoutBlocking() gets called first, it can stop | 92 // Since OnFileCanWriteWithoutBlocking() gets called first, it can stop |
92 // watching the file descriptor. | 93 // watching the file descriptor. |
93 if (!watcher_) | 94 if (!watcher_) |
94 return; | 95 return; |
95 watcher_->OnFileCanReadWithoutBlocking(fd); | 96 watcher_->OnFileCanReadWithoutBlocking(fd); |
96 } | 97 } |
97 | 98 |
98 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( | 99 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( |
99 int fd, MessagePumpLibevent* pump) { | 100 int fd, |
| 101 MessagePumpLibevent*) { |
100 DCHECK(watcher_); | 102 DCHECK(watcher_); |
101 watcher_->OnFileCanWriteWithoutBlocking(fd); | 103 watcher_->OnFileCanWriteWithoutBlocking(fd); |
102 } | 104 } |
103 | 105 |
104 MessagePumpLibevent::MessagePumpLibevent() | 106 MessagePumpLibevent::MessagePumpLibevent() |
105 : keep_running_(true), | 107 : keep_running_(true), |
106 in_run_(false), | 108 in_run_(false), |
107 processed_io_events_(false), | 109 processed_io_events_(false), |
108 event_base_(event_base_new()), | 110 event_base_(event_base_new()), |
109 wakeup_pipe_in_(-1), | 111 wakeup_pipe_in_(-1), |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // Transfer ownership of evt to controller. | 190 // Transfer ownership of evt to controller. |
189 controller->Init(evt.release()); | 191 controller->Init(evt.release()); |
190 | 192 |
191 controller->set_watcher(delegate); | 193 controller->set_watcher(delegate); |
192 controller->set_pump(this); | 194 controller->set_pump(this); |
193 | 195 |
194 return true; | 196 return true; |
195 } | 197 } |
196 | 198 |
197 // Tell libevent to break out of inner loop. | 199 // Tell libevent to break out of inner loop. |
198 static void timer_callback(int fd, short events, void *context) | 200 static void timer_callback(int /*fd*/, short /*events*/, void* context) { |
199 { | |
200 event_base_loopbreak((struct event_base *)context); | 201 event_base_loopbreak((struct event_base *)context); |
201 } | 202 } |
202 | 203 |
203 // Reentrant! | 204 // Reentrant! |
204 void MessagePumpLibevent::Run(Delegate* delegate) { | 205 void MessagePumpLibevent::Run(Delegate* delegate) { |
205 AutoReset<bool> auto_reset_keep_running(&keep_running_, true); | 206 AutoReset<bool> auto_reset_keep_running(&keep_running_, true); |
206 AutoReset<bool> auto_reset_in_run(&in_run_, true); | 207 AutoReset<bool> auto_reset_in_run(&in_run_, true); |
207 | 208 |
208 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. | 209 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. |
209 // Instead, make our own timer and reuse it on each call to event_base_loop(). | 210 // Instead, make our own timer and reuse it on each call to event_base_loop(). |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 controller->was_destroyed_ = nullptr; | 341 controller->was_destroyed_ = nullptr; |
341 } else if (flags & EV_WRITE) { | 342 } else if (flags & EV_WRITE) { |
342 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 343 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
343 } else if (flags & EV_READ) { | 344 } else if (flags & EV_READ) { |
344 controller->OnFileCanReadWithoutBlocking(fd, pump); | 345 controller->OnFileCanReadWithoutBlocking(fd, pump); |
345 } | 346 } |
346 } | 347 } |
347 | 348 |
348 // Called if a byte is received on the wakeup pipe. | 349 // Called if a byte is received on the wakeup pipe. |
349 // static | 350 // static |
350 void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { | 351 void MessagePumpLibevent::OnWakeup(int socket, short /*flags*/, void* context) { |
351 MessagePumpLibevent* that = static_cast<MessagePumpLibevent*>(context); | 352 MessagePumpLibevent* that = static_cast<MessagePumpLibevent*>(context); |
352 DCHECK(that->wakeup_pipe_out_ == socket); | 353 DCHECK(that->wakeup_pipe_out_ == socket); |
353 | 354 |
354 // Remove and discard the wakeup byte. | 355 // Remove and discard the wakeup byte. |
355 char buf; | 356 char buf; |
356 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 357 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
357 DCHECK_EQ(nread, 1); | 358 DCHECK_EQ(nread, 1); |
358 that->processed_io_events_ = true; | 359 that->processed_io_events_ = true; |
359 // Tell libevent to break out of inner loop. | 360 // Tell libevent to break out of inner loop. |
360 event_base_loopbreak(that->event_base_); | 361 event_base_loopbreak(that->event_base_); |
361 } | 362 } |
362 | 363 |
363 } // namespace base | 364 } // namespace base |
OLD | NEW |