pthread_getcpuclockid.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #include <errno.h>
  16. #include <pthreadP.h>
  17. #include <sys/time.h>
  18. #include <tls.h>
  19. int
  20. pthread_getcpuclockid (
  21. pthread_t threadid,
  22. clockid_t *clockid)
  23. {
  24. struct pthread *pd = (struct pthread *) threadid;
  25. /* Make sure the descriptor is valid. */
  26. if (INVALID_TD_P (pd))
  27. /* Not a valid thread handle. */
  28. return ESRCH;
  29. #ifdef CLOCK_THREAD_CPUTIME_ID
  30. /* We need to store the thread ID in the CLOCKID variable together
  31. with a number identifying the clock. We reserve the low 3 bits
  32. for the clock ID and the rest for the thread ID. This is
  33. problematic if the thread ID is too large. But 29 bits should be
  34. fine.
  35. If some day more clock IDs are needed the ID part can be
  36. enlarged. The IDs are entirely internal. */
  37. if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
  38. return ERANGE;
  39. /* Store the number. */
  40. *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE);
  41. return 0;
  42. #else
  43. /* We don't have a timer for that. */
  44. return ENOENT;
  45. #endif
  46. }