/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ // Copyright (c) 2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// Lifecycle of struct event // Libevent uses two main data structures: // struct event_base (of which there is one per message pump), and // struct event (of which there is roughly one per socket). // The socket's struct event is created in // MessagePumpLibevent::WatchFileDescriptor(), // is owned by the FileDescriptorWatcher, and is destroyed in // StopWatchingFileDescriptor(). // It is moved into and out of lists in struct event_base by // the libevent functions event_add() and event_del(). // // TODO(dkegel): // At the moment bad things happen if a FileDescriptorWatcher // is active after its MessagePumpLibevent has been destroyed. // See MessageLoopTest.FileDescriptorWatcherOutlivesMessageLoop // Not clear yet whether that situation occurs in practice, // but if it does, we need to fix it.
namespace base {
// Return 0 on success // Too small a function to bother putting in a library? staticint SetNonBlocking(int fd) { int flags = fcntl(fd, F_GETFL, 0); if (flags == -1) flags = 0; return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() {
event* e = ReleaseEvent(); if (e == NULL) returntrue;
// event_del() is a no-op if the event isn't active. int rv = event_del(e); delete e; return (rv == 0);
}
bool MessagePumpLibevent::awake_ = false;
// Called if a byte is received on the wakeup pipe. void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) { if (!awake_) {
profiler_thread_wake();
awake_ = true;
}
base::MessagePumpLibevent* that = static_cast<base::MessagePumpLibevent*>(context);
DCHECK(that->wakeup_pipe_out_ == socket);
// Remove and discard the wakeup byte. char buf; int nread = HANDLE_EINTR(read(socket, &buf, 1));
DCHECK_EQ(nread, 1); // Tell libevent to break out of inner loop.
event_base_loopbreak(that->event_base_);
}
int event_mask = persistent ? EV_PERSIST : 0; if ((mode & WATCH_READ) != 0) {
event_mask |= EV_READ;
} if ((mode & WATCH_WRITE) != 0) {
event_mask |= EV_WRITE;
}
// |should_delete_event| is true if we're modifying an event that's currently // active in |controller|. // If we're modifying an existing event and there's an error then we need to // tell libevent to clean it up via event_delete() before returning. bool should_delete_event = true;
mozilla::UniquePtr<event> evt(controller->ReleaseEvent()); if (evt.get() == NULL) {
should_delete_event = false; // Ownership is transferred to the controller.
evt = mozilla::MakeUnique<event>();
} else { // It's illegal to use this function to listen on 2 separate fds with the // same |controller|. if (EVENT_FD(evt.get()) != fd) {
NOTREACHED() << "FDs don't match" << EVENT_FD(evt.get()) << "!=" << fd; returnfalse;
}
// Make sure we don't pick up any funky internal libevent masks. int old_interest_mask =
evt.get()->ev_events & (EV_READ | EV_WRITE | EV_PERSIST);
// Must disarm the event before we can reuse it.
event_del(evt.get());
}
// Set current interest mask and message pump for this event.
event_set(evt.get(), fd, event_mask, OnLibeventNotification, delegate);
// Tell libevent which message pump this socket will belong to when we add it. if (event_base_set(event_base_, evt.get()) != 0) { if (should_delete_event) {
event_del(evt.get());
} returnfalse;
}
// Add this socket to the list of monitored sockets. if (event_add(evt.get(), NULL) != 0) { if (should_delete_event) {
event_del(evt.get());
} returnfalse;
}
// Transfer ownership of evt to controller.
controller->Init(evt.release(), persistent); returntrue;
}
void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, void* context) { if (!awake_) {
profiler_thread_wake();
awake_ = true;
}
AUTO_PROFILER_LABEL("MessagePumpLibevent::OnLibeventNotification", OTHER);
if (flags & EV_WRITE) {
watcher->OnFileCanWriteWithoutBlocking(fd);
} if (flags & EV_READ) {
watcher->OnFileCanReadWithoutBlocking(fd);
}
}
// Reentrant! void MessagePumpLibevent::Run(Delegate* delegate) {
DCHECK(keep_running_) << "Quit must have been called outside of Run!";
bool old_in_run = in_run_;
in_run_ = true;
for (;;) {
ScopedNSAutoreleasePool autorelease_pool;
bool did_work = delegate->DoWork(); if (!keep_running_) break;
did_work |= delegate->DoDelayedWork(&delayed_work_time_); if (!keep_running_) break;
if (did_work) continue;
did_work = delegate->DoIdleWork(); if (!keep_running_) break;
if (did_work) continue;
// EVLOOP_ONCE tells libevent to only block once, // but to service all pending events when it wakes up.
AUTO_PROFILER_LABEL("MessagePumpLibevent::Run::Wait", IDLE); if (delayed_work_time_.is_null()) {
profiler_thread_sleep();
awake_ = false;
event_base_loop(event_base_, EVLOOP_ONCE);
} else {
TimeDelta delay = delayed_work_time_ - TimeTicks::Now(); if (delay > TimeDelta()) { struct timeval poll_tv;
poll_tv.tv_sec = delay.InSeconds();
poll_tv.tv_usec = delay.InMicroseconds() % Time::kMicrosecondsPerSecond;
event_base_loopexit(event_base_, &poll_tv);
profiler_thread_sleep();
awake_ = false;
event_base_loop(event_base_, EVLOOP_ONCE);
} else { // It looks like delayed_work_time_ indicates a time in the past, so we // need to call DoDelayedWork now.
delayed_work_time_ = TimeTicks();
}
}
}
keep_running_ = true;
in_run_ = old_in_run;
}
void MessagePumpLibevent::Quit() {
DCHECK(in_run_); // Tell both libevent and Run that they should break out of their loops.
keep_running_ = false;
ScheduleWork();
}
void MessagePumpLibevent::ScheduleWork() { // Tell libevent (in a threadsafe way) that it should break out of its loop. char buf = 0; int nwrite = HANDLE_EINTR(write(wakeup_pipe_in_, &buf, 1));
DCHECK(nwrite == 1 || errno == EAGAIN)
<< "[nwrite:" << nwrite << "] [errno:" << errno << "]";
}
void MessagePumpLibevent::ScheduleDelayedWork( const TimeTicks& delayed_work_time) { // We know that we can't be blocked on Wait right now since this method can // only be called on the same thread as Run, so we only need to update our // record of how long to sleep when we do sleep.
delayed_work_time_ = delayed_work_time;
}
} // namespace base
Messung V0.5
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.