Skip to content

Commit 4838318

Browse files
committed
Time::HiRes add nv_gettimeofday() to usleep() and sleep()
1 parent c0d605b commit 4838318

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

dist/Time-HiRes/HiRes.xs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,11 +1312,19 @@ void
13121312
usleep(useconds)
13131313
NV useconds
13141314
PREINIT:
1315+
#ifndef HAS_NV_GETTIMEOFDAY
13151316
struct timeval Ta, Tb;
1317+
#else
1318+
NV Ta_nv, Tb_nv;
1319+
#endif
13161320
SV* rsv;
13171321
NV RETVAL;
13181322
CODE:
1323+
#ifndef HAS_NV_GETTIMEOFDAY
13191324
gettimeofday(&Ta, NULL);
1325+
#else
1326+
Ta_nv = nv_gettimeofday();
1327+
#endif
13201328
if (items > 0) {
13211329
if (useconds >= NV_1E6) {
13221330
IV seconds = (IV) (useconds / NV_1E6);
@@ -1334,12 +1342,16 @@ usleep(useconds)
13341342
usleep((U32)useconds);
13351343
} else
13361344
PerlProc_pause();
1337-
1345+
#ifndef HAS_NV_GETTIMEOFDAY
13381346
gettimeofday(&Tb, NULL);
13391347
# if 0
13401348
printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
13411349
# endif
13421350
RETVAL = NV_1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
1351+
#else
1352+
Tb_nv = nv_gettimeofday();
1353+
RETVAL = NV_1E6*(Tb_nv - Ta_nv);
1354+
#endif
13431355
TMR_TARGn(rsv, RETVAL, 1);
13441356
SETs(rsv);
13451357
return; /* no PUTBACK no PUSH, 1 in, 1 out */
@@ -1384,11 +1396,19 @@ nanosleep(nsec)
13841396
void
13851397
sleep(...)
13861398
PREINIT:
1399+
#ifndef HAS_NV_GETTIMEOFDAY
13871400
struct timeval Ta, Tb;
1401+
#else
1402+
NV Ta_nv, Tb_nv;
1403+
#endif
13881404
SV* rsv;
13891405
NV RETVAL;
13901406
PPCODE:
1407+
#ifndef HAS_NV_GETTIMEOFDAY
13911408
gettimeofday(&Ta, NULL);
1409+
#else
1410+
Ta_nv = nv_gettimeofday();
1411+
#endif
13921412
if (items > 0) {
13931413
NV seconds = SvNV(ST(0));
13941414
if (seconds >= 0.0) {
@@ -1416,12 +1436,16 @@ sleep(...)
14161436
"): negative time not invented yet");
14171437
} else
14181438
PerlProc_pause();
1419-
1439+
#ifndef HAS_NV_GETTIMEOFDAY
14201440
gettimeofday(&Tb, NULL);
14211441
# if 0
14221442
printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
14231443
# endif
14241444
RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
1445+
#else
1446+
Tb_nv = nv_gettimeofday();
1447+
RETVAL = Tb_nv - Ta_nv;
1448+
#endif
14251449
TMR_TARGn(rsv, RETVAL, 1);
14261450
PUSHs(rsv);
14271451
PUTBACK;

0 commit comments

Comments
 (0)