clock_settime.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 1999-2019 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, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <time.h>
  16. #include <sys/time.h>
  17. #include <ldsodefs.h>
  18. #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
  19. /* Clock frequency of the processor. We make it a 64-bit variable
  20. because some jokers are already playing with processors with more
  21. than 4GHz. */
  22. static hp_timing_t freq;
  23. /* This function is defined in the thread library. */
  24. extern void __pthread_clock_settime (clockid_t clock_id, hp_timing_t offset)
  25. __attribute__ ((__weak__));
  26. static int
  27. hp_timing_settime (clockid_t clock_id, const struct timespec *tp)
  28. {
  29. hp_timing_t tsc;
  30. hp_timing_t usertime;
  31. /* First thing is to get the current time. */
  32. HP_TIMING_NOW (tsc);
  33. if (__glibc_unlikely (freq == 0))
  34. {
  35. /* This can only happen if we haven't initialized the `freq'
  36. variable yet. Do this now. We don't have to protect this
  37. code against multiple execution since all of them should lead
  38. to the same result. */
  39. freq = __get_clockfreq ();
  40. if (__glibc_unlikely (freq == 0))
  41. /* Something went wrong. */
  42. return -1;
  43. }
  44. /* Convert the user-provided time into CPU ticks. */
  45. usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull;
  46. /* Determine the offset and use it as the new base value. */
  47. if (clock_id == CLOCK_PROCESS_CPUTIME_ID
  48. || __pthread_clock_settime == NULL)
  49. GL(dl_cpuclock_offset) = tsc - usertime;
  50. else
  51. __pthread_clock_settime (clock_id, tsc - usertime);
  52. return 0;
  53. }
  54. #endif
  55. /* Set CLOCK to value TP. */
  56. int
  57. __clock_settime (clockid_t clock_id, const struct timespec *tp)
  58. {
  59. int retval;
  60. /* Make sure the time cvalue is OK. */
  61. if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
  62. {
  63. __set_errno (EINVAL);
  64. return -1;
  65. }
  66. switch (clock_id)
  67. {
  68. #define HANDLE_REALTIME \
  69. do { \
  70. struct timeval tv; \
  71. TIMESPEC_TO_TIMEVAL (&tv, tp); \
  72. \
  73. retval = __settimeofday (&tv, NULL); \
  74. } while (0)
  75. #ifdef SYSDEP_SETTIME
  76. SYSDEP_SETTIME;
  77. #endif
  78. #ifndef HANDLED_REALTIME
  79. case CLOCK_REALTIME:
  80. HANDLE_REALTIME;
  81. break;
  82. #endif
  83. default:
  84. #ifdef SYSDEP_SETTIME_CPU
  85. SYSDEP_SETTIME_CPU;
  86. #endif
  87. #ifndef HANDLED_CPUTIME
  88. # if HP_TIMING_AVAIL
  89. if (CPUCLOCK_WHICH (clock_id) == CLOCK_PROCESS_CPUTIME_ID
  90. || CPUCLOCK_WHICH (clock_id) == CLOCK_THREAD_CPUTIME_ID)
  91. retval = hp_timing_settime (clock_id, tp);
  92. else
  93. # endif
  94. {
  95. __set_errno (EINVAL);
  96. retval = -1;
  97. }
  98. #endif
  99. break;
  100. }
  101. return retval;
  102. }
  103. weak_alias (__clock_settime, clock_settime)