Updated to fedora-glibc-20090204T2135
[glibc/history.git] / nptl / sysdeps / unix / sysv / linux / pthread_getcpuclockid.c
blob82c2446d55d422e6c6033cf5e0a9d9cd886fadae
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.
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <pthreadP.h>
22 #include <sys/time.h>
23 #include <tls.h>
24 #include <kernel-features.h>
25 #include <kernel-posix-cpu-timers.h>
28 #if !(__ASSUME_POSIX_CPU_TIMERS > 0)
29 int __libc_missing_posix_cpu_timers attribute_hidden;
30 #endif
31 #if !(__ASSUME_POSIX_TIMERS > 0)
32 int __libc_missing_posix_timers attribute_hidden;
33 #endif
35 int
36 pthread_getcpuclockid (threadid, clockid)
37 pthread_t threadid;
38 clockid_t *clockid;
40 struct pthread *pd = (struct pthread *) threadid;
42 /* Make sure the descriptor is valid. */
43 if (INVALID_TD_P (pd))
44 /* Not a valid thread handle. */
45 return ESRCH;
47 #ifdef __NR_clock_getres
48 /* The clockid_t value is a simple computation from the TID.
49 But we do a clock_getres call to validate it if we aren't
50 yet sure we have the kernel support. */
52 const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
54 # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
55 # if !(__ASSUME_POSIX_TIMERS > 0)
56 if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers)
57 __libc_missing_posix_cpu_timers = 1;
58 # endif
59 if (!__libc_missing_posix_cpu_timers)
61 INTERNAL_SYSCALL_DECL (err);
62 int r = INTERNAL_SYSCALL (clock_getres, err, 2, tidclock, NULL);
63 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
64 # endif
66 *clockid = tidclock;
67 return 0;
70 # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
71 # if !(__ASSUME_POSIX_TIMERS > 0)
72 if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
74 /* The kernel doesn't support these calls at all. */
75 __libc_missing_posix_timers = 1;
76 __libc_missing_posix_cpu_timers = 1;
78 else
79 # endif
80 if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
82 /* The kernel doesn't support these clocks at all. */
83 __libc_missing_posix_cpu_timers = 1;
85 else
86 return INTERNAL_SYSCALL_ERRNO (r, err);
88 # endif
89 #endif
91 #ifdef CLOCK_THREAD_CPUTIME_ID
92 /* We need to store the thread ID in the CLOCKID variable together
93 with a number identifying the clock. We reserve the low 3 bits
94 for the clock ID and the rest for the thread ID. This is
95 problematic if the thread ID is too large. But 29 bits should be
96 fine.
98 If some day more clock IDs are needed the ID part can be
99 enlarged. The IDs are entirely internal. */
100 if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
101 return ERANGE;
103 /* Store the number. */
104 *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE);
106 return 0;
107 #else
108 /* We don't have a timer for that. */
109 return ENOENT;
110 #endif