summaryrefslogtreecommitdiff
path: root/src/TimerA2.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-06-08 02:03:31 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-06-09 02:05:34 +0900
commit99cd73c0926f0c07d27777fde2f15816e8040030 (patch)
tree7c6a077b0590c49029c76686afb9c2ab1d00bb6d /src/TimerA2.cc
parent2874f6ab095a30a4d297cdc85fa3730bec41b1c2 (diff)
Replace timer facility with chrono lib
Diffstat (limited to 'src/TimerA2.cc')
-rw-r--r--src/TimerA2.cc127
1 files changed, 15 insertions, 112 deletions
diff --git a/src/TimerA2.cc b/src/TimerA2.cc
index 29e5d32f..7c16ba9d 100644
--- a/src/TimerA2.cc
+++ b/src/TimerA2.cc
@@ -2,7 +2,7 @@
/*
* aria2 - The high speed download utility
*
- * Copyright (C) 2010 Tatsuhiro Tsujikawa
+ * Copyright (C) 2015 Tatsuhiro Tsujikawa
*
* 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
@@ -35,141 +35,44 @@
#include "TimerA2.h"
-#include <cassert>
-
-#include "util.h"
-
namespace aria2 {
-Timer::Timer()
+Timer::Timer() : tp_(Clock::now())
{
reset();
}
-Timer::Timer(const Timer& timer):tv_(timer.tv_) {}
-
-Timer::Timer(time_t sec)
+Timer::Timer(const Clock::time_point& tp) : tp_(tp)
{
- reset(sec);
}
-Timer::Timer(const struct timeval& tv):tv_(tv) {}
-
-static bool useClockGettime()
+void Timer::reset()
{
- static timespec ts;
- static int r = clock_gettime(CLOCK_MONOTONIC, &ts);
- return r == 0;
+ tp_ = Clock::now();
}
-Timer& Timer::operator=(const Timer& timer)
+Timer::Clock::duration Timer::difference() const
{
- if(this != &timer) {
- tv_ = timer.tv_;
+ auto now = Clock::now();
+ if (now < tp_) {
+ return Timer::Clock::duration(std::chrono::seconds(0));
}
- return *this;
-}
-bool Timer::operator<(const Timer& timer) const
-{
- return util::difftv(timer.tv_, tv_) > 0;
-}
-
-bool Timer::operator<=(const Timer& timer) const
-{
- return util::difftv(timer.tv_, tv_) >= 0;
-}
-
-bool Timer::operator>(const Timer& timer) const
-{
- return util::difftv(tv_, timer.tv_) > 0;
+ return now - tp_;
}
-static timeval getCurrentTime()
+Timer::Clock::duration Timer::difference(const Timer& timer) const
{
- timeval tv;
- if(useClockGettime()) {
- timespec ts;
- int r = clock_gettime(CLOCK_MONOTONIC, &ts);
- assert(r == 0);
- tv.tv_sec = ts.tv_sec+2678400; // 1month offset(24*3600*31)
- tv.tv_usec = ts.tv_nsec/1000;
- } else {
- gettimeofday(&tv, nullptr);
+ if (timer.tp_ < tp_) {
+ return Timer::Clock::duration(std::chrono::seconds(0));
}
- return tv;
-}
-
-void Timer::reset()
-{
- tv_ = getCurrentTime();
-}
-
-void Timer::reset(time_t sec)
-{
- tv_.tv_sec = sec;
- tv_.tv_usec = 0;
-}
-
-bool Timer::elapsed(time_t sec) const
-{
- return
- util::difftv(getCurrentTime(), tv_) >= static_cast<int64_t>(sec)*1000000;
-}
-
-bool Timer::elapsedInMillis(int64_t millis) const
-{
- return util::difftv(getCurrentTime(), tv_)/1000 >= millis;
-}
-time_t Timer::difference() const
-{
- return util::difftv(getCurrentTime(), tv_)/1000000;
-}
-
-time_t Timer::difference(const timeval& tv) const
-{
- return util::difftv(tv, tv_)/1000000;
-}
-
-int64_t Timer::differenceInMillis() const
-{
- return util::difftv(getCurrentTime(), tv_)/1000;
-}
-
-int64_t Timer::differenceInMillis(const timeval& tv) const
-{
- return util::difftv(tv, tv_)/1000;
+ return timer.tp_ - tp_;
}
bool Timer::isZero() const
{
- return tv_.tv_sec == 0 && tv_.tv_usec == 0;
-}
-
-int64_t Timer::getTimeInMicros() const
-{
- return (int64_t)tv_.tv_sec*1000*1000+tv_.tv_usec;
-}
-
-int64_t Timer::getTimeInMillis() const
-{
- return (int64_t)tv_.tv_sec*1000+tv_.tv_usec/1000;
-}
-
-time_t Timer::getTime() const
-{
- return tv_.tv_sec;
-}
-
-void Timer::advance(time_t sec)
-{
- tv_.tv_sec += sec;
-}
-
-bool Timer::monotonicClock()
-{
- return useClockGettime();
+ return tp_.time_since_epoch() == Clock::duration::zero();
}
} // namespace aria2