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