pthread_getcpuclockid.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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
  13. not, see <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <pthreadP.h>
  16. #include <sys/time.h>
  17. #include <tls.h>
  18. #define CPUCLOCK_PERTHREAD_MASK 4
  19. #define CPUCLOCK_SCHED 2
  20. int
  21. pthread_getcpuclockid (
  22. pthread_t threadid,
  23. clockid_t *clockid)
  24. {
  25. struct pthread *pd = (struct pthread *) threadid;
  26. /* Make sure the descriptor is valid. */
  27. if (INVALID_TD_P (pd))
  28. /* Not a valid thread handle. */
  29. return ESRCH;
  30. #ifdef CLOCK_THREAD_CPUTIME_ID
  31. /* We need to store the thread ID in the CLOCKID variable together
  32. with a number identifying the clock. We reserve the low 3 bits
  33. for the clock ID and the rest for the thread ID. This is
  34. problematic if the thread ID is too large. But 29 bits should be
  35. fine.
  36. If some day more clock IDs are needed the ID part can be
  37. enlarged. The IDs are entirely internal. */
  38. if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
  39. return ERANGE;
  40. /* Store the number. */
  41. *clockid = ((~(clockid_t) (pd->tid)) << CLOCK_IDFIELD_SIZE)
  42. | CPUCLOCK_SCHED | CPUCLOCK_PERTHREAD_MASK;
  43. return 0;
  44. #else
  45. /* We don't have a timer for that. */
  46. return ENOENT;
  47. #endif
  48. }