event/TimeoutMonitor: move code to new class TimerEvent

This commit is contained in:
Max Kellermann 2017-08-29 15:53:57 +02:00
parent 2f0d683378
commit 30a5dd267b
6 changed files with 115 additions and 40 deletions

View File

@ -514,7 +514,8 @@ libevent_a_SOURCES = \
src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx \ src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx \
src/event/PollResultGeneric.hxx \ src/event/PollResultGeneric.hxx \
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \ src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \ src/event/TimerEvent.hxx src/event/TimerEvent.cxx \
src/event/TimeoutMonitor.hxx \
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \ src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx \ src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx \
src/event/DeferredCall.hxx \ src/event/DeferredCall.hxx \

View File

@ -82,7 +82,7 @@ EventLoop::RemoveIdle(IdleMonitor &i)
} }
void void
EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d) EventLoop::AddTimer(TimerEvent &t, std::chrono::steady_clock::duration d)
{ {
assert(IsInside()); assert(IsInside());
@ -92,7 +92,7 @@ EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d)
} }
void void
EventLoop::CancelTimer(TimeoutMonitor &t) EventLoop::CancelTimer(TimerEvent &t)
{ {
assert(IsInside()); assert(IsInside());
@ -139,14 +139,14 @@ EventLoop::Run()
break; break;
} }
TimeoutMonitor &m = *i; TimerEvent &t = *i;
timeout = m.due - now; timeout = t.due - now;
if (timeout > timeout.zero()) if (timeout > timeout.zero())
break; break;
timers.erase(i); timers.erase(i);
m.Run(); t.Run();
if (quit) if (quit)
return; return;

View File

