event/TimerEvent: rename to FineTimerEvent

... and make TimerEvent a type alias for FineTimerEvent (i.e. swap
names).
This commit is contained in:
Max Kellermann 2021-02-04 22:45:11 +01:00 committed by Max Kellermann
parent def962b6cb
commit 1b4fd74575
9 changed files with 160 additions and 143 deletions

View File

@ -0,0 +1,59 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "FineTimerEvent.hxx"
#include "Loop.hxx"
void
FineTimerEvent::Schedule(Event::Duration d) noexcept
{
Cancel();
due = loop.SteadyNow() + d;
loop.Insert(*this);
}
void
FineTimerEvent::ScheduleEarlier(Event::Duration d) noexcept
{
const auto new_due = loop.SteadyNow() + d;
if (IsPending()) {
if (new_due >= due)
return;
Cancel();
}
due = new_due;
loop.Insert(*this);
}

View File

@ -32,7 +32,12 @@
#pragma once #pragma once
#include "TimerEvent.hxx" #include "Chrono.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
class EventLoop;
/** /**
* This class invokes a callback function after a certain amount of * This class invokes a callback function after a certain amount of
@ -45,5 +50,52 @@
* thread that runs the #EventLoop, except where explicitly documented * thread that runs the #EventLoop, except where explicitly documented
* as thread-safe. * as thread-safe.
*/ */
using FineTimerEvent = TimerEvent; class FineTimerEvent final
// TODO: implement : public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
{
friend class TimerList;
EventLoop &loop;
using Callback = BoundMethod<void() noexcept>;
const Callback callback;
/**
* When is this timer due? This is only valid if IsPending()
* returns true.
*/
Event::Clock::time_point due;
public:
FineTimerEvent(EventLoop &_loop, Callback _callback) noexcept
:loop(_loop), callback(_callback) {}
auto &GetEventLoop() const noexcept {
return loop;
}
constexpr auto GetDue() const noexcept {
return due;
}
bool IsPending() const noexcept {
return is_linked();
}
void Schedule(Event::Duration d) noexcept;
/**
* Like Schedule(), but is a no-op if there is a due time
* earlier than the given one.
*/
void ScheduleEarlier(Event::Duration d) noexcept;
void Cancel() noexcept {
unlink();
}
private:
void Run() noexcept {
callback();
}
};

View File

