EventLoop: new implementation using epoll
Implement an event loop without GLib.
This commit is contained in:
@@ -32,6 +32,19 @@
|
||||
#include <sys/socket.h>
|
||||
#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
|
||||
|
Reference in New Issue
Block a user