|
Packit |
6c4009 |
/* High-resolution sleep with the specified clock.
|
|
Packit |
6c4009 |
Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <assert.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <time.h>
|
|
Packit Service |
6de65a |
#include <hp-timing.h>
|
|
Packit |
6c4009 |
#include <sysdep-cancel.h>
|
|
Packit Service |
6de65a |
|
|
Packit Service |
6de65a |
#if HP_TIMING_AVAIL
|
|
Packit Service |
6de65a |
# define CPUCLOCK_P(clock) \
|
|
Packit Service |
6de65a |
((clock) == CLOCK_PROCESS_CPUTIME_ID \
|
|
Packit Service |
6de65a |
|| ((clock) & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID)
|
|
Packit Service |
6de65a |
#else
|
|
Packit Service |
6de65a |
# define CPUCLOCK_P(clock) 0
|
|
Packit Service |
6de65a |
#endif
|
|
Packit Service |
6de65a |
|
|
Packit Service |
6de65a |
#ifndef INVALID_CLOCK_P
|
|
Packit Service |
6de65a |
# define INVALID_CLOCK_P(cl) \
|
|
Packit Service |
6de65a |
((cl) < CLOCK_REALTIME || (cl) > CLOCK_THREAD_CPUTIME_ID)
|
|
Packit Service |
6de65a |
#endif
|
|
Packit Service |
6de65a |
|
|
Packit Service |
93dc7a |
|
|
Packit |
6c4009 |
/* This implementation assumes that these is only a `nanosleep' system
|
|
Packit |
6c4009 |
call. So we have to remap all other activities. */
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
|
|
Packit |
6c4009 |
struct timespec *rem)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct timespec now;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__builtin_expect (req->tv_nsec, 0) < 0
|
|
Packit |
6c4009 |
|| __builtin_expect (req->tv_nsec, 0) >= 1000000000)
|
|
Packit |
6c4009 |
return EINVAL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (clock_id == CLOCK_THREAD_CPUTIME_ID)
|
|
Packit |
6c4009 |
return EINVAL; /* POSIX specifies EINVAL for this case. */
|
|
Packit |
6c4009 |
|
|
Packit Service |
6de65a |
#ifdef SYSDEP_NANOSLEEP
|
|
Packit Service |
6de65a |
SYSDEP_NANOSLEEP;
|
|
Packit Service |
6de65a |
#endif
|
|
Packit Service |
6de65a |
|
|
Packit Service |
6de65a |
if (CPUCLOCK_P (clock_id))
|
|
Packit Service |
6de65a |
return ENOTSUP;
|
|
Packit Service |
6de65a |
|
|
Packit Service |
6de65a |
if (INVALID_CLOCK_P (clock_id))
|
|
Packit |
6c4009 |
return EINVAL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If we got an absolute time, remap it. */
|
|
Packit |
6c4009 |
if (flags == TIMER_ABSTIME)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
long int nsec;
|
|
Packit |
6c4009 |
long int sec;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make sure we use safe data types. */
|
|
Packit |
6c4009 |
assert (sizeof (sec) >= sizeof (now.tv_sec));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the current time for this clock. */
|
|
Packit Service |
6de65a |
if (__builtin_expect (__clock_gettime (clock_id, &now), 0) != 0)
|
|
Packit |
6c4009 |
return errno;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Compute the difference. */
|
|
Packit |
6c4009 |
nsec = req->tv_nsec - now.tv_nsec;
|
|
Packit |
6c4009 |
sec = req->tv_sec - now.tv_sec - (nsec < 0);
|
|
Packit |
6c4009 |
if (sec < 0)
|
|
Packit |
6c4009 |
/* The time has already elapsed. */
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
now.tv_sec = sec;
|
|
Packit |
6c4009 |
now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* From now on this is our time. */
|
|
Packit |
6c4009 |
req = &now;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make sure we are not modifying the struct pointed to by REM. */
|
|
Packit |
6c4009 |
rem = NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
6de65a |
else if (__builtin_expect (flags, 0) != 0)
|
|
Packit |
6c4009 |
return EINVAL;
|
|
Packit |
6c4009 |
else if (clock_id != CLOCK_REALTIME)
|
|
Packit |
6c4009 |
/* Not supported. */
|
|
Packit |
6c4009 |
return ENOTSUP;
|
|
Packit |
6c4009 |
|
|
Packit Service |
6de65a |
return __builtin_expect (__nanosleep (req, rem), 0) ? errno : 0;
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
6de65a |
weak_alias (__clock_nanosleep, clock_nanosleep)
|