event/TimerEvent: add type alias for std::chrono::steady_clock::duration

This commit is contained in:
Max Kellermann
2020-10-08 19:35:36 +02:00
parent ce93e58944
commit eeb96eb367
19 changed files with 84 additions and 51 deletions

36
src/event/Chrono.hxx Normal file
View File

@@ -0,0 +1,36 @@
/*
* Copyright 2003-2020 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_EVENT_CHRONO_HXX
#define MPD_EVENT_CHRONO_HXX
#include <chrono>
namespace Event {
/**
* The clock used by class #EventLoop and class #TimerEvent.
*/
using Clock = std::chrono::steady_clock;
using Duration = Clock::duration;
} // namespace Event
#endif /* MAIN_NOTIFY_H */

View File

@@ -122,7 +122,7 @@ EventLoop::RemoveIdle(IdleMonitor &i) noexcept
}
void
EventLoop::AddTimer(TimerEvent &t, std::chrono::steady_clock::duration d) noexcept
EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept
{
assert(IsInside());
@@ -131,10 +131,10 @@ EventLoop::AddTimer(TimerEvent &t, std::chrono::steady_clock::duration d) noexce
again = true;
}
inline std::chrono::steady_clock::duration
inline Event::Duration
EventLoop::HandleTimers() noexcept
{
std::chrono::steady_clock::duration timeout;
Event::Duration timeout;
while (!quit) {
auto i = timers.begin();
@@ -151,7 +151,7 @@ EventLoop::HandleTimers() noexcept
t.Run();
}
return std::chrono::steady_clock::duration(-1);
return Event::Duration(-1);
}
/**
@@ -160,7 +160,7 @@ EventLoop::HandleTimers() noexcept
* value (= never times out) is translated to the magic value -1.
*/
static constexpr int
ExportTimeoutMS(std::chrono::steady_clock::duration timeout)
ExportTimeoutMS(Event::Duration timeout)
{
return timeout >= timeout.zero()
/* round up (+1) to avoid unnecessary wakeups */

View File

@@ -20,6 +20,7 @@
#ifndef MPD_EVENT_LOOP_HXX
#define MPD_EVENT_LOOP_HXX
#include "Chrono.hxx"
#include "PollGroup.hxx"
#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
@@ -91,7 +92,7 @@ class EventLoop final : SocketMonitor
std::unique_ptr<Uring::Manager> uring;
#endif
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
Event::Clock::time_point now = Event::Clock::now();
/**
* Is this #EventLoop alive, i.e. can events be scheduled?
@@ -140,9 +141,9 @@ public:
~EventLoop() noexcept;
/**
* A caching wrapper for std::chrono::steady_clock::now().
* A caching wrapper for Event::Clock::now().
*/
std::chrono::steady_clock::time_point GetTime() const {
auto GetTime() const {
assert(IsInside());
return now;
@@ -184,8 +185,7 @@ public:
void AddIdle(IdleMonitor &i) noexcept;
void RemoveIdle(IdleMonitor &i) noexcept;
void AddTimer(TimerEvent &t,
std::chrono::steady_clock::duration d) noexcept;
void AddTimer(TimerEvent &t, Event::Duration d) noexcept;
/**
* Schedule a call to DeferEvent::RunDeferred().
@@ -221,7 +221,7 @@ private:
* duration until the next timer expires. Returns a negative
* duration if there is no timeout.
*/
std::chrono::steady_clock::duration HandleTimers() noexcept;
Event::Duration HandleTimers() noexcept;
bool OnSocketReady(unsigned flags) noexcept override;

View File

@@ -117,7 +117,7 @@ MultiSocketMonitor::Prepare() noexcept
/* if there was at least one file descriptor not
supported by epoll, install a very short timeout
because we assume it's always ready */
constexpr std::chrono::steady_clock::duration ready_timeout =
constexpr Event::Duration ready_timeout =
std::chrono::milliseconds(1);
if (timeout < timeout.zero() || timeout > ready_timeout)
timeout = ready_timeout;

View File

@@ -226,7 +226,7 @@ protected:
*
* @return timeout or a negative value for no timeout
*/
virtual std::chrono::steady_clock::duration PrepareSockets() noexcept = 0;
virtual Event::Duration PrepareSockets() noexcept = 0;
/**
* At least one socket is ready or the timeout has expired.

View File

@@ -21,7 +21,7 @@
#include "Loop.hxx"
void
TimerEvent::Schedule(std::chrono::steady_clock::duration d) noexcept
TimerEvent::Schedule(Event::Duration d) noexcept
{
Cancel();

View File

@@ -20,12 +20,11 @@
#ifndef MPD_TIMER_EVENT_HXX
#define MPD_TIMER_EVENT_HXX
#include "Chrono.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/set_hook.hpp>
#include <chrono>
class EventLoop;
/**
@@ -50,7 +49,7 @@ class TimerEvent final
* When is this timer due? This is only valid if IsActive()
* returns true.
*/
std::chrono::steady_clock::time_point due;
Event::Clock::time_point due;
public:
TimerEvent(EventLoop &_loop, Callback _callback) noexcept
@@ -65,7 +64,7 @@ public:
return is_linked();
}
void Schedule(std::chrono::steady_clock::duration d) noexcept;
void Schedule(Event::Duration d) noexcept;
void Cancel() noexcept {
unlink();