@ -18,7 +18,6 @@
*/ */
#include "Loop.hxx" #include "Loop.hxx"
#include "TimerEvent.hxx"
#include "DeferEvent.hxx" #include "DeferEvent.hxx"
#include "SocketEvent.hxx" #include "SocketEvent.hxx"
#include "IdleEvent.hxx" #include "IdleEvent.hxx"
@ -144,7 +143,7 @@ EventLoop::AbandonFD(SocketEvent &event) noexcept
} }
void void
EventLoop::Insert(TimerEvent &t) noexcept EventLoop::Insert(FineTimerEvent &t) noexcept
{ {
assert(IsInside()); assert(IsInside());

View File

@ -204,7 +204,7 @@ public:
*/ */
bool AbandonFD(SocketEvent &event) noexcept; bool AbandonFD(SocketEvent &event) noexcept;
void Insert(TimerEvent &t) noexcept; void Insert(FineTimerEvent &t) noexcept;
/** /**
* Schedule a call to DeferEvent::RunDeferred(). * Schedule a call to DeferEvent::RunDeferred().

View File

@ -1,46 +0,0 @@
/*
* Copyright 2003-2021 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.
*/
#include "TimerEvent.hxx"
#include "Loop.hxx"
void
TimerEvent::Schedule(Event::Duration d) noexcept
{
Cancel();
due = loop.SteadyNow() + d;
loop.Insert(*this);
}
void
TimerEvent::ScheduleEarlier(Event::Duration d) noexcept
{
const auto new_due = loop.SteadyNow() + d;
if (IsPending()) {
if (new_due >= due)
return;
Cancel();
}
due = new_due;
loop.Insert(*this);
}

View File

@ -1,88 +1,41 @@
/* /*
* Copyright 2003-2021 The Music Player Daemon Project * Copyright 2007-2021 CM4all GmbH
* http://www.musicpd.org * All rights reserved.
* *
* This program is free software; you can redistribute it and/or modify * author: Max Kellermann <mk@cm4all.com>
* 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, * Redistribution and use in source and binary forms, with or without
* but WITHOUT ANY WARRANTY; without even the implied warranty of * modification, are permitted provided that the following conditions
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * are met:
* GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along * - Redistributions of source code must retain the above copyright
* with this program; if not, write to the Free Software Foundation, Inc., * notice, this list of conditions and the following disclaimer.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef MPD_TIMER_EVENT_HXX #pragma once
#define MPD_TIMER_EVENT_HXX
#include "Chrono.hxx" #include "FineTimerEvent.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
class EventLoop;
/** /**
* This class invokes a callback function after a certain amount of * This is a transitional alias. Use #FineTimerEvent or
* time. Use Schedule() to start the timer or Cancel() to cancel it. * #CoarseTimerEvent instead.
*
* 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 using TimerEvent = FineTimerEvent;
: public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
{
friend class TimerList;
EventLoop &loop;
using Callback = BoundMethod<void() noexcept>;
const Callback callback;
/**
* When is this timer due? This is only valid if IsPending()
* returns true.
*/
Event::Clock::time_point due;
public:
TimerEvent(EventLoop &_loop, Callback _callback) noexcept
:loop(_loop), callback(_callback) {}
auto &GetEventLoop() const noexcept {
return loop;
}
constexpr auto GetDue() const noexcept {
return due;
}
bool IsPending() const noexcept {
return is_linked();
}
void Schedule(Event::Duration d) noexcept;
/**
* Like Schedule(), but is a no-op if there is a due time
* earlier than the given one.
*/
void ScheduleEarlier(Event::Duration d) noexcept;
void Cancel() noexcept {
unlink();
}
private:
void Run() noexcept {
callback();
}
};
#endif /* MAIN_NOTIFY_H */

View File

@ -31,11 +31,11 @@
*/ */
#include "Loop.hxx" #include "Loop.hxx"
#include "TimerEvent.hxx" #include "FineTimerEvent.hxx"
constexpr bool constexpr bool
TimerList::Compare::operator()(const TimerEvent &a, TimerList::Compare::operator()(const FineTimerEvent &a,
const TimerEvent &b) const noexcept const FineTimerEvent &b) const noexcept
{ {
return a.due < b.due; return a.due < b.due;
} }
@ -48,7 +48,7 @@ TimerList::~TimerList() noexcept
} }
void void
TimerList::Insert(TimerEvent &t) noexcept TimerList::Insert(FineTimerEvent &t) noexcept
{ {
timers.insert(t); timers.insert(t);
} }
@ -61,7 +61,7 @@ TimerList::Run(const Event::Clock::time_point now) noexcept
if (i == timers.end()) if (i == timers.end())
break; break;
TimerEvent &t = *i; auto &t = *i;
const auto timeout = t.due - now; const auto timeout = t.due - now;
if (timeout > timeout.zero()) if (timeout > timeout.zero())
return timeout; return timeout;

View File

@ -37,18 +37,18 @@
#include <boost/intrusive/set.hpp> #include <boost/intrusive/set.hpp>
class TimerEvent; class FineTimerEvent;
/** /**
* A list of #TimerEvent instances sorted by due time point. * A list of #FineTimerEvent instances sorted by due time point.
*/ */
class TimerList final { class TimerList final {
struct Compare { struct Compare {
constexpr bool operator()(const TimerEvent &a, constexpr bool operator()(const FineTimerEvent &a,
const TimerEvent &b) const noexcept; const FineTimerEvent &b) const noexcept;
}; };
boost::intrusive::multiset<TimerEvent, 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;
@ -64,10 +64,10 @@ public:
return timers.empty(); return timers.empty();
} }
void Insert(TimerEvent &t) noexcept; void Insert(FineTimerEvent &t) noexcept;
/** /**
* Invoke all expired #TimerEvent instances and return the * Invoke all expired #FineTimerEvent instances and return the
* duration until the next timer expires. Returns a negative * duration until the next timer expires. Returns a negative
* duration if there is no timeout. * duration if there is no timeout.
*/ */

View File

@ -23,7 +23,7 @@ event = static_library(
'event', 'event',
'SignalMonitor.cxx', 'SignalMonitor.cxx',
'TimerList.cxx', 'TimerList.cxx',
'TimerEvent.cxx', 'FineTimerEvent.cxx',
'IdleEvent.cxx', 'IdleEvent.cxx',
'InjectEvent.cxx', 'InjectEvent.cxx',
'DeferEvent.cxx', 'DeferEvent.cxx',