timer_settime.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2000, 2001 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 (timerid, flags, value, ovalue)
  22. timer_t timerid;
  23. int flags;
  24. const struct itimerspec *value;
  25. struct itimerspec *ovalue;
  26. {
  27. struct timer_node *timer;
  28. struct thread_node *thread = NULL;
  29. struct timespec now;
  30. int have_now = 0, need_wakeup = 0;
  31. int retval = -1;
  32. timer = timer_id2ptr (timerid);
  33. if (timer == NULL)
  34. {
  35. __set_errno (EINVAL);
  36. goto bail;
  37. }
  38. if (value->it_interval.tv_nsec < 0
  39. || value->it_interval.tv_nsec >= 1000000000
  40. || value->it_value.tv_nsec < 0
  41. || value->it_value.tv_nsec >= 1000000000)
  42. {
  43. __set_errno (EINVAL);
  44. goto bail;
  45. }
  46. /* Will need to know current time since this is a relative timer;
  47. might as well make the system call outside of the lock now! */
  48. if ((flags & TIMER_ABSTIME) == 0)
  49. {
  50. clock_gettime (timer->clock, &now);
  51. have_now = 1;
  52. }
  53. pthread_mutex_lock (&__timer_mutex);
  54. timer_addref (timer);
  55. /* One final check of timer validity; this one is possible only
  56. until we have the mutex, because it accesses the inuse flag. */
  57. if (! timer_valid(timer))
  58. {
  59. __set_errno (EINVAL);
  60. goto unlock_bail;
  61. }
  62. if (ovalue != NULL)
  63. {
  64. ovalue->it_interval = timer->value.it_interval;
  65. if (timer->armed)
  66. {
  67. if (! have_now)
  68. {
  69. pthread_mutex_unlock (&__timer_mutex);
  70. clock_gettime (timer->clock, &now);
  71. have_now = 1;
  72. pthread_mutex_lock (&__timer_mutex);
  73. timer_addref (timer);
  74. }
  75. timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
  76. }
  77. else
  78. {
  79. ovalue->it_value.tv_sec = 0;
  80. ovalue->it_value.tv_nsec = 0;
  81. }
  82. }
  83. timer->value = *value;
  84. list_unlink_ip (&timer->links);
  85. timer->armed = 0;
  86. thread = timer->thread;
  87. /* A value of { 0, 0 } causes the timer to be stopped. */
  88. if (value->it_value.tv_sec != 0
  89. || __builtin_expect (value->it_value.tv_nsec != 0, 1))
  90. {
  91. if ((flags & TIMER_ABSTIME) != 0)
  92. /* The user specified the expiration time. */
  93. timer->expirytime = value->it_value;
  94. else
  95. timespec_add (&timer->expirytime, &now, &value->it_value);
  96. /* Only need to wake up the thread if timer is inserted
  97. at the head of the queue. */
  98. if (thread != NULL)
  99. need_wakeup = __timer_thread_queue_timer (thread, timer);
  100. timer->armed = 1;
  101. }
  102. retval = 0;
  103. unlock_bail:
  104. timer_delref (timer);
  105. pthread_mutex_unlock (&__timer_mutex);
  106. bail:
  107. if (thread != NULL && need_wakeup)
  108. __timer_thread_wakeup (thread);
  109. return retval;
  110. }