timer_settime.c 3.5 KB

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