/sysdeps/posix/clock_getres.c

https://github.com/nitinkamble/x32-glibc · C · 118 lines · 71 code · 18 blank · 29 comment · 10 complexity · 2c31f093efb7f6ec73059a225203a436 MD5 · raw file

  1. /* clock_getres -- Get the resolution of a POSIX clockid_t.
  2. Copyright (C) 1999,2000,2001,2003,2004,2008 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <stdint.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <sys/param.h>
  21. #include <libc-internal.h>
  22. #if HP_TIMING_AVAIL
  23. static long int nsec; /* Clock frequency of the processor. */
  24. static int
  25. hp_timing_getres (struct timespec *res)
  26. {
  27. if (__builtin_expect (nsec == 0, 0))
  28. {
  29. hp_timing_t freq;
  30. /* This can only happen if we haven't initialized the `nsec'
  31. variable yet. Do this now. We don't have to protect this
  32. code against multiple execution since all of them should
  33. lead to the same result. */
  34. freq = __get_clockfreq ();
  35. if (__builtin_expect (freq == 0, 0))
  36. /* Something went wrong. */
  37. return -1;
  38. nsec = MAX (UINT64_C (1000000000) / freq, 1);
  39. }
  40. /* Fill in the values.
  41. The seconds are always zero (unless we have a 1Hz machine). */
  42. res->tv_sec = 0;
  43. res->tv_nsec = nsec;
  44. return 0;
  45. }
  46. #endif
  47. static inline int
  48. realtime_getres (struct timespec *res)
  49. {
  50. long int clk_tck = sysconf (_SC_CLK_TCK);
  51. if (__builtin_expect (clk_tck != -1, 1))
  52. {
  53. /* This implementation assumes that the realtime clock has a
  54. resolution higher than 1 second. This is the case for any
  55. reasonable implementation. */
  56. res->tv_sec = 0;
  57. res->tv_nsec = 1000000000 / clk_tck;
  58. return 0;
  59. }
  60. return -1;
  61. }
  62. /* Get resolution of clock. */
  63. int
  64. clock_getres (clockid_t clock_id, struct timespec *res)
  65. {
  66. int retval = -1;
  67. switch (clock_id)
  68. {
  69. #ifdef SYSDEP_GETRES
  70. SYSDEP_GETRES;
  71. #endif
  72. #ifndef HANDLED_REALTIME
  73. case CLOCK_REALTIME:
  74. retval = realtime_getres (res);
  75. break;
  76. #endif /* handled REALTIME */
  77. default:
  78. #ifdef SYSDEP_GETRES_CPU
  79. SYSDEP_GETRES_CPU;
  80. #endif
  81. #if HP_TIMING_AVAIL
  82. if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
  83. == CLOCK_THREAD_CPUTIME_ID)
  84. retval = hp_timing_getres (res);
  85. else
  86. #endif
  87. __set_errno (EINVAL);
  88. break;
  89. #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
  90. case CLOCK_PROCESS_CPUTIME_ID:
  91. case CLOCK_THREAD_CPUTIME_ID:
  92. retval = hp_timing_getres (res);
  93. break;
  94. #endif
  95. }
  96. return retval;
  97. }