=item usleep ( $useconds )
-Issues a usleep for the number of microseconds specified. See also
-Time::HiRes::sleep() below.
+Issues a usleep for the number of microseconds specified. Returns the
+number of microseconds actually slept. The number of microseconds
+B<must> be between 0 and 1_000_0000 (inclusive): you B<cannot> sleep
+a minute by usleep(60_000_000). See also Time::HiRes::sleep() below.
=item ualarm ( $useconds [, $interval_useconds ] )
=item sleep ( $floating_seconds )
-Converts $floating_seconds to microseconds and issues a usleep for the
-result. This function can be imported, resulting in a nice drop-in
-replacement for the C<sleep> provided with perl, see the EXAMPLES below.
+Converts $floating_seconds to microseconds and issues a usleep for the
+result. Returns the number of seconds actually slept (a floating
+point value). This function can be imported, resulting in a nice
+drop-in replacement for the C<sleep> provided with perl, see the
+EXAMPLES below.
=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
@INC = '../lib';
}
-BEGIN { $| = 1; print "1..19\n"; }
+BEGIN { $| = 1; print "1..21\n"; }
END {print "not ok 1\n" unless $loaded;}
$SIG{VTALRM} = 'DEFAULT';
}
+print abs(sleep(1) - 1) < 0.001 ? "ok 20\n" : "not ok 20\n";
+
+print abs(usleep(1000000) / 1000000 - 1) < 0.001 ? "ok 21\n" : "not ok 21\n";
+
char * name
int arg
-#ifdef HAS_USLEEP
+#if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
-void
+int
usleep(useconds)
int 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);
+ } else
+ PerlProc_pause();
+ gettimeofday(&Tb, NULL);
+ RETVAL = 1000000*(Tb.tv_sec-Ta.tv_sec)+(Tb.tv_usec-Ta.tv_usec);
-void
+ OUTPUT:
+ RETVAL
+
+NV
sleep(...)
- PROTOTYPE: ;$
+ PREINIT:
+ struct timeval Ta, Tb;
CODE:
+ gettimeofday(&Ta, NULL);
if (items > 0)
usleep((int)(SvNV(ST(0)) * 1000000));
else
PerlProc_pause();
+ gettimeofday(&Tb, NULL);
+ RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
+
+ OUTPUT:
+ RETVAL
#endif