2000-06-26 18:12:12 +00:00
|
|
|
/* High-resolution sleep with the specified clock.
|
2019-01-01 00:11:28 +00:00
|
|
|
Copyright (C) 2000-2019 Free Software Foundation, Inc.
|
2000-06-26 18:12:12 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-06-26 18:12:12 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
2000-06-26 18:12:12 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2000-06-26 18:12:12 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2001-01-28 00:00:08 +00:00
|
|
|
#include <time.h>
|
Update.
2003-06-15 Ulrich Drepper <drepper@redhat.com>
Fix cancellation point handling wrt exception based cleanup.
* io/Makefile: Compile fcntl.c, poll.c, and lockf.c with exceptions.
* misc/Makefile: Compile pselect.c, readv.c, writev.c, and usleep.c
with exceptions.
* posix/Makefile: Compile pread.c, pread64.c, pwrite.c, pwrite64.c,
sleep.c, wait.c, waitid.c, and waitpid.c with exceptions.
* rt/Makefile: Compile aio_suspend.c and clock_nanosleep.c with
exceptions.
* signal/Makefile: Compile sigpause.c, sigsuspend.c, sigtimedwait.c,
sigwait.c, and sigwaitinfo.c with exceptions.
* stdlib/Makefile: Compile system.c with exceptions.
* sysvipc/Makefile: Compile msgrcv.c and msgsnd.c with exceptions.
* termios/Makefile: Compile tcdrain.c with exceptions.
* sysdeps/generic/lockf.c: Add comment explaining the cancellation
situation.
* sysdeps/generic/pselect.c: Likewise.
* sysdeps/posix/sigpause.c: Likewise.
* sysdeps/posix/system.c: Likewise.
* sysdeps/posix/waitid.c: Likewise.
* sysdeps/unix/sysv/linux/sleep.c: Likewise.
* sysdeps/unix/sysv/linux/usleep.c: Likewise.
* sysdeps/unix/sysv/linux/i386/sysdep.h: Major rewrite of
INTERNAL_SYSCALL to not use push inside asm statement so that
unwind info is correct around the syscall.
* sysdeps/unix/clock_nanosleep.c: Add cancellation support.
* sysdeps/unix/sysv/linux/clock_nanosleep.c: Likewise.
2003-06-15 21:22:26 +00:00
|
|
|
#include <sysdep-cancel.h>
|
Finish move of clock_* functions to libc. [BZ #24959]
In glibc 2.17, the functions clock_getcpuclockid, clock_getres,
clock_gettime, clock_nanosleep, and clock_settime were moved from
librt.so to libc.so, leaving compatibility stubs behind. Now that the
dynamic linker no longer insists on finding versioned symbols in the
same library that originally defined them, we do not need the stubs
anymore, and this means we don't need GLIBC_PRIVATE __-prefix aliases
for most of the functions anymore either. (clock_gettime still needs
one.) For ports added before 2.17, libc.so needs to provide two
symbol versions for each, the default at GLIBC_2.17 plus a compat
version matching what librt had.
While I'm at it, move the clock_*.c files and their tests from rt/ to
time/.
2019-09-04 06:18:57 +00:00
|
|
|
#include <shlib-compat.h>
|
2000-06-26 18:12:12 +00:00
|
|
|
|
|
|
|
/* This implementation assumes that these is only a `nanosleep' system
|
|
|
|
call. So we have to remap all other activities. */
|
|
|
|
int
|
2013-06-11 05:41:11 +00:00
|
|
|
__clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
|
|
|
|
struct timespec *rem)
|
2000-06-26 18:12:12 +00:00
|
|
|
{
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
if (__builtin_expect (req->tv_nsec, 0) < 0
|
|
|
|
|| __builtin_expect (req->tv_nsec, 0) >= 1000000000)
|
|
|
|
return EINVAL;
|
|
|
|
|
2004-12-06 22:29:01 +00:00
|
|
|
if (clock_id == CLOCK_THREAD_CPUTIME_ID)
|
|
|
|
return EINVAL; /* POSIX specifies EINVAL for this case. */
|
|
|
|
|
2019-01-16 16:22:29 +00:00
|
|
|
if (clock_id < CLOCK_REALTIME || clock_id > CLOCK_THREAD_CPUTIME_ID)
|
2003-03-03 04:57:09 +00:00
|
|
|
return EINVAL;
|
|
|
|
|
2000-06-26 18:12:12 +00:00
|
|
|
/* If we got an absolute time, remap it. */
|
|
|
|
if (flags == TIMER_ABSTIME)
|
|
|
|
{
|
|
|
|
long int nsec;
|
|
|
|
long int sec;
|
|
|
|
|
|
|
|
/* Make sure we use safe data types. */
|
|
|
|
assert (sizeof (sec) >= sizeof (now.tv_sec));
|
|
|
|
|
|
|
|
/* Get the current time for this clock. */
|
2019-01-16 16:22:29 +00:00
|
|
|
if (__clock_gettime (clock_id, &now) != 0)
|
2000-06-26 18:12:12 +00:00
|
|
|
return errno;
|
|
|
|
|
|
|
|
/* Compute the difference. */
|
|
|
|
nsec = req->tv_nsec - now.tv_nsec;
|
|
|
|
sec = req->tv_sec - now.tv_sec - (nsec < 0);
|
|
|
|
if (sec < 0)
|
|
|
|
/* The time has already elapsed. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
now.tv_sec = sec;
|
|
|
|
now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
|
|
|
|
|
|
|
|
/* From now on this is our time. */
|
|
|
|
req = &now;
|
|
|
|
|
|
|
|
/* Make sure we are not modifying the struct pointed to by REM. */
|
|
|
|
rem = NULL;
|
|
|
|
}
|
2019-01-16 16:22:29 +00:00
|
|
|
else if (flags != 0)
|
2000-06-26 18:12:12 +00:00
|
|
|
return EINVAL;
|
|
|
|
else if (clock_id != CLOCK_REALTIME)
|
2003-03-03 04:57:09 +00:00
|
|
|
/* Not supported. */
|
|
|
|
return ENOTSUP;
|
2000-06-26 18:12:12 +00:00
|
|
|
|
2019-01-16 16:22:29 +00:00
|
|
|
return __nanosleep (req, rem), 0 ? errno : 0;
|
2000-06-26 18:12:12 +00:00
|
|
|
}
|
Finish move of clock_* functions to libc. [BZ #24959]
In glibc 2.17, the functions clock_getcpuclockid, clock_getres,
clock_gettime, clock_nanosleep, and clock_settime were moved from
librt.so to libc.so, leaving compatibility stubs behind. Now that the
dynamic linker no longer insists on finding versioned symbols in the
same library that originally defined them, we do not need the stubs
anymore, and this means we don't need GLIBC_PRIVATE __-prefix aliases
for most of the functions anymore either. (clock_gettime still needs
one.) For ports added before 2.17, libc.so needs to provide two
symbol versions for each, the default at GLIBC_2.17 plus a compat
version matching what librt had.
While I'm at it, move the clock_*.c files and their tests from rt/ to
time/.
2019-09-04 06:18:57 +00:00
|
|
|
|
|
|
|
versioned_symbol (libc, __clock_nanosleep, clock_nanosleep, GLIBC_2_17);
|
|
|
|
/* clock_nanosleep moved to libc in version 2.17;
|
|
|
|
old binaries may expect the symbol version it had in librt. */
|
|
|
|
#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
|
|
|
|
strong_alias (__clock_nanosleep, __clock_nanosleep_2);
|
|
|
|
compat_symbol (libc, __clock_nanosleep_2, clock_nanosleep, GLIBC_2_2);
|
|
|
|
#endif
|