26#include <mach/mach_time.h>
27#elif !defined(__WINDOWS__)
36#define gettime_ms __gettime_ms
37 inline int64_t __gettime_ms()
42 static mach_timebase_info_data_t timebase;
44 if (timebase.denom == 0)
45 (void)mach_timebase_info(&timebase);
47 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
48 return ticks / 1000000;
49#elif defined(__WINDOWS__)
50 LARGE_INTEGER tickPerSecond;
52 if (QueryPerformanceFrequency(&tickPerSecond))
54 QueryPerformanceCounter(&tick);
55 return (int64_t) (tick.QuadPart / (tickPerSecond.QuadPart / 1000.0));
60 clock_gettime(CLOCK_MONOTONIC, &time);
61 return (int64_t)time.tv_sec * 1000 + time.tv_nsec / 1000000;
68 Timeout() : m_time(0) { }
69 Timeout(
unsigned millisec) : m_time(0) { set(millisec); }
71 void set(
unsigned millisec)
73 m_time = gettime_ms() + millisec;
78 return (m_time > 0 ?
true :
false);
86 unsigned time_left()
const
88 int64_t time = gettime_ms();
89 return (time > m_time ? 0 :
static_cast<unsigned>(m_time - time));
92 bool operator==(
const Timeout& other)
const
94 return m_time == other.m_time;
97 bool operator!=(
const Timeout& other)
const
99 return m_time != other.m_time;
102 bool operator<(
const Timeout& other)
const
104 return m_time < other.m_time;
107 bool operator>(
const Timeout& other)
const
109 return m_time > other.m_time;
112 bool operator>=(
const Timeout& other)
const
114 return !(m_time < other.m_time);
117 bool operator<=(
const Timeout& other)
const
119 return !(m_time > other.m_time);