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:
parent
010855a294
commit
a1309a90ac
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
|
|
||||||
|
#include <boost/intrusive/list_hook.hpp>
|
||||||
|
|
||||||
class EventLoop;
|
class EventLoop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,14 +32,16 @@ class EventLoop;
|
|||||||
* This class is thread-safe.
|
* This class is thread-safe.
|
||||||
*/
|
*/
|
||||||
class DeferredMonitor {
|
class DeferredMonitor {
|
||||||
EventLoop &loop;
|
|
||||||
|
|
||||||
friend class EventLoop;
|
friend class EventLoop;
|
||||||
bool pending;
|
|
||||||
|
typedef boost::intrusive::list_member_hook<> ListHook;
|
||||||
|
ListHook list_hook;
|
||||||
|
|
||||||
|
EventLoop &loop;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DeferredMonitor(EventLoop &_loop)
|
DeferredMonitor(EventLoop &_loop)
|
||||||
:loop(_loop), pending(false) {}
|
:loop(_loop) {}
|
||||||
|
|
||||||
~DeferredMonitor() {
|
~DeferredMonitor() {
|
||||||
Cancel();
|
Cancel();
|
||||||
@ -50,8 +54,13 @@ public:
|
|||||||
void Schedule();
|
void Schedule();
|
||||||
void Cancel();
|
void Cancel();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool IsPending() const {
|
||||||
|
return list_hook.is_linked();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void RunDeferred() = 0;
|
virtual void RunDeferred() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* MAIN_NOTIFY_H */
|
#endif
|
||||||
|
@ -31,7 +31,6 @@ IdleMonitor::Cancel()
|
|||||||
if (!IsActive())
|
if (!IsActive())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
active = false;
|
|
||||||
loop.RemoveIdle(*this);
|
loop.RemoveIdle(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +43,6 @@ IdleMonitor::Schedule()
|
|||||||
/* already scheduled */
|
/* already scheduled */
|
||||||
return;
|
return;
|
||||||
|
|
||||||
active = true;
|
|
||||||
loop.AddIdle(*this);
|
loop.AddIdle(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +51,5 @@ IdleMonitor::Run()
|
|||||||
{
|
{
|
||||||
assert(loop.IsInside());
|
assert(loop.IsInside());
|
||||||
|
|
||||||
assert(active);
|
|
||||||
active = false;
|
|
||||||
|
|
||||||
OnIdle();
|
OnIdle();
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
|
|
||||||
|
#include <boost/intrusive/list_hook.hpp>
|
||||||
|
|
||||||
class EventLoop;
|
class EventLoop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,13 +37,14 @@ class EventLoop;
|
|||||||
class IdleMonitor {
|
class IdleMonitor {
|
||||||
friend class EventLoop;
|
friend class EventLoop;
|
||||||
|
|
||||||
EventLoop &loop;
|
typedef boost::intrusive::list_member_hook<> ListHook;
|
||||||
|
ListHook list_hook;
|
||||||
|
|
||||||
bool active;
|
EventLoop &loop;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IdleMonitor(EventLoop &_loop)
|
IdleMonitor(EventLoop &_loop)
|
||||||
:loop(_loop), active(false) {}
|
:loop(_loop) {}
|
||||||
|
|
||||||
~IdleMonitor() {
|
~IdleMonitor() {
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
@ -57,7 +60,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool IsActive() const {
|
bool IsActive() const {
|
||||||
return active;
|
return list_hook.is_linked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Schedule();
|
void Schedule();
|
||||||
|
@ -24,8 +24,6 @@
|
|||||||
#include "DeferredMonitor.hxx"
|
#include "DeferredMonitor.hxx"
|
||||||
#include "util/ScopeExit.hxx"
|
#include "util/ScopeExit.hxx"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
EventLoop::EventLoop(ThreadId _thread)
|
EventLoop::EventLoop(ThreadId _thread)
|
||||||
:SocketMonitor(*this), thread(_thread)
|
:SocketMonitor(*this), thread(_thread)
|
||||||
{
|
{
|
||||||
@ -70,9 +68,8 @@ void
|
|||||||
EventLoop::AddIdle(IdleMonitor &i)
|
EventLoop::AddIdle(IdleMonitor &i)
|
||||||
{
|
{
|
||||||
assert(IsInside());
|
assert(IsInside());
|
||||||
assert(std::find(idle.begin(), idle.end(), &i) == idle.end());
|
|
||||||
|
|
||||||
idle.push_back(&i);
|
idle.push_back(i);
|
||||||
again = true;
|
again = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,10 +78,7 @@ EventLoop::RemoveIdle(IdleMonitor &i)
|
|||||||
{
|
{
|
||||||
assert(IsInside());
|
assert(IsInside());
|
||||||
|
|
||||||
auto it = std::find(idle.begin(), idle.end(), &i);
|
idle.erase(idle.iterator_to(i));
|
||||||
assert(it != idle.end());
|
|
||||||
|
|
||||||
idle.erase(it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -161,7 +155,7 @@ EventLoop::Run()
|
|||||||
/* invoke idle */
|
/* invoke idle */
|
||||||
|
|
||||||
while (!idle.empty()) {
|
while (!idle.empty()) {
|
||||||
IdleMonitor &m = *idle.front();
|
IdleMonitor &m = idle.front();
|
||||||
idle.pop_front();
|
idle.pop_front();
|
||||||
m.Run();
|
m.Run();
|
||||||
|
|
||||||
@ -223,18 +217,14 @@ EventLoop::AddDeferred(DeferredMonitor &d)
|
|||||||
|
|
||||||
{
|
{
|
||||||
const std::lock_guard<Mutex> lock(mutex);
|
const std::lock_guard<Mutex> lock(mutex);
|
||||||
if (d.pending)
|
if (d.IsPending())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
assert(std::find(deferred.begin(),
|
|
||||||
deferred.end(), &d) == deferred.end());
|
|
||||||
|
|
||||||
/* we don't need to wake up the EventLoop if another
|
/* we don't need to wake up the EventLoop if another
|
||||||
DeferredMonitor has already done it */
|
DeferredMonitor has already done it */
|
||||||
must_wake = !busy && deferred.empty();
|
must_wake = !busy && deferred.empty();
|
||||||
|
|
||||||
d.pending = true;
|
deferred.push_back(d);
|
||||||
deferred.push_back(&d);
|
|
||||||
again = true;
|
again = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,29 +237,18 @@ EventLoop::RemoveDeferred(DeferredMonitor &d)
|
|||||||
{
|
{
|
||||||
const std::lock_guard<Mutex> protect(mutex);
|
const std::lock_guard<Mutex> protect(mutex);
|
||||||
|
|
||||||
if (!d.pending) {
|
if (!d.IsPending())
|
||||||
assert(std::find(deferred.begin(),
|
deferred.erase(deferred.iterator_to(d));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
EventLoop::HandleDeferred()
|
EventLoop::HandleDeferred()
|
||||||
{
|
{
|
||||||
while (!deferred.empty() && !quit) {
|
while (!deferred.empty() && !quit) {
|
||||||
DeferredMonitor &m = *deferred.front();
|
DeferredMonitor &m = deferred.front();
|
||||||
assert(m.pending);
|
assert(m.IsPending());
|
||||||
|
|
||||||
deferred.pop_front();
|
deferred.pop_front();
|
||||||
m.pending = false;
|
|
||||||
|
|
||||||
const ScopeUnlock unlock(mutex);
|
const ScopeUnlock unlock(mutex);
|
||||||
m.RunDeferred();
|
m.RunDeferred();
|
||||||
|
@ -29,16 +29,13 @@
|
|||||||
#include "WakeFD.hxx"
|
#include "WakeFD.hxx"
|
||||||
#include "SocketMonitor.hxx"
|
#include "SocketMonitor.hxx"
|
||||||
#include "TimeoutMonitor.hxx"
|
#include "TimeoutMonitor.hxx"
|
||||||
|
#include "IdleMonitor.hxx"
|
||||||
|
#include "DeferredMonitor.hxx"
|
||||||
|
|
||||||
#include <boost/intrusive/set.hpp>
|
#include <boost/intrusive/set.hpp>
|
||||||
|
#include <boost/intrusive/list.hpp>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <list>
|
|
||||||
#include <set>
|
|
||||||
|
|
||||||
class TimeoutMonitor;
|
|
||||||
class IdleMonitor;
|
|
||||||
class DeferredMonitor;
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@ -70,10 +67,21 @@ class EventLoop final : SocketMonitor
|
|||||||
boost::intrusive::constant_time_size<false>> TimerSet;
|
boost::intrusive::constant_time_size<false>> TimerSet;
|
||||||
TimerSet timers;
|
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;
|
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();
|
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user