TimerA2.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "TimerA2.h"
  36. #include <cassert>
  37. #include "util.h"
  38. namespace aria2 {
  39. Timer::Timer()
  40. {
  41. reset();
  42. }
  43. Timer::Timer(const Timer& timer):_tv(timer._tv) {}
  44. Timer::Timer(time_t sec)
  45. {
  46. reset(sec);
  47. }
  48. Timer::Timer(const struct timeval& tv):_tv(tv) {}
  49. static bool useClockGettime()
  50. {
  51. static timespec ts;
  52. static int r = clock_gettime(CLOCK_MONOTONIC, &ts);
  53. return r == 0;
  54. }
  55. Timer& Timer::operator=(const Timer& timer)
  56. {
  57. if(this != &timer) {
  58. _tv = timer._tv;
  59. }
  60. return *this;
  61. }
  62. bool Timer::operator<(const Timer& timer) const
  63. {
  64. return util::difftv(timer._tv, _tv) > 0;
  65. }
  66. bool Timer::operator>(const Timer& timer) const
  67. {
  68. return util::difftv(_tv, timer._tv) > 0;
  69. }
  70. static timeval getCurrentTime()
  71. {
  72. timeval tv;
  73. if(useClockGettime()) {
  74. timespec ts;
  75. int r = clock_gettime(CLOCK_MONOTONIC, &ts);
  76. assert(r == 0);
  77. tv.tv_sec = ts.tv_sec+2678400; // 1month offset(24*3600*31)
  78. tv.tv_usec = ts.tv_nsec/1000;
  79. } else {
  80. gettimeofday(&tv, 0);
  81. }
  82. return tv;
  83. }
  84. void Timer::reset()
  85. {
  86. _tv = getCurrentTime();
  87. }
  88. void Timer::reset(time_t sec)
  89. {
  90. _tv.tv_sec = sec;
  91. _tv.tv_usec = 0;
  92. }
  93. bool Timer::elapsed(time_t sec) const
  94. {
  95. return
  96. util::difftv(getCurrentTime(), _tv) >= static_cast<int64_t>(sec)*1000000;
  97. }
  98. bool Timer::elapsedInMillis(int64_t millis) const
  99. {
  100. return util::difftv(getCurrentTime(), _tv)/1000 >= millis;
  101. }
  102. time_t Timer::difference() const
  103. {
  104. return util::difftv(getCurrentTime(), _tv)/1000000;
  105. }
  106. time_t Timer::difference(const timeval& tv) const
  107. {
  108. return util::difftv(tv, _tv)/1000000;
  109. }
  110. int64_t Timer::differenceInMillis() const
  111. {
  112. return util::difftv(getCurrentTime(), _tv)/1000;
  113. }
  114. int64_t Timer::differenceInMillis(const timeval& tv) const
  115. {
  116. return util::difftv(tv, _tv)/1000;
  117. }
  118. bool Timer::isZero() const
  119. {
  120. return _tv.tv_sec == 0 && _tv.tv_usec == 0;
  121. }
  122. int64_t Timer::getTimeInMicros() const
  123. {
  124. return (int64_t)_tv.tv_sec*1000*1000+_tv.tv_usec;
  125. }
  126. int64_t Timer::getTimeInMillis() const
  127. {
  128. return (int64_t)_tv.tv_sec*1000+_tv.tv_usec/1000;
  129. }
  130. time_t Timer::getTime() const
  131. {
  132. return _tv.tv_sec;
  133. }
  134. void Timer::advance(time_t sec)
  135. {
  136. _tv.tv_sec += sec;
  137. }
  138. bool Timer::monotonicClock()
  139. {
  140. return useClockGettime();
  141. }
  142. } // namespace aria2