]>
Commit | Line | Data |
---|---|---|
c000cdad | 1 | /* High-resolution sleep with the specified clock. |
8be918b7 | 2 | Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
c000cdad UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
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 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include <assert.h> | |
21 | #include <errno.h> | |
8be918b7 | 22 | #include <time.h> |
3b5c1b57 | 23 | #include <hp-timing.h> |
c000cdad UD |
24 | |
25 | ||
3b5c1b57 UD |
26 | #if HP_TIMING_AVAIL |
27 | # define CLOCK_P(clock) \ | |
28 | (clock) != CLOCK_PROCESS_CPUTIME_ID \ | |
29 | && (clock) != CLOCK_THREAD_CPUTIME_ID | |
30 | #else | |
c000cdad UD |
31 | # define CLOCK_P(clock) 0 |
32 | #endif | |
33 | ||
34 | ||
35 | /* This implementation assumes that these is only a `nanosleep' system | |
36 | call. So we have to remap all other activities. */ | |
37 | int | |
38 | clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req, | |
39 | struct timespec *rem) | |
40 | { | |
41 | struct timespec now; | |
42 | ||
43 | if (__builtin_expect (req->tv_nsec, 0) < 0 | |
44 | || __builtin_expect (req->tv_nsec, 0) >= 1000000000) | |
45 | return EINVAL; | |
46 | ||
47 | /* If we got an absolute time, remap it. */ | |
48 | if (flags == TIMER_ABSTIME) | |
49 | { | |
50 | long int nsec; | |
51 | long int sec; | |
52 | ||
53 | /* Make sure we use safe data types. */ | |
54 | assert (sizeof (sec) >= sizeof (now.tv_sec)); | |
55 | ||
56 | /* Get the current time for this clock. */ | |
d8317630 | 57 | if (__builtin_expect (clock_gettime (clock_id, &now), 0) != 0) |
c000cdad UD |
58 | return errno; |
59 | ||
60 | /* Compute the difference. */ | |
61 | nsec = req->tv_nsec - now.tv_nsec; | |
62 | sec = req->tv_sec - now.tv_sec - (nsec < 0); | |
63 | if (sec < 0) | |
64 | /* The time has already elapsed. */ | |
65 | return 0; | |
66 | ||
67 | now.tv_sec = sec; | |
68 | now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0); | |
69 | ||
70 | /* From now on this is our time. */ | |
71 | req = &now; | |
72 | ||
73 | /* Make sure we are not modifying the struct pointed to by REM. */ | |
74 | rem = NULL; | |
75 | } | |
76 | else if (__builtin_expect (flags, 0) != 0) | |
77 | return EINVAL; | |
78 | else if (clock_id != CLOCK_REALTIME) | |
79 | { | |
80 | /* Make sure the clock ID is correct. */ | |
81 | if (__builtin_expect (! CLOCK_P (clock_id), 0)) | |
82 | return EINVAL; | |
83 | } | |
84 | ||
85 | return __builtin_expect (nanosleep (req, rem), 0) ? errno : 0; | |
86 | } |