Back to index

glibc  2.9
pthread_getcpuclockid.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public License as
00006    published by the Free Software Foundation; either version 2.1 of the
00007    License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.  */
00018 
00019 #include <errno.h>
00020 #include <pthreadP.h>
00021 #include <sys/time.h>
00022 #include <tls.h>
00023 
00024 
00025 int
00026 pthread_getcpuclockid (threadid, clockid)
00027      pthread_t threadid;
00028      clockid_t *clockid;
00029 {
00030   struct pthread *pd = (struct pthread *) threadid;
00031 
00032   /* Make sure the descriptor is valid.  */
00033   if (INVALID_TD_P (pd))
00034     /* Not a valid thread handle.  */
00035     return ESRCH;
00036 
00037 #ifdef CLOCK_THREAD_CPUTIME_ID
00038   /* We need to store the thread ID in the CLOCKID variable together
00039      with a number identifying the clock.  We reserve the low 3 bits
00040      for the clock ID and the rest for the thread ID.  This is
00041      problematic if the thread ID is too large.  But 29 bits should be
00042      fine.
00043 
00044      If some day more clock IDs are needed the ID part can be
00045      enlarged.  The IDs are entirely internal.  */
00046   if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
00047     return ERANGE;
00048 
00049   /* Store the number.  */
00050   *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE);
00051 
00052   return 0;
00053 #else
00054   /* We don't have a timer for that.  */
00055   return ENOENT;
00056 #endif
00057 }