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