getcpuclockid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <internals.h>
  21. #include <spinlock.h>
  22. #include <kernel-features.h>
  23. #include <kernel-posix-cpu-timers.h>
  24. #if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  25. int __libc_missing_posix_cpu_timers attribute_hidden;
  26. #endif
  27. #if !(__ASSUME_POSIX_TIMERS > 0)
  28. int __libc_missing_posix_timers attribute_hidden;
  29. #endif
  30. int
  31. pthread_getcpuclockid (pthread_t thread_id, clockid_t *clock_id)
  32. {
  33. #ifdef __NR_clock_getres
  34. pthread_handle handle = thread_handle(thread_id);
  35. int pid;
  36. __pthread_lock (&handle->h_lock, NULL);
  37. if (nonexisting_handle (handle, thread_id))
  38. {
  39. __pthread_unlock (&handle->h_lock);
  40. return ESRCH;
  41. }
  42. pid = handle->h_descr->p_pid;
  43. __pthread_unlock (&handle->h_lock);
  44. /* The clockid_t value is a simple computation from the PID.
  45. But we do a clock_getres call to validate it if we aren't
  46. yet sure we have the kernel support. */
  47. const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
  48. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  49. # if !(__ASSUME_POSIX_TIMERS > 0)
  50. if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers)
  51. __libc_missing_posix_cpu_timers = 1;
  52. # endif
  53. if (!__libc_missing_posix_cpu_timers)
  54. {
  55. INTERNAL_SYSCALL_DECL (err);
  56. int r = INTERNAL_SYSCALL (clock_getres, err, 2, pidclock, NULL);
  57. if (!INTERNAL_SYSCALL_ERROR_P (r, err))
  58. # endif
  59. {
  60. *clock_id = pidclock;
  61. return 0;
  62. }
  63. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  64. # if !(__ASSUME_POSIX_TIMERS > 0)
  65. if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
  66. {
  67. /* The kernel doesn't support these calls at all. */
  68. __libc_missing_posix_timers = 1;
  69. __libc_missing_posix_cpu_timers = 1;
  70. }
  71. else
  72. # endif
  73. if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
  74. {
  75. /* The kernel doesn't support these clocks at all. */
  76. __libc_missing_posix_cpu_timers = 1;
  77. }
  78. else
  79. return INTERNAL_SYSCALL_ERRNO (r, err);
  80. }
  81. # endif
  82. #endif
  83. #ifdef CLOCK_THREAD_CPUTIME_ID
  84. /* We need to store the thread ID in the CLOCKID variable together
  85. with a number identifying the clock. We reserve the low 3 bits
  86. for the clock ID and the rest for the thread ID. This is
  87. problematic if the thread ID is too large. But 29 bits should be
  88. fine.
  89. If some day more clock IDs are needed the ID part can be
  90. enlarged. The IDs are entirely internal. */
  91. if (2 * PTHREAD_THREADS_MAX
  92. >= 1 << (8 * sizeof (*clock_id) - CLOCK_IDFIELD_SIZE))
  93. return ERANGE;
  94. /* Store the number. */
  95. *clock_id = CLOCK_THREAD_CPUTIME_ID | (thread_id << CLOCK_IDFIELD_SIZE);
  96. return 0;
  97. #else
  98. /* We don't have a timer for that. */
  99. return ENOENT;
  100. #endif
  101. }