CPPMyth
Library to interoperate with MythTV server
timeout.h
1 #pragma once
2 /*
3  * Copyright (C) 2015 Jean-Luc Barriere
4  *
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your option)
8  * any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; see the file COPYING. If not, write to
17  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301 USA
19  * http://www.gnu.org/copyleft/gpl.html
20  *
21  */
22 
23 #include "../os.h"
24 
25 #if defined(__APPLE__)
26 #include <mach/mach_time.h>
27 #endif
28 
29 #ifdef NSROOT
30 namespace NSROOT {
31 #endif
32 namespace OS
33 {
34 #define gettime_ms __gettime_ms
35  inline int64_t __gettime_ms()
36  {
37 #if defined(__APPLE__)
38  // Recommended by Apple's QA1398.
39  int64_t ticks = 0;
40  static mach_timebase_info_data_t timebase;
41  // Get the timebase if this is the first time we run.
42  if (timebase.denom == 0)
43  (void)mach_timebase_info(&timebase);
44  // Use timebase to convert absolute time tick units into nanoseconds.
45  ticks = mach_absolute_time() * timebase.numer / timebase.denom;
46  return ticks / 1000000;
47 #elif defined(__WINDOWS__)
48  LARGE_INTEGER tickPerSecond;
49  LARGE_INTEGER tick;
50  if (QueryPerformanceFrequency(&tickPerSecond))
51  {
52  QueryPerformanceCounter(&tick);
53  return (int64_t) (tick.QuadPart / (tickPerSecond.QuadPart / 1000.0));
54  }
55  return -1;
56 #else
57  timespec time;
58  clock_gettime(CLOCK_MONOTONIC, &time);
59  return (int64_t)time.tv_sec * 1000 + time.tv_nsec / 1000000;
60 #endif
61  }
62 
63  class CTimeout
64  {
65  public:
66  CTimeout() : m_time(0) { }
67  CTimeout(unsigned millisec) : m_time(0) { Set(millisec); }
68 
69  void Set(unsigned millisec)
70  {
71  m_time = gettime_ms() + millisec;
72  }
73 
74  bool IsSet() const
75  {
76  return (m_time > 0 ? true : false);
77  }
78 
79  void Clear()
80  {
81  m_time = 0;
82  }
83 
84  unsigned TimeLeft() const
85  {
86  int64_t time = gettime_ms();
87  return (time > m_time ? 0 : static_cast<unsigned>(m_time - time));
88  }
89 
90  bool operator==(const CTimeout& other) const
91  {
92  return m_time == other.m_time;
93  }
94 
95  bool operator!=(const CTimeout& other) const
96  {
97  return m_time != other.m_time;
98  }
99 
100  bool operator<(const CTimeout& other) const
101  {
102  return m_time < other.m_time;
103  }
104 
105  bool operator>(const CTimeout& other) const
106  {
107  return m_time > other.m_time;
108  }
109 
110  bool operator>=(const CTimeout& other) const
111  {
112  return !(m_time < other.m_time);
113  }
114 
115  bool operator<=(const CTimeout& other) const
116  {
117  return !(m_time > other.m_time);
118  }
119 
120  private:
121  int64_t m_time;
122  };
123 }
124 #ifdef NSROOT
125 }
126 #endif