pthread_getcpuclockid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* pthread_getcpuclockid -- Get POSIX clockid_t for a pthread_t. Linux version
  2. Copyright (C) 2000,2001,2002,2003,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 <pthreadP.h>
  18. #include <sys/time.h>
  19. #include <tls.h>
  20. #include <bits/kernel-features.h>
  21. #include <kernel-posix-cpu-timers.h>
  22. #if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  23. int __libc_missing_posix_cpu_timers attribute_hidden;
  24. #endif
  25. #if !(__ASSUME_POSIX_TIMERS > 0)
  26. int __libc_missing_posix_timers attribute_hidden;
  27. #endif
  28. int
  29. pthread_getcpuclockid (threadid, clockid)
  30. pthread_t threadid;
  31. clockid_t *clockid;
  32. {
  33. struct pthread *pd = (struct pthread *) threadid;
  34. /* Make sure the descriptor is valid. */
  35. if (INVALID_TD_P (pd))
  36. /* Not a valid thread handle. */
  37. return ESRCH;
  38. #ifdef __NR_clock_getres
  39. /* The clockid_t value is a simple computation from the TID.
  40. But we do a clock_getres call to validate it if we aren't
  41. yet sure we have the kernel support. */
  42. const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
  43. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  44. # if !(__ASSUME_POSIX_TIMERS > 0)
  45. if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers)
  46. __libc_missing_posix_cpu_timers = 1;
  47. # endif
  48. if (!__libc_missing_posix_cpu_timers)
  49. {
  50. INTERNAL_SYSCALL_DECL (err);
  51. int r = INTERNAL_SYSCALL (clock_getres, err, 2, tidclock, NULL);
  52. if (!INTERNAL_SYSCALL_ERROR_P (r, err))
  53. # endif
  54. {
  55. *clockid = tidclock;
  56. return 0;
  57. }
  58. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  59. # if !(__ASSUME_POSIX_TIMERS > 0)
  60. if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
  61. {
  62. /* The kernel doesn't support these calls at all. */
  63. __libc_missing_posix_timers = 1;
  64. __libc_missing_posix_cpu_timers = 1;
  65. }
  66. else
  67. # endif
  68. if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
  69. {
  70. /* The kernel doesn't support these clocks at all. */
  71. __libc_missing_posix_cpu_timers = 1;
  72. }
  73. else
  74. return INTERNAL_SYSCALL_ERRNO (r, err);
  75. }
  76. # endif
  77. #endif
  78. #ifdef CLOCK_THREAD_CPUTIME_ID
  79. /* We need to store the thread ID in the CLOCKID variable together
  80. with a number identifying the clock. We reserve the low 3 bits
  81. for the clock ID and the rest for the thread ID. This is
  82. problematic if the thread ID is too large. But 29 bits should be
  83. fine.
  84. If some day more clock IDs are needed the ID part can be
  85. enlarged. The IDs are entirely internal. */
  86. if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
  87. return ERANGE;
  88. /* Store the number. */
  89. *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE);
  90. return 0;
  91. #else
  92. /* We don't have a timer for that. */
  93. return ENOENT;
  94. #endif
  95. }