renamed QSE_INITNTIME to QSE_INIT_NTIME

made similar changes to other NTIME macros
implemented vm_gettime() for WIN32 using GetTickCount()/GetTickCount64()
This commit is contained in:
hyunghwan.chung 2018-11-01 09:04:19 +00:00
parent 6e8b772d9b
commit ae06fb7b8b
3 changed files with 71 additions and 27 deletions

View File

@ -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 * 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 * can be represented in a small integer with leaving almost zero chance
* of overflow. */ * 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) 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 /* this code assumes that the monotonic clock returns a small value
* that can fit into a SmallInteger, even after some additions. */ * that can fit into a SmallInteger, even after some additions. */
vm_gettime (moo, &now); 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) if (ft.sec < 0 || ft.sec > MOO_SMOOI_MAX)
{ {
/* soft error - cannot represent the expiry time in a small integer. */ /* 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_sec));
MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(moo->sem_heap[0]->u.timed.ftime_nsec)); 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_sec),
MOO_OOP_TO_SMOOI(moo->sem_heap[0]->u.timed.ftime_nsec) 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; 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) else if (moo->processor->active == moo->nil_process)
{ {
/* no running process. before firing time. */ /* 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) if (moo->sem_io_wait_count > 0)
{ {
@ -4198,7 +4198,7 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo)
do 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); vm_muxwait (moo, &ft);
} }
while (moo->processor->active == moo->nil_process && !moo->abort_req); while (moo->processor->active == moo->nil_process && !moo->abort_req);

View File

@ -268,6 +268,10 @@ struct xtn_t
#if defined(_WIN32) #if defined(_WIN32)
HANDLE waitable_timer; HANDLE waitable_timer;
DWORD tc_last;
DWORD tc_overflow;
#elif defined(__OS2__)
ULONG tc_last;
#endif #endif
#if defined(USE_DEVPOLL) #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); 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"); bb->fp = fopen (bb->fn, "rb");
#else #else
bb->fp = fopen (bb->fn, "r"); 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) static void vm_gettime (moo_t* moo, moo_ntime_t* now)
{ {
#if defined(_WIN32) #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()? */ /* 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 */ /* 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__)) #elif defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))
clock_t c; clock_t c;
@ -1655,24 +1695,25 @@ static void vm_gettime (moo_t* moo, moo_ntime_t* now)
#else #else
# error UNSUPPORTED CLOCKS_PER_SEC # error UNSUPPORTED CLOCKS_PER_SEC
#endif #endif
#elif defined(macintosh) #elif defined(macintosh)
UnsignedWide tick; UnsignedWide tick;
moo_uint64_t tick64; moo_uint64_t tick64;
Microseconds (&tick); Microseconds (&tick);
tick64 = *(moo_uint64_t*)&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) #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
struct timespec ts; struct timespec ts;
clock_gettime (CLOCK_MONOTONIC, &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) #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
struct timespec ts; struct timespec ts;
clock_gettime (CLOCK_REALTIME, &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 #else
struct timeval tv; struct timeval tv;
gettimeofday (&tv, MOO_NULL); 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 #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); ns.nsec = MOO_USEC_TO_NSEC(tv.tv_usec);
} }
#endif #endif
MOO_ADDNTIME (&ns, &ns, dur); MOO_ADD_NTIME (&ns, &ns, dur);
ts.tv_sec = ns.sec; ts.tv_sec = ns.sec;
ts.tv_nsec = ns.nsec; 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) if (xtn->waitable_timer)
{ {
LARGE_INTEGER li; LARGE_INTEGER li;
goto normal_sleep;
li.QuadPart = -MOO_SECNSEC_TO_NSEC(dur->sec, dur->nsec); 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; if(SetWaitableTimer(xtn->waitable_timer, &li, 0, MOO_NULL, MOO_NULL, FALSE) == FALSE) goto normal_sleep;
WaitForSingleObject(xtn->waitable_timer, INFINITE); WaitForSingleObject(xtn->waitable_timer, INFINITE);
@ -2261,6 +2303,7 @@ static void setup_tick (void)
#elif defined(__OS2__) #elif defined(__OS2__)
/* TODO: */ /* TODO: */
#error UNSUPPORTED
#elif defined(macintosh) #elif defined(macintosh)
@ -2309,8 +2352,9 @@ static void cancel_tick (void)
} }
#elif defined(__OS2__) #elif defined(__OS2__)
/* TODO: */ /* TODO: */
#error UNSUPPORTED
#elif defined(macintosh) #elif defined(macintosh)
RmvTime ((QElem*)&g_tmtask); RmvTime ((QElem*)&g_tmtask);
/*DisposeTimerProc (g_tmtask.tmAddr);*/ /*DisposeTimerProc (g_tmtask.tmAddr);*/

View File

@ -392,31 +392,31 @@ struct moo_ntime_t
moo_int32_t nsec; /* nanoseconds */ moo_int32_t nsec; /* nanoseconds */
}; };
#define MOO_INITNTIME(c,s,ns) (((c)->sec = (s)), ((c)->nsec = (ns))) #define MOO_INIT_NTIME(c,s,ns) (((c)->sec = (s)), ((c)->nsec = (ns)))
#define MOO_CLEARNTIME(c) MOO_INITNTIME(c, 0, 0) #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 { \ do { \
(c)->sec = (a)->sec + (b)->sec; \ (c)->sec = (a)->sec + (b)->sec; \
(c)->nsec = (a)->nsec + (b)->nsec; \ (c)->nsec = (a)->nsec + (b)->nsec; \
while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \ while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \
} while(0) } while(0)
#define MOO_ADDNTIMESNS(c,a,s,ns) \ #define MOO_ADD_NTIME_SNS(c,a,s,ns) \
do { \ do { \
(c)->sec = (a)->sec + (s); \ (c)->sec = (a)->sec + (s); \
(c)->nsec = (a)->nsec + (ns); \ (c)->nsec = (a)->nsec + (ns); \
while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \ while ((c)->nsec >= MOO_NSECS_PER_SEC) { (c)->sec++; (c)->nsec -= MOO_NSECS_PER_SEC; } \
} while(0) } while(0)
#define MOO_SUBNTIME(c,a,b) \ #define MOO_SUB_NTIME(c,a,b) \
do { \ do { \
(c)->sec = (a)->sec - (b)->sec; \ (c)->sec = (a)->sec - (b)->sec; \
(c)->nsec = (a)->nsec - (b)->nsec; \ (c)->nsec = (a)->nsec - (b)->nsec; \
while ((c)->nsec < 0) { (c)->sec--; (c)->nsec += MOO_NSECS_PER_SEC; } \ while ((c)->nsec < 0) { (c)->sec--; (c)->nsec += MOO_NSECS_PER_SEC; } \
} while(0) } while(0)
#define MOO_SUBNTIMESNS(c,a,s,ns) \ #define MOO_SUB_NTIME_SNS(c,a,s,ns) \
do { \ do { \
(c)->sec = (a)->sec - s; \ (c)->sec = (a)->sec - s; \
(c)->nsec = (a)->nsec - ns; \ (c)->nsec = (a)->nsec - ns; \
@ -424,7 +424,7 @@ struct moo_ntime_t
} while(0) } 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 * PRIMITIVE MACROS