clock_gettime.c 2.3 KB

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