clock_gettime.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
  2. Copyright (C) 2003, 2004 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <sysdep.h>
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include "kernel-posix-cpu-timers.h"
  20. #ifdef __VDSO_SUPPORT__
  21. #include "ldso.h"
  22. #endif
  23. #if defined(__UCLIBC_USE_TIME64__)
  24. #include "internal/time64_helpers.h"
  25. #endif
  26. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
  27. #define SYSCALL_GETTIME \
  28. { \
  29. struct __ts64_struct __ts64; \
  30. retval = INLINE_SYSCALL (clock_gettime64, 2, clock_id, &__ts64); \
  31. if (tp) { \
  32. tp->tv_sec = __ts64.tv_sec; \
  33. tp->tv_nsec = __ts64.tv_nsec; \
  34. } \
  35. break; \
  36. }
  37. #else
  38. #define SYSCALL_GETTIME \
  39. retval = INLINE_SYSCALL (clock_gettime, 2, clock_id, tp); \
  40. break
  41. #endif
  42. /* The REALTIME and MONOTONIC clock are definitely supported in the kernel. */
  43. #define SYSDEP_GETTIME \
  44. SYSDEP_GETTIME_CPUTIME \
  45. case CLOCK_REALTIME: \
  46. case CLOCK_MONOTONIC: \
  47. SYSCALL_GETTIME
  48. /* We handled the REALTIME clock here. */
  49. #define HANDLED_REALTIME 1
  50. #define HANDLED_CPUTIME 1
  51. #define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
  52. #define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
  53. static inline int
  54. realtime_gettime (struct timespec *tp)
  55. {
  56. struct timeval tv;
  57. int retval = gettimeofday (&tv, NULL);
  58. if (retval == 0)
  59. /* Convert into `timespec'. */
  60. TIMEVAL_TO_TIMESPEC (&tv, tp);
  61. return retval;
  62. }
  63. int
  64. __libc_clock_gettime (clockid_t clock_id, struct timespec *tp)
  65. {
  66. int retval = -1;
  67. #ifndef HANDLED_REALTIME
  68. struct timeval tv;
  69. #endif
  70. switch (clock_id)
  71. {
  72. #ifdef SYSDEP_GETTIME
  73. SYSDEP_GETTIME;
  74. #endif
  75. #ifndef HANDLED_REALTIME
  76. case CLOCK_REALTIME:
  77. retval = gettimeofday (&tv, NULL);
  78. if (retval == 0)
  79. TIMEVAL_TO_TIMESPEC (&tv, tp);
  80. break;
  81. #endif
  82. default:
  83. #ifdef SYSDEP_GETTIME_CPU
  84. SYSDEP_GETTIME_CPU;
  85. #endif
  86. __set_errno (EINVAL);
  87. break;
  88. }
  89. return retval;
  90. }
  91. /* Get current value of CLOCK and store it in TP. */
  92. int
  93. clock_gettime (clockid_t clock_id, struct timespec *tp)
  94. {
  95. #if defined(__VDSO_SUPPORT__) && defined(ARCH_VDSO_CLOCK_GETTIME)
  96. return ARCH_VDSO_CLOCK_GETTIME(clock_id, tp);
  97. #else
  98. return __libc_clock_gettime(clock_id, tp);
  99. #endif
  100. }