From c1f4f1fdb64d97b5c3461723a8482ca64efea30e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 7 Aug 2013 22:16:59 +0200 Subject: EventLoop: new implementation using epoll Implement an event loop without GLib. --- src/event/SocketMonitor.cxx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/event/SocketMonitor.cxx') diff --git a/src/event/SocketMonitor.cxx b/src/event/SocketMonitor.cxx index 0b929dd72..76dab9346 100644 --- a/src/event/SocketMonitor.cxx +++ b/src/event/SocketMonitor.cxx @@ -32,6 +32,19 @@ #include #endif +#ifdef USE_EPOLL + +void +SocketMonitor::Dispatch(unsigned flags) +{ + flags &= GetScheduledFlags(); + + if (flags != 0 && !OnSocketReady(flags) && IsDefined()) + Cancel(); +} + +#else + /* * GSource methods * @@ -88,6 +101,8 @@ SocketMonitor::SocketMonitor(int _fd, EventLoop &_loop) Open(_fd); } +#endif + SocketMonitor::~SocketMonitor() { if (IsDefined()) @@ -98,10 +113,14 @@ void SocketMonitor::Open(int _fd) { assert(fd < 0); +#ifndef USE_EPOLL assert(source == nullptr); +#endif assert(_fd >= 0); fd = _fd; + +#ifndef USE_EPOLL poll = {fd, 0, 0}; source = (Source *)g_source_new(&socket_monitor_source_funcs, @@ -110,6 +129,7 @@ SocketMonitor::Open(int _fd) g_source_attach(&source->base, loop.GetContext()); g_source_add_poll(&source->base, &poll); +#endif } int @@ -122,9 +142,11 @@ SocketMonitor::Steal() int result = fd; fd = -1; +#ifndef USE_EPOLL g_source_destroy(&source->base); g_source_unref(&source->base); source = nullptr; +#endif return result; } @@ -143,10 +165,21 @@ SocketMonitor::Schedule(unsigned flags) if (flags == GetScheduledFlags()) return; +#ifdef USE_EPOLL + if (scheduled_flags == 0) + loop.AddFD(fd, flags, *this); + else if (flags == 0) + loop.RemoveFD(fd, *this); + else + loop.ModifyFD(fd, flags, *this); + + scheduled_flags = flags; +#else poll.events = flags; poll.revents &= flags; loop.WakeUp(); +#endif } SocketMonitor::ssize_t -- cgit v1.2.3