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
#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
@ -45,5 +50,52 @@
* thread that runs the #EventLoop, except where explicitly documented
* as thread-safe.
*/
using FineTimerEvent = TimerEvent;
// TODO: implement
class FineTimerEvent final
: 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 "TimerEvent.hxx"
#include "DeferEvent.hxx"
#include "SocketEvent.hxx"
#include "IdleEvent.hxx"
@ -144,7 +143,7 @@ EventLoop::AbandonFD(SocketEvent &event) noexcept
}
void
EventLoop::Insert(TimerEvent &t) noexcept
EventLoop::Insert(FineTimerEvent &t) noexcept
{
assert(IsInside());

View File

@ -204,7 +204,7 @@ public:
*/
bool AbandonFD(SocketEvent &event) noexcept;
void Insert(TimerEvent &t) noexcept;
void Insert(FineTimerEvent &t) noexcept;
/**
* 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
* http://www.musicpd.org
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* 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.
* author: Max Kellermann <mk@cm4all.com>
*
* 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.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 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.
* - 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.
*/
#ifndef MPD_TIMER_EVENT_HXX
#define MPD_TIMER_EVENT_HXX
#pragma once
#include "Chrono.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
class EventLoop;
#include "FineTimerEvent.hxx"
/**
* 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.
* This is a transitional alias. Use #FineTimerEvent or
* #CoarseTimerEvent instead.
*/
class TimerEvent final
: 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 */
using TimerEvent = FineTimerEvent;

View File

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

View File

@ -37,18 +37,18 @@
#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 {
struct Compare {
constexpr bool operator()(const TimerEvent &a,
const TimerEvent &b) const noexcept;
constexpr bool operator()(const FineTimerEvent &a,
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::compare<Compare>,
boost::intrusive::constant_time_size<false>> timers;
@ -64,10 +64,10 @@ public:
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 if there is no timeout.
*/

View File

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