1 /** 2 * \file os_ticks.c 3 * \brief Return the system tick counter (in microsecond units). 4 */ 5 6 #include <stdlib.h> 7 #include "premake.h" 8 9 // Epochs used by Windows and Unix time APIs. These adjustments, 10 // when added to the time value returned by the OS, will yield 11 // the number of microsecond intervals since Jan 1, year 1. 12 #define TICK_EPOCH_WINDOWS ((lua_Integer)0x0701ce1722770000) 13 #define TICK_EPOCH_UNIX ((lua_Integer)0x00dcbffeff2bc000) 14 15 #define TICKS_PER_SECOND ((lua_Integer)1000000) 16 os_ticks(lua_State * L)17int os_ticks(lua_State* L) 18 { 19 lua_Integer ticks = 0; 20 21 #if PLATFORM_WINDOWS 22 FILETIME fileTimeUtc; 23 GetSystemTimeAsFileTime(&fileTimeUtc); 24 ticks = 25 TICK_EPOCH_WINDOWS 26 + ((lua_Integer)fileTimeUtc.dwHighDateTime << 32 27 | (lua_Integer)fileTimeUtc.dwLowDateTime); 28 ticks /= (lua_Integer)10; 29 #else 30 struct timeval tp; 31 if (gettimeofday(&tp, NULL) == 0) { 32 ticks = 33 TICK_EPOCH_UNIX 34 + (lua_Integer)tp.tv_sec * TICKS_PER_SECOND 35 + (lua_Integer)tp.tv_usec; 36 } 37 #endif 38 39 lua_pushinteger(L, ticks); 40 return 1; 41 } 42