]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/timer_create.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / timer_create.c
1 /* Copyright (C) 2003-2015 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 <internaltypes.h>
27 #include <nptl/pthreadP.h>
28 #include "kernel-posix-timers.h"
29 #include "kernel-posix-cpu-timers.h"
30
31
32 #ifdef timer_create_alias
33 # define timer_create timer_create_alias
34 #endif
35
36
37 int
38 timer_create (clock_id, evp, timerid)
39 clockid_t clock_id;
40 struct sigevent *evp;
41 timer_t *timerid;
42 {
43 #undef timer_create
44 {
45 clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
46 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
47 : clock_id == CLOCK_THREAD_CPUTIME_ID
48 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
49 : clock_id);
50
51 /* If the user wants notification via a thread we need to handle
52 this special. */
53 if (evp == NULL
54 || __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1))
55 {
56 struct sigevent local_evp;
57
58 /* We avoid allocating too much memory by basically
59 using struct timer as a derived class with the
60 first two elements being in the superclass. We only
61 need these two elements here. */
62 struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
63 thrfunc));
64 if (newp == NULL)
65 /* No more memory. */
66 return -1;
67
68 if (evp == NULL)
69 {
70 /* The kernel has to pass up the timer ID which is a
71 userlevel object. Therefore we cannot leave it up to
72 the kernel to determine it. */
73 local_evp.sigev_notify = SIGEV_SIGNAL;
74 local_evp.sigev_signo = SIGALRM;
75 local_evp.sigev_value.sival_ptr = newp;
76
77 evp = &local_evp;
78 }
79
80 kernel_timer_t ktimerid;
81 int retval = INLINE_SYSCALL (timer_create, 3, syscall_clockid, evp,
82 &ktimerid);
83
84 if (retval != -1)
85 {
86 newp->sigev_notify = (evp != NULL
87 ? evp->sigev_notify : SIGEV_SIGNAL);
88 newp->ktimerid = ktimerid;
89
90 *timerid = (timer_t) newp;
91 }
92 else
93 {
94 /* Cannot allocate the timer, fail. */
95 free (newp);
96 retval = -1;
97 }
98
99 return retval;
100 }
101 else
102 {
103 /* Create the helper thread. */
104 pthread_once (&__helper_once, __start_helper_thread);
105 if (__helper_tid == 0)
106 {
107 /* No resources to start the helper thread. */
108 __set_errno (EAGAIN);
109 return -1;
110 }
111
112 struct timer *newp;
113 newp = (struct timer *) malloc (sizeof (struct timer));
114 if (newp == NULL)
115 return -1;
116
117 /* Copy the thread parameters the user provided. */
118 newp->sival = evp->sigev_value;
119 newp->thrfunc = evp->sigev_notify_function;
120 newp->sigev_notify = SIGEV_THREAD;
121
122 /* We cannot simply copy the thread attributes since the
123 implementation might keep internal information for
124 each instance. */
125 (void) pthread_attr_init (&newp->attr);
126 if (evp->sigev_notify_attributes != NULL)
127 {
128 struct pthread_attr *nattr;
129 struct pthread_attr *oattr;
130
131 nattr = (struct pthread_attr *) &newp->attr;
132 oattr = (struct pthread_attr *) evp->sigev_notify_attributes;
133
134 nattr->schedparam = oattr->schedparam;
135 nattr->schedpolicy = oattr->schedpolicy;
136 nattr->flags = oattr->flags;
137 nattr->guardsize = oattr->guardsize;
138 nattr->stackaddr = oattr->stackaddr;
139 nattr->stacksize = oattr->stacksize;
140 }
141
142 /* In any case set the detach flag. */
143 (void) pthread_attr_setdetachstate (&newp->attr,
144 PTHREAD_CREATE_DETACHED);
145
146 /* Create the event structure for the kernel timer. */
147 struct sigevent sev =
148 { .sigev_value.sival_ptr = newp,
149 .sigev_signo = SIGTIMER,
150 .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
151 ._sigev_un = { ._pad = { [0] = __helper_tid } } };
152
153 /* Create the timer. */
154 INTERNAL_SYSCALL_DECL (err);
155 int res;
156 res = INTERNAL_SYSCALL (timer_create, err, 3,
157 syscall_clockid, &sev, &newp->ktimerid);
158 if (! INTERNAL_SYSCALL_ERROR_P (res, err))
159 {
160 /* Add to the queue of active timers with thread
161 delivery. */
162 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
163 newp->next = __active_timer_sigev_thread;
164 __active_timer_sigev_thread = newp;
165 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
166
167 *timerid = (timer_t) newp;
168 return 0;
169 }
170
171 /* Free the resources. */
172 free (newp);
173
174 __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
175
176 return -1;
177 }
178 }
179 }