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
#include "fs/AllocatedPath.hxx"
#include <chrono>
#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;

View File

@ -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;

View File

@ -20,11 +20,11 @@
#ifndef MPD_CLIENT_CONFIG_HXX
#define MPD_CLIENT_CONFIG_HXX
#include <chrono>
#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;

View File

@ -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

View File

@ -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

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();

View File

@ -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<AlsaInputStream>(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);

View File

@ -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

View File

@ -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 <alsa/asoundlib.h>
#include <chrono>
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<pollfd> 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

View File

@ -37,7 +37,7 @@ extern "C" {
#include <poll.h> /* 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

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}