X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fmisc.c;h=657bc164f495b6f61e09fcbfcf9e4d6d53e6d99d;hp=14007b7e3d20868f510911e8a4150e93060c864f;hb=4b0c6356359ee52f98cee8fa578179c6c41d4ef1;hpb=d4eb344ff45f9e02bf2de2192bc752f5d09be287 diff --git a/src/misc.c b/src/misc.c index 14007b7e..657bc164 100644 --- a/src/misc.c +++ b/src/misc.c @@ -26,14 +26,31 @@ void microsleep(unsigned long usec) { - struct timeval delay; + if (usec < 5000) + { + /* we want to wait less than 5 ms -- if we assume that we have a + kernel timer resolution of 10 ms, we would wait far to long; + therefore it's better to do a short interval of busy waiting + to get our sleeping time more accurate */ - delay.tv_sec = usec / 1000000; - delay.tv_usec = usec % 1000000; + long base_counter = Counter2(), actual_counter = Counter2(); + long delay = usec/1000; - if (select(0,NULL,NULL,NULL,&delay)!=0) - fprintf(stderr,"%s: in function microsleep: select failed!\n", - progname); + while (actual_counter < base_counter+delay && + actual_counter >= base_counter) + actual_counter = Counter2(); + } + else + { + struct timeval delay; + + delay.tv_sec = usec / 1000000; + delay.tv_usec = usec % 1000000; + + if (select(0,NULL,NULL,NULL,&delay) != 0) + fprintf(stderr,"%s: in function microsleep: select failed!\n", + progname); + } } long mainCounter(int mode)