diff --git a/HISTORY.md b/HISTORY.md index b5641bde..d4066410 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,7 @@ Dev version * socket.listen support put port into address. (address:port) * Redesign harbor/master/dummy, remove lots of C code and rewite in lua. * Remove block connect api, queue sending message during connecting now. +* Add skynet.time() v0.3.2 (2014-6-23) ---------- diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 385a6d38..68800d33 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -247,6 +247,10 @@ function skynet.starttime() return tonumber(c.command("STARTTIME")) end +function skynet.time() + return skynet.starttime() + skynet.now()/100 +end + function skynet.exit() skynet.send(".launcher","lua","REMOVE",skynet.self()) c.command("EXIT") diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index 43eb97e7..73f1ab7f 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -222,7 +222,7 @@ skynet_timeout(uint32_t handle, int time, int session) { // centisecond: 1/100 second static void -_systime(uint32_t *sec, uint32_t *cs) { +systime(uint32_t *sec, uint32_t *cs) { #if !defined(__APPLE__) struct timespec ti; clock_gettime(CLOCK_REALTIME, &ti); @@ -237,33 +237,44 @@ _systime(uint32_t *sec, uint32_t *cs) { } static uint64_t -_gettime() { +gettime() { uint64_t t; #if !defined(__APPLE__) + +#ifdef CLOCK_MONOTONIC_RAW +#define CLOCK_TIMER CLOCK_MONOTONIC_RAW +#else +#define CLOCK_TIMER CLOCK_MONOTONIC +#endif + struct timespec ti; - clock_gettime(CLOCK_MONOTONIC, &ti); - t = ti.tv_sec * 100; - t += (uint32_t)(ti.tv_nsec / 10000000); + clock_gettime(CLOCK_TIMER, &ti); + t = (uint64_t)ti.tv_sec * 100; + t += ti.tv_nsec / 10000000; #else struct timeval tv; gettimeofday(&tv, NULL); - t = ti.tv_sec * 100; - t += (uint32_t)(tv.tv_usec / 10000); + t = (uint64_t)ti.tv_sec * 100; + t += tv.tv_usec / 10000; #endif return t; } void skynet_updatetime(void) { - uint64_t cp = _gettime(); + uint64_t cp = gettime(); if(cp < TI->current_point) { skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; - // when cs > 0xffffffff(about 49710), time rewind + uint32_t oc = TI->current; TI->current += diff; + if (TI->current < oc) { + // when cs > 0xffffffff(about 497 days), time rewind + TI->starttime += 0xffffffff / 100; + } int i; for (i=0;istarttime, &TI->current); - uint64_t point = _gettime(); + systime(&TI->starttime, &TI->current); + uint64_t point = gettime(); TI->current_point = point; TI->origin_point = point; }