]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/timer_settime.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / pthread / timer_settime.c
1 /* Copyright (C) 2000-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <time.h>
22
23 #include "posix-timer.h"
24
25
26 /* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
27 int
28 timer_settime (timer_t timerid, int flags, const struct itimerspec *value,
29 struct itimerspec *ovalue)
30 {
31 struct timer_node *timer;
32 struct thread_node *thread = NULL;
33 struct timespec now;
34 int have_now = 0, need_wakeup = 0;
35 int retval = -1;
36
37 timer = timer_id2ptr (timerid);
38 if (timer == NULL)
39 {
40 __set_errno (EINVAL);
41 goto bail;
42 }
43
44 if (! valid_nanoseconds (value->it_interval.tv_nsec)
45 || ! valid_nanoseconds (value->it_value.tv_nsec))
46 {
47 __set_errno (EINVAL);
48 goto bail;
49 }
50
51 /* Will need to know current time since this is a relative timer;
52 might as well make the system call outside of the lock now! */
53
54 if ((flags & TIMER_ABSTIME) == 0)
55 {
56 __clock_gettime (timer->clock, &now);
57 have_now = 1;
58 }
59
60 pthread_mutex_lock (&__timer_mutex);
61 timer_addref (timer);
62
63 /* One final check of timer validity; this one is possible only
64 until we have the mutex, because it accesses the inuse flag. */
65
66 if (! timer_valid(timer))
67 {
68 __set_errno (EINVAL);
69 goto unlock_bail;
70 }
71
72 if (ovalue != NULL)
73 {
74 ovalue->it_interval = timer->value.it_interval;
75
76 if (timer->armed)
77 {
78 if (! have_now)
79 {
80 pthread_mutex_unlock (&__timer_mutex);
81 __clock_gettime (timer->clock, &now);
82 have_now = 1;
83 pthread_mutex_lock (&__timer_mutex);
84 timer_addref (timer);
85 }
86
87 timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
88 }
89 else
90 {
91 ovalue->it_value.tv_sec = 0;
92 ovalue->it_value.tv_nsec = 0;
93 }
94 }
95
96 timer->value = *value;
97
98 list_unlink_ip (&timer->links);
99 timer->armed = 0;
100
101 thread = timer->thread;
102
103 /* A value of { 0, 0 } causes the timer to be stopped. */
104 if (value->it_value.tv_sec != 0
105 || __builtin_expect (value->it_value.tv_nsec != 0, 1))
106 {
107 if ((flags & TIMER_ABSTIME) != 0)
108 /* The user specified the expiration time. */
109 timer->expirytime = value->it_value;
110 else
111 timespec_add (&timer->expirytime, &now, &value->it_value);
112
113 /* Only need to wake up the thread if timer is inserted
114 at the head of the queue. */
115 if (thread != NULL)
116 need_wakeup = __timer_thread_queue_timer (thread, timer);
117 timer->armed = 1;
118 }
119
120 retval = 0;
121
122 unlock_bail:
123 timer_delref (timer);
124 pthread_mutex_unlock (&__timer_mutex);
125
126 bail:
127 if (thread != NULL && need_wakeup)
128 __timer_thread_wakeup (thread);
129
130 return retval;
131 }