From: Jarkko Hietaniemi Date: Wed, 17 Oct 2001 14:48:38 +0000 (+0000) Subject: Limiting the Time::HiRes::sleep and usleep to X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=92bc48ca26b0fb2ba59389610ad13851aac4fcb8;p=p5sagit%2Fp5-mst-13.2.git Limiting the Time::HiRes::sleep and usleep to one second is not a good policy if drop-in replacement is still a goal. p4raw-id: //depot/perl@12473 --- diff --git a/ext/Time/HiRes/HiRes.pm b/ext/Time/HiRes/HiRes.pm index 4e5244b..72fc606 100644 --- a/ext/Time/HiRes/HiRes.pm +++ b/ext/Time/HiRes/HiRes.pm @@ -106,9 +106,8 @@ seconds like Time::HiRes::time() (see below). =item usleep ( $useconds ) Issues a usleep for the number of microseconds specified. Returns the -number of microseconds actually slept. The number of microseconds -B be between 0 and 1_000_0000 (inclusive): you B sleep -a minute by usleep(60_000_000). See also Time::HiRes::sleep() below. +number of microseconds actually slept. See also Time::HiRes::sleep() +below. =item ualarm ( $useconds [, $interval_useconds ] ) diff --git a/ext/Time/HiRes/HiRes.t b/ext/Time/HiRes/HiRes.t index a2cb206..e0f0cf7 100644 --- a/ext/Time/HiRes/HiRes.t +++ b/ext/Time/HiRes/HiRes.t @@ -223,9 +223,9 @@ unless (defined &Time::HiRes::setitimer $SIG{VTALRM} = 'DEFAULT'; } -$a = abs(sleep(1) - 1); +$a = abs(sleep(1.5) - 1.5); print $a < 0.1 ? "ok 20 # $a\n" : "not ok 20 # $a\n"; -$a = abs(usleep(1000000) / 1000000 - 1); +$a = abs(usleep(1_500_000) / 1_500_000 - 1.0); print $a < 0.1 ? "ok 21 # $a\n" : "not ok 21 # $a\n"; diff --git a/ext/Time/HiRes/HiRes.xs b/ext/Time/HiRes/HiRes.xs index 395e2b9..3bb1aa4 100644 --- a/ext/Time/HiRes/HiRes.xs +++ b/ext/Time/HiRes/HiRes.xs @@ -369,21 +369,27 @@ constant(name, arg) #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) -int +NV usleep(useconds) - int useconds + NV useconds PREINIT: struct timeval Ta, Tb; CODE: gettimeofday(&Ta, NULL); if (items > 0) { - if (useconds > 1000000) - croak("usleep: useconds must be between 0 and 1000000 (inclusive)"); - usleep(useconds); + if (useconds > 1E6) { + IV seconds = (IV) (useconds / 1E6); + sleep(seconds); + useconds -= 1E6 * seconds; + } + usleep((UV)useconds); } else PerlProc_pause(); gettimeofday(&Tb, NULL); - RETVAL = 1000000*(Tb.tv_sec-Ta.tv_sec)+(Tb.tv_usec-Ta.tv_usec); +#if 0 + printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); +#endif + RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec); OUTPUT: RETVAL @@ -394,11 +400,17 @@ sleep(...) struct timeval Ta, Tb; CODE: gettimeofday(&Ta, NULL); - if (items > 0) - usleep((int)(SvNV(ST(0)) * 1000000)); - else + if (items > 0) { + NV seconds = SvNV(ST(0)); + IV useconds = 1E6 * (seconds - (IV)seconds); + sleep(seconds); + usleep(useconds); + } else PerlProc_pause(); gettimeofday(&Tb, NULL); +#if 0 + printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); +#endif RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec); OUTPUT: