1 module kaleidic.api.rabbitmq.platform_utils;
2 import std.conv:to;
3 // import kaleidic.api.rabbitmq;
4 
5 /+
6 	#include <stdint.h>
7 	#include <sys/time.h>
8 	#include <time.h>
9 	#include <unistd.h>
10 +/
11 
12 version(Posix)
13 {
14 	import core.sys.posix.sys.time: timeval,timespec;
15 
16 	extern(C) int nanosleep(in timespec*, timespec*);
17 	extern(C) int gettimeofday(timeval*, void*);
18 	ulong now_microseconds()
19 	{
20 	  timeval tv;
21 	  gettimeofday(&tv, null);
22 	  return tv.tv_sec.to!ulong * 1000000 + tv.tv_usec.to!ulong;
23 	}
24 
25 	void microsleep(int usec)
26 	{
27 	  timespec req;
28 	  req.tv_sec = 0;
29 	  req.tv_nsec = 1000 * usec;
30 	  nanosleep(&req, null);
31 	}
32 }
33 
34 else version(Windows)
35 {
36 	import core.sys.windows.winbase : FILETIME, GetSystemTimeAsFileTime, Sleep;
37 	// import core.sys.windows.winnt;
38 	// import core.sys.windows.syserror;
39 
40 	/+
41 		#include <stdint.h>
42 		#include <windows.h>
43 	+/
44 
45 	ulong now_microseconds()
46 	{
47 		FILETIME ft;
48 		GetSystemTimeAsFileTime(&ft);
49 		return ((ft.dwHighDateTime.to!ulong << 32) | ft.dwLowDateTime.to!ulong) / 10;
50 	}
51 
52 	void microsleep(int usec) { Sleep(usec / 1000); }
53 }
54 else pragma(msg, "only posix and windows platforms supported");
55