1 /* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
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 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If
16    not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <pthreadP.h>
20 #include <sys/time.h>
21 #include <tls.h>
22 
23 #define CPUCLOCK_PERTHREAD_MASK	4
24 #define CPUCLOCK_SCHED		2
25 
26 int
pthread_getcpuclockid(pthread_t threadid,clockid_t * clockid)27 pthread_getcpuclockid (
28      pthread_t threadid,
29      clockid_t *clockid)
30 {
31   struct pthread *pd = (struct pthread *) threadid;
32 
33   /* Make sure the descriptor is valid.  */
34   if (INVALID_TD_P (pd))
35     /* Not a valid thread handle.  */
36     return ESRCH;
37 
38 #ifdef CLOCK_THREAD_CPUTIME_ID
39   /* We need to store the thread ID in the CLOCKID variable together
40      with a number identifying the clock.  We reserve the low 3 bits
41      for the clock ID and the rest for the thread ID.  This is
42      problematic if the thread ID is too large.  But 29 bits should be
43      fine.
44 
45      If some day more clock IDs are needed the ID part can be
46      enlarged.  The IDs are entirely internal.  */
47   if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
48     return ERANGE;
49 
50   /* Store the number.  */
51   *clockid = ((~(clockid_t) (pd->tid)) << CLOCK_IDFIELD_SIZE)
52     | CPUCLOCK_SCHED | CPUCLOCK_PERTHREAD_MASK;
53 
54   return 0;
55 #else
56   /* We don't have a timer for that.  */
57   return ENOENT;
58 #endif
59 }
60