Subject: | bad implementation of hrt_usleep with TIME_HIRES_NANOSLEEP |
I was browsing the source code for HiRes.xs in Time-HiRes-1.9715, and I
noticed something very broken. One of the implementations for
usleep/hrt_usleep was setting its timespec structure as follows:
ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
ts1.tv_nsec = 0;
Of course, this is backwards; 1us is not 1000s, it's 1000ns. =) I
assume that the intended code was
ts1.tv_sec = 0;
ts1.tv_nsec = usec * 1000; /* Ignoring wraparound. */
although it would be even better *not* to ignore wraparound:
ts1.tv_sec = (usec / IV_1e6);
ts1.tv_nsec = (usec % IV_1e6) * 1000;
(I haven't tested the changes myself, but they ought to work).
BTW, here is the current code with a bit more context:
--------------------------------------------------
#if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
#define HAS_USLEEP
#define usleep hrt_usleep /* could conflict with ncurses for static
build */
void
hrt_usleep(unsigned long usec)
{
struct timespec ts1;
ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
ts1.tv_nsec = 0;
nanosleep(&ts1, NULL);
}
#endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
--------------------------------------------------