diff options
author | Oscar Lim <olim@ucla.edu> | 2016-05-07 14:37:25 -0700 |
---|---|---|
committer | Oscar Lim <olim@ucla.edu> | 2016-05-08 16:01:40 -0700 |
commit | 8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701 (patch) | |
tree | 9624af38c4ba35de244da2281e08049589140e20 /src/time.c | |
parent | 040d7066649ddfcb72424b1cb70d6ad00ea84a21 (diff) | |
download | luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.tar.gz luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.tar.bz2 luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.zip |
Support for monotime
Provide `monotime` function with at least 1 millisecond resolution.
Diffstat (limited to 'src/time.c')
-rw-r--r-- | src/time.c | 40 |
1 files changed, 37 insertions, 3 deletions
@@ -10,6 +10,10 @@ | |||
10 | #include <sys/time.h> | 10 | #include <sys/time.h> |
11 | #endif | 11 | #endif |
12 | 12 | ||
13 | #ifdef __APPLE__ | ||
14 | #include "time_osx.h" | ||
15 | #endif | ||
16 | |||
13 | #include "compat.h" | 17 | #include "compat.h" |
14 | 18 | ||
15 | /*------------------------------------------------------------------------- | 19 | /*------------------------------------------------------------------------- |
@@ -23,7 +27,7 @@ static double time_gettime(void) { | |||
23 | double t; | 27 | double t; |
24 | GetSystemTimeAsFileTime(&ft); | 28 | GetSystemTimeAsFileTime(&ft); |
25 | /* Windows file time (time since January 1, 1601 (UTC)) */ | 29 | /* Windows file time (time since January 1, 1601 (UTC)) */ |
26 | t = ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7); | 30 | t = ft.dwLowDateTime*1.0e-7 + ft.dwHighDateTime*(4294967296.0*1.0e-7); |
27 | /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ | 31 | /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ |
28 | return (t - 11644473600.0); | 32 | return (t - 11644473600.0); |
29 | } | 33 | } |
@@ -32,12 +36,32 @@ static double time_gettime(void) { | |||
32 | struct timeval v; | 36 | struct timeval v; |
33 | gettimeofday(&v, (struct timezone *) NULL); | 37 | gettimeofday(&v, (struct timezone *) NULL); |
34 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ | 38 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ |
35 | return v.tv_sec + v.tv_usec/1.0e6; | 39 | return v.tv_sec + v.tv_usec*1.0e-6; |
36 | } | 40 | } |
37 | #endif | 41 | #endif |
38 | 42 | ||
39 | /*------------------------------------------------------------------------- | 43 | /*------------------------------------------------------------------------- |
40 | * Returns the time the system has been up, in secconds. | 44 | * Gets monotonic time in s |
45 | * Returns | ||
46 | * time in s. | ||
47 | *-------------------------------------------------------------------------*/ | ||
48 | #ifdef _WIN32 | ||
49 | WINBASEAPI ULONGLONG WINAPI GetTickCount64(VOID); | ||
50 | |||
51 | static double time_monotime(void) { | ||
52 | ULONGLONG ms = GetTickCount64(); | ||
53 | return ms*1.0e-3; | ||
54 | } | ||
55 | #else | ||
56 | static double time_monotime(void) { | ||
57 | struct timespec ts; | ||
58 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
59 | return ts.tv_sec + ts.tv_nsec*1.0e-9; | ||
60 | } | ||
61 | #endif | ||
62 | |||
63 | /*------------------------------------------------------------------------- | ||
64 | * Returns the current system time, 1970 (UTC), in secconds. | ||
41 | *-------------------------------------------------------------------------*/ | 65 | *-------------------------------------------------------------------------*/ |
42 | static int time_lua_gettime(lua_State *L) | 66 | static int time_lua_gettime(lua_State *L) |
43 | { | 67 | { |
@@ -46,6 +70,15 @@ static int time_lua_gettime(lua_State *L) | |||
46 | } | 70 | } |
47 | 71 | ||
48 | /*------------------------------------------------------------------------- | 72 | /*------------------------------------------------------------------------- |
73 | * Returns the monotonic time the system has been up, in secconds. | ||
74 | *-------------------------------------------------------------------------*/ | ||
75 | static int time_lua_monotime(lua_State *L) | ||
76 | { | ||
77 | lua_pushnumber(L, time_monotime()); | ||
78 | return 1; | ||
79 | } | ||
80 | |||
81 | /*------------------------------------------------------------------------- | ||
49 | * Sleep for n seconds. | 82 | * Sleep for n seconds. |
50 | *-------------------------------------------------------------------------*/ | 83 | *-------------------------------------------------------------------------*/ |
51 | #ifdef _WIN32 | 84 | #ifdef _WIN32 |
@@ -79,6 +112,7 @@ static int time_lua_sleep(lua_State *L) | |||
79 | 112 | ||
80 | static luaL_Reg func[] = { | 113 | static luaL_Reg func[] = { |
81 | { "gettime", time_lua_gettime }, | 114 | { "gettime", time_lua_gettime }, |
115 | { "monotime", time_lua_monotime }, | ||
82 | { "sleep", time_lua_sleep }, | 116 | { "sleep", time_lua_sleep }, |
83 | { NULL, NULL } | 117 | { NULL, NULL } |
84 | }; | 118 | }; |