/arm-gp2x-linux-glibc-2.3.6/glibc-2.3.6/sysdeps/posix/clock_getres.c

# · C · 109 lines · 65 code · 15 blank · 29 comment · 12 complexity · 3edbd3520cc4cbc64bc83e98feaca25f MD5 · raw file

  1. /* Copyright (C) 1999, 2000, 2001, 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
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the 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; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <errno.h>
  16. #include <stdint.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <sys/param.h>
  20. #include <libc-internal.h>
  21. #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
  22. /* Clock frequency of the processor. */
  23. static long int nsec;
  24. #endif
  25. /* Get resolution of clock. */
  26. int
  27. clock_getres (clockid_t clock_id, struct timespec *res)
  28. {
  29. int retval = -1;
  30. switch (clock_id)
  31. {
  32. #define HANDLE_REALTIME \
  33. do { \
  34. long int clk_tck = sysconf (_SC_CLK_TCK); \
  35. \
  36. if (__builtin_expect (clk_tck != -1, 1)) \
  37. { \
  38. /* This implementation assumes that the realtime clock has a \
  39. resolution higher than 1 second. This is the case for any \
  40. reasonable implementation. */ \
  41. res->tv_sec = 0; \
  42. res->tv_nsec = 1000000000 / clk_tck; \
  43. \
  44. retval = 0; \
  45. } \
  46. } while (0)
  47. #ifdef SYSDEP_GETRES
  48. SYSDEP_GETRES;
  49. #endif
  50. #ifndef HANDLED_REALTIME
  51. case CLOCK_REALTIME:
  52. HANDLE_REALTIME;
  53. break;
  54. #endif /* handled REALTIME */
  55. default:
  56. #if HP_TIMING_AVAIL
  57. if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
  58. != CLOCK_THREAD_CPUTIME_ID)
  59. #endif
  60. {
  61. __set_errno (EINVAL);
  62. break;
  63. }
  64. #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
  65. /* FALLTHROUGH. */
  66. case CLOCK_PROCESS_CPUTIME_ID:
  67. {
  68. if (__builtin_expect (nsec == 0, 0))
  69. {
  70. hp_timing_t freq;
  71. /* This can only happen if we haven't initialized the `freq'
  72. variable yet. Do this now. We don't have to protect this
  73. code against multiple execution since all of them should
  74. lead to the same result. */
  75. freq = __get_clockfreq ();
  76. if (__builtin_expect (freq == 0, 0))
  77. /* Something went wrong. */
  78. break;
  79. nsec = MAX (UINT64_C (1000000000) / freq, 1);
  80. }
  81. /* File in the values. The seconds are always zero (unless we
  82. have a 1Hz machine). */
  83. res->tv_sec = 0;
  84. res->tv_nsec = nsec;
  85. retval = 0;
  86. }
  87. break;
  88. #endif
  89. }
  90. return retval;
  91. }