timer_settime.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2000-2026 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 License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. 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; see the file COPYING.LIB. If
  13. not, see <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <pthread.h>
  16. #include <time.h>
  17. #include "posix-timer.h"
  18. /* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
  19. int
  20. timer_settime (timer_t timerid, int flags, const struct itimerspec *value,
  21. struct itimerspec *ovalue)
  22. {
  23. struct timer_node *timer;
  24. struct thread_node *thread = NULL;
  25. struct timespec now;
  26. int have_now = 0, need_wakeup = 0;
  27. int retval = -1;
  28. timer = timer_id2ptr (timerid);
  29. if (timer == NULL)
  30. {
  31. __set_errno (EINVAL);
  32. goto bail;
  33. }
  34. if (! valid_nanoseconds (value->it_interval.tv_nsec)
  35. || ! valid_nanoseconds (value->it_value.tv_nsec))
  36. {
  37. __set_errno (EINVAL);
  38. goto bail;
  39. }
  40. /* Will need to know current time since this is a relative timer;
  41. might as well make the system call outside of the lock now! */
  42. if ((flags & TIMER_ABSTIME) == 0)
  43. {
  44. __clock_gettime (timer->clock, &now);
  45. have_now = 1;
  46. }
  47. pthread_mutex_lock (&__timer_mutex);
  48. timer_addref (timer);
  49. /* One final check of timer validity; this one is possible only
  50. until we have the mutex, because it accesses the inuse flag. */
  51. if (! timer_valid(timer))
  52. {
  53. __set_errno (EINVAL);
  54. goto unlock_bail;
  55. }
  56. if (ovalue != NULL)
  57. {
  58. ovalue->it_interval = timer->value.it_interval;
  59. if (timer->armed)
  60. {
  61. if (! have_now)
  62. {
  63. pthread_mutex_unlock (&__timer_mutex);
  64. __clock_gettime (timer->clock, &now);
  65. have_now = 1;
  66. pthread_mutex_lock (&__timer_mutex);
  67. timer_addref (timer);
  68. }
  69. timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
  70. }
  71. else
  72. {
  73. ovalue->it_value.tv_sec = 0;
  74. ovalue->it_value.tv_nsec = 0;
  75. }
  76. }
  77. timer->value = *value;
  78. list_unlink_ip (&timer->links);
  79. timer->armed = 0;
  80. thread = timer->thread;
  81. /* A value of { 0, 0 } causes the timer to be stopped. */
  82. if (value->it_value.tv_sec != 0
  83. || __builtin_expect (value->it_value.tv_nsec != 0, 1))
  84. {
  85. if ((flags & TIMER_ABSTIME) != 0)
  86. /* The user specified the expiration time. */
  87. timer->expirytime = value->it_value;
  88. else
  89. timespec_add (&timer->expirytime, &now, &value->it_value);
  90. /* Only need to wake up the thread if timer is inserted
  91. at the head of the queue. */
  92. if (thread != NULL)
  93. need_wakeup = __timer_thread_queue_timer (thread, timer);
  94. timer->armed = 1;
  95. }
  96. retval = 0;
  97. unlock_bail:
  98. timer_delref (timer);
  99. pthread_mutex_unlock (&__timer_mutex);
  100. bail:
  101. if (thread != NULL && need_wakeup)
  102. __timer_thread_wakeup (thread);
  103. return retval;
  104. }