CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
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#elif !defined(__WINDOWS__)
28#include <time.h>
29#endif
30
31#ifdef NSROOT
32namespace NSROOT {
33#endif
34namespace OS
35{
36#define gettime_ms __gettime_ms
37 inline int64_t __gettime_ms()
38 {
39#if defined(__APPLE__)
40 // Recommended by Apple's QA1398.
41 int64_t ticks = 0;
42 static mach_timebase_info_data_t timebase;
43 // Get the timebase if this is the first time we run.
44 if (timebase.denom == 0)
45 (void)mach_timebase_info(&timebase);
46 // Use timebase to convert absolute time tick units into nanoseconds.
47 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
48 return ticks / 1000000;
49#elif defined(__WINDOWS__)
50 LARGE_INTEGER tickPerSecond;
51 LARGE_INTEGER tick;
52 if (QueryPerformanceFrequency(&tickPerSecond))
53 {
54 QueryPerformanceCounter(&tick);
55 return (int64_t) (tick.QuadPart / (tickPerSecond.QuadPart / 1000.0));
56 }
57 return -1;
58#else
59 timespec time;
60 clock_gettime(CLOCK_MONOTONIC, &time);
61 return (int64_t)time.tv_sec * 1000 + time.tv_nsec / 1000000;
62#endif
63 }
64
65 class Timeout
66 {
67 public:
68 Timeout() : m_time(0) { }
69 Timeout(unsigned millisec) : m_time(0) { set(millisec); }
70
71 void set(unsigned millisec)
72 {
73 m_time = gettime_ms() + millisec;
74 }
75
76 bool is_set() const
77 {
78 return (m_time > 0 ? true : false);
79 }
80
81 void clear()
82 {
83 m_time = 0;
84 }
85
86 unsigned time_left() const
87 {
88 int64_t time = gettime_ms();
89 return (time > m_time ? 0 : static_cast<unsigned>(m_time - time));
90 }
91
92 bool operator==(const Timeout& other) const
93 {
94 return m_time == other.m_time;
95 }
96
97 bool operator!=(const Timeout& other) const
98 {
99 return m_time != other.m_time;
100 }
101
102 bool operator<(const Timeout& other) const
103 {
104 return m_time < other.m_time;
105 }
106
107 bool operator>(const Timeout& other) const
108 {
109 return m_time > other.m_time;
110 }
111
112 bool operator>=(const Timeout& other) const
113 {
114 return !(m_time < other.m_time);
115 }
116
117 bool operator<=(const Timeout& other) const
118 {
119 return !(m_time > other.m_time);
120 }
121
122 private:
123 int64_t m_time;
124 };
125}
126#ifdef NSROOT
127}
128#endif