4 * Implementations of sleep-related functions for platforms that
5 * do not already have them.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General
11 * Public License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 #ifndef HAVE_NANOSLEEP
28 * This is a close approximation of nanosleep() for platforms that
31 int nanosleep(const struct timespec
*req
, struct timespec
*rem
)
33 static pthread_mutex_t timer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
34 static pthread_cond_t timer
= PTHREAD_COND_INITIALIZER
;
35 struct timespec timeout
;
40 /* Copy relative exit time */
43 /* Compute absolute exit time */
44 gettimeofday(&tv
, NULL
);
45 timeout
.tv_nsec
+= tv
.tv_usec
* 1000;
46 timeout
.tv_sec
+= tv
.tv_sec
;
47 while (timeout
.tv_nsec
>= 1000000000) {
48 timeout
.tv_nsec
-= 1000000000;
52 Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout
.tv_sec
);
54 /* Mutex is unlocked during the timedwait */
57 stat
= pthread_cond_timedwait(&timer
, &timer_mutex
, &timeout
);
58 Dmsg1(200, "pthread_cond_timedwait stat=%d\n", stat
);
62 /* Assume no time leftover */
70 #endif /* HAVE_NANOSLEEP */