aboutsummaryrefslogtreecommitdiffstats
path: root/src/event/SocketMonitor.hxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-08-07 22:16:59 +0200
committerMax Kellermann <max@duempel.org>2013-08-10 13:54:23 +0200
commitc1f4f1fdb64d97b5c3461723a8482ca64efea30e (patch)
tree54c8a9c1466beec0dbfac1c0b5f5773060c1aa2b /src/event/SocketMonitor.hxx
parent342333f72a484e9f394026666c4b20e54dc9b756 (diff)
downloadmpd-c1f4f1fdb64d97b5c3461723a8482ca64efea30e.tar.gz
mpd-c1f4f1fdb64d97b5c3461723a8482ca64efea30e.tar.xz
mpd-c1f4f1fdb64d97b5c3461723a8482ca64efea30e.zip
EventLoop: new implementation using epoll
Implement an event loop without GLib.
Diffstat (limited to 'src/event/SocketMonitor.hxx')
-rw-r--r--src/event/SocketMonitor.hxx38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx
index 85866280c..d6efeac17 100644
--- a/src/event/SocketMonitor.hxx
+++ b/src/event/SocketMonitor.hxx
@@ -22,7 +22,11 @@
#include "check.h"
+#ifdef USE_EPOLL
+#include <sys/epoll.h>
+#else
#include <glib.h>
+#endif
#include <type_traits>
@@ -40,29 +44,55 @@
class EventLoop;
class SocketMonitor {
+#ifdef USE_EPOLL
+#else
struct Source {
GSource base;
SocketMonitor *monitor;
};
+#endif
int fd;
EventLoop &loop;
+
+#ifdef USE_EPOLL
+ /**
+ * A bit mask of events that is currently registered in the EventLoop.
+ */
+ unsigned scheduled_flags;
+#else
Source *source;
GPollFD poll;
+#endif
public:
+#ifdef USE_EPOLL
+ static constexpr unsigned READ = EPOLLIN;
+ static constexpr unsigned WRITE = EPOLLOUT;
+ static constexpr unsigned ERROR = EPOLLERR;
+ static constexpr unsigned HANGUP = EPOLLHUP;
+#else
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_EPOLL
+ SocketMonitor(EventLoop &_loop)
+ :fd(-1), loop(_loop), scheduled_flags(0) {}
+
+ SocketMonitor(int _fd, EventLoop &_loop)
+ :fd(_fd), loop(_loop), scheduled_flags(0) {}
+#else
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), source(nullptr) {}
SocketMonitor(int _fd, EventLoop &_loop);
+#endif
~SocketMonitor();
@@ -93,7 +123,11 @@ public:
unsigned GetScheduledFlags() const {
assert(IsDefined());
+#ifdef USE_EPOLL
+ return scheduled_flags;
+#else
return poll.events;
+#endif
}
void Schedule(unsigned flags);
@@ -128,6 +162,9 @@ protected:
virtual bool OnSocketReady(unsigned flags) = 0;
public:
+#ifdef USE_EPOLL
+ void Dispatch(unsigned flags);
+#else
/* GSource callbacks */
static gboolean Prepare(GSource *source, gint *timeout_r);
static gboolean Check(GSource *source);
@@ -146,6 +183,7 @@ private:
OnSocketReady(poll.revents & poll.events);
}
+#endif
};
#endif