rnd-19980915
[rocksndiamonds.git] / src / misc.c
index 14007b7e3d20868f510911e8a4150e93060c864f..657bc164f495b6f61e09fcbfcf9e4d6d53e6d99d 100644 (file)
 
 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)