#include "base/message_loop/message_pump_libevent.h"
#include <errno.h>
#include <unistd.h>
#include <memory>
#include <utility>
#include "base/auto_reset.h"
#include "base/compiler_specific.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/posix/eintr_wrapper.h"
#include "base/time/time.h"
#include "base/trace_event/base_tracing.h"
#include "build/build_config.h"
#include "third_party/libevent/event.h"
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
#include "base/message_loop/message_pump_epoll.h"
#endif
namespace base {
namespace {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
bool g_use_epoll = false;
BASE_FEATURE(kMessagePumpEpoll, "MessagePumpEpoll", FEATURE_ENABLED_BY_DEFAULT);
#endif
}
MessagePumpLibevent::FdWatchController::FdWatchController(
const Location& from_here)
: FdWatchControllerInterface(from_here) {}
MessagePumpLibevent::FdWatchController::~FdWatchController() {
CHECK(StopWatchingFileDescriptor());
if (was_destroyed_) {
DCHECK(!*was_destroyed_);
*was_destroyed_ = true;
}
}
bool MessagePumpLibevent::FdWatchController::StopWatchingFileDescriptor() {
watcher_ = nullptr;
std::unique_ptr<event> e = ReleaseEvent();
if (e) {
int rv = event_del(e.get());
libevent_pump_ = nullptr;
return (rv == 0);
}
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (epoll_interest_ && epoll_pump_) {
epoll_pump_->UnregisterInterest(epoll_interest_);
epoll_interest_.reset();
epoll_pump_.reset();
}
#endif
return true;
}
void MessagePumpLibevent::FdWatchController::Init(std::unique_ptr<event> e) {
DCHECK(e);
DCHECK(!event_);
event_ = std::move(e);
}
std::unique_ptr<event> MessagePumpLibevent::FdWatchController::ReleaseEvent() {
return std::move(event_);
}
void MessagePumpLibevent::FdWatchController::OnFileCanReadWithoutBlocking(
int fd,
MessagePumpLibevent* pump) {
if (!watcher_)
return;
watcher_->OnFileCanReadWithoutBlocking(fd);
}
void MessagePumpLibevent::FdWatchController::OnFileCanWriteWithoutBlocking(
int fd,
MessagePumpLibevent* pump) {
DCHECK(watcher_);
watcher_->OnFileCanWriteWithoutBlocking(fd);
}
const scoped_refptr<MessagePumpLibevent::EpollInterest>&
MessagePumpLibevent::FdWatchController::AssignEpollInterest(
const EpollInterestParams& params) {
epoll_interest_ = MakeRefCounted<EpollInterest>(this, params);
return epoll_interest_;
}
void MessagePumpLibevent::FdWatchController::OnFdReadable() {
if (!watcher_) {
return;
}
watcher_->OnFileCanReadWithoutBlocking(epoll_interest_->params().fd);
}
void MessagePumpLibevent::FdWatchController::OnFdWritable() {
DCHECK(watcher_);
watcher_->OnFileCanWriteWithoutBlocking(epoll_interest_->params().fd);
}
MessagePumpLibevent::MessagePumpLibevent() {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (g_use_epoll) {
epoll_pump_ = std::make_unique<MessagePumpEpoll>();
return;
}
#endif
if (!Init())
NOTREACHED();
DCHECK_NE(wakeup_pipe_in_, -1);
DCHECK_NE(wakeup_pipe_out_, -1);
DCHECK(wakeup_event_);
}
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
MessagePumpLibevent::MessagePumpLibevent(decltype(kUseEpoll))
: epoll_pump_(std::make_unique<MessagePumpEpoll>()) {}
#endif
MessagePumpLibevent::~MessagePumpLibevent() {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
const bool using_libevent = !epoll_pump_;
#else
const bool using_libevent = true;
#endif
DCHECK(event_base_);
if (using_libevent) {
DCHECK(wakeup_event_);
event_del(wakeup_event_.get());
wakeup_event_.reset();
if (wakeup_pipe_in_ >= 0) {
if (IGNORE_EINTR(close(wakeup_pipe_in_)) < 0)
DPLOG(ERROR) << "close";
}
if (wakeup_pipe_out_ >= 0) {
if (IGNORE_EINTR(close(wakeup_pipe_out_)) < 0)
DPLOG(ERROR) << "close";
}
}
event_base_.reset();
}
void MessagePumpLibevent::InitializeFeatures() {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
g_use_epoll = FeatureList::IsEnabled(kMessagePumpEpoll);
#endif
}
bool MessagePumpLibevent::WatchFileDescriptor(int fd,
bool persistent,
int mode,
FdWatchController* controller,
FdWatcher* delegate) {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (epoll_pump_) {
return epoll_pump_->WatchFileDescriptor(fd, persistent, mode, controller,
delegate);
}
#endif
TRACE_EVENT("base", "MessagePumpLibevent::WatchFileDescriptor", "fd", fd,
"persistent", persistent, "watch_read", mode & WATCH_READ,
"watch_write", mode & WATCH_WRITE);
DCHECK_GE(fd, 0);
DCHECK(controller);
DCHECK(delegate);
DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || mode == WATCH_READ_WRITE);
DCHECK(watch_file_descriptor_caller_checker_.CalledOnValidThread());
short event_mask = persistent ? EV_PERSIST : 0;
if (mode & WATCH_READ) {
event_mask |= EV_READ;
}
if (mode & WATCH_WRITE) {
event_mask |= EV_WRITE;
}
std::unique_ptr<event> evt(controller->ReleaseEvent());
if (!evt) {
evt = std::make_unique<event>();
} else {
int old_interest_mask = evt->ev_events & (EV_READ | EV_WRITE | EV_PERSIST);
event_mask |= old_interest_mask;
event_del(evt.get());
if (EVENT_FD(evt.get()) != fd) {
NOTREACHED() << "FDs don't match" << EVENT_FD(evt.get()) << "!=" << fd;
return false;
}
}
event_set(evt.get(), fd, event_mask, OnLibeventNotification, controller);
if (event_base_set(event_base_.get(), evt.get())) {
DPLOG(ERROR) << "event_base_set(fd=" << EVENT_FD(evt.get()) << ")";
return false;
}
if (event_add(evt.get(), nullptr)) {
DPLOG(ERROR) << "event_add failed(fd=" << EVENT_FD(evt.get()) << ")";
return false;
}
controller->Init(std::move(evt));
controller->set_watcher(delegate);
controller->set_libevent_pump(this);
return true;
}
static void timer_callback(int fd, short events, void* context) {
event_base_loopbreak((struct event_base*)context);
}
void MessagePumpLibevent::Run(Delegate* delegate) {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (epoll_pump_) {
epoll_pump_->Run(delegate);
return;
}
#endif
RunState run_state(delegate);
AutoReset<raw_ptr<RunState>> auto_reset_run_state(&run_state_, &run_state);
std::unique_ptr<event> timer_event(new event);
for (;;) {
Delegate::NextWorkInfo next_work_info = delegate->DoWork();
bool immediate_work_available = next_work_info.is_immediate();
if (run_state.should_quit)
break;
event_base_loop(event_base_.get(), EVLOOP_NONBLOCK);
bool attempt_more_work = immediate_work_available || processed_io_events_;
processed_io_events_ = false;
if (run_state.should_quit)
break;
if (attempt_more_work)
continue;
attempt_more_work = delegate->DoIdleWork();
if (run_state.should_quit)
break;
if (attempt_more_work)
continue;
bool did_set_timer = false;
DCHECK(!next_work_info.delayed_run_time.is_null());
if (!next_work_info.delayed_run_time.is_max()) {
const TimeDelta delay = next_work_info.remaining_delay();
struct timeval poll_tv;
poll_tv.tv_sec = static_cast<time_t>(delay.InSeconds());
poll_tv.tv_usec = delay.InMicroseconds() % Time::kMicrosecondsPerSecond;
event_set(timer_event.get(), -1, 0, timer_callback, event_base_.get());
event_base_set(event_base_.get(), timer_event.get());
event_add(timer_event.get(), &poll_tv);
did_set_timer = true;
}
delegate->BeforeWait();
event_base_loop(event_base_.get(), EVLOOP_ONCE);
if (did_set_timer) {
event_del(timer_event.get());
}
if (run_state.should_quit)
break;
}
}
void MessagePumpLibevent::Quit() {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (epoll_pump_) {
epoll_pump_->Quit();
return;
}
#endif
DCHECK(run_state_) << "Quit was called outside of Run!";
run_state_->should_quit = true;
ScheduleWork();
}
void MessagePumpLibevent::ScheduleWork() {
#if BUILDFLAG(ENABLE_MESSAGE_PUMP_EPOLL)
if (epoll_pump_) {
epoll_pump_->ScheduleWork();
return;
}
#endif
char buf = 0;
long nwrite = HANDLE_EINTR(write(wakeup_pipe_in_, &buf, 1));
DPCHECK(nwrite == 1 || errno == EAGAIN) << "nwrite:" << nwrite;
}
void MessagePumpLibevent::ScheduleDelayedWork(
const Delegate::NextWorkInfo& next_work_info) {
}
bool MessagePumpLibevent::Init() {
int fds[2];
if (!CreateLocalNonBlockingPipe(fds)) {
DPLOG(ERROR) << "pipe creation failed";
return false;
}
wakeup_pipe_out_ = fds[0];
wakeup_pipe_in_ = fds[1];
wakeup_event_ = std::make_unique<event>();
event_set(wakeup_event_.get(), wakeup_pipe_out_, EV_READ | EV_PERSIST,
OnWakeup, this);
event_base_set(event_base_.get(), wakeup_event_.get());
if (event_add(wakeup_event_.get(), nullptr))
return false;
return true;
}
void MessagePumpLibevent::OnLibeventNotification(int fd,
short flags,
void* context) {
FdWatchController* controller = static_cast<FdWatchController*>(context);
DCHECK(controller);
MessagePumpLibevent* pump = controller->libevent_pump();
pump->processed_io_events_ = true;
Delegate::ScopedDoWorkItem scoped_do_work_item;
if (pump->run_state_)
scoped_do_work_item = pump->run_state_->delegate->BeginWorkItem();
TRACE_EVENT("toplevel", "OnLibevent", "controller_created_from",
controller->created_from_location(), "fd", fd, "flags", flags,
"context", context);
TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION heap_profiler_scope(
controller->created_from_location().file_name());
if ((flags & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
bool controller_was_destroyed = false;
controller->was_destroyed_ = &controller_was_destroyed;
controller->OnFileCanWriteWithoutBlocking(fd, pump);
if (!controller_was_destroyed)
controller->OnFileCanReadWithoutBlocking(fd, pump);
if (!controller_was_destroyed)
controller->was_destroyed_ = nullptr;
} else if (flags & EV_WRITE) {
controller->OnFileCanWriteWithoutBlocking(fd, pump);
} else if (flags & EV_READ) {
controller->OnFileCanReadWithoutBlocking(fd, pump);
}
}
void MessagePumpLibevent::OnWakeup(int socket, short flags, void* context) {
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("base"),
"MessagePumpLibevent::OnWakeup", "socket", socket, "flags", flags,
"context", context);
MessagePumpLibevent* that = static_cast<MessagePumpLibevent*>(context);
DCHECK(that->wakeup_pipe_out_ == socket);
char buf;
long nread = HANDLE_EINTR(read(socket, &buf, 1));
DCHECK_EQ(nread, 1);
that->processed_io_events_ = true;
event_base_loopbreak(that->event_base_.get());
}
MessagePumpLibevent::EpollInterest::EpollInterest(
FdWatchController* controller,
const EpollInterestParams& params)
: controller_(controller), params_(params) {}
MessagePumpLibevent::EpollInterest::~EpollInterest() = default;
}