Update.
[jlayton/glibc.git] / nptl / sysdeps / pthread / timer_create.c
1 /* Copyright (C) 2000, 2003, 2004 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 <signal.h>
22 #include <pthread.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "posix-timer.h"
27
28
29 /* Create new per-process timer using CLOCK.  */
30 int
31 timer_create (clock_id, evp, timerid)
32      clockid_t clock_id;
33      struct sigevent *evp;
34      timer_t *timerid;
35 {
36   int retval = -1;
37   struct timer_node *newtimer = NULL;
38   struct thread_node *thread = NULL;
39
40   if (0
41 #ifdef _POSIX_CPUTIME
42       || clock_id == CLOCK_PROCESS_CPUTIME_ID
43 #endif
44 #ifdef _POSIX_THREAD_CPUTIME
45       || clock_id == CLOCK_THREAD_CPUTIME_ID
46 #endif
47       )
48     {
49       /* We don't allow timers for CPU clocks.  At least not in the
50          moment.  */
51       __set_errno (ENOTSUP);
52       return -1;
53     }
54
55   if (clock_id != CLOCK_REALTIME)
56     {
57       __set_errno (EINVAL);
58       return -1;
59     }
60
61   pthread_once (&__timer_init_once_control, __timer_init_once);
62
63   if (__timer_init_failed)
64     {
65       __set_errno (ENOMEM);
66       return -1;
67     }
68
69   pthread_mutex_lock (&__timer_mutex);
70
71   newtimer = __timer_alloc ();
72   if (__builtin_expect (newtimer == NULL, 0))
73     {
74       __set_errno (EAGAIN);
75       goto unlock_bail;
76     }
77
78   if (evp != NULL)
79     newtimer->event = *evp;
80   else
81     {
82       newtimer->event.sigev_notify = SIGEV_SIGNAL;
83       newtimer->event.sigev_signo = SIGALRM;
84       newtimer->event.sigev_value.sival_ptr = timer_ptr2id (newtimer);
85       newtimer->event.sigev_notify_function = 0;
86     }
87
88   newtimer->event.sigev_notify_attributes = &newtimer->attr;
89   newtimer->creator_pid = getpid ();
90
91   switch (__builtin_expect (newtimer->event.sigev_notify, SIGEV_SIGNAL))
92     {
93     case SIGEV_NONE:
94     case SIGEV_SIGNAL:
95       /* We have a global thread for delivering timed signals.
96          If it is not running, try to start it up.  */
97       switch (clock_id)
98         {
99         case CLOCK_REALTIME:
100         default:
101           thread = &__timer_signal_thread_rclk;
102           break;
103 #ifdef _POSIX_CPUTIME
104         case CLOCK_PROCESS_CPUTIME_ID:
105           thread = &__timer_signal_thread_pclk;
106           break;
107 #endif
108 #ifdef _POSIX_THREAD_CPUTIME
109         case CLOCK_THREAD_CPUTIME_ID:
110           thread = &__timer_signal_thread_tclk;
111           break;
112 #endif
113         }
114
115       if (! thread->exists)
116         {
117           if (__builtin_expect (__timer_thread_start (thread),
118                                 1) < 0)
119             {
120               __set_errno (EAGAIN);
121               goto unlock_bail;
122             }
123         }
124       break;
125
126     case SIGEV_THREAD:
127       /* Copy over thread attributes or set up default ones.  */
128       if (evp->sigev_notify_attributes)
129         newtimer->attr = *(pthread_attr_t *) evp->sigev_notify_attributes;
130       else
131         pthread_attr_init (&newtimer->attr);
132
133       /* Ensure thread attributes call for deatched thread.  */
134       pthread_attr_setdetachstate (&newtimer->attr, PTHREAD_CREATE_DETACHED);
135
136       /* Try to find existing thread having the right attributes.  */
137       thread = __timer_thread_find_matching (&newtimer->attr, clock_id);
138
139       /* If no existing thread has these attributes, try to allocate one.  */
140       if (thread == NULL)
141         thread = __timer_thread_alloc (&newtimer->attr, clock_id);
142
143       /* Out of luck; no threads are available.  */
144       if (__builtin_expect (thread == NULL, 0))
145         {
146           __set_errno (EAGAIN);
147           goto unlock_bail;
148         }
149
150       /* If the thread is not running already, try to start it.  */
151       if (! thread->exists
152           && __builtin_expect (! __timer_thread_start (thread), 0))
153         {
154           __set_errno (EAGAIN);
155           goto unlock_bail;
156         }
157       break;
158
159     default:
160       __set_errno (EINVAL);
161       goto unlock_bail;
162     }
163
164   newtimer->clock = clock_id;
165   newtimer->abstime = 0;
166   newtimer->armed = 0;
167   newtimer->thread = thread;
168
169   *timerid = timer_ptr2id (newtimer);
170   retval = 0;
171
172   if (__builtin_expect (retval, 0) == -1)
173     {
174     unlock_bail:
175       if (thread != NULL)
176         __timer_thread_dealloc (thread);
177       if (newtimer != NULL)
178         {
179           timer_delref (newtimer);
180           __timer_dealloc (newtimer);
181         }
182     }
183
184   pthread_mutex_unlock (&__timer_mutex);
185
186   return retval;
187 }