getcpuclockid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* pthread_getcpuclockid -- Get POSIX clockid_t for a pthread_t. Linux version
  2. Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <internals.h>
  20. #include <spinlock.h>
  21. #include <bits/kernel-features.h>
  22. #include <kernel-posix-cpu-timers.h>
  23. #if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  24. int __libc_missing_posix_cpu_timers attribute_hidden;
  25. #endif
  26. #if !(__ASSUME_POSIX_TIMERS > 0)
  27. int __libc_missing_posix_timers attribute_hidden;
  28. #endif
  29. int
  30. pthread_getcpuclockid (pthread_t thread_id, clockid_t *clock_id)
  31. {
  32. #ifdef __NR_clock_getres
  33. pthread_handle handle = thread_handle(thread_id);
  34. int pid;
  35. __pthread_lock (&handle->h_lock, NULL);
  36. if (nonexisting_handle (handle, thread_id))
  37. {
  38. __pthread_unlock (&handle->h_lock);
  39. return ESRCH;
  40. }
  41. pid = handle->h_descr->p_pid;
  42. __pthread_unlock (&handle->h_lock);
  43. /* The clockid_t value is a simple computation from the PID.
  44. But we do a clock_getres call to validate it if we aren't
  45. yet sure we have the kernel support. */
  46. const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
  47. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  48. # if !(__ASSUME_POSIX_TIMERS > 0)
  49. if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers)
  50. __libc_missing_posix_cpu_timers = 1;
  51. # endif
  52. if (!__libc_missing_posix_cpu_timers)
  53. {
  54. INTERNAL_SYSCALL_DECL (err);
  55. int r = INTERNAL_SYSCALL (clock_getres, err, 2, pidclock, NULL);
  56. if (!INTERNAL_SYSCALL_ERROR_P (r, err))
  57. # endif
  58. {
  59. *clock_id = pidclock;
  60. return 0;
  61. }
  62. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  63. # if !(__ASSUME_POSIX_TIMERS > 0)
  64. if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
  65. {
  66. /* The kernel doesn't support these calls at all. */
  67. __libc_missing_posix_timers = 1;
  68. __libc_missing_posix_cpu_timers = 1;
  69. }
  70. else
  71. # endif
  72. if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
  73. {
  74. /* The kernel doesn't support these clocks at all. */
  75. __libc_missing_posix_cpu_timers = 1;
  76. }
  77. else
  78. return INTERNAL_SYSCALL_ERRNO (r, err);
  79. }
  80. # endif
  81. #endif
  82. #ifdef CLOCK_THREAD_CPUTIME_ID
  83. /* We need to store the thread ID in the CLOCKID variable together
  84. with a number identifying the clock. We reserve the low 3 bits
  85. for the clock ID and the rest for the thread ID. This is
  86. problematic if the thread ID is too large. But 29 bits should be
  87. fine.
  88. If some day more clock IDs are needed the ID part can be
  89. enlarged. The IDs are entirely internal. */
  90. if (2 * PTHREAD_THREADS_MAX
  91. >= 1 << (8 * sizeof (*clock_id) - CLOCK_IDFIELD_SIZE))
  92. return ERANGE;
  93. /* Store the number. */
  94. *clock_id = CLOCK_THREAD_CPUTIME_ID | (thread_id << CLOCK_IDFIELD_SIZE);
  95. return 0;
  96. #else
  97. /* We don't have a timer for that. */
  98. return ENOENT;
  99. #endif
  100. }