]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/timer_settime.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / pthread / timer_settime.c
CommitLineData
04277e02 1/* Copyright (C) 2000-2019 Free Software Foundation, Inc.
76a50749
UD
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
59ba27a6
PE
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
76a50749
UD
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. */
27int
f63f2bfd
JM
28timer_settime (timer_t timerid, int flags, const struct itimerspec *value,
29 struct itimerspec *ovalue)
76a50749
UD
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 (value->it_interval.tv_nsec < 0
45 || value->it_interval.tv_nsec >= 1000000000
46 || value->it_value.tv_nsec < 0
47 || value->it_value.tv_nsec >= 1000000000)
48 {
49 __set_errno (EINVAL);
50 goto bail;
51 }
52
53 /* Will need to know current time since this is a relative timer;
54 might as well make the system call outside of the lock now! */
55
56 if ((flags & TIMER_ABSTIME) == 0)
57 {
dba2bdbe 58 __clock_gettime (timer->clock, &now);
76a50749
UD
59 have_now = 1;
60 }
61
62 pthread_mutex_lock (&__timer_mutex);
63 timer_addref (timer);
64
65 /* One final check of timer validity; this one is possible only
66 until we have the mutex, because it accesses the inuse flag. */
67
68 if (! timer_valid(timer))
69 {
70 __set_errno (EINVAL);
71 goto unlock_bail;
72 }
73
74 if (ovalue != NULL)
75 {
76 ovalue->it_interval = timer->value.it_interval;
77
78 if (timer->armed)
79 {
80 if (! have_now)
81 {
82 pthread_mutex_unlock (&__timer_mutex);
dba2bdbe 83 __clock_gettime (timer->clock, &now);
76a50749
UD
84 have_now = 1;
85 pthread_mutex_lock (&__timer_mutex);
86 timer_addref (timer);
87 }
88
89 timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
90 }
91 else
92 {
93 ovalue->it_value.tv_sec = 0;
94 ovalue->it_value.tv_nsec = 0;
95 }
96 }
97
98 timer->value = *value;
99
100 list_unlink_ip (&timer->links);
101 timer->armed = 0;
102
103 thread = timer->thread;
104
105 /* A value of { 0, 0 } causes the timer to be stopped. */
106 if (value->it_value.tv_sec != 0
107 || __builtin_expect (value->it_value.tv_nsec != 0, 1))
108 {
109 if ((flags & TIMER_ABSTIME) != 0)
110 /* The user specified the expiration time. */
111 timer->expirytime = value->it_value;
112 else
113 timespec_add (&timer->expirytime, &now, &value->it_value);
114
115 /* Only need to wake up the thread if timer is inserted
116 at the head of the queue. */
117 if (thread != NULL)
118 need_wakeup = __timer_thread_queue_timer (thread, timer);
119 timer->armed = 1;
120 }
121
122 retval = 0;
123
124unlock_bail:
125 timer_delref (timer);
126 pthread_mutex_unlock (&__timer_mutex);
127
128bail:
129 if (thread != NULL && need_wakeup)
130 __timer_thread_wakeup (thread);
131
132 return retval;
133}