timer_settime.c 3.4 KB

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