32 #include "platform.hpp" 41 #if defined _WIN32_WCE 42 #include <cmnintrin.h> 48 #if !defined ZMQ_HAVE_WINDOWS 52 #if defined HAVE_CLOCK_GETTIME || defined HAVE_GETHRTIME 56 #if defined ZMQ_HAVE_OSX 57 #include <mach/clock.h> 58 #include <mach/mach.h> 62 int clock_gettime (
int clock_id, timespec *ts)
65 if (clock_id != CLOCK_REALTIME) {
72 host_get_clock_service (mach_host_self (), CALENDAR_CLOCK, &cclock);
73 clock_get_time (cclock, &mts);
74 mach_port_deallocate (mach_task_self (), cclock);
75 ts->tv_sec = mts.tv_sec;
76 ts->tv_nsec = mts.tv_nsec;
81 #ifdef ZMQ_HAVE_WINDOWS 82 typedef ULONGLONG (*f_compatible_get_tick_count64)();
86 ULONGLONG compatible_get_tick_count64()
88 compatible_get_tick_count64_mutex.
lock();
89 static DWORD s_wrap = 0;
90 static DWORD s_last_tick = 0;
91 const DWORD current_tick = ::GetTickCount();
92 if (current_tick < s_last_tick)
95 s_last_tick = current_tick;
96 const ULONGLONG result = (
static_cast<ULONGLONG
>(s_wrap) << 32) +
static_cast<ULONGLONG
>(current_tick);
97 compatible_get_tick_count64_mutex.unlock();
101 f_compatible_get_tick_count64 init_compatible_get_tick_count64()
103 f_compatible_get_tick_count64 func = NULL;
104 HMODULE module = ::LoadLibraryA(
"Kernel32.dll");
106 func =
reinterpret_cast<f_compatible_get_tick_count64
>(::GetProcAddress(module,
"GetTickCount64"));
109 func = compatible_get_tick_count64;
111 ::FreeLibrary(module);
116 static f_compatible_get_tick_count64 my_get_tick_count64 = init_compatible_get_tick_count64();
121 #ifdef ZMQ_HAVE_WINDOWS
122 last_time (static_cast<uint64_t>((*my_get_tick_count64)()))
124 last_time (now_us () / 1000)
135 #if defined ZMQ_HAVE_WINDOWS 138 LARGE_INTEGER ticksPerSecond;
139 QueryPerformanceFrequency (&ticksPerSecond);
143 QueryPerformanceCounter (&tick);
147 double ticks_div = ticksPerSecond.QuadPart / 1000000.0;
148 return (uint64_t) (tick.QuadPart / ticks_div);
150 #elif defined HAVE_CLOCK_GETTIME && defined CLOCK_MONOTONIC 154 int rc = clock_gettime (CLOCK_MONOTONIC, &tv);
161 int rc = gettimeofday (&tv, NULL);
163 return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
165 return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_nsec / 1000);
167 #elif defined HAVE_GETHRTIME 169 return (gethrtime () / 1000);
175 int rc = gettimeofday (&tv, NULL);
177 return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
184 uint64_t tsc =
rdtsc ();
189 #ifdef ZMQ_HAVE_WINDOWS 194 return static_cast<uint64_t
>((*my_get_tick_count64)());
207 #ifdef ZMQ_HAVE_WINDOWS 208 last_time =
static_cast<uint64_t
>((*my_get_tick_count64)());
217 #if (defined _MSC_VER && (defined _M_IX86 || defined _M_X64)) 219 #elif (defined __GNUC__ && (defined __i386__ || defined __x86_64__)) 221 __asm__
volatile (
"rdtsc" :
"=a" (low),
"=d" (high));
222 return (uint64_t) high << 32 | low;
223 #elif (defined __SUNPRO_CC && (__SUNPRO_CC >= 0x5100) && (defined __i386 || \ 224 defined __amd64 || defined __x86_64)) 229 asm(
"rdtsc" :
"=a" (tsc.u32val [0]),
"=d" (tsc.u32val [1]));
231 #elif defined(__s390__) 233 asm(
"\tstck\t%0\n" :
"=Q" (tsc) : :
"cc");