* ------------------------------------------------------------------- */
#include "Timestamp.hpp"
-
+#include "util.h"
#include "delay.hpp"
/* -------------------------------------------------------------------
- * A micro-second delay function. This uses gettimeofday (underneith
- * the Timestamp) which has a resolution of upto microseconds. I've
- * found it's good to within about 10 usecs.
- * I used to do calibration, but iperf automatically adjusts itself
- * so that isn't necesary, and it causes some problems if the
- * calibration adjustment is larger than your sleep time.
+ * A micro-second delay function.
* ------------------------------------------------------------------- */
+void delay_loop(unsigned long usec)
+{
+#ifdef SLEEP_VIA_SELECT
+ // This is a hack but works apparently quite well
+ // I found that it sometimes under-estimates the timeout
+ struct timeval tv;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = usec;
-void delay_loop( unsigned long usec ) {
+ select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv);
+#elif defined(SLEEP_OLD_METHOD)
+ /* Tis uses gettimeofday (underneath the Timestamp), which has a
+ * resolution of upto microseconds. I've found it's good to within
+ * about 10 usecs. I used to do calibration, but iperf automatically
+ * adjusts itself so that isn't necesary, and it causes some problems
+ * if the calibration adjustment is larger than your sleep time.
+ * Note: I found this has problems - if the loop gets interrupted,
+ * long sleeps can result. I found up to 50..60msec. -- Gerrit */
Timestamp end;
end.add( usec * 1e-6 );
while ( now.before( end ) ) {
now.setnow();
}
+#else
+ // This is preferred; it is even better than usleep (see manpage).
+ struct timespec ts;
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = usec * 1000L;
+
+ if (nanosleep(&ts, NULL) < 0)
+ WARN_errno(1, "nanosleep");
+#endif
}