Commit f0d3b47a authored by Max Kellermann's avatar Max Kellermann

event/Loop: remove the GLib implementation

Now that the remaining known bugs in poll() implementation are fixed, we can go on without the GLib implementation.
parent bfe75335
......@@ -188,44 +188,13 @@ if test x$host_is_linux = xyes; then
MPD_OPTIONAL_FUNC_NODEF(epoll, epoll_create1)
fi
AC_ARG_WITH(eventloop,
AS_HELP_STRING(
[--with-eventloop=@<:@glib|internal|auto@:>@],
[specify event loop implementation (default=auto)]),,
[with_eventloop=auto])
AC_ARG_WITH(pollmethod,
AS_HELP_STRING(
[--with-pollmethod=@<:@epoll|poll|winselect|auto@:>@],
[specify poll method for internal event loop (default=auto)]),,
[with_pollmethod=auto])
if test "x$with_eventloop" = xauto; then
if
test "x$enable_epoll" = xyes ||
test "x$host_is_windows" = xyes; then
with_eventloop=internal
else
with_eventloop=glib
fi
fi
case "$with_eventloop" in
glib)
AC_DEFINE(USE_GLIB_EVENTLOOP, 1,
[Define to use GLib event loop])
;;
internal)
AC_DEFINE(USE_INTERNAL_EVENTLOOP, 1,
[Define to use internal event loop])
;;
*)
AC_MSG_ERROR([unknown eventloop option: $with_eventloop])
;;
esac
if test "x$with_eventloop" = xinternal; then
if test "x$with_pollmethod" = xauto; then
if test "x$with_pollmethod" = xauto; then
if test "x$enable_epoll" = xyes; then
with_pollmethod=epoll
elif test "x$enable_poll" = xyes; then
......@@ -235,22 +204,21 @@ if test "x$with_eventloop" = xinternal; then
else
AC_MSG_ERROR([no poll method is available for your platform])
fi
fi
case "$with_pollmethod" in
epoll)
fi
case "$with_pollmethod" in
epoll)
AC_DEFINE(USE_EPOLL, 1, [Define to poll sockets with epoll])
;;
poll)
poll)
AC_DEFINE(USE_POLL, 1, [Define to poll sockets with poll])
;;
winselect)
winselect)
AC_DEFINE(USE_WINSELECT, 1,
[Define to poll sockets with Windows select])
;;
*)
*)
AC_MSG_ERROR([unknown pollmethod option: $with_pollmethod])
esac
fi
esac
dnl ---------------------------------------------------------------------------
dnl Allow tools to be specifically built
......@@ -1728,14 +1696,7 @@ printf '\n\t'
results(mms,[MMS])
printf '\nEvent loop:\n\t'
case $with_eventloop in
glib)
printf 'GLib'
;;
internal)
printf 'Internal (%s)' $with_pollmethod
;;
esac
printf $with_pollmethod
printf '\n\n##########################################\n\n'
......
......@@ -24,46 +24,11 @@
void
DeferredMonitor::Cancel()
{
#ifdef USE_INTERNAL_EVENTLOOP
loop.RemoveDeferred(*this);
#endif
#ifdef USE_GLIB_EVENTLOOP
const auto id = source_id.exchange(0);
if (id != 0)
g_source_remove(id);
#endif
}
void
DeferredMonitor::Schedule()
{
#ifdef USE_INTERNAL_EVENTLOOP
loop.AddDeferred(*this);
#endif
#ifdef USE_GLIB_EVENTLOOP
const unsigned id = loop.AddIdle(Callback, this);
const auto old_id = source_id.exchange(id);
if (old_id != 0)
g_source_remove(old_id);
#endif
}
#ifdef USE_GLIB_EVENTLOOP
void
DeferredMonitor::Run()
{
const auto id = source_id.exchange(0);
if (id != 0)
RunDeferred();
}
gboolean
DeferredMonitor::Callback(gpointer data)
{
DeferredMonitor &monitor = *(DeferredMonitor *)data;
monitor.Run();
return false;
}
#endif
......@@ -23,10 +23,6 @@
#include "check.h"
#include "Compiler.h"
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
#include <atomic>
class EventLoop;
......@@ -39,25 +35,12 @@ class EventLoop;
class DeferredMonitor {
EventLoop &loop;
#ifdef USE_INTERNAL_EVENTLOOP
friend class EventLoop;
bool pending;
#endif
#ifdef USE_GLIB_EVENTLOOP
std::atomic<guint> source_id;
#endif
public:
#ifdef USE_INTERNAL_EVENTLOOP
DeferredMonitor(EventLoop &_loop)
:loop(_loop), pending(false) {}
#endif
#ifdef USE_GLIB_EVENTLOOP
DeferredMonitor(EventLoop &_loop)
:loop(_loop), source_id(0) {}
#endif
~DeferredMonitor() {
Cancel();
......@@ -72,12 +55,6 @@ public:
protected:
virtual void RunDeferred() = 0;
private:
#ifdef USE_GLIB_EVENTLOOP
void Run();
static gboolean Callback(gpointer data);
#endif
};
#endif /* MAIN_NOTIFY_H */
......@@ -31,14 +31,8 @@ IdleMonitor::Cancel()
if (!IsActive())
return;
#ifdef USE_INTERNAL_EVENTLOOP
active = false;
loop.RemoveIdle(*this);
#endif
#ifdef USE_GLIB_EVENTLOOP
g_source_remove(source_id);
source_id = 0;
#endif
}
void
......@@ -50,13 +44,8 @@ IdleMonitor::Schedule()
/* already scheduled */
return;
#ifdef USE_INTERNAL_EVENTLOOP
active = true;
loop.AddIdle(*this);
#endif
#ifdef USE_GLIB_EVENTLOOP
source_id = loop.AddIdle(Callback, this);
#endif
}
void
......@@ -64,26 +53,8 @@ IdleMonitor::Run()
{
assert(loop.IsInside());
#ifdef USE_INTERNAL_EVENTLOOP
assert(active);
active = false;
#endif
#ifdef USE_GLIB_EVENTLOOP
assert(source_id != 0);
source_id = 0;
#endif
OnIdle();
}
#ifdef USE_GLIB_EVENTLOOP
gboolean
IdleMonitor::Callback(gpointer data)
{
IdleMonitor &monitor = *(IdleMonitor *)data;
monitor.Run();
return false;
}
#endif
......@@ -22,10 +22,6 @@
#include "check.h"
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
class EventLoop;
/**
......@@ -38,30 +34,15 @@ class EventLoop;
* as thread-safe.
*/
class IdleMonitor {
#ifdef USE_INTERNAL_EVENTLOOP
friend class EventLoop;
#endif
EventLoop &loop;
#ifdef USE_INTERNAL_EVENTLOOP
bool active;
#endif
#ifdef USE_GLIB_EVENTLOOP
guint source_id;
#endif
public:
#ifdef USE_INTERNAL_EVENTLOOP
IdleMonitor(EventLoop &_loop)
:loop(_loop), active(false) {}
#endif
#ifdef USE_GLIB_EVENTLOOP
IdleMonitor(EventLoop &_loop)
:loop(_loop), source_id(0) {}
#endif
~IdleMonitor() {
Cancel();
......@@ -72,13 +53,7 @@ public:
}
bool IsActive() const {
#ifdef USE_INTERNAL_EVENTLOOP
return active;
#endif
#ifdef USE_GLIB_EVENTLOOP
return source_id != 0;
#endif
}
void Schedule();
......@@ -89,9 +64,6 @@ protected:
private:
void Run();
#ifdef USE_GLIB_EVENTLOOP
static gboolean Callback(gpointer data);
#endif
};
#endif /* MAIN_NOTIFY_H */
......@@ -20,8 +20,6 @@
#include "config.h"
#include "Loop.hxx"
#ifdef USE_INTERNAL_EVENTLOOP
#include "system/Clock.hxx"
#include "TimeoutMonitor.hxx"
#include "SocketMonitor.hxx"
......@@ -104,15 +102,12 @@ EventLoop::CancelTimer(TimeoutMonitor &t)
}
}
#endif
void
EventLoop::Run()
{
assert(thread.IsNull());
thread = ThreadId::GetCurrent();
#ifdef USE_INTERNAL_EVENTLOOP
assert(!quit);
do {
......@@ -180,17 +175,10 @@ EventLoop::Run()
poll_result.Reset();
} while (!quit);
#endif
#ifdef USE_GLIB_EVENTLOOP
g_main_loop_run(loop);
#endif
assert(thread.IsInside());
}
#ifdef USE_INTERNAL_EVENTLOOP
void
EventLoop::AddDeferred(DeferredMonitor &d)
{
......@@ -254,39 +242,3 @@ EventLoop::OnSocketReady(gcc_unused unsigned flags)
return true;
}
#endif
#ifdef USE_GLIB_EVENTLOOP
guint
EventLoop::AddIdle(GSourceFunc function, gpointer data)
{
GSource *source = g_idle_source_new();
g_source_set_callback(source, function, data, nullptr);
guint id = g_source_attach(source, GetContext());
g_source_unref(source);
return id;
}
GSource *
EventLoop::AddTimeout(guint interval_ms,
GSourceFunc function, gpointer data)
{
GSource *source = g_timeout_source_new(interval_ms);
g_source_set_callback(source, function, data, nullptr);
g_source_attach(source, GetContext());
return source;
}
GSource *
EventLoop::AddTimeoutSeconds(guint interval_s,
GSourceFunc function, gpointer data)
{
GSource *source = g_timeout_source_new_seconds(interval_s);
g_source_set_callback(source, function, data, nullptr);
g_source_attach(source, GetContext());
return source;
}
#endif
......@@ -24,7 +24,6 @@
#include "thread/Id.hxx"
#include "Compiler.h"
#ifdef USE_INTERNAL_EVENTLOOP
#include "PollGroup.hxx"
#include "thread/Mutex.hxx"
#include "WakeFD.hxx"
......@@ -32,18 +31,11 @@
#include <list>
#include <set>
#endif
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
#ifdef USE_INTERNAL_EVENTLOOP
class TimeoutMonitor;
class IdleMonitor;
class DeferredMonitor;
class SocketMonitor;
#endif
#include <assert.h>
......@@ -56,12 +48,8 @@ class SocketMonitor;
*
* @see SocketMonitor, MultiSocketMonitor, TimeoutMonitor, IdleMonitor
*/
class EventLoop final
#ifdef USE_INTERNAL_EVENTLOOP
: private SocketMonitor
#endif
class EventLoop final : SocketMonitor
{
#ifdef USE_INTERNAL_EVENTLOOP
struct TimerRecord {
/**
* Projected monotonic_clock_ms() value when this
......@@ -98,12 +86,6 @@ class EventLoop final
PollGroup poll_group;
PollResult poll_result;
#endif
#ifdef USE_GLIB_EVENTLOOP
GMainContext *context;
GMainLoop *loop;
#endif
/**
* A reference to the thread that is currently inside Run().
......@@ -111,7 +93,6 @@ class EventLoop final
ThreadId thread;
public:
#ifdef USE_INTERNAL_EVENTLOOP
struct Default {};
EventLoop(Default dummy=Default());
......@@ -179,47 +160,6 @@ private:
virtual bool OnSocketReady(unsigned flags) override;
public:
#endif
#ifdef USE_GLIB_EVENTLOOP
EventLoop()
:context(g_main_context_new()),
loop(g_main_loop_new(context, false)),
thread(ThreadId::Null()) {}
struct Default {};
EventLoop(gcc_unused Default _dummy)
:context(g_main_context_ref(g_main_context_default())),
loop(g_main_loop_new(context, false)),
thread(ThreadId::Null()) {}
~EventLoop() {
g_main_loop_unref(loop);
g_main_context_unref(context);
}
GMainContext *GetContext() {
return context;
}
void WakeUp() {
g_main_context_wakeup(context);
}
void Break() {
g_main_loop_quit(loop);
}
void Run();
guint AddIdle(GSourceFunc function, gpointer data);
GSource *AddTimeout(guint interval_ms,
GSourceFunc function, gpointer data);
GSource *AddTimeoutSeconds(guint interval_s,
GSourceFunc function, gpointer data);
#endif
/**
* Are we currently running inside this EventLoop's thread?
......
......@@ -25,8 +25,6 @@
#include <assert.h>
#ifdef USE_INTERNAL_EVENTLOOP
MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop)
:IdleMonitor(_loop), TimeoutMonitor(_loop), ready(false) {
}
......@@ -64,102 +62,3 @@ MultiSocketMonitor::OnIdle()
Prepare();
}
}
#endif
#ifdef USE_GLIB_EVENTLOOP
/**
* The vtable for our GSource implementation. Unfortunately, we
* cannot declare it "const", because g_source_new() takes a non-const
* pointer, for whatever reason.
*/
static GSourceFuncs multi_socket_monitor_source_funcs = {
MultiSocketMonitor::Prepare,
MultiSocketMonitor::Check,
MultiSocketMonitor::Dispatch,
nullptr,
nullptr,
nullptr,
};
MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop)
:loop(_loop),
source((Source *)g_source_new(&multi_socket_monitor_source_funcs,
sizeof(*source))),
absolute_timeout_us(-1) {
source->monitor = this;
g_source_attach(&source->base, loop.GetContext());
}
MultiSocketMonitor::~MultiSocketMonitor()
{
g_source_destroy(&source->base);
g_source_unref(&source->base);
source = nullptr;
}
bool
MultiSocketMonitor::Prepare(gint *timeout_r)
{
int timeout_ms = *timeout_r = PrepareSockets();
absolute_timeout_us = timeout_ms < 0
? uint64_t(-1)
: GetTime() + uint64_t(timeout_ms) * 1000;
return false;
}
bool
MultiSocketMonitor::Check() const
{
if (GetTime() >= absolute_timeout_us)
return true;
for (const auto &i : fds)
if (i.GetReturnedEvents() != 0)
return true;
return false;
}
/*
* GSource methods
*
*/
gboolean
MultiSocketMonitor::Prepare(GSource *_source, gint *timeout_r)
{
Source &source = *(Source *)_source;
MultiSocketMonitor &monitor = *source.monitor;
assert(_source == &monitor.source->base);
return monitor.Prepare(timeout_r);
}
gboolean
MultiSocketMonitor::Check(GSource *_source)
{
const Source &source = *(const Source *)_source;
const MultiSocketMonitor &monitor = *source.monitor;
assert(_source == &monitor.source->base);
return monitor.Check();
}
gboolean
MultiSocketMonitor::Dispatch(GSource *_source,
gcc_unused GSourceFunc callback,
gcc_unused gpointer user_data)
{
Source &source = *(Source *)_source;
MultiSocketMonitor &monitor = *source.monitor;
assert(_source == &monitor.source->base);
monitor.Dispatch();
return true;
}
#endif
......@@ -23,15 +23,9 @@
#include "check.h"
#include "Compiler.h"
#ifdef USE_INTERNAL_EVENTLOOP
#include "IdleMonitor.hxx"
#include "TimeoutMonitor.hxx"
#include "SocketMonitor.hxx"
#endif
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
#include <forward_list>
#include <iterator>
......@@ -55,12 +49,8 @@ class EventLoop;
* In PrepareSockets(), use UpdateSocketList() and AddSocket().
* DispatchSockets() will be called if at least one socket is ready.
*/
class MultiSocketMonitor
#ifdef USE_INTERNAL_EVENTLOOP
: private IdleMonitor, private TimeoutMonitor
#endif
class MultiSocketMonitor : IdleMonitor, TimeoutMonitor
{
#ifdef USE_INTERNAL_EVENTLOOP
class SingleFD final : public SocketMonitor {
MultiSocketMonitor &multi;
......@@ -105,99 +95,28 @@ class MultiSocketMonitor
friend class SingleFD;
bool ready, refresh;
#endif
#ifdef USE_GLIB_EVENTLOOP
struct Source {
GSource base;
MultiSocketMonitor *monitor;
};
struct SingleFD {
GPollFD pfd;
constexpr SingleFD(gcc_unused MultiSocketMonitor &m,
int fd, unsigned events)
:pfd{fd, gushort(events), 0} {}
constexpr int GetFD() const {
return pfd.fd;
}
constexpr unsigned GetEvents() const {
return pfd.events;
}
constexpr unsigned GetReturnedEvents() const {
return pfd.revents;
}
void SetEvents(unsigned _events) {
pfd.events = _events;
}
};
EventLoop &loop;
Source *source;
uint64_t absolute_timeout_us;
#endif
std::forward_list<SingleFD> fds;
public:
#ifdef USE_INTERNAL_EVENTLOOP
static constexpr unsigned READ = SocketMonitor::READ;
static constexpr unsigned WRITE = SocketMonitor::WRITE;
static constexpr unsigned ERROR = SocketMonitor::ERROR;
static constexpr unsigned HANGUP = SocketMonitor::HANGUP;
#endif
#ifdef USE_GLIB_EVENTLOOP
static constexpr unsigned READ = G_IO_IN;
static constexpr unsigned WRITE = G_IO_OUT;
static constexpr unsigned ERROR = G_IO_ERR;
static constexpr unsigned HANGUP = G_IO_HUP;
#endif
MultiSocketMonitor(EventLoop &_loop);
~MultiSocketMonitor();
#ifdef USE_INTERNAL_EVENTLOOP
using IdleMonitor::GetEventLoop;
#endif
#ifdef USE_GLIB_EVENTLOOP
EventLoop &GetEventLoop() {
return loop;
}
#endif
public:
#ifdef USE_GLIB_EVENTLOOP
gcc_pure
uint64_t GetTime() const {
return g_source_get_time(&source->base);
}
#endif
void InvalidateSockets() {
#ifdef USE_INTERNAL_EVENTLOOP
refresh = true;
IdleMonitor::Schedule();
#endif
#ifdef USE_GLIB_EVENTLOOP
/* no-op because GLib always calls the GSource's
"prepare" method before each poll() anyway */
#endif
}
void AddSocket(int fd, unsigned events) {
fds.emplace_front(*this, fd, events);
#ifdef USE_GLIB_EVENTLOOP
g_source_add_poll(&source->base, &fds.front().pfd);
#endif
}
template<typename E>
......@@ -212,13 +131,7 @@ public:
i->SetEvents(events);
prev = i;
} else {
#ifdef USE_INTERNAL_EVENTLOOP
i->Steal();
#endif
#ifdef USE_GLIB_EVENTLOOP
g_source_remove_poll(&source->base, &i->pfd);
#endif
fds.erase_after(prev);
}
}
......@@ -231,7 +144,6 @@ protected:
virtual int PrepareSockets() = 0;
virtual void DispatchSockets() = 0;
#ifdef USE_INTERNAL_EVENTLOOP
private:
void SetReady() {
ready = true;
......@@ -246,25 +158,6 @@ private:
}
virtual void OnIdle() final;
#endif
#ifdef USE_GLIB_EVENTLOOP
public:
/* GSource callbacks */
static gboolean Prepare(GSource *source, gint *timeout_r);
static gboolean Check(GSource *source);
static gboolean Dispatch(GSource *source, GSourceFunc callback,
gpointer user_data);
private:
bool Prepare(gint *timeout_r);
bool Check() const;
void Dispatch() {
DispatchSockets();
}
#endif
};
#endif
......@@ -31,8 +31,6 @@
#include <sys/socket.h>
#endif
#ifdef USE_INTERNAL_EVENTLOOP
void
SocketMonitor::Dispatch(unsigned flags)
{
......@@ -42,68 +40,6 @@ SocketMonitor::Dispatch(unsigned flags)
Cancel();
}
#endif
#ifdef USE_GLIB_EVENTLOOP
/*
* GSource methods
*
*/
gboolean
SocketMonitor::Prepare(gcc_unused GSource *source, gcc_unused gint *timeout_r)
{
return false;
}
gboolean
SocketMonitor::Check(GSource *_source)
{
const Source &source = *(const Source *)_source;
const SocketMonitor &monitor = *source.monitor;
assert(_source == &monitor.source->base);
return monitor.Check();
}
gboolean
SocketMonitor::Dispatch(GSource *_source,
gcc_unused GSourceFunc callback,
gcc_unused gpointer user_data)
{
Source &source = *(Source *)_source;
SocketMonitor &monitor = *source.monitor;
assert(_source == &monitor.source->base);
monitor.Dispatch();
return true;
}
/**
* The vtable for our GSource implementation. Unfortunately, we
* cannot declare it "const", because g_source_new() takes a non-const
* pointer, for whatever reason.
*/
static GSourceFuncs socket_monitor_source_funcs = {
SocketMonitor::Prepare,
SocketMonitor::Check,
SocketMonitor::Dispatch,
nullptr,
nullptr,
nullptr,
};
SocketMonitor::SocketMonitor(int _fd, EventLoop &_loop)
:fd(-1), loop(_loop),
source(nullptr) {
assert(_fd >= 0);
Open(_fd);
}
#endif
SocketMonitor::~SocketMonitor()
{
if (IsDefined())
......@@ -114,23 +50,9 @@ void
SocketMonitor::Open(int _fd)
{
assert(fd < 0);
#ifdef USE_GLIB_EVENTLOOP
assert(source == nullptr);
#endif
assert(_fd >= 0);
fd = _fd;
#ifdef USE_GLIB_EVENTLOOP
poll = {fd, 0, 0};
source = (Source *)g_source_new(&socket_monitor_source_funcs,
sizeof(*source));
source->monitor = this;
g_source_attach(&source->base, loop.GetContext());
g_source_add_poll(&source->base, &poll);
#endif
}
int
......@@ -143,12 +65,6 @@ SocketMonitor::Steal()
int result = fd;
fd = -1;
#ifdef USE_GLIB_EVENTLOOP
g_source_destroy(&source->base);
g_source_unref(&source->base);
source = nullptr;
#endif
return result;
}
......@@ -157,13 +73,9 @@ SocketMonitor::Abandon()
{
assert(IsDefined());
#ifdef USE_INTERNAL_EVENTLOOP
int old_fd = fd;
fd = -1;
loop.Abandon(old_fd, *this);
#else
Steal();
#endif
}
void
......@@ -180,7 +92,6 @@ SocketMonitor::Schedule(unsigned flags)
if (flags == GetScheduledFlags())
return;
#ifdef USE_INTERNAL_EVENTLOOP
if (scheduled_flags == 0)
loop.AddFD(fd, flags, *this);
else if (flags == 0)
......@@ -189,14 +100,6 @@ SocketMonitor::Schedule(unsigned flags)
loop.ModifyFD(fd, flags, *this);
scheduled_flags = flags;
#endif
#ifdef USE_GLIB_EVENTLOOP
poll.events = flags;
poll.revents &= flags;
loop.WakeUp();
#endif
}
SocketMonitor::ssize_t
......
......@@ -21,14 +21,7 @@
#define MPD_SOCKET_MONITOR_HXX
#include "check.h"
#ifdef USE_INTERNAL_EVENTLOOP
#include "PollGroup.hxx"
#endif
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
#include <type_traits>
......@@ -56,60 +49,27 @@ class EventLoop;
* as thread-safe.
*/
class SocketMonitor {
#ifdef USE_GLIB_EVENTLOOP
struct Source {
GSource base;
SocketMonitor *monitor;
};
#endif
int fd;
EventLoop &loop;
#ifdef USE_INTERNAL_EVENTLOOP
/**
* A bit mask of events that is currently registered in the EventLoop.
*/
unsigned scheduled_flags;
#endif
#ifdef USE_GLIB_EVENTLOOP
Source *source;
GPollFD poll;
#endif
public:
#ifdef USE_INTERNAL_EVENTLOOP
static constexpr unsigned READ = PollGroup::READ;
static constexpr unsigned WRITE = PollGroup::WRITE;
static constexpr unsigned ERROR = PollGroup::ERROR;
static constexpr unsigned HANGUP = PollGroup::HANGUP;
#endif
#ifdef USE_GLIB_EVENTLOOP
static constexpr unsigned READ = G_IO_IN;
static constexpr unsigned WRITE = G_IO_OUT;
static constexpr unsigned ERROR = G_IO_ERR;
static constexpr unsigned HANGUP = G_IO_HUP;
#endif
typedef std::make_signed<size_t>::type ssize_t;
#ifdef USE_INTERNAL_EVENTLOOP
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), scheduled_flags(0) {}
SocketMonitor(int _fd, EventLoop &_loop)
:fd(_fd), loop(_loop), scheduled_flags(0) {}
#endif
#ifdef USE_GLIB_EVENTLOOP
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), source(nullptr) {}
SocketMonitor(int _fd, EventLoop &_loop);
#endif
~SocketMonitor();
......@@ -145,13 +105,7 @@ public:
unsigned GetScheduledFlags() const {
assert(IsDefined());
#ifdef USE_INTERNAL_EVENTLOOP
return scheduled_flags;
#endif
#ifdef USE_GLIB_EVENTLOOP
return poll.events;
#endif
}
void Schedule(unsigned flags);
......@@ -186,30 +140,7 @@ protected:
virtual bool OnSocketReady(unsigned flags) = 0;
public:
#ifdef USE_INTERNAL_EVENTLOOP
void Dispatch(unsigned flags);
#endif
#ifdef USE_GLIB_EVENTLOOP
/* GSource callbacks */
static gboolean Prepare(GSource *source, gint *timeout_r);
static gboolean Check(GSource *source);
static gboolean Dispatch(GSource *source, GSourceFunc callback,
gpointer user_data);
private:
bool Check() const {
assert(IsDefined());
return (poll.revents & poll.events) != 0;
}
void Dispatch() {
assert(IsDefined());
OnSocketReady(poll.revents & poll.events);
}
#endif
};
#endif
......@@ -25,32 +25,19 @@ void
TimeoutMonitor::Cancel()
{
if (IsActive()) {
#ifdef USE_INTERNAL_EVENTLOOP
active = false;
loop.CancelTimer(*this);
#endif
#ifdef USE_GLIB_EVENTLOOP
g_source_destroy(source);
g_source_unref(source);
source = nullptr;
#endif
}
}
void
TimeoutMonitor::Schedule(unsigned ms)
{
Cancel();
#ifdef USE_INTERNAL_EVENTLOOP
active = true;
loop.AddTimer(*this, ms);
#endif
#ifdef USE_GLIB_EVENTLOOP
source = loop.AddTimeout(ms, Callback, this);
#endif
}
void
......@@ -58,33 +45,11 @@ TimeoutMonitor::ScheduleSeconds(unsigned s)
{
Cancel();
#ifdef USE_INTERNAL_EVENTLOOP
Schedule(s * 1000u);
#endif
#ifdef USE_GLIB_EVENTLOOP
source = loop.AddTimeoutSeconds(s, Callback, this);
#endif
}
void
TimeoutMonitor::Run()
{
#ifdef USE_GLIB_EVENTLOOP
Cancel();
#endif
OnTimeout();
}
#ifdef USE_GLIB_EVENTLOOP
gboolean
TimeoutMonitor::Callback(gpointer data)
{
TimeoutMonitor &monitor = *(TimeoutMonitor *)data;
monitor.Run();
return false;
}
#endif
......@@ -22,10 +22,6 @@
#include "check.h"
#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
class EventLoop;
/**
......@@ -37,31 +33,16 @@ class EventLoop;
* as thread-safe.
*/
class TimeoutMonitor {
#ifdef USE_INTERNAL_EVENTLOOP
friend class EventLoop;
#endif
EventLoop &loop;
#ifdef USE_INTERNAL_EVENTLOOP
bool active;
#endif
#ifdef USE_GLIB_EVENTLOOP
GSource *source;
#endif
public:
#ifdef USE_INTERNAL_EVENTLOOP
TimeoutMonitor(EventLoop &_loop)
:loop(_loop), active(false) {
}
#endif
#ifdef USE_GLIB_EVENTLOOP
TimeoutMonitor(EventLoop &_loop)
:loop(_loop), source(nullptr) {}
#endif
~TimeoutMonitor() {
Cancel();
......@@ -72,13 +53,7 @@ public:
}
bool IsActive() const {
#ifdef USE_INTERNAL_EVENTLOOP
return active;
#endif
#ifdef USE_GLIB_EVENTLOOP
return source != nullptr;
#endif
}
void Schedule(unsigned ms);
......@@ -90,10 +65,6 @@ protected:
private:
void Run();
#ifdef USE_GLIB_EVENTLOOP
static gboolean Callback(gpointer data);
#endif
};
#endif /* MAIN_NOTIFY_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment