pthread_getcpuclockid.c 1.8 KB

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