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 @@ ...@@ -33,9 +33,14 @@
#pragma once #pragma once
#include "Chrono.hxx" #include "Chrono.hxx"
#include "event/Features.h"
#include "util/BindMethod.hxx" #include "util/BindMethod.hxx"
#ifdef NO_BOOST
#include "util/IntrusiveList.hxx"
#else
#include <boost/intrusive/set_hook.hpp> #include <boost/intrusive/set_hook.hpp>
#endif
class EventLoop; class EventLoop;
...@@ -50,10 +55,17 @@ class EventLoop; ...@@ -50,10 +55,17 @@ class EventLoop;
* thread that runs the #EventLoop, except where explicitly documented * thread that runs the #EventLoop, except where explicitly documented
* as thread-safe. * as thread-safe.
*/ */
class FineTimerEvent final class FineTimerEvent final :
: public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>> #ifdef NO_BOOST
AutoUnlinkIntrusiveListHook
#else
public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
#endif
{ {
friend class TimerList; friend class TimerList;
#ifdef NO_BOOST
friend class IntrusiveList<FineTimerEvent>;
#endif
EventLoop &loop; EventLoop &loop;
...@@ -91,7 +103,10 @@ public: ...@@ -91,7 +103,10 @@ public:
void ScheduleEarlier(Event::Duration d) noexcept; void ScheduleEarlier(Event::Duration d) noexcept;
void Cancel() noexcept { void Cancel() noexcept {
unlink(); #ifdef NO_BOOST
if (IsPending())
#endif
unlink();
} }
private: private:
......
...@@ -33,6 +33,10 @@ ...@@ -33,6 +33,10 @@
#include "Loop.hxx" #include "Loop.hxx"
#include "FineTimerEvent.hxx" #include "FineTimerEvent.hxx"
#ifdef NO_BOOST
#include <algorithm>
#endif
constexpr bool constexpr bool
TimerList::Compare::operator()(const FineTimerEvent &a, TimerList::Compare::operator()(const FineTimerEvent &a,
const FineTimerEvent &b) const noexcept const FineTimerEvent &b) const noexcept
...@@ -50,7 +54,15 @@ TimerList::~TimerList() noexcept ...@@ -50,7 +54,15 @@ TimerList::~TimerList() noexcept
void void
TimerList::Insert(FineTimerEvent &t) noexcept 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); timers.insert(t);
#endif
} }
Event::Duration Event::Duration
...@@ -66,7 +78,11 @@ TimerList::Run(const Event::TimePoint now) noexcept ...@@ -66,7 +78,11 @@ TimerList::Run(const Event::TimePoint now) noexcept
if (timeout > timeout.zero()) if (timeout > timeout.zero())
return timeout; return timeout;
#ifdef NO_BOOST
t.Cancel();
#else
timers.erase(i); timers.erase(i);
#endif
t.Run(); t.Run();
} }
......
...@@ -33,9 +33,12 @@ ...@@ -33,9 +33,12 @@
#pragma once #pragma once
#include "Chrono.hxx" #include "Chrono.hxx"
#include "event/Features.h"
#include "util/IntrusiveList.hxx" #include "util/IntrusiveList.hxx"
#ifndef NO_BOOST
#include <boost/intrusive/set.hpp> #include <boost/intrusive/set.hpp>
#endif
class FineTimerEvent; class FineTimerEvent;
...@@ -48,10 +51,17 @@ class TimerList final { ...@@ -48,10 +51,17 @@ class TimerList final {
const FineTimerEvent &b) const noexcept; 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::multiset<FineTimerEvent,
boost::intrusive::base_hook<boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>>, boost::intrusive::base_hook<boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>>,
boost::intrusive::compare<Compare>, boost::intrusive::compare<Compare>,
boost::intrusive::constant_time_size<false>> timers; boost::intrusive::constant_time_size<false>> timers;
#endif
public: public:
TimerList(); 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