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

View File

@ -21,17 +21,16 @@
#define MPD_STATE_FILE_CONFIG_HXX #define MPD_STATE_FILE_CONFIG_HXX
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "event/Chrono.hxx"
#include <chrono>
struct ConfigData; struct ConfigData;
struct StateFileConfig { struct StateFileConfig {
static constexpr std::chrono::steady_clock::duration DEFAULT_INTERVAL = std::chrono::minutes(2); static constexpr Event::Duration DEFAULT_INTERVAL = std::chrono::minutes(2);
AllocatedPath path; AllocatedPath path;
std::chrono::steady_clock::duration interval; Event::Duration interval;
bool restore_paused; bool restore_paused;

View File

@ -24,7 +24,7 @@
#define CLIENT_MAX_COMMAND_LIST_DEFAULT (2048*1024) #define CLIENT_MAX_COMMAND_LIST_DEFAULT (2048*1024)
#define CLIENT_MAX_OUTPUT_BUFFER_SIZE_DEFAULT (8192*1024) #define CLIENT_MAX_OUTPUT_BUFFER_SIZE_DEFAULT (8192*1024)
std::chrono::steady_clock::duration client_timeout; Event::Duration client_timeout;
size_t client_max_command_list_size; size_t client_max_command_list_size;
size_t client_max_output_buffer_size; size_t client_max_output_buffer_size;

View File

@ -20,11 +20,11 @@
#ifndef MPD_CLIENT_CONFIG_HXX #ifndef MPD_CLIENT_CONFIG_HXX
#define MPD_CLIENT_CONFIG_HXX #define MPD_CLIENT_CONFIG_HXX
#include <chrono> #include "event/Chrono.hxx"
struct ConfigData; struct ConfigData;
extern std::chrono::steady_clock::duration client_timeout; extern Event::Duration client_timeout;
extern size_t client_max_command_list_size; extern size_t client_max_command_list_size;
extern size_t client_max_output_buffer_size; extern size_t client_max_output_buffer_size;

View File

@ -34,7 +34,7 @@ Client::SetExpired() noexcept
} }
FullyBufferedSocket::Close(); FullyBufferedSocket::Close();
timeout_event.Schedule(std::chrono::steady_clock::duration::zero()); timeout_event.Schedule(Event::Duration::zero());
} }
void void

View File

@ -29,7 +29,7 @@
* UpdateService::Enqueue(). This increases the probability that * UpdateService::Enqueue(). This increases the probability that
* updates can be bundled. * updates can be bundled.
*/ */
static constexpr std::chrono::steady_clock::duration INOTIFY_UPDATE_DELAY = static constexpr Event::Duration INOTIFY_UPDATE_DELAY =
std::chrono::seconds(5); std::chrono::seconds(5);
void void

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

View File

@ -20,6 +20,7 @@
#ifndef MPD_EVENT_LOOP_HXX #ifndef MPD_EVENT_LOOP_HXX
#define MPD_EVENT_LOOP_HXX #define MPD_EVENT_LOOP_HXX
#include "Chrono.hxx"
#include "PollGroup.hxx" #include "PollGroup.hxx"
#include "WakeFD.hxx" #include "WakeFD.hxx"
#include "SocketMonitor.hxx" #include "SocketMonitor.hxx"
@ -91,7 +92,7 @@ class EventLoop final : SocketMonitor
std::unique_ptr<Uring::Manager> uring; std::unique_ptr<Uring::Manager> uring;
#endif #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? * Is this #EventLoop alive, i.e. can events be scheduled?
@ -140,9 +141,9 @@ public:
~EventLoop() noexcept; ~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()); assert(IsInside());
return now; return now;
@ -184,8 +185,7 @@ public:
void AddIdle(IdleMonitor &i) noexcept; void AddIdle(IdleMonitor &i) noexcept;
void RemoveIdle(IdleMonitor &i) noexcept; void RemoveIdle(IdleMonitor &i) noexcept;
void AddTimer(TimerEvent &t, void AddTimer(TimerEvent &t, Event::Duration d) noexcept;
std::chrono::steady_clock::duration d) noexcept;
/** /**
* Schedule a call to DeferEvent::RunDeferred(). * Schedule a call to DeferEvent::RunDeferred().
@ -221,7 +221,7 @@ private:
* 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.
*/ */
std::chrono::steady_clock::duration HandleTimers() noexcept; Event::Duration HandleTimers() noexcept;
bool OnSocketReady(unsigned flags) noexcept override; bool OnSocketReady(unsigned flags) noexcept override;

