clock_nanosleep.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (C) 2003-2018 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 2.1 of the License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <http://www.gnu.org/licenses/>. */
  13. #include <time.h>
  14. #include <errno.h>
  15. #include <cancel.h>
  16. #include <sys/syscall.h>
  17. #include "kernel-posix-cpu-timers.h"
  18. #if defined(__UCLIBC_USE_TIME64__)
  19. #include "internal/time64_helpers.h"
  20. #endif
  21. /* We can simply use the syscall. The CPU clocks are not supported
  22. with this function. */
  23. int
  24. clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
  25. struct timespec *rem)
  26. {
  27. INTERNAL_SYSCALL_DECL (err);
  28. int r;
  29. if (clock_id == CLOCK_THREAD_CPUTIME_ID)
  30. return EINVAL;
  31. if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
  32. clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
  33. if (SINGLE_THREAD_P) {
  34. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_nanosleep_time64)
  35. struct __ts64_struct __req, __rem;
  36. __req.tv_sec = req->tv_sec;
  37. __req.tv_nsec = req->tv_nsec;
  38. r = INTERNAL_SYSCALL (clock_nanosleep_time64, err, 4, clock_id, flags, &__req, &__rem);
  39. if (rem) {
  40. rem->tv_sec = (time_t) __rem.tv_sec;
  41. rem->tv_nsec = __rem.tv_nsec;
  42. }
  43. #else
  44. r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, rem);
  45. #endif
  46. }
  47. else
  48. {
  49. #ifdef __NEW_THREADS
  50. int oldstate = LIBC_CANCEL_ASYNC ();
  51. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_nanosleep_time64)
  52. struct __ts64_struct __req, __rem;
  53. __req.tv_sec = req->tv_sec;
  54. __req.tv_nsec = req->tv_nsec;
  55. r = INTERNAL_SYSCALL (clock_nanosleep_time64, err, 4, clock_id, flags, &__req, &__rem);
  56. if (rem) {
  57. rem->tv_sec = (time_t) __rem.tv_sec;
  58. rem->tv_nsec = __rem.tv_nsec;
  59. }
  60. #else
  61. r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req,
  62. rem);
  63. #endif
  64. LIBC_CANCEL_RESET (oldstate);
  65. #endif
  66. }
  67. return (INTERNAL_SYSCALL_ERROR_P (r, err)
  68. ? INTERNAL_SYSCALL_ERRNO (r, err) : 0);
  69. }