]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/pthread/timer_settime.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / sysdeps / pthread / timer_settime.c
CommitLineData
d4697bc9 1/* Copyright (C) 2000-2014 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
28timer_settime (timerid, flags, value, ovalue)
29 timer_t timerid;
30 int flags;
31 const struct itimerspec *value;
32 struct itimerspec *ovalue;
33{
34 struct timer_node *timer;
35 struct thread_node *thread = NULL;
36 struct timespec now;
37 int have_now = 0, need_wakeup = 0;
38 int retval = -1;
39
40 timer = timer_id2ptr (timerid);
41 if (timer == NULL)
42 {
43 __set_errno (EINVAL);
44 goto bail;
45 }
46
47 if (value->it_interval.tv_nsec < 0
48 || value->it_interval.tv_nsec >= 1000000000
49 || value->it_value.tv_nsec < 0
50 || value->it_value.tv_nsec >= 1000000000)
51 {
52 __set_errno (EINVAL);
53 goto bail;
54 }
55
56 /* Will need to know current time since this is a relative timer;
57 might as well make the system call outside of the lock now! */
58
59 if ((flags & TIMER_ABSTIME) == 0)
60 {
61 clock_gettime (timer->clock, &now);
62 have_now = 1;
63 }
64
65 pthread_mutex_lock (&__timer_mutex);
66 timer_addref (timer);
67
68 /* One final check of timer validity; this one is possible only
69 until we have the mutex, because it accesses the inuse flag. */
70
71 if (! timer_valid(timer))
72 {
73 __set_errno (EINVAL);
74 goto unlock_bail;
75 }
76
77 if (ovalue != NULL)
78 {
79 ovalue->it_interval = timer->value.it_interval;
80
81 if (timer->armed)
82 {
83 if (! have_now)
84 {
85 pthread_mutex_unlock (&__timer_mutex);
86 clock_gettime (timer->clock, &now);
87 have_now = 1;
88 pthread_mutex_lock (&__timer_mutex);
89 timer_addref (timer);
90 }
91
92 timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
93 }
94 else
95 {
96 ovalue->it_value.tv_sec = 0;
97 ovalue->it_value.tv_nsec = 0;
98 }
99 }
100
101 timer->value = *value;
102
103 list_unlink_ip (&timer->links);
104 timer->armed = 0;
105
106 thread = timer->thread;
107
108 /* A value of { 0, 0 } causes the timer to be stopped. */
109 if (value->it_value.tv_sec != 0
110 || __builtin_expect (value->it_value.tv_nsec != 0, 1))
111 {
112 if ((flags & TIMER_ABSTIME) != 0)
113 /* The user specified the expiration time. */
114 timer->expirytime = value->it_value;
115 else
116 timespec_add (&timer->expirytime, &now, &value->it_value);
117
118 /* Only need to wake up the thread if timer is inserted
119 at the head of the queue. */
120 if (thread != NULL)
121 need_wakeup = __timer_thread_queue_timer (thread, timer);
122 timer->armed = 1;
123 }
124
125 retval = 0;
126
127unlock_bail:
128 timer_delref (timer);
129 pthread_mutex_unlock (&__timer_mutex);
130
131bail:
132 if (thread != NULL && need_wakeup)
133 __timer_thread_wakeup (thread);
134
135 return retval;
136}