From 1b4fd7457595c9addeeae699f62ca71a368b3632 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 4 Feb 2021 22:45:11 +0100 Subject: [PATCH] event/TimerEvent: rename to FineTimerEvent ... and make TimerEvent a type alias for FineTimerEvent (i.e. swap names). --- src/event/FineTimerEvent.cxx | 59 +++++++++++++++++++ src/event/FineTimerEvent.hxx | 58 ++++++++++++++++++- src/event/Loop.cxx | 3 +- src/event/Loop.hxx | 2 +- src/event/TimerEvent.cxx | 46 --------------- src/event/TimerEvent.hxx | 109 ++++++++++------------------------- src/event/TimerList.cxx | 10 ++-- src/event/TimerList.hxx | 14 ++--- src/event/meson.build | 2 +- 9 files changed, 160 insertions(+), 143 deletions(-) create mode 100644 src/event/FineTimerEvent.cxx delete mode 100644 src/event/TimerEvent.cxx diff --git a/src/event/FineTimerEvent.cxx b/src/event/FineTimerEvent.cxx new file mode 100644 index 000000000..006d30360 --- /dev/null +++ b/src/event/FineTimerEvent.cxx @@ -0,0 +1,59 @@ +/* + * Copyright 2007-2021 CM4all GmbH + * All rights reserved. + * + * author: Max Kellermann + * + * 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); +} diff --git a/src/event/FineTimerEvent.hxx b/src/event/FineTimerEvent.hxx index d5a5afed8..57fd88586 100644 --- a/src/event/FineTimerEvent.hxx +++ b/src/event/FineTimerEvent.hxx @@ -32,7 +32,12 @@ #pragma once -#include "TimerEvent.hxx" +#include "Chrono.hxx" +#include "util/BindMethod.hxx" + +#include + +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> +{ + friend class TimerList; + + EventLoop &loop; + + using Callback = BoundMethod; + 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(); + } +}; diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index baab93d8b..124efc0a5 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -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()); diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 8b32c6df6..eb69c7973 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -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(). diff --git a/src/event/TimerEvent.cxx b/src/event/TimerEvent.cxx deleted file mode 100644 index dcb24d7b6..000000000 --- a/src/event/TimerEvent.cxx +++ /dev/null @@ -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); -} diff --git a/src/event/TimerEvent.hxx b/src/event/TimerEvent.hxx index 82c69fa6a..196b1f1a2 100644 --- a/src/event/TimerEvent.hxx +++ b/src/event/TimerEvent.hxx @@ -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 * - * 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 - -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> -{ - friend class TimerList; - - EventLoop &loop; - - using Callback = BoundMethod; - 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; diff --git a/src/event/TimerList.cxx b/src/event/TimerList.cxx index fbac9b968..ae305c9b6 100644 --- a/src/event/TimerList.cxx +++ b/src/event/TimerList.cxx @@ -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; diff --git a/src/event/TimerList.hxx b/src/event/TimerList.hxx index da9a34161..0a8fed7f7 100644 --- a/src/event/TimerList.hxx +++ b/src/event/TimerList.hxx @@ -37,18 +37,18 @@ #include -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>>, boost::intrusive::compare, boost::intrusive::constant_time_size> 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. */ diff --git a/src/event/meson.build b/src/event/meson.build index 430d90107..fac85a692 100644 --- a/src/event/meson.build +++ b/src/event/meson.build @@ -23,7 +23,7 @@ event = static_library( 'event', 'SignalMonitor.cxx', 'TimerList.cxx', - 'TimerEvent.cxx', + 'FineTimerEvent.cxx', 'IdleEvent.cxx', 'InjectEvent.cxx', 'DeferEvent.cxx',