| 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 <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 210 } |
| 211 | 211 |
| 212 // Tell libevent to break out of inner loop. | 212 // Tell libevent to break out of inner loop. |
| 213 static void timer_callback(int fd, short events, void *context) | 213 static void timer_callback(int fd, short events, void *context) |
| 214 { | 214 { |
| 215 event_base_loopbreak((struct event_base *)context); | 215 event_base_loopbreak((struct event_base *)context); |
| 216 } | 216 } |
| 217 | 217 |
| 218 // Reentrant! | 218 // Reentrant! |
| 219 void MessagePumpLibevent::Run(Delegate* delegate) { | 219 void MessagePumpLibevent::Run(Delegate* delegate) { |
| 220 DCHECK(keep_running_) << "Quit must have been called outside of Run!"; | 220 AutoReset<bool> auto_reset_keep_running(&keep_running_, true); |
| 221 AutoReset<bool> auto_reset_in_run(&in_run_, true); | 221 AutoReset<bool> auto_reset_in_run(&in_run_, true); |
| 222 | 222 |
| 223 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. | 223 // event_base_loopexit() + EVLOOP_ONCE is leaky, see http://crbug.com/25641. |
| 224 // Instead, make our own timer and reuse it on each call to event_base_loop(). | 224 // Instead, make our own timer and reuse it on each call to event_base_loop(). |
| 225 scoped_ptr<event> timer_event(new event); | 225 scoped_ptr<event> timer_event(new event); |
| 226 | 226 |
| 227 for (;;) { | 227 for (;;) { |
| 228 #if defined(OS_MACOSX) | 228 #if defined(OS_MACOSX) |
| 229 mac::ScopedNSAutoreleasePool autorelease_pool; | 229 mac::ScopedNSAutoreleasePool autorelease_pool; |
| 230 #endif | 230 #endif |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 event_add(timer_event.get(), &poll_tv); | 268 event_add(timer_event.get(), &poll_tv); |
| 269 event_base_loop(event_base_, EVLOOP_ONCE); | 269 event_base_loop(event_base_, EVLOOP_ONCE); |
| 270 event_del(timer_event.get()); | 270 event_del(timer_event.get()); |
| 271 } else { | 271 } else { |
| 272 // It looks like delayed_work_time_ indicates a time in the past, so we | 272 // It looks like delayed_work_time_ indicates a time in the past, so we |
| 273 // need to call DoDelayedWork now. | 273 // need to call DoDelayedWork now. |
| 274 delayed_work_time_ = TimeTicks(); | 274 delayed_work_time_ = TimeTicks(); |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 | |
| 279 keep_running_ = true; | |
| 280 } | 278 } |
| 281 | 279 |
| 282 void MessagePumpLibevent::Quit() { | 280 void MessagePumpLibevent::Quit() { |
| 283 DCHECK(in_run_); | 281 DCHECK(in_run_) << "Quit was called outside of Run!"; |
| 284 // Tell both libevent and Run that they should break out of their loops. | 282 // Tell both libevent and Run that they should break out of their loops. |
| 285 keep_running_ = false; | 283 keep_running_ = false; |
| 286 ScheduleWork(); | 284 ScheduleWork(); |
| 287 } | 285 } |
| 288 | 286 |
| 289 void MessagePumpLibevent::ScheduleWork() { | 287 void MessagePumpLibevent::ScheduleWork() { |
| 290 // Tell libevent (in a threadsafe way) that it should break out of its loop. | 288 // Tell libevent (in a threadsafe way) that it should break out of its loop. |
| 291 char buf = 0; | 289 char buf = 0; |
| 292 int nwrite = HANDLE_EINTR(write(wakeup_pipe_in_, &buf, 1)); | 290 int nwrite = HANDLE_EINTR(write(wakeup_pipe_in_, &buf, 1)); |
| 293 DCHECK(nwrite == 1 || errno == EAGAIN) | 291 DCHECK(nwrite == 1 || errno == EAGAIN) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // Remove and discard the wakeup byte. | 364 // Remove and discard the wakeup byte. |
| 367 char buf; | 365 char buf; |
| 368 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 366 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
| 369 DCHECK_EQ(nread, 1); | 367 DCHECK_EQ(nread, 1); |
| 370 that->processed_io_events_ = true; | 368 that->processed_io_events_ = true; |
| 371 // Tell libevent to break out of inner loop. | 369 // Tell libevent to break out of inner loop. |
| 372 event_base_loopbreak(that->event_base_); | 370 event_base_loopbreak(that->event_base_); |
| 373 } | 371 } |
| 374 | 372 |
| 375 } // namespace base | 373 } // namespace base |
| OLD | NEW |