Skip to content

Commit

Permalink
Add inactivity timeout to event loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-seddon committed May 30, 2015
1 parent 668e9e7 commit 4c5320c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 19 additions & 1 deletion rct/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ EventLoop::EventLoop()
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
pollFd(-1),
#endif
nextTimerId(0), stop(false), timeout(false), flgs(0)
nextTimerId(0), stop(false), timeout(false), flgs(0), inactivityTimeout(0)
{
std::call_once(mainOnce, [](){
mainEventPipe = -1;
Expand Down Expand Up @@ -802,6 +802,11 @@ unsigned int EventLoop::processSocketEvents(NativeEvent* events, int eventCount)
return all;
}

void EventLoop::setInactivityTimeout(int timeout)
{
inactivityTimeout = timeout;
}

unsigned int EventLoop::exec(int timeoutTime)
{
int quitTimerId = -1;
Expand All @@ -821,6 +826,7 @@ unsigned int EventLoop::exec(int timeoutTime)
break;
}
int waitUntil = -1;
bool waitingForInactivityTimeout = false;
{
std::lock_guard<std::mutex> locker(mutex);

Expand All @@ -841,6 +847,15 @@ unsigned int EventLoop::exec(int timeoutTime)
const uint64_t now = currentTime();
waitUntil = std::max<int>((*timer)->when - now, 0);
}

if (inactivityTimeout > 0) {
if (inactivityTimeout > waitUntil) {
waitUntil = inactivityTimeout;
waitingForInactivityTimeout = true;
}
}
printf("inactivityTimeout = %d waitUntil = %d waitingForInactivityTimeout = %d\n",
inactivityTimeout, waitUntil, waitingForInactivityTimeout);
}
int eventCount;
#if defined(HAVE_EPOLL)
Expand Down Expand Up @@ -903,6 +918,9 @@ unsigned int EventLoop::exec(int timeoutTime)
ret = processSocketEvents(events, eventCount);
if (ret & (Success|GeneralError|Timeout))
break;
} else if (eventCount == 0 && waitingForInactivityTimeout) {
this->timeout = true;
quit();
}
}

Expand Down
8 changes: 7 additions & 1 deletion rct/EventLoop.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef EVENTLOOP_H
#ifndef EVENTLOOP_H // -*- mode:c++ -*-
#define EVENTLOOP_H

#include <map>
Expand Down Expand Up @@ -145,6 +145,10 @@ class EventLoop : public std::enable_shared_from_this<EventLoop>
int registerTimer(std::function<void(int)>&& func, int timeout, unsigned int flags = 0);
void unregisterTimer(int id);

// Changes to the inactivity timeout while the loop is running may
// not be honoured.
void setInactivityTimeout(int timeout);

enum { Success = 0x100, GeneralError = 0x200, Timeout = 0x400 };
unsigned int exec(int timeout = -1);
void quit();
Expand Down Expand Up @@ -251,6 +255,8 @@ class EventLoop : public std::enable_shared_from_this<EventLoop>
static EventLoop::WeakPtr mainLoop;

unsigned int flgs;

int inactivityTimeout;
private:
EventLoop(const EventLoop&) = delete;
EventLoop& operator=(const EventLoop&) = delete;
Expand Down

0 comments on commit 4c5320c

Please sign in to comment.