From eeb96eb36753c60ec9a515c5a74ebd7bc2b4daf5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 8 Oct 2020 19:35:36 +0200 Subject: [PATCH] event/TimerEvent: add type alias for std::chrono::steady_clock::duration --- src/StateFileConfig.hxx | 7 +++-- src/client/Config.cxx | 2 +- src/client/Config.hxx | 4 +-- src/client/Expire.cxx | 2 +- src/db/update/InotifyQueue.cxx | 2 +- src/event/Chrono.hxx | 36 +++++++++++++++++++++++++ src/event/Loop.cxx | 10 +++---- src/event/Loop.hxx | 12 ++++----- src/event/MultiSocketMonitor.cxx | 2 +- src/event/MultiSocketMonitor.hxx | 2 +- src/event/TimerEvent.cxx | 2 +- src/event/TimerEvent.hxx | 7 +++-- src/input/plugins/AlsaInputPlugin.cxx | 6 ++--- src/lib/alsa/NonBlock.cxx | 10 +++---- src/lib/alsa/NonBlock.hxx | 11 ++++---- src/lib/nfs/Connection.cxx | 2 +- src/lib/systemd/Watchdog.hxx | 2 +- src/mixer/plugins/AlsaMixerPlugin.cxx | 6 ++--- src/output/plugins/AlsaOutputPlugin.cxx | 10 +++---- 19 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 src/event/Chrono.hxx diff --git a/src/StateFileConfig.hxx b/src/StateFileConfig.hxx index fc12993af..6df927017 100644 --- a/src/StateFileConfig.hxx +++ b/src/StateFileConfig.hxx @@ -21,17 +21,16 @@ #define MPD_STATE_FILE_CONFIG_HXX #include "fs/AllocatedPath.hxx" - -#include +#include "event/Chrono.hxx" struct ConfigData; 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; - std::chrono::steady_clock::duration interval; + Event::Duration interval; bool restore_paused; diff --git a/src/client/Config.cxx b/src/client/Config.cxx index 299b0ac10..70c58bbff 100644 --- a/src/client/Config.cxx +++ b/src/client/Config.cxx @@ -24,7 +24,7 @@ #define CLIENT_MAX_COMMAND_LIST_DEFAULT (2048*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_output_buffer_size; diff --git a/src/client/Config.hxx b/src/client/Config.hxx index 27d1c478f..6a7b63cee 100644 --- a/src/client/Config.hxx +++ b/src/client/Config.hxx @@ -20,11 +20,11 @@ #ifndef MPD_CLIENT_CONFIG_HXX #define MPD_CLIENT_CONFIG_HXX -#include +#include "event/Chrono.hxx" 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_output_buffer_size; diff --git a/src/client/Expire.cxx b/src/client/Expire.cxx index 0c2c4e269..7ba3cb3ac 100644 --- a/src/client/Expire.cxx +++ b/src/client/Expire.cxx @@ -34,7 +34,7 @@ Client::SetExpired() noexcept } FullyBufferedSocket::Close(); - timeout_event.Schedule(std::chrono::steady_clock::duration::zero()); + timeout_event.Schedule(Event::Duration::zero()); } void diff --git a/src/db/update/InotifyQueue.cxx b/src/db/update/InotifyQueue.cxx index b34af04b1..b9b22d6f8 100644 --- a/src/db/update/InotifyQueue.cxx +++ b/src/db/update/InotifyQueue.cxx @@ -29,7 +29,7 @@ * UpdateService::Enqueue(). This increases the probability that * 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); void diff --git a/src/event/Chrono.hxx b/src/event/Chrono.hxx new file mode 100644 index 000000000..dcdec590c --- /dev/null +++ b/src/event/Chrono.hxx @@ -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 + +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 */ diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index 0c721517b..27386c279 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -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 */ diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 442b53004..a1fe7f585 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -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; #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; diff --git a/src/event/MultiSocketMonitor.cxx b/src/event/MultiSocketMonitor.cxx index 24365cfbc..2df12c8f9 100644 --- a/src/event/MultiSocketMonitor.cxx +++ b/src/event/MultiSocketMonitor.cxx @@ -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; diff --git a/src/event/MultiSocketMonitor.hxx b/src/event/MultiSocketMonitor.hxx index 549d69ee1..de3c5e9ed 100644 --- a/src/event/MultiSocketMonitor.hxx +++ b/src/event/MultiSocketMonitor.hxx @@ -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. diff --git a/src/event/TimerEvent.cxx b/src/event/TimerEvent.cxx index 4eff77369..e2fc384e2 100644 --- a/src/event/TimerEvent.cxx +++ b/src/event/TimerEvent.cxx @@ -21,7 +21,7 @@ #include "Loop.hxx" void -TimerEvent::Schedule(std::chrono::steady_clock::duration d) noexcept +TimerEvent::Schedule(Event::Duration d) noexcept { Cancel(); diff --git a/src/event/TimerEvent.hxx b/src/event/TimerEvent.hxx index ff02bd729..baadcdb4e 100644 --- a/src/event/TimerEvent.hxx +++ b/src/event/TimerEvent.hxx @@ -20,12 +20,11 @@ #ifndef MPD_TIMER_EVENT_HXX #define MPD_TIMER_EVENT_HXX +#include "Chrono.hxx" #include "util/BindMethod.hxx" #include -#include - 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(); diff --git a/src/input/plugins/AlsaInputPlugin.cxx b/src/input/plugins/AlsaInputPlugin.cxx index 9a6358410..96ba33e79 100644 --- a/src/input/plugins/AlsaInputPlugin.cxx +++ b/src/input/plugins/AlsaInputPlugin.cxx @@ -127,7 +127,7 @@ private: int Recover(int err); /* virtual methods from class MultiSocketMonitor */ - std::chrono::steady_clock::duration PrepareSockets() noexcept override; + Event::Duration PrepareSockets() noexcept override; void DispatchSockets() noexcept override; }; @@ -219,12 +219,12 @@ AlsaInputStream::Create(EventLoop &event_loop, const char *uri, return std::make_unique(event_loop, mutex, spec); } -std::chrono::steady_clock::duration +Event::Duration AlsaInputStream::PrepareSockets() noexcept { if (IsPaused()) { ClearSocketList(); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } return non_block.PrepareSockets(*this, capture_handle); diff --git a/src/lib/alsa/NonBlock.cxx b/src/lib/alsa/NonBlock.cxx index 8fd74650b..e77f50520 100644 --- a/src/lib/alsa/NonBlock.cxx +++ b/src/lib/alsa/NonBlock.cxx @@ -21,7 +21,7 @@ #include "event/MultiSocketMonitor.hxx" #include "util/RuntimeError.hxx" -std::chrono::steady_clock::duration +Event::Duration AlsaNonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *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); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } void @@ -75,13 +75,13 @@ AlsaNonBlockPcm::DispatchSockets(MultiSocketMonitor &m, snd_strerror(-err)); } -std::chrono::steady_clock::duration +Event::Duration AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept { int count = snd_mixer_poll_descriptors_count(mixer); if (count <= 0) { m.ClearSocketList(); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } struct pollfd *pfds = pfd_buffer.Get(count); @@ -91,7 +91,7 @@ AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noe count = 0; m.ReplaceSocketList(pfds, count); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } void diff --git a/src/lib/alsa/NonBlock.hxx b/src/lib/alsa/NonBlock.hxx index 78c5bbfcd..f1a153799 100644 --- a/src/lib/alsa/NonBlock.hxx +++ b/src/lib/alsa/NonBlock.hxx @@ -20,12 +20,11 @@ #ifndef MPD_ALSA_NON_BLOCK_HXX #define MPD_ALSA_NON_BLOCK_HXX +#include "event/Chrono.hxx" #include "util/ReusableArray.hxx" #include -#include - class MultiSocketMonitor; /** @@ -39,8 +38,8 @@ public: /** * Throws on error. */ - std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m, - snd_pcm_t *pcm); + Event::Duration PrepareSockets(MultiSocketMonitor &m, + snd_pcm_t *pcm); /** * Wrapper for snd_pcm_poll_descriptors_revents(), to be @@ -59,8 +58,8 @@ class AlsaNonBlockMixer { ReusableArray pfd_buffer; public: - std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m, - snd_mixer_t *mixer) noexcept; + Event::Duration PrepareSockets(MultiSocketMonitor &m, + snd_mixer_t *mixer) noexcept; /** * Wrapper for snd_mixer_poll_descriptors_revents(), to be diff --git a/src/lib/nfs/Connection.cxx b/src/lib/nfs/Connection.cxx index f853b40eb..933b67cc3 100644 --- a/src/lib/nfs/Connection.cxx +++ b/src/lib/nfs/Connection.cxx @@ -37,7 +37,7 @@ extern "C" { #include /* for POLLIN, POLLOUT */ #endif -static constexpr std::chrono::steady_clock::duration NFS_MOUNT_TIMEOUT = +static constexpr Event::Duration NFS_MOUNT_TIMEOUT = std::chrono::minutes(1); inline void diff --git a/src/lib/systemd/Watchdog.hxx b/src/lib/systemd/Watchdog.hxx index 6803f137a..1c0fa8d5b 100644 --- a/src/lib/systemd/Watchdog.hxx +++ b/src/lib/systemd/Watchdog.hxx @@ -42,7 +42,7 @@ namespace Systemd { class Watchdog { TimerEvent timer; - std::chrono::steady_clock::duration interval; + Event::Duration interval; public: explicit Watchdog(EventLoop &_loop) noexcept; diff --git a/src/mixer/plugins/AlsaMixerPlugin.cxx b/src/mixer/plugins/AlsaMixerPlugin.cxx index cce432492..ccf17389e 100644 --- a/src/mixer/plugins/AlsaMixerPlugin.cxx +++ b/src/mixer/plugins/AlsaMixerPlugin.cxx @@ -64,7 +64,7 @@ public: } private: - std::chrono::steady_clock::duration PrepareSockets() noexcept override; + Event::Duration PrepareSockets() noexcept override; void DispatchSockets() noexcept override; }; @@ -99,12 +99,12 @@ public: static constexpr Domain alsa_mixer_domain("alsa_mixer"); -std::chrono::steady_clock::duration +Event::Duration AlsaMixerMonitor::PrepareSockets() noexcept { if (mixer == nullptr) { ClearSocketList(); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } return non_block.PrepareSockets(*this, mixer); diff --git a/src/output/plugins/AlsaOutputPlugin.cxx b/src/output/plugins/AlsaOutputPlugin.cxx index bca78fd9f..2ba9e1f9f 100644 --- a/src/output/plugins/AlsaOutputPlugin.cxx +++ b/src/output/plugins/AlsaOutputPlugin.cxx @@ -119,7 +119,7 @@ class AlsaOutput final */ 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 @@ -398,7 +398,7 @@ private: } /* virtual methods from class MultiSocketMonitor */ - std::chrono::steady_clock::duration PrepareSockets() noexcept override; + Event::Duration PrepareSockets() noexcept override; void DispatchSockets() noexcept override; }; @@ -1052,12 +1052,12 @@ AlsaOutput::Play(const void *chunk, size_t size) return size; } -std::chrono::steady_clock::duration +Event::Duration AlsaOutput::PrepareSockets() noexcept { if (!LockIsActiveAndNotWaiting()) { ClearSocketList(); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } try { @@ -1065,7 +1065,7 @@ AlsaOutput::PrepareSockets() noexcept } catch (...) { ClearSocketList(); LockCaughtError(); - return std::chrono::steady_clock::duration(-1); + return Event::Duration(-1); } }