event: introduce generic API for internal event loop
This commit is contained in:
parent
f90abe9530
commit
bb288f0284
@ -306,6 +306,8 @@ libsystem_a_SOURCES = \
|
|||||||
|
|
||||||
libevent_a_SOURCES = \
|
libevent_a_SOURCES = \
|
||||||
src/event/WakeFD.hxx \
|
src/event/WakeFD.hxx \
|
||||||
|
src/event/PollGroup.hxx \
|
||||||
|
src/event/PollGroupEPoll.hxx \
|
||||||
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
||||||
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
|
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
|
||||||
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
|
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
|
||||||
|
@ -33,9 +33,6 @@ EventLoop::EventLoop(Default)
|
|||||||
:SocketMonitor(*this),
|
:SocketMonitor(*this),
|
||||||
now_ms(::MonotonicClockMS()),
|
now_ms(::MonotonicClockMS()),
|
||||||
quit(false),
|
quit(false),
|
||||||
#ifdef USE_EPOLL
|
|
||||||
n_events(0),
|
|
||||||
#endif
|
|
||||||
thread(ThreadId::Null())
|
thread(ThreadId::Null())
|
||||||
{
|
{
|
||||||
SocketMonitor::Open(wake_fd.Get());
|
SocketMonitor::Open(wake_fd.Get());
|
||||||
@ -60,23 +57,18 @@ EventLoop::Break()
|
|||||||
AddCall([this]() { Break(); });
|
AddCall([this]() { Break(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
bool
|
||||||
EventLoop::Abandon(SocketMonitor &m)
|
EventLoop::Abandon(int _fd, SocketMonitor &m)
|
||||||
{
|
{
|
||||||
#ifdef USE_EPOLL
|
poll_result.Clear(&m);
|
||||||
for (unsigned i = 0, n = n_events; i < n; ++i)
|
return poll_group.Abandon(_fd);
|
||||||
if (events[i].data.ptr == &m)
|
|
||||||
events[i].events = 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
EventLoop::RemoveFD(int _fd, SocketMonitor &m)
|
EventLoop::RemoveFD(int _fd, SocketMonitor &m)
|
||||||
{
|
{
|
||||||
#ifdef USE_EPOLL
|
poll_result.Clear(&m);
|
||||||
Abandon(m);
|
return poll_group.Remove(_fd);
|
||||||
return epoll.Remove(_fd);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -168,34 +160,29 @@ EventLoop::Run()
|
|||||||
timeout */
|
timeout */
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
#ifdef USE_EPOLL
|
|
||||||
/* wait for new event */
|
/* wait for new event */
|
||||||
|
|
||||||
const int n = epoll.Wait(events, MAX_EVENTS, timeout_ms);
|
poll_group.ReadEvents(poll_result, timeout_ms);
|
||||||
n_events = std::max(n, 0);
|
|
||||||
|
|
||||||
now_ms = ::MonotonicClockMS();
|
now_ms = ::MonotonicClockMS();
|
||||||
|
|
||||||
assert(!quit);
|
assert(!quit);
|
||||||
|
|
||||||
/* invoke sockets */
|
/* invoke sockets */
|
||||||
|
for (int i = 0; i < poll_result.GetSize(); ++i) {
|
||||||
for (int i = 0; i < n; ++i) {
|
auto events = poll_result.GetEvents(i);
|
||||||
const auto &e = events[i];
|
if (events != 0) {
|
||||||
|
auto m = (SocketMonitor *)poll_result.GetObject(i);
|
||||||
if (e.events != 0) {
|
m->Dispatch(events);
|
||||||
SocketMonitor &m = *(SocketMonitor *)e.data.ptr;
|
|
||||||
m.Dispatch(e.events);
|
|
||||||
|
|
||||||
if (quit)
|
if (quit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n_events = 0;
|
poll_result.Reset();
|
||||||
#endif
|
|
||||||
} while (!quit);
|
|
||||||
|
|
||||||
|
} while (!quit);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_GLIB_EVENTLOOP
|
#ifdef USE_GLIB_EVENTLOOP
|
||||||
|
@ -25,10 +25,7 @@
|
|||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
#ifdef USE_INTERNAL_EVENTLOOP
|
#ifdef USE_INTERNAL_EVENTLOOP
|
||||||
#ifdef USE_EPOLL
|
#include "PollGroup.hxx"
|
||||||
#include "system/EPollFD.hxx"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "thread/Mutex.hxx"
|
#include "thread/Mutex.hxx"
|
||||||
#include "WakeFD.hxx"
|
#include "WakeFD.hxx"
|
||||||
#include "SocketMonitor.hxx"
|
#include "SocketMonitor.hxx"
|
||||||
@ -99,13 +96,8 @@ class EventLoop final
|
|||||||
|
|
||||||
bool quit;
|
bool quit;
|
||||||
|
|
||||||
#ifdef USE_EPOLL
|
PollGroup poll_group;
|
||||||
EPollFD epoll;
|
PollResult poll_result;
|
||||||
static constexpr unsigned MAX_EVENTS = 16;
|
|
||||||
unsigned n_events;
|
|
||||||
epoll_event events[MAX_EVENTS];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_GLIB_EVENTLOOP
|
#ifdef USE_GLIB_EVENTLOOP
|
||||||
@ -140,15 +132,11 @@ public:
|
|||||||
void Break();
|
void Break();
|
||||||
|
|
||||||
bool AddFD(int _fd, unsigned flags, SocketMonitor &m) {
|
bool AddFD(int _fd, unsigned flags, SocketMonitor &m) {
|
||||||
#ifdef USE_EPOLL
|
return poll_group.Add(_fd, flags, &m);
|
||||||
return epoll.Add(_fd, flags, &m);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) {
|
bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) {
|
||||||
#ifdef USE_EPOLL
|
return poll_group.Modify(_fd, flags, &m);
|
||||||
return epoll.Modify(_fd, flags, &m);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,7 +144,7 @@ public:
|
|||||||
* has been closed. This is like RemoveFD(), but does not
|
* has been closed. This is like RemoveFD(), but does not
|
||||||
* attempt to use #EPOLL_CTL_DEL.
|
* attempt to use #EPOLL_CTL_DEL.
|
||||||
*/
|
*/
|
||||||
void Abandon(SocketMonitor &m);
|
bool Abandon(int fd, SocketMonitor &m);
|
||||||
|
|
||||||
bool RemoveFD(int fd, SocketMonitor &m);
|
bool RemoveFD(int fd, SocketMonitor &m);
|
||||||
|
|
||||||
|
29
src/event/PollGroup.hxx
Normal file
29
src/event/PollGroup.hxx
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_EVENT_POLLGROUP_HXX
|
||||||
|
#define MPD_EVENT_POLLGROUP_HXX
|
||||||
|
|
||||||
|
#ifdef USE_EPOLL
|
||||||
|
#include "PollGroupEPoll.hxx"
|
||||||
|
typedef PollResultEPoll PollResult;
|
||||||
|
typedef PollGroupEPoll PollGroup;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
91
src/event/PollGroupEPoll.hxx
Normal file
91
src/event/PollGroupEPoll.hxx
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_EVENT_POLLGROUP_EPOLL_HXX
|
||||||
|
#define MPD_EVENT_POLLGROUP_EPOLL_HXX
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#include "Compiler.h"
|
||||||
|
#include "system/EPollFD.hxx"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
class PollResultEPoll
|
||||||
|
{
|
||||||
|
friend class PollGroupEPoll;
|
||||||
|
|
||||||
|
std::array<epoll_event, 16> events;
|
||||||
|
int n_events;
|
||||||
|
public:
|
||||||
|
PollResultEPoll() : n_events(0) { }
|
||||||
|
|
||||||
|
int GetSize() const { return n_events; }
|
||||||
|
unsigned GetEvents(int i) { return events[i].events; }
|
||||||
|
void *GetObject(int i) { return events[i].data.ptr; }
|
||||||
|
void Reset() { n_events = 0; }
|
||||||
|
|
||||||
|
void Clear(void *obj) {
|
||||||
|
for (int i = 0; i < n_events; ++i)
|
||||||
|
if (events[i].data.ptr == obj)
|
||||||
|
events[i].events = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class PollGroupEPoll
|
||||||
|
{
|
||||||
|
EPollFD epoll;
|
||||||
|
|
||||||
|
PollGroupEPoll(PollGroupEPoll &) = delete;
|
||||||
|
PollGroupEPoll &operator=(PollGroupEPoll &) = delete;
|
||||||
|
public:
|
||||||
|
static constexpr unsigned READ = EPOLLIN;
|
||||||
|
static constexpr unsigned WRITE = EPOLLOUT;
|
||||||
|
static constexpr unsigned ERROR = EPOLLERR;
|
||||||
|
static constexpr unsigned HANGUP = EPOLLHUP;
|
||||||
|
|
||||||
|
PollGroupEPoll() = default;
|
||||||
|
|
||||||
|
void ReadEvents(PollResultEPoll &result, int timeout_ms) {
|
||||||
|
int ret = epoll.Wait(result.events.data(), result.events.size(),
|
||||||
|
timeout_ms);
|
||||||
|
result.n_events = std::max(0, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Add(int fd, unsigned events, void *obj) {
|
||||||
|
return epoll.Add(fd, events, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Modify(int fd, unsigned events, void *obj) {
|
||||||
|
return epoll.Modify(fd, events, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Remove(int fd) {
|
||||||
|
return epoll.Remove(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Abandon(gcc_unused int fd) {
|
||||||
|
// Nothing to do in this implementation.
|
||||||
|
// Closed descriptors are automatically unregistered.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -158,8 +158,9 @@ SocketMonitor::Abandon()
|
|||||||
assert(IsDefined());
|
assert(IsDefined());
|
||||||
|
|
||||||
#ifdef USE_INTERNAL_EVENTLOOP
|
#ifdef USE_INTERNAL_EVENTLOOP
|
||||||
|
int old_fd = fd;
|
||||||
fd = -1;
|
fd = -1;
|
||||||
loop.Abandon(*this);
|
loop.Abandon(old_fd, *this);
|
||||||
#else
|
#else
|
||||||
Steal();
|
Steal();
|
||||||
#endif
|
#endif
|
||||||
|
@ -23,9 +23,7 @@
|
|||||||
#include "check.h"
|
#include "check.h"
|
||||||
|
|
||||||
#ifdef USE_INTERNAL_EVENTLOOP
|
#ifdef USE_INTERNAL_EVENTLOOP
|
||||||
#ifdef USE_EPOLL
|
#include "PollGroup.hxx"
|
||||||
#include <sys/epoll.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_GLIB_EVENTLOOP
|
#ifdef USE_GLIB_EVENTLOOP
|
||||||
@ -79,12 +77,10 @@ class SocketMonitor {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
#ifdef USE_INTERNAL_EVENTLOOP
|
#ifdef USE_INTERNAL_EVENTLOOP
|
||||||
#ifdef USE_EPOLL
|
static constexpr unsigned READ = PollGroup::READ;
|
||||||
static constexpr unsigned READ = EPOLLIN;
|
static constexpr unsigned WRITE = PollGroup::WRITE;
|
||||||
static constexpr unsigned WRITE = EPOLLOUT;
|
static constexpr unsigned ERROR = PollGroup::ERROR;
|
||||||
static constexpr unsigned ERROR = EPOLLERR;
|
static constexpr unsigned HANGUP = PollGroup::HANGUP;
|
||||||
static constexpr unsigned HANGUP = EPOLLHUP;
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_GLIB_EVENTLOOP
|
#ifdef USE_GLIB_EVENTLOOP
|
||||||
|
Loading…
Reference in New Issue
Block a user