Commit 1ac16516 authored by Max Kellermann's avatar Max Kellermann

event/TimerList: add option to avoid the Boost dependency

parent 75e8795e
......@@ -33,9 +33,14 @@
#pragma once
#include "Chrono.hxx"
#include "event/Features.h"
#include "util/BindMethod.hxx"
#ifdef NO_BOOST
#include "util/IntrusiveList.hxx"
#else
#include <boost/intrusive/set_hook.hpp>
#endif
class EventLoop;
......@@ -50,10 +55,17 @@ class EventLoop;
* thread that runs the #EventLoop, except where explicitly documented
* as thread-safe.
*/
class FineTimerEvent final
: public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
class FineTimerEvent final :
#ifdef NO_BOOST
AutoUnlinkIntrusiveListHook
#else
public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
#endif
{
friend class TimerList;
#ifdef NO_BOOST
friend class IntrusiveList<FineTimerEvent>;
#endif
EventLoop &loop;
......@@ -91,6 +103,9 @@ public:
void ScheduleEarlier(Event::Duration d) noexcept;
void Cancel() noexcept {
#ifdef NO_BOOST
if (IsPending())
#endif
unlink();
}
......
......@@ -33,6 +33,10 @@
#include "Loop.hxx"
#include "FineTimerEvent.hxx"
#ifdef NO_BOOST
#include <algorithm>
#endif
constexpr bool
TimerList::Compare::operator()(const FineTimerEvent &a,
const FineTimerEvent &b) const noexcept
......@@ -50,7 +54,15 @@ TimerList::~TimerList() noexcept
void
TimerList::Insert(FineTimerEvent &t) noexcept
{
#ifdef NO_BOOST
auto i = std::find_if(timers.begin(), timers.end(), [due = t.GetDue()](const auto &other){
return other.GetDue() >= due;
});
timers.insert(i, t);
#else
timers.insert(t);
#endif
}
Event::Duration
......@@ -66,7 +78,11 @@ TimerList::Run(const Event::TimePoint now) noexcept
if (timeout > timeout.zero())
return timeout;
#ifdef NO_BOOST
t.Cancel();
#else
timers.erase(i);
#endif
t.Run();
}
......
......@@ -33,9 +33,12 @@
#pragma once
#include "Chrono.hxx"
#include "event/Features.h"
#include "util/IntrusiveList.hxx"
#ifndef NO_BOOST
#include <boost/intrusive/set.hpp>
#endif
class FineTimerEvent;
......@@ -48,10 +51,17 @@ class TimerList final {
const FineTimerEvent &b) const noexcept;
};
#ifdef NO_BOOST
/* when building without Boost, then this is just a sorted
doubly-linked list - this doesn't scale well, but is good
enough for most programs */
IntrusiveList<FineTimerEvent> timers;
#else
boost::intrusive::multiset<FineTimerEvent,
boost::intrusive::base_hook<boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>>,
boost::intrusive::compare<Compare>,
boost::intrusive::constant_time_size<false>> timers;
#endif
public:
TimerList();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment