Updated to fedora-glibc-20041124T0741
[glibc.git] / sysdeps / unix / clock_nanosleep.c
blob6b170fd702f0f2cf7cbcf4c059ec5cc1d5cab6c8
1 /* High-resolution sleep with the specified clock.
2 Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <time.h>
23 #include <hp-timing.h>
24 #include <sysdep-cancel.h>
26 #if HP_TIMING_AVAIL
27 # define CPUCLOCK_P(clock) \
28 ((clock) == CLOCK_PROCESS_CPUTIME_ID \
29 || ((clock) & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID)
30 #else
31 # define CPUCLOCK_P(clock) 0
32 #endif
34 #ifndef INVALID_CLOCK_P
35 # define INVALID_CLOCK_P(cl) \
36 ((cl) < CLOCK_REALTIME || (cl) > CLOCK_THREAD_CPUTIME_ID)
37 #endif
40 /* This implementation assumes that these is only a `nanosleep' system
41 call. So we have to remap all other activities. */
42 int
43 clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
44 struct timespec *rem)
46 struct timespec now;
48 if (__builtin_expect (req->tv_nsec, 0) < 0
49 || __builtin_expect (req->tv_nsec, 0) >= 1000000000)
50 return EINVAL;
52 if (CPUCLOCK_P (clock_id))
53 return ENOTSUP;
55 if (INVALID_CLOCK_P (clock_id))
56 return EINVAL;
58 #ifdef SYSDEP_NANOSLEEP
59 SYSDEP_NANOSLEEP;
60 #endif
62 /* If we got an absolute time, remap it. */
63 if (flags == TIMER_ABSTIME)
65 long int nsec;
66 long int sec;
68 /* Make sure we use safe data types. */
69 assert (sizeof (sec) >= sizeof (now.tv_sec));
71 /* Get the current time for this clock. */
72 if (__builtin_expect (clock_gettime (clock_id, &now), 0) != 0)
73 return errno;
75 /* Compute the difference. */
76 nsec = req->tv_nsec - now.tv_nsec;
77 sec = req->tv_sec - now.tv_sec - (nsec < 0);
78 if (sec < 0)
79 /* The time has already elapsed. */
80 return 0;
82 now.tv_sec = sec;
83 now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
85 /* From now on this is our time. */
86 req = &now;
88 /* Make sure we are not modifying the struct pointed to by REM. */
89 rem = NULL;
91 else if (__builtin_expect (flags, 0) != 0)
92 return EINVAL;
93 else if (clock_id != CLOCK_REALTIME)
94 /* Not supported. */
95 return ENOTSUP;
97 return __builtin_expect (nanosleep (req, rem), 0) ? errno : 0;