1 /* pthread_getcpuclockid -- Get POSIX clockid_t for a pthread_t. Linux version
2 Copyright (C) 2000-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
23 #include <kernel-features.h>
24 #include <kernel-posix-cpu-timers.h>
27 #if !(__ASSUME_POSIX_CPU_TIMERS > 0)
28 int __libc_missing_posix_cpu_timers attribute_hidden
;
32 pthread_getcpuclockid (threadid
, clockid
)
36 struct pthread
*pd
= (struct pthread
*) threadid
;
38 /* Make sure the descriptor is valid. */
39 if (INVALID_TD_P (pd
))
40 /* Not a valid thread handle. */
43 #ifdef __NR_clock_getres
44 /* The clockid_t value is a simple computation from the TID.
45 But we do a clock_getres call to validate it if we aren't
46 yet sure we have the kernel support. */
48 const clockid_t tidclock
= MAKE_THREAD_CPUCLOCK (pd
->tid
, CPUCLOCK_SCHED
);
50 # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
51 if (!__libc_missing_posix_cpu_timers
)
53 INTERNAL_SYSCALL_DECL (err
);
54 int r
= INTERNAL_SYSCALL (clock_getres
, err
, 2, tidclock
, NULL
);
55 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
62 # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
63 if (INTERNAL_SYSCALL_ERRNO (r
, err
) == EINVAL
)
65 /* The kernel doesn't support these clocks at all. */
66 __libc_missing_posix_cpu_timers
= 1;
69 return INTERNAL_SYSCALL_ERRNO (r
, err
);
74 #ifdef CLOCK_THREAD_CPUTIME_ID
75 /* We need to store the thread ID in the CLOCKID variable together
76 with a number identifying the clock. We reserve the low 3 bits
77 for the clock ID and the rest for the thread ID. This is
78 problematic if the thread ID is too large. But 29 bits should be
81 If some day more clock IDs are needed the ID part can be
82 enlarged. The IDs are entirely internal. */
83 if (pd
->tid
>= 1 << (8 * sizeof (*clockid
) - CLOCK_IDFIELD_SIZE
))
86 /* Store the number. */
87 *clockid
= CLOCK_THREAD_CPUTIME_ID
| (pd
->tid
<< CLOCK_IDFIELD_SIZE
);
91 /* We don't have a timer for that. */