View File

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

View File

@ -226,7 +226,7 @@ protected:
* *
* @return timeout or a negative value for no timeout * @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. * At least one socket is ready or the timeout has expired.

View File

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

View File

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

View File

@ -127,7 +127,7 @@ private:
int Recover(int err); int Recover(int err);
/* virtual methods from class MultiSocketMonitor */ /* virtual methods from class MultiSocketMonitor */
std::chrono::steady_clock::duration PrepareSockets() noexcept override; Event::Duration PrepareSockets() noexcept override;
void DispatchSockets() noexcept override; void DispatchSockets() noexcept override;
}; };
@ -219,12 +219,12 @@ AlsaInputStream::Create(EventLoop &event_loop, const char *uri,
return std::make_unique<AlsaInputStream>(event_loop, mutex, spec); return std::make_unique<AlsaInputStream>(event_loop, mutex, spec);
} }
std::chrono::steady_clock::duration Event::Duration
AlsaInputStream::PrepareSockets() noexcept AlsaInputStream::PrepareSockets() noexcept
{ {
if (IsPaused()) { if (IsPaused()) {
ClearSocketList(); ClearSocketList();
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
return non_block.PrepareSockets(*this, capture_handle); return non_block.PrepareSockets(*this, capture_handle);

View File

@ -21,7 +21,7 @@
#include "event/MultiSocketMonitor.hxx" #include "event/MultiSocketMonitor.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
std::chrono::steady_clock::duration Event::Duration
AlsaNonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm) AlsaNonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm)
{ {
int count = snd_pcm_poll_descriptors_count(pcm); int count = snd_pcm_poll_descriptors_count(pcm);
@ -45,7 +45,7 @@ AlsaNonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm)
} }
m.ReplaceSocketList(pfds, count); m.ReplaceSocketList(pfds, count);
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
void void
@ -75,13 +75,13 @@ AlsaNonBlockPcm::DispatchSockets(MultiSocketMonitor &m,
snd_strerror(-err)); snd_strerror(-err));
} }
std::chrono::steady_clock::duration Event::Duration
AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept
{ {
int count = snd_mixer_poll_descriptors_count(mixer); int count = snd_mixer_poll_descriptors_count(mixer);
if (count <= 0) { if (count <= 0) {
m.ClearSocketList(); m.ClearSocketList();
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
struct pollfd *pfds = pfd_buffer.Get(count); struct pollfd *pfds = pfd_buffer.Get(count);
@ -91,7 +91,7 @@ AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noe
count = 0; count = 0;
m.ReplaceSocketList(pfds, count); m.ReplaceSocketList(pfds, count);
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
void void

View File

@ -20,12 +20,11 @@
#ifndef MPD_ALSA_NON_BLOCK_HXX #ifndef MPD_ALSA_NON_BLOCK_HXX
#define MPD_ALSA_NON_BLOCK_HXX #define MPD_ALSA_NON_BLOCK_HXX
#include "event/Chrono.hxx"
#include "util/ReusableArray.hxx" #include "util/ReusableArray.hxx"
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <chrono>
class MultiSocketMonitor; class MultiSocketMonitor;
/** /**
@ -39,8 +38,8 @@ public:
/** /**
* Throws on error. * Throws on error.
*/ */
std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m, Event::Duration PrepareSockets(MultiSocketMonitor &m,
snd_pcm_t *pcm); snd_pcm_t *pcm);
/** /**
* Wrapper for snd_pcm_poll_descriptors_revents(), to be * Wrapper for snd_pcm_poll_descriptors_revents(), to be
@ -59,8 +58,8 @@ class AlsaNonBlockMixer {
ReusableArray<pollfd> pfd_buffer; ReusableArray<pollfd> pfd_buffer;
public: public:
std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m, Event::Duration PrepareSockets(MultiSocketMonitor &m,
snd_mixer_t *mixer) noexcept; snd_mixer_t *mixer) noexcept;
/** /**
* Wrapper for snd_mixer_poll_descriptors_revents(), to be * Wrapper for snd_mixer_poll_descriptors_revents(), to be

View File

@ -37,7 +37,7 @@ extern "C" {
#include <poll.h> /* for POLLIN, POLLOUT */ #include <poll.h> /* for POLLIN, POLLOUT */
#endif #endif
static constexpr std::chrono::steady_clock::duration NFS_MOUNT_TIMEOUT = static constexpr Event::Duration NFS_MOUNT_TIMEOUT =
std::chrono::minutes(1); std::chrono::minutes(1);
inline void inline void

View File

@ -42,7 +42,7 @@ namespace Systemd {
class Watchdog { class Watchdog {
TimerEvent timer; TimerEvent timer;
std::chrono::steady_clock::duration interval; Event::Duration interval;
public: public:
explicit Watchdog(EventLoop &_loop) noexcept; explicit Watchdog(EventLoop &_loop) noexcept;

View File

@ -64,7 +64,7 @@ public:
} }
private: private:
std::chrono::steady_clock::duration PrepareSockets() noexcept override; Event::Duration PrepareSockets() noexcept override;
void DispatchSockets() noexcept override; void DispatchSockets() noexcept override;
}; };
@ -99,12 +99,12 @@ public:
static constexpr Domain alsa_mixer_domain("alsa_mixer"); static constexpr Domain alsa_mixer_domain("alsa_mixer");
std::chrono::steady_clock::duration Event::Duration
AlsaMixerMonitor::PrepareSockets() noexcept AlsaMixerMonitor::PrepareSockets() noexcept
{ {
if (mixer == nullptr) { if (mixer == nullptr) {
ClearSocketList(); ClearSocketList();
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
return non_block.PrepareSockets(*this, mixer); return non_block.PrepareSockets(*this, mixer);

View File

@ -119,7 +119,7 @@ class AlsaOutput final
*/ */
snd_pcm_uframes_t period_frames; snd_pcm_uframes_t period_frames;
std::chrono::steady_clock::duration effective_period_duration; Event::Duration effective_period_duration;
/** /**
* If snd_pcm_avail() goes above this value and no more data * If snd_pcm_avail() goes above this value and no more data
@ -398,7 +398,7 @@ private:
} }
/* virtual methods from class MultiSocketMonitor */ /* virtual methods from class MultiSocketMonitor */
std::chrono::steady_clock::duration PrepareSockets() noexcept override; Event::Duration PrepareSockets() noexcept override;
void DispatchSockets() noexcept override; void DispatchSockets() noexcept override;
}; };
@ -1052,12 +1052,12 @@ AlsaOutput::Play(const void *chunk, size_t size)
return size; return size;
} }
std::chrono::steady_clock::duration Event::Duration
AlsaOutput::PrepareSockets() noexcept AlsaOutput::PrepareSockets() noexcept
{ {
if (!LockIsActiveAndNotWaiting()) { if (!LockIsActiveAndNotWaiting()) {
ClearSocketList(); ClearSocketList();
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
try { try {
@ -1065,7 +1065,7 @@ AlsaOutput::PrepareSockets() noexcept
} catch (...) { } catch (...) {
ClearSocketList(); ClearSocketList();
LockCaughtError(); LockCaughtError();
return std::chrono::steady_clock::duration(-1); return Event::Duration(-1);
} }
} }