Blame sysdeps/unix/clock_nanosleep.c

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 6c4009
#include <sysdep-cancel.h>
Packit Service 688471
#include <shlib-compat.h>
Packit Service c82647
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 f5ebdc
  if (clock_id < CLOCK_REALTIME || clock_id > CLOCK_THREAD_CPUTIME_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 f5ebdc
      if (__clock_gettime (clock_id, &now) != 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 f5ebdc
  else if (flags != 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 f5ebdc
  return __nanosleep (req, rem), 0 ? errno : 0;
Packit 6c4009
}
Packit Service 688471
Packit Service 688471
versioned_symbol (libc, __clock_nanosleep, clock_nanosleep, GLIBC_2_17);
Packit Service 688471
/* clock_nanosleep moved to libc in version 2.17;
Packit Service 688471
   old binaries may expect the symbol version it had in librt.  */
Packit Service 688471
#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
Packit Service 688471
strong_alias (__clock_nanosleep, __clock_nanosleep_2);
Packit Service 688471
compat_symbol (libc, __clock_nanosleep_2, clock_nanosleep, GLIBC_2_2);
Packit Service 688471
#endif