system/PeriodClock: use std::chrono::steady_clock

This commit is contained in:
Max Kellermann 2016-12-27 22:47:20 +01:00
parent 28e743ba70
commit 4011899846
3 changed files with 27 additions and 25 deletions

View File

@ -52,7 +52,7 @@ int
volume_level_get(const MultipleOutputs &outputs) volume_level_get(const MultipleOutputs &outputs)
{ {
if (last_hardware_volume >= 0 && if (last_hardware_volume >= 0 &&
!hardware_volume_clock.CheckUpdate(1000)) !hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
/* throttle access to hardware mixers */ /* throttle access to hardware mixers */
return last_hardware_volume; return last_hardware_volume;

View File

@ -30,9 +30,9 @@
#include <assert.h> #include <assert.h>
/** after a failure, wait this number of seconds before /** after a failure, wait this duration before
automatically reopening the device */ automatically reopening the device */
static constexpr unsigned REOPEN_AFTER = 10; static constexpr PeriodClock::Duration REOPEN_AFTER = std::chrono::seconds(10);
struct notify audio_output_client_notify; struct notify audio_output_client_notify;

View File

@ -20,15 +20,17 @@
#ifndef MPD_PERIOD_CLOCK_HXX #ifndef MPD_PERIOD_CLOCK_HXX
#define MPD_PERIOD_CLOCK_HXX #define MPD_PERIOD_CLOCK_HXX
#include "Clock.hxx" #include <chrono>
/** /**
* This is a stopwatch which saves the timestamp of an event, and can * This is a stopwatch which saves the timestamp of an event, and can
* check whether a specified time span has passed since then. * check whether a specified time span has passed since then.
*/ */
class PeriodClock { class PeriodClock {
protected: public:
typedef unsigned Stamp; typedef std::chrono::steady_clock::duration Duration;
typedef Duration Delta;
typedef std::chrono::steady_clock::time_point Stamp;
private: private:
Stamp last; Stamp last;
@ -41,20 +43,20 @@ public:
* object. * object.
*/ */
constexpr constexpr
PeriodClock():last(0) {} PeriodClock():last() {}
protected: protected:
static Stamp GetNow() { static Stamp GetNow() {
return MonotonicClockMS(); return std::chrono::steady_clock::now();
} }
constexpr int Elapsed(Stamp now) const { constexpr Delta Elapsed(Stamp now) const {
return last == 0 return last == Stamp()
? -1 ? Delta(-1)
: now - last; : Delta(now - last);
} }
constexpr bool Check(Stamp now, unsigned duration) const { constexpr bool Check(Stamp now, Duration duration) const {
return now >= last + duration; return now >= last + duration;
} }
@ -64,30 +66,30 @@ protected:
public: public:
constexpr bool IsDefined() const { constexpr bool IsDefined() const {
return last != 0; return last > Stamp();
} }
/** /**
* Resets the clock. * Resets the clock.
*/ */
void Reset() { void Reset() {
last = 0; last = Stamp();
} }
/** /**
* Returns the number of milliseconds elapsed since the last * Returns the time elapsed since the last update(). Returns
* update(). Returns -1 if update() was never called. * a negative value if update() was never called.
*/ */
int Elapsed() const { Delta Elapsed() const {
return Elapsed(GetNow()); return Elapsed(GetNow());
} }
/** /**
* Combines a call to Elapsed() and Update(). * Combines a call to Elapsed() and Update().
*/ */
int ElapsedUpdate() { Delta ElapsedUpdate() {
const auto now = GetNow(); const auto now = GetNow();
int result = Elapsed(now); const auto result = Elapsed(now);
Update(now); Update(now);
return result; return result;
} }
@ -96,9 +98,9 @@ public:
* Checks whether the specified duration has passed since the last * Checks whether the specified duration has passed since the last
* update. * update.
* *
* @param duration the duration in milliseconds * @param duration the duration
*/ */
bool Check(unsigned duration) const { bool Check(Duration duration) const {
return Check(GetNow(), duration); return Check(GetNow(), duration);
} }
@ -113,7 +115,7 @@ public:
* Updates the time stamp, setting it to the current clock plus the * Updates the time stamp, setting it to the current clock plus the
* specified offset. * specified offset.
*/ */
void UpdateWithOffset(int offset) { void UpdateWithOffset(Delta offset) {
Update(GetNow() + offset); Update(GetNow() + offset);
} }
@ -123,7 +125,7 @@ public:
* *
* @param duration the duration in milliseconds * @param duration the duration in milliseconds
*/ */
bool CheckUpdate(unsigned duration) { bool CheckUpdate(Duration duration) {
Stamp now = GetNow(); Stamp now = GetNow();
if (Check(now, duration)) { if (Check(now, duration)) {
Update(now); Update(now);
@ -138,7 +140,7 @@ public:
* *
* @param duration the duration in milliseconds * @param duration the duration in milliseconds
*/ */
bool CheckAlwaysUpdate(unsigned duration) { bool CheckAlwaysUpdate(Duration duration) {
Stamp now = GetNow(); Stamp now = GetNow();
bool ret = Check(now, duration); bool ret = Check(now, duration);
Update(now); Update(now);