@ -28,7 +28,7 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "WakeFD.hxx" #include "WakeFD.hxx"
#include "SocketMonitor.hxx" #include "SocketMonitor.hxx"
#include "TimeoutMonitor.hxx" #include "TimerEvent.hxx"
#include "IdleMonitor.hxx" #include "IdleMonitor.hxx"
#include "DeferredMonitor.hxx" #include "DeferredMonitor.hxx"
@ -46,23 +46,23 @@
* thread that runs it, except where explicitly documented as * thread that runs it, except where explicitly documented as
* thread-safe. * thread-safe.
* *
* @see SocketMonitor, MultiSocketMonitor, TimeoutMonitor, IdleMonitor * @see SocketMonitor, MultiSocketMonitor, TimerEvent, IdleMonitor
*/ */
class EventLoop final : SocketMonitor class EventLoop final : SocketMonitor
{ {
WakeFD wake_fd; WakeFD wake_fd;
struct TimerCompare { struct TimerCompare {
constexpr bool operator()(const TimeoutMonitor &a, constexpr bool operator()(const TimerEvent &a,
const TimeoutMonitor &b) const { const TimerEvent &b) const {
return a.due < b.due; return a.due < b.due;
} }
}; };
typedef boost::intrusive::multiset<TimeoutMonitor, typedef boost::intrusive::multiset<TimerEvent,
boost::intrusive::member_hook<TimeoutMonitor, boost::intrusive::member_hook<TimerEvent,
TimeoutMonitor::TimerSetHook, TimerEvent::TimerSetHook,
&TimeoutMonitor::timer_set_hook>, &TimerEvent::timer_set_hook>,
boost::intrusive::compare<TimerCompare>, boost::intrusive::compare<TimerCompare>,
boost::intrusive::constant_time_size<false>> TimerSet; boost::intrusive::constant_time_size<false>> TimerSet;
TimerSet timers; TimerSet timers;
@ -155,9 +155,9 @@ public:
void AddIdle(IdleMonitor &i); void AddIdle(IdleMonitor &i);
void RemoveIdle(IdleMonitor &i); void RemoveIdle(IdleMonitor &i);
void AddTimer(TimeoutMonitor &t, void AddTimer(TimerEvent &t,
std::chrono::steady_clock::duration d); std::chrono::steady_clock::duration d);
void CancelTimer(TimeoutMonitor &t); void CancelTimer(TimerEvent &t);
/** /**
* Schedule a call to DeferredMonitor::RunDeferred(). * Schedule a call to DeferredMonitor::RunDeferred().

View File

@ -21,6 +21,7 @@
#define MPD_SOCKET_TIMEOUT_MONITOR_HXX #define MPD_SOCKET_TIMEOUT_MONITOR_HXX
#include "check.h" #include "check.h"
#include "TimerEvent.hxx"
#include <boost/intrusive/set_hook.hpp> #include <boost/intrusive/set_hook.hpp>
@ -37,38 +38,28 @@ class EventLoop;
* as thread-safe. * as thread-safe.
*/ */
class TimeoutMonitor { class TimeoutMonitor {
friend class EventLoop; TimerEvent timer;
typedef boost::intrusive::set_member_hook<> TimerSetHook;
TimerSetHook timer_set_hook;
EventLoop &loop;
/**
* When is this timer due? This is only valid if #active is
* true.
*/
std::chrono::steady_clock::time_point due;
public: public:
TimeoutMonitor(EventLoop &_loop) TimeoutMonitor(EventLoop &_loop)
:loop(_loop) { :timer(_loop, BIND_THIS_METHOD(Run)) {
}
~TimeoutMonitor() {
Cancel();
} }
EventLoop &GetEventLoop() { EventLoop &GetEventLoop() {
return loop; return timer.GetEventLoop();
} }
bool IsActive() const { bool IsActive() const {
return timer_set_hook.is_linked(); return timer.IsActive();
} }
void Schedule(std::chrono::steady_clock::duration d); void Schedule(std::chrono::steady_clock::duration d) {
void Cancel(); timer.Schedule(d);
}
void Cancel() {
timer.Cancel();
}
protected: protected:
virtual void OnTimeout() = 0; virtual void OnTimeout() = 0;

View File

@ -18,18 +18,18 @@
*/ */
#include "config.h" #include "config.h"
#include "TimeoutMonitor.hxx" #include "TimerEvent.hxx"
#include "Loop.hxx" #include "Loop.hxx"
void void
TimeoutMonitor::Cancel() TimerEvent::Cancel()
{ {
if (IsActive()) if (IsActive())
loop.CancelTimer(*this); loop.CancelTimer(*this);
} }
void void
TimeoutMonitor::Schedule(std::chrono::steady_clock::duration d) TimerEvent::Schedule(std::chrono::steady_clock::duration d)
{ {
Cancel(); Cancel();

83
src/event/TimerEvent.hxx Normal file
View File

@ -0,0 +1,83 @@
/*
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_TIMER_EVENT_HXX
#define MPD_TIMER_EVENT_HXX
#include "check.h"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
#include <chrono>
class EventLoop;
/**
* This class invokes a callback function after a certain amount of
* time. Use Schedule() to start the timer or Cancel() to cancel it.
*
* This class is not thread-safe, all methods must be called from the
* thread that runs the #EventLoop, except where explicitly documented
* as thread-safe.
*/
class TimerEvent final {
friend class EventLoop;
typedef boost::intrusive::set_member_hook<> TimerSetHook;
TimerSetHook timer_set_hook;
EventLoop &loop;
typedef BoundMethod<void()> Callback;
const Callback callback;
/**
* When is this timer due? This is only valid if IsActive()
* returns true.
*/
std::chrono::steady_clock::time_point due;
public:
TimerEvent(EventLoop &_loop, Callback _callback)
:loop(_loop), callback(_callback) {
}
~TimerEvent() {
Cancel();
}
EventLoop &GetEventLoop() {
return loop;
}
bool IsActive() const {
return timer_set_hook.is_linked();
}
void Schedule(std::chrono::steady_clock::duration d);
void Cancel();
private:
void Run() {
callback();
}
};
#endif /* MAIN_NOTIFY_H */