|
| 1 | +/* High-resolution sleep with the specified clock. |
| 2 | + Copyright (C) 2000-2012 Free Software Foundation, Inc. |
| 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 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. |
| 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 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with the GNU C Library; if not, see |
| 17 | + <http://www.gnu.org/licenses/>. */ |
| 18 | + |
| 19 | +#include <assert.h> |
| 20 | +#include <errno.h> |
| 21 | +#include <time.h> |
| 22 | +#include <sys/time.h> |
| 23 | + |
| 24 | +__typeof__(clock_nanosleep) __clock_nanosleep; |
| 25 | + |
| 26 | +/* This implementation assumes that these is only a `nanosleep' system |
| 27 | + call. So we have to remap all other activities. */ |
| 28 | +int |
| 29 | +__clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req, struct timespec *rem) |
| 30 | +{ |
| 31 | + struct timespec now; |
| 32 | + |
| 33 | + if (__builtin_expect (req->tv_nsec, 0) < 0 || __builtin_expect (req->tv_nsec, 0) >= 1000000000){ |
| 34 | + return EINVAL; |
| 35 | + } |
| 36 | + |
| 37 | + /* If we got an absolute time, remap it. */ |
| 38 | + if (flags == TIMER_ABSTIME) |
| 39 | + { |
| 40 | + long int nsec; |
| 41 | + long int sec; |
| 42 | + |
| 43 | + /* Make sure we use safe data types. */ |
| 44 | + assert (sizeof (sec) >= sizeof (now.tv_sec)); |
| 45 | + |
| 46 | + /* Get the current time for this clock. */ |
| 47 | + if (__builtin_expect (clock_gettime (clock_id, &now), 0) != 0){ |
| 48 | + return errno; |
| 49 | + } |
| 50 | + |
| 51 | + /* Compute the difference. */ |
| 52 | + nsec = req->tv_nsec - now.tv_nsec; |
| 53 | + sec = req->tv_sec - now.tv_sec - (nsec < 0); |
| 54 | + |
| 55 | + if (sec < 0){ |
| 56 | + /* The time has already elapsed. */ |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + now.tv_sec = sec; |
| 61 | + now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0); |
| 62 | + |
| 63 | + /* From now on this is our time. */ |
| 64 | + req = &now; |
| 65 | + |
| 66 | + /* Make sure we are not modifying the struct pointed to by REM. */ |
| 67 | + rem = NULL; |
| 68 | + } |
| 69 | + else if (__builtin_expect (flags, 0) != 0){ |
| 70 | + return EINVAL; |
| 71 | + } |
| 72 | + else if ((clock_id != CLOCK_REALTIME) && clock_id != CLOCK_MONOTONIC){ |
| 73 | + /* Not supported. */ |
| 74 | + return ENOTSUP; |
| 75 | + } |
| 76 | + |
| 77 | + return __builtin_expect (nanosleep (req, rem), 0) ? errno : 0; |
| 78 | +} |
| 79 | + |
| 80 | +weak_alias (__clock_nanosleep, clock_nanosleep) |
0 commit comments