From ae06fb7b8b09679246f4d3f7e8cd88958adf0fcf Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 1 Nov 2018 09:04:19 +0000 Subject: [PATCH] renamed QSE_INITNTIME to QSE_INIT_NTIME made similar changes to other NTIME macros implemented vm_gettime() for WIN32 using GetTickCount()/GetTickCount64() --- moo/lib/exec.c | 12 ++++---- moo/lib/main.c | 72 ++++++++++++++++++++++++++++++++++++++--------- moo/lib/moo-cmn.h | 14 ++++----- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 574ab61..ce88a5c 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -217,7 +217,7 @@ static MOO_INLINE void vm_gettime (moo_t* moo, moo_ntime_t* now) * thus becomes relative to it. this way, it is kept small such that it * can be represented in a small integer with leaving almost zero chance * of overflow. */ - MOO_SUBNTIME (now, now, &moo->exec_start_time); /* now = now - exec_start_time */ + MOO_SUB_NTIME (now, now, &moo->exec_start_time); /* now = now - exec_start_time */ } static MOO_INLINE void vm_sleep (moo_t* moo, const moo_ntime_t* dur) @@ -2473,7 +2473,7 @@ static moo_pfrc_t pf_semaphore_signal_timed (moo_t* moo, moo_mod_t* mod, moo_ooi /* this code assumes that the monotonic clock returns a small value * that can fit into a SmallInteger, even after some additions. */ vm_gettime (moo, &now); - MOO_ADDNTIMESNS (&ft, &now, MOO_OOP_TO_SMOOI(sec), MOO_OOP_TO_SMOOI(nsec)); + MOO_ADD_NTIME_SNS (&ft, &now, MOO_OOP_TO_SMOOI(sec), MOO_OOP_TO_SMOOI(nsec)); if (ft.sec < 0 || ft.sec > MOO_SMOOI_MAX) { /* soft error - cannot represent the expiry time in a small integer. */ @@ -4110,12 +4110,12 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo) MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(moo->sem_heap[0]->u.timed.ftime_sec)); MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(moo->sem_heap[0]->u.timed.ftime_nsec)); - MOO_INITNTIME (&ft, + MOO_INIT_NTIME (&ft, MOO_OOP_TO_SMOOI(moo->sem_heap[0]->u.timed.ftime_sec), MOO_OOP_TO_SMOOI(moo->sem_heap[0]->u.timed.ftime_nsec) ); - if (MOO_CMPNTIME(&ft, (moo_ntime_t*)&now) <= 0) + if (MOO_CMP_NTIME(&ft, (moo_ntime_t*)&now) <= 0) { moo_oop_process_t proc; @@ -4155,7 +4155,7 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo) else if (moo->processor->active == moo->nil_process) { /* no running process. before firing time. */ - MOO_SUBNTIME (&ft, &ft, (moo_ntime_t*)&now); + MOO_SUB_NTIME (&ft, &ft, (moo_ntime_t*)&now); if (moo->sem_io_wait_count > 0) { @@ -4198,7 +4198,7 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo) do { - MOO_INITNTIME (&ft, 3, 0); /* TODO: use a configured time */ + MOO_INIT_NTIME (&ft, 3, 0); /* TODO: use a configured time */ vm_muxwait (moo, &ft); } while (moo->processor->active == moo->nil_process && !moo->abort_req); diff --git a/moo/lib/main.c b/moo/lib/main.c index 5ab0515..e0b3b26 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -268,6 +268,10 @@ struct xtn_t #if defined(_WIN32) HANDLE waitable_timer; + DWORD tc_last; + DWORD tc_overflow; + #elif defined(__OS2__) + ULONG tc_last; #endif #if defined(USE_DEVPOLL) @@ -429,7 +433,7 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg) moo_copy_bcstr (bb->fn, pathlen + 1, xtn->source_path); } -#if defined(__DOS__) || defined(_WIN32) || defined(__OS2__) +#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) bb->fp = fopen (bb->fn, "rb"); #else bb->fp = fopen (bb->fn, "r"); @@ -1629,15 +1633,51 @@ static void vm_cleanup (moo_t* moo) static void vm_gettime (moo_t* moo, moo_ntime_t* now) { #if defined(_WIN32) - /* TODO: */ -#elif defined(__OS2__) - ULONG out; -/* TODO: handle overflow?? */ + #if defined(_WIN64) || (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)) + moo_uint64_t bigsec, bigmsec; + bigmsec = GetTickCount64(); + #else + xtn_t* xtn = (xtn_t*)moo_getxtn(moo); + moo_uint64_t bigsec, bigmsec; + DWORD msec; + + msec = GetTickCount(); /* this can sustain for 49.7 days */ + if (msec < xtn->tc_last) + { + /* i assume the difference is never bigger than 49.7 days */ + //diff = (MOO_TYPE_MAX(DWORD) - xtn->tc_last) + 1 + msec; + xtn->tc_overflow++; + bigmsec = ((moo_uint64_t)MOO_TYPE_MAX(DWORD) * xtn->tc_overflow) + msec; + } + else bigmsec = msec; + xtn->tc_last = msec; + #endif + + bigsec = MOO_MSEC_TO_SEC(bigmsec); + bigmsec -= MOO_SEC_TO_MSEC(bigsec); + MOO_INIT_NTIME(now, bigsec, MOO_MSEC_TO_NSEC(bigmsec)); + +#elif defined(__OS2__) + xtn_t* xtn = (xtn_t*)moo_getxtn(moo); + moo_uint64_t bigsec, bigmsec; + ULONG msec; + /* TODO: use DosTmrQueryTime() and DosTmrQueryFreq()? */ - DosQuerySysInfo (QSV_MS_COUNT, QSV_MS_COUNT, &out, MOO_SIZEOF(out)); /* milliseconds */ + DosQuerySysInfo (QSV_MS_COUNT, QSV_MS_COUNT, &msec, MOO_SIZEOF(msec)); /* milliseconds */ /* it must return NO_ERROR */ - MOO_INITNTIME (now, MOO_MSEC_TO_SEC(out), MOO_MSEC_TO_NSEC(out)); + if (msec < xtn->tc_last) + { + xtn->tc_overflow++; + bigmsec = ((moo_uint64_t)MOO_TYPE_MAX(ULONG) * xtn->tc_overflow) + msec; + } + else bigmsec = msec; + xtn->tc_last = msec; + + bigsec = MOO_MSEC_TO_SEC(bigmsec); + bigmsec -= MOO_SEC_TO_MSEC(bigsec); + MOO_INIT_NTIME (now, bigsec, MOO_MSEC_TO_NSEC(bigmsec)); + #elif defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__)) clock_t c; @@ -1655,24 +1695,25 @@ static void vm_gettime (moo_t* moo, moo_ntime_t* now) #else # error UNSUPPORTED CLOCKS_PER_SEC #endif + #elif defined(macintosh) UnsignedWide tick; moo_uint64_t tick64; Microseconds (&tick); tick64 = *(moo_uint64_t*)&tick; - MOO_INITNTIME (now, MOO_USEC_TO_SEC(tick64), MOO_USEC_TO_NSEC(tick64)); + MOO_INIT_NTIME (now, MOO_USEC_TO_SEC(tick64), MOO_USEC_TO_NSEC(tick64)); #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) struct timespec ts; clock_gettime (CLOCK_MONOTONIC, &ts); - MOO_INITNTIME(now, ts.tv_sec, ts.tv_nsec); + MOO_INIT_NTIME(now, ts.tv_sec, ts.tv_nsec); #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) struct timespec ts; clock_gettime (CLOCK_REALTIME, &ts); - MOO_INITNTIME(now, ts.tv_sec, ts.tv_nsec); + MOO_INIT_NTIME(now, ts.tv_sec, ts.tv_nsec); #else struct timeval tv; gettimeofday (&tv, MOO_NULL); - MOO_INITNTIME(now, tv.tv_sec, MOO_USEC_TO_NSEC(tv.tv_usec)); + MOO_INIT_NTIME(now, tv.tv_sec, MOO_USEC_TO_NSEC(tv.tv_usec)); #endif } @@ -1899,7 +1940,7 @@ static void vm_muxwait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_c ns.nsec = MOO_USEC_TO_NSEC(tv.tv_usec); } #endif - MOO_ADDNTIME (&ns, &ns, dur); + MOO_ADD_NTIME (&ns, &ns, dur); ts.tv_sec = ns.sec; ts.tv_nsec = ns.nsec; @@ -2116,6 +2157,7 @@ static void vm_sleep (moo_t* moo, const moo_ntime_t* dur) if (xtn->waitable_timer) { LARGE_INTEGER li; +goto normal_sleep; li.QuadPart = -MOO_SECNSEC_TO_NSEC(dur->sec, dur->nsec); if(SetWaitableTimer(xtn->waitable_timer, &li, 0, MOO_NULL, MOO_NULL, FALSE) == FALSE) goto normal_sleep; WaitForSingleObject(xtn->waitable_timer, INFINITE); @@ -2260,7 +2302,8 @@ static void setup_tick (void) } #elif defined(__OS2__) - /* TODO: */ + /* TODO: */ + #error UNSUPPORTED #elif defined(macintosh) @@ -2309,8 +2352,9 @@ static void cancel_tick (void) } #elif defined(__OS2__) + /* TODO: */ + #error UNSUPPORTED - /* TODO: */ #elif defined(macintosh) RmvTime ((QElem*)&g_tmtask); /*DisposeTimerProc (g_tmtask.tmAddr);*/ diff --git a/moo/lib/moo-cmn.h b/moo/lib/moo-cmn.h index 16db360..580ee7d 100644 --- a/moo/lib/moo-cmn.h +++ b/moo/lib/moo-cmn.h @@ -392,31 +392,31 @@ struct moo_ntime_t moo_int32_t nsec; /* nanoseconds */ }; -#define MOO_INITNTIME(c,s,ns) (((c)->sec = (s)), ((c)->nsec = (ns))) -#define MOO_CLEARNTIME(c) MOO_INITNTIME(c, 0, 0) +#define MOO_INIT_NTIME(c,s,ns) (((c)->sec = (s)), ((c)->nsec = (ns))) +#define MOO_CLEAR_NTIME(c) MOO_INIT_NTIME(c, 0, 0) -#define MOO_ADDNTIME(c,a,b) \ +#define MOO_ADD_NTIME(c,a,b) \ do { \ (c)->sec = (a)->sec + (b)->sec; \ (c)->nsec = (a)->nsec + (b)->nsec; \ while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \ } while(0) -#define MOO_ADDNTIMESNS(c,a,s,ns) \ +#define MOO_ADD_NTIME_SNS(c,a,s,ns) \ do { \ (c)->sec = (a)->sec + (s); \ (c)->nsec = (a)->nsec + (ns); \ while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \ } while(0) -#define MOO_SUBNTIME(c,a,b) \ +#define MOO_SUB_NTIME(c,a,b) \ do { \ (c)->sec = (a)->sec - (b)->sec; \ (c)->nsec = (a)->nsec - (b)->nsec; \ while ((c)->nsec < 0) { (c)->sec--; (c)->nsec += MOO_NSECS_PER_SEC; } \ } while(0) -#define MOO_SUBNTIMESNS(c,a,s,ns) \ +#define MOO_SUB_NTIME_SNS(c,a,s,ns) \ do { \ (c)->sec = (a)->sec - s; \ (c)->nsec = (a)->nsec - ns; \ @@ -424,7 +424,7 @@ struct moo_ntime_t } while(0) -#define MOO_CMPNTIME(a,b) (((a)->sec == (b)->sec)? ((a)->nsec - (b)->nsec): ((a)->sec - (b)->sec)) +#define MOO_CMP_NTIME(a,b) (((a)->sec == (b)->sec)? ((a)->nsec - (b)->nsec): ((a)->sec - (b)->sec)) /* ========================================================================= * PRIMITIVE MACROS