+static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
+
+/*
+ * This routine will sleep (sec, microsec). Note, however, that if a
+ * signal occurs, it will return early. It is up to the caller
+ * to recall this routine if he/she REALLY wants to sleep the
+ * requested time.
+ */
+int bmicrosleep(time_t sec, long usec)
+{
+ struct timespec timeout;
+ struct timeval tv;
+ struct timezone tz;
+ int stat;
+
+ timeout.tv_sec = sec;
+ timeout.tv_nsec = usec * 1000;
+
+#ifdef HAVE_NANOSLEEP
+ stat = nanosleep(&timeout, NULL);
+ if (!(stat < 0 && errno == ENOSYS)) {
+ return stat;
+ }
+ /* If we reach here it is because nanosleep is not supported by the OS */
+#endif
+
+ /* Do it the old way */
+ gettimeofday(&tv, &tz);
+ timeout.tv_nsec += tv.tv_usec * 1000;
+ timeout.tv_sec += tv.tv_sec;
+ while (timeout.tv_nsec >= 1000000000) {
+ timeout.tv_nsec -= 1000000000;
+ timeout.tv_sec++;
+ }
+
+ Dmsg2(200, "pthread_cond_timedwait sec=%d usec=%d\n", sec, usec);
+ /* Note, this unlocks mutex during the sleep */
+ P(timer_mutex);
+ stat = pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
+ if (stat != 0) {
+ berrno be;
+ Dmsg2(200, "pthread_cond_timedwait stat=%d ERR=%s\n", stat,
+ be.strerror(stat));
+ }
+ V(timer_mutex);
+ return stat;
+}
+