1 /* Copyright (C) 2003,2004, 2007, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <sysdep.h>
26 #include <bits/kernel-features.h>
27 #include <internaltypes.h>
28 #include <pthreadP.h>
29 #include "kernel-posix-timers.h"
30 #include "kernel-posix-cpu-timers.h"
31
32
33 #ifdef __NR_timer_create
34 # ifndef __ASSUME_POSIX_TIMERS
35 static int compat_timer_create (clockid_t clock_id, struct sigevent *evp,
36 timer_t *timerid);
37 # define timer_create static compat_timer_create
38 # include <nptl/sysdeps/pthread/timer_create.c>
39 # undef timer_create
40
41 /* Nonzero if the system calls are not available. */
42 int __no_posix_timers attribute_hidden;
43 # endif
44
45 # ifdef timer_create_alias
46 # define timer_create timer_create_alias
47 # endif
48
49
50 int
timer_create(clockid_t clock_id,struct sigevent * evp,timer_t * timerid)51 timer_create (
52 clockid_t clock_id,
53 struct sigevent *evp,
54 timer_t *timerid)
55 {
56 # undef timer_create
57 # ifndef __ASSUME_POSIX_TIMERS
58 if (__no_posix_timers >= 0)
59 # endif
60 {
61 clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
62 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
63 : clock_id == CLOCK_THREAD_CPUTIME_ID
64 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
65 : clock_id);
66
67 /* If the user wants notification via a thread we need to handle
68 this special. */
69 if (evp == NULL
70 || __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1))
71 {
72 struct sigevent local_evp;
73
74 /* We avoid allocating too much memory by basically
75 using struct timer as a derived class with the
76 first two elements being in the superclass. We only
77 need these two elements here. */
78 struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
79 thrfunc));
80 if (newp == NULL)
81 /* No more memory. */
82 return -1;
83
84 if (evp == NULL)
85 {
86 /* The kernel has to pass up the timer ID which is a
87 userlevel object. Therefore we cannot leave it up to
88 the kernel to determine it. */
89 local_evp.sigev_notify = SIGEV_SIGNAL;
90 local_evp.sigev_signo = SIGALRM;
91 local_evp.sigev_value.sival_ptr = newp;
92
93 evp = &local_evp;
94 }
95
96 kernel_timer_t ktimerid;
97 int retval = INLINE_SYSCALL (timer_create, 3, syscall_clockid, evp,
98 &ktimerid);
99
100 # ifndef __ASSUME_POSIX_TIMERS
101 if (retval != -1 || errno != ENOSYS)
102 # endif
103 {
104 # ifndef __ASSUME_POSIX_TIMERS
105 __no_posix_timers = 1;
106 # endif
107
108 if (retval != -1)
109 {
110 newp->sigev_notify = (evp != NULL
111 ? evp->sigev_notify : SIGEV_SIGNAL);
112 newp->ktimerid = ktimerid;
113
114 *timerid = (timer_t) newp;
115 }
116 else
117 {
118 /* Cannot allocate the timer, fail. */
119 free (newp);
120 retval = -1;
121 }
122
123 return retval;
124 }
125
126 free (newp);
127
128 # ifndef __ASSUME_POSIX_TIMERS
129 /* When we come here the syscall does not exist. Make sure we
130 do not try to use it again. */
131 __no_posix_timers = -1;
132 # endif
133 }
134 else
135 {
136 # ifndef __ASSUME_POSIX_TIMERS
137 /* Make sure we have the necessary kernel support. */
138 if (__no_posix_timers == 0)
139 {
140 INTERNAL_SYSCALL_DECL (err);
141 struct timespec ts;
142 int res;
143 res = INTERNAL_SYSCALL (clock_getres, err, 2,
144 CLOCK_REALTIME, &ts);
145 __no_posix_timers = (INTERNAL_SYSCALL_ERROR_P (res, err)
146 ? -1 : 1);
147 }
148
149 if (__no_posix_timers > 0)
150 # endif
151 {
152 /* Create the helper thread. */
153 pthread_once (&__helper_once, __start_helper_thread);
154 if (__helper_tid == 0)
155 {
156 /* No resources to start the helper thread. */
157 __set_errno (EAGAIN);
158 return -1;
159 }
160
161 struct timer *newp;
162 newp = (struct timer *) malloc (sizeof (struct timer));
163 if (newp == NULL)
164 return -1;
165
166 /* Copy the thread parameters the user provided. */
167 newp->sival = evp->sigev_value;
168 newp->thrfunc = evp->sigev_notify_function;
169 newp->sigev_notify = SIGEV_THREAD;
170
171 /* We cannot simply copy the thread attributes since the
172 implementation might keep internal information for
173 each instance. */
174 (void) pthread_attr_init (&newp->attr);
175 if (evp->sigev_notify_attributes != NULL)
176 {
177 struct pthread_attr *nattr;
178 struct pthread_attr *oattr;
179
180 nattr = (struct pthread_attr *) &newp->attr;
181 oattr = (struct pthread_attr *) evp->sigev_notify_attributes;
182
183 nattr->schedparam = oattr->schedparam;
184 nattr->schedpolicy = oattr->schedpolicy;
185 nattr->flags = oattr->flags;
186 nattr->guardsize = oattr->guardsize;
187 nattr->stackaddr = oattr->stackaddr;
188 nattr->stacksize = oattr->stacksize;
189 }
190
191 /* In any case set the detach flag. */
192 (void) pthread_attr_setdetachstate (&newp->attr,
193 PTHREAD_CREATE_DETACHED);
194
195 /* Create the event structure for the kernel timer. */
196 struct sigevent sev =
197 { .sigev_value.sival_ptr = newp,
198 .sigev_signo = SIGTIMER,
199 .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
200 ._sigev_un = { ._pad = { [0] = __helper_tid } } };
201
202 /* Create the timer. */
203 INTERNAL_SYSCALL_DECL (err);
204 int res;
205 res = INTERNAL_SYSCALL (timer_create, err, 3,
206 syscall_clockid, &sev, &newp->ktimerid);
207 if (! INTERNAL_SYSCALL_ERROR_P (res, err))
208 {
209 /* Add to the queue of active timers with thread
210 delivery. */
211 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
212 newp->next = __active_timer_sigev_thread;
213 __active_timer_sigev_thread = newp;
214 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
215
216 *timerid = (timer_t) newp;
217 return 0;
218 }
219
220 /* Free the resources. */
221 free (newp);
222
223 __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
224
225 return -1;
226 }
227 }
228 }
229
230 # ifndef __ASSUME_POSIX_TIMERS
231 /* Compatibility code. */
232 return compat_timer_create (clock_id, evp, timerid);
233 # endif
234 }
235 #else
236 # ifdef timer_create_alias
237 # define timer_create timer_create_alias
238 # endif
239 /* The new system calls are not available. Use the userlevel
240 implementation. */
241 # include <nptl/sysdeps/pthread/timer_create.c>
242 #endif
243