event/Loop: use boost::intrusive::list to store IdleMonitors and DeferredMonitors

The intrusive contains can easily erase items without searching
through the whole list.  This removes a good amount of runtime
overhead.
This commit is contained in:
Max Kellermann 2017-08-29 15:43:16 +02:00
parent 010855a294
commit a1309a90ac
5 changed files with 46 additions and 52 deletions

View File

@ -22,6 +22,8 @@
#include "check.h"
#include <boost/intrusive/list_hook.hpp>
class EventLoop;
/**
@ -30,14 +32,16 @@ class EventLoop;
* This class is thread-safe.
*/
class DeferredMonitor {
EventLoop &loop;
friend class EventLoop;
bool pending;
typedef boost::intrusive::list_member_hook<> ListHook;
ListHook list_hook;
EventLoop &loop;
public:
DeferredMonitor(EventLoop &_loop)
:loop(_loop), pending(false) {}
:loop(_loop) {}
~DeferredMonitor() {
Cancel();
@ -50,8 +54,13 @@ public:
void Schedule();
void Cancel();
private:
bool IsPending() const {
return list_hook.is_linked();
}
protected:
virtual void RunDeferred() = 0;
};
#endif /* MAIN_NOTIFY_H */
#endif

View File

@ -31,7 +31,6 @@ IdleMonitor::Cancel()
if (!IsActive())
return;
active = false;
loop.RemoveIdle(*this);
}
@ -44,7 +43,6 @@ IdleMonitor::Schedule()
/* already scheduled */
return;
active = true;
loop.AddIdle(*this);
}
@ -53,8 +51,5 @@ IdleMonitor::Run()
{
assert(loop.IsInside());
assert(active);
active = false;
OnIdle();
}

View File

@ -22,6 +22,8 @@
#include "check.h"
#include <boost/intrusive/list_hook.hpp>
class EventLoop;
/**
@ -35,13 +37,14 @@ class EventLoop;
class IdleMonitor {
friend class EventLoop;
EventLoop &loop;
typedef boost::intrusive::list_member_hook<> ListHook;
ListHook list_hook;
bool active;
EventLoop &loop;
public:
IdleMonitor(EventLoop &_loop)
:loop(_loop), active(false) {}
:loop(_loop) {}
~IdleMonitor() {
#ifndef NDEBUG
@ -57,7 +60,7 @@ public:
}
bool IsActive() const {
return active;
return list_hook.is_linked();
}
void Schedule();

View File

@ -24,8 +24,6 @@
#include "DeferredMonitor.hxx"
#include "util/ScopeExit.hxx"
#include <algorithm>
EventLoop::EventLoop(ThreadId _thread)
:SocketMonitor(*this), thread(_thread)
{
@ -70,9 +68,8 @@ void
EventLoop::AddIdle(IdleMonitor &i)
{
assert(IsInside());
assert(std::find(idle.begin(), idle.end(), &i) == idle.end());
idle.push_back(&i);
idle.push_back(i);
again = true;
}
@ -81,10 +78,7 @@ EventLoop::RemoveIdle(IdleMonitor &i)
{
assert(IsInside());
auto it = std::find(idle.begin(), idle.end(), &i);
assert(it != idle.end());
idle.erase(it);
idle.erase(idle.iterator_to(i));
}
void
@ -161,7 +155,7 @@ EventLoop::Run()
/* invoke idle */
while (!idle.empty()) {
IdleMonitor &m = *idle.front();
IdleMonitor &m = idle.front();
idle.pop_front();
m.Run();
@ -223,18 +217,14 @@ EventLoop::AddDeferred(DeferredMonitor &d)
{
const std::lock_guard<Mutex> lock(mutex);
if (d.pending)
if (d.IsPending())
return;
assert(std::find(deferred.begin(),
deferred.end(), &d) == deferred.end());
/* we don't need to wake up the EventLoop if another
DeferredMonitor has already done it */
must_wake = !busy && deferred.empty();
d.pending = true;
deferred.push_back(&d);
deferred.push_back(d);
again = true;
}
@ -247,29 +237,18 @@ EventLoop::RemoveDeferred(DeferredMonitor &d)
{
const std::lock_guard<Mutex> protect(mutex);
if (!d.pending) {
assert(std::find(deferred.begin(),
deferred.end(), &d) == deferred.end());
return;
}
d.pending = false;
auto i = std::find(deferred.begin(), deferred.end(), &d);
assert(i != deferred.end());
deferred.erase(i);
if (!d.IsPending())
deferred.erase(deferred.iterator_to(d));
}
void
EventLoop::HandleDeferred()
{
while (!deferred.empty() && !quit) {
DeferredMonitor &m = *deferred.front();
assert(m.pending);
DeferredMonitor &m = deferred.front();
assert(m.IsPending());
deferred.pop_front();
m.pending = false;
const ScopeUnlock unlock(mutex);
m.RunDeferred();

View File

@ -29,16 +29,13 @@
#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
#include "TimeoutMonitor.hxx"
#include "IdleMonitor.hxx"
#include "DeferredMonitor.hxx"
#include <boost/intrusive/set.hpp>
#include <boost/intrusive/list.hpp>
#include <chrono>
#include <list>
#include <set>
class TimeoutMonitor;
class IdleMonitor;
class DeferredMonitor;
#include <assert.h>
@ -70,10 +67,21 @@ class EventLoop final : SocketMonitor
boost::intrusive::constant_time_size<false>> TimerSet;
TimerSet timers;
std::list<IdleMonitor *> idle;
typedef boost::intrusive::list<IdleMonitor,
boost::intrusive::member_hook<IdleMonitor,
IdleMonitor::ListHook,
&IdleMonitor::list_hook>,
boost::intrusive::constant_time_size<false>> IdleList;
IdleList idle;
Mutex mutex;
std::list<DeferredMonitor *> deferred;
typedef boost::intrusive::list<DeferredMonitor,
boost::intrusive::member_hook<DeferredMonitor,
DeferredMonitor::ListHook,
&DeferredMonitor::list_hook>,
boost::intrusive::constant_time_size<false>> DeferredList;
DeferredList deferred;
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();