clock_nanosleep.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <sysdep-cancel.h>
  17. #include <bits/kernel-features.h>
  18. #include "kernel-posix-cpu-timers.h"
  19. #ifdef __ASSUME_POSIX_TIMERS
  20. /* We can simply use the syscall. The CPU clocks are not supported
  21. with this function. */
  22. int
  23. clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
  24. struct timespec *rem)
  25. {
  26. INTERNAL_SYSCALL_DECL (err);
  27. int r;
  28. if (clock_id == CLOCK_THREAD_CPUTIME_ID)
  29. return EINVAL;
  30. if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
  31. clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
  32. if (SINGLE_THREAD_P)
  33. r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, rem);
  34. else
  35. {
  36. int oldstate = LIBC_CANCEL_ASYNC ();
  37. r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req,
  38. rem);
  39. LIBC_CANCEL_RESET (oldstate);
  40. }
  41. return (INTERNAL_SYSCALL_ERROR_P (r, err)
  42. ? INTERNAL_SYSCALL_ERRNO (r, err) : 0);
  43. }
  44. #else
  45. # ifdef __NR_clock_nanosleep
  46. /* Is the syscall known to exist? */
  47. extern int __libc_missing_posix_timers attribute_hidden;
  48. /* The REALTIME and MONOTONIC clock might be available. Try the
  49. syscall first. */
  50. # define SYSDEP_NANOSLEEP \
  51. if (!__libc_missing_posix_timers) \
  52. { \
  53. clockid_t syscall_clockid; \
  54. INTERNAL_SYSCALL_DECL (err); \
  55. \
  56. if (clock_id == CLOCK_THREAD_CPUTIME_ID) \
  57. return EINVAL; \
  58. if (clock_id == CLOCK_PROCESS_CPUTIME_ID) \
  59. syscall_clockid = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED); \
  60. else \
  61. syscall_clockid = clock_id; \
  62. \
  63. int oldstate = LIBC_CANCEL_ASYNC (); \
  64. \
  65. int r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, \
  66. syscall_clockid, flags, req, rem); \
  67. \
  68. LIBC_CANCEL_RESET (oldstate); \
  69. \
  70. if (!INTERNAL_SYSCALL_ERROR_P (r, err)) \
  71. return 0; \
  72. \
  73. if (INTERNAL_SYSCALL_ERRNO (r, err) != ENOSYS) \
  74. return INTERNAL_SYSCALL_ERRNO (r, err); \
  75. \
  76. __libc_missing_posix_timers = 1; \
  77. }
  78. # endif
  79. # include <sysdeps/unix/clock_nanosleep.c>
  80. #endif