clock_gettime.c 2.5 KB

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