1 /* Copyright (C) 2000-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <https://www.gnu.org/licenses/>. */
22 #include "posix-timer.h"
25 /* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
27 timer_settime (timer_t timerid, int flags, const struct itimerspec *value,
28 struct itimerspec *ovalue)
30 struct timer_node *timer;
31 struct thread_node *thread = NULL;
33 int have_now = 0, need_wakeup = 0;
36 timer = timer_id2ptr (timerid);
43 if (! valid_nanoseconds (value->it_interval.tv_nsec)
44 || ! valid_nanoseconds (value->it_value.tv_nsec))
50 /* Will need to know current time since this is a relative timer;
51 might as well make the system call outside of the lock now! */
53 if ((flags & TIMER_ABSTIME) == 0)
55 __clock_gettime (timer->clock, &now);
59 pthread_mutex_lock (&__timer_mutex);
62 /* One final check of timer validity; this one is possible only
63 until we have the mutex, because it accesses the inuse flag. */
65 if (! timer_valid(timer))
73 ovalue->it_interval = timer->value.it_interval;
79 pthread_mutex_unlock (&__timer_mutex);
80 __clock_gettime (timer->clock, &now);
82 pthread_mutex_lock (&__timer_mutex);
86 timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
90 ovalue->it_value.tv_sec = 0;
91 ovalue->it_value.tv_nsec = 0;
95 timer->value = *value;
97 list_unlink_ip (&timer->links);
100 thread = timer->thread;
102 /* A value of { 0, 0 } causes the timer to be stopped. */
103 if (value->it_value.tv_sec != 0
104 || __builtin_expect (value->it_value.tv_nsec != 0, 1))
106 if ((flags & TIMER_ABSTIME) != 0)
107 /* The user specified the expiration time. */
108 timer->expirytime = value->it_value;
110 timespec_add (&timer->expirytime, &now, &value->it_value);
112 /* Only need to wake up the thread if timer is inserted
113 at the head of the queue. */
115 need_wakeup = __timer_thread_queue_timer (thread, timer);
122 timer_delref (timer);
123 pthread_mutex_unlock (&__timer_mutex);
126 if (thread != NULL && need_wakeup)
127 __timer_thread_wakeup (thread);