clock_gettime.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #if defined(__UCLIBC_USE_TIME64__)
  21. #include "internal/time64_helpers.h"
  22. #endif
  23. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
  24. #define SYSCALL_GETTIME \
  25. { \
  26. struct __ts64_struct __ts64; \
  27. retval = INLINE_SYSCALL (clock_gettime64, 2, clock_id, &__ts64); \
  28. if (tp) { \
  29. tp->tv_sec = __ts64.tv_sec; \
  30. tp->tv_nsec = __ts64.tv_nsec; \
  31. } \
  32. break; \
  33. }
  34. #else
  35. #define SYSCALL_GETTIME \
  36. retval = INLINE_SYSCALL (clock_gettime, 2, clock_id, tp); \
  37. break
  38. #endif
  39. /* The REALTIME and MONOTONIC clock are definitely supported in the kernel. */
  40. #define SYSDEP_GETTIME \
  41. SYSDEP_GETTIME_CPUTIME \
  42. case CLOCK_REALTIME: \
  43. case CLOCK_MONOTONIC: \
  44. SYSCALL_GETTIME
  45. /* We handled the REALTIME clock here. */
  46. #define HANDLED_REALTIME 1
  47. #define HANDLED_CPUTIME 1
  48. #define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
  49. #define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
  50. static inline int
  51. realtime_gettime (struct timespec *tp)
  52. {
  53. struct timeval tv;
  54. int retval = gettimeofday (&tv, NULL);
  55. if (retval == 0)
  56. /* Convert into `timespec'. */
  57. TIMEVAL_TO_TIMESPEC (&tv, tp);
  58. return retval;
  59. }
  60. /* Get current value of CLOCK and store it in TP. */
  61. int
  62. clock_gettime (clockid_t clock_id, struct timespec *tp)
  63. {
  64. int retval = -1;
  65. #ifndef HANDLED_REALTIME
  66. struct timeval tv;
  67. #endif
  68. switch (clock_id)
  69. {
  70. #ifdef SYSDEP_GETTIME
  71. SYSDEP_GETTIME;
  72. #endif
  73. #ifndef HANDLED_REALTIME
  74. case CLOCK_REALTIME:
  75. retval = gettimeofday (&tv, NULL);
  76. if (retval == 0)
  77. TIMEVAL_TO_TIMESPEC (&tv, tp);
  78. break;
  79. #endif
  80. default:
  81. #ifdef SYSDEP_GETTIME_CPU
  82. SYSDEP_GETTIME_CPU;
  83. #endif
  84. __set_errno (EINVAL);
  85. break;
  86. }
  87. return retval;
  88. }