Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / timer_routines.c
blob2c383bafdfd1b77b45294b1f17bf713026db4625
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <setjmp.h>
21 #include <signal.h>
22 #include <stdbool.h>
23 #include <sysdep.h>
24 #include <kernel-features.h>
25 #include <nptl/pthreadP.h>
26 #include "kernel-posix-timers.h"
29 /* List of active SIGEV_THREAD timers. */
30 struct timer *__active_timer_sigev_thread;
31 /* Lock for the __active_timer_sigev_thread. */
32 pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
35 struct thread_start_data
37 void (*thrfunc) (sigval_t);
38 sigval_t sival;
42 /* Helper thread to call the user-provided function. */
43 static void *
44 timer_sigev_thread (void *arg)
46 /* The parent thread has all signals blocked. This is a bit
47 surprising for user code, although valid. We unblock all
48 signals. */
49 sigset_t ss;
50 sigemptyset (&ss);
51 INTERNAL_SYSCALL_DECL (err);
52 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
54 struct thread_start_data *td = (struct thread_start_data *) arg;
56 void (*thrfunc) (sigval_t) = td->thrfunc;
57 sigval_t sival = td->sival;
59 /* The TD object was allocated in timer_helper_thread. */
60 free (td);
62 /* Call the user-provided function. */
63 thrfunc (sival);
65 return NULL;
69 /* Helper function to support starting threads for SIGEV_THREAD. */
70 static void *
71 timer_helper_thread (void *arg)
73 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
74 none else. */
75 sigset_t ss;
76 sigemptyset (&ss);
77 __sigaddset (&ss, SIGTIMER);
79 /* Endless loop of waiting for signals. The loop is only ended when
80 the thread is canceled. */
81 while (1)
83 siginfo_t si;
85 /* sigwaitinfo cannot be used here, since it deletes
86 SIGCANCEL == SIGTIMER from the set. */
88 int oldtype = LIBC_CANCEL_ASYNC ();
90 /* XXX The size argument hopefully will have to be changed to the
91 real size of the user-level sigset_t. */
92 int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
93 _NSIG / 8);
95 LIBC_CANCEL_RESET (oldtype);
97 if (result > 0)
99 if (si.si_code == SI_TIMER)
101 struct timer *tk = (struct timer *) si.si_ptr;
103 /* Check the timer is still used and will not go away
104 while we are reading the values here. */
105 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
107 struct timer *runp = __active_timer_sigev_thread;
108 while (runp != NULL)
109 if (runp == tk)
110 break;
111 else
112 runp = runp->next;
114 if (runp != NULL)
116 struct thread_start_data *td = malloc (sizeof (*td));
118 /* There is not much we can do if the allocation fails. */
119 if (td != NULL)
121 /* This is the signal we are waiting for. */
122 td->thrfunc = tk->thrfunc;
123 td->sival = tk->sival;
125 pthread_t th;
126 (void) pthread_create (&th, &tk->attr,
127 timer_sigev_thread, td);
131 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
133 else if (si.si_code == SI_TKILL)
134 /* The thread is canceled. */
135 pthread_exit (NULL);
141 /* Control variable for helper thread creation. */
142 pthread_once_t __helper_once attribute_hidden;
145 /* TID of the helper thread. */
146 pid_t __helper_tid attribute_hidden;
149 /* Reset variables so that after a fork a new helper thread gets started. */
150 static void
151 reset_helper_control (void)
153 __helper_once = PTHREAD_ONCE_INIT;
154 __helper_tid = 0;
158 void
159 attribute_hidden
160 __start_helper_thread (void)
162 /* The helper thread needs only very little resources
163 and should go away automatically when canceled. */
164 pthread_attr_t attr;
165 (void) pthread_attr_init (&attr);
166 (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
168 /* Block all signals in the helper thread but SIGSETXID. To do this
169 thoroughly we temporarily have to block all signals here. The
170 helper can lose wakeups if SIGCANCEL is not blocked throughout,
171 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
172 explicitly here. */
173 sigset_t ss;
174 sigset_t oss;
175 sigfillset (&ss);
176 __sigaddset (&ss, SIGCANCEL);
177 INTERNAL_SYSCALL_DECL (err);
178 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
180 /* Create the helper thread for this timer. */
181 pthread_t th;
182 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
183 if (res == 0)
184 /* We managed to start the helper thread. */
185 __helper_tid = ((struct pthread *) th)->tid;
187 /* Restore the signal mask. */
188 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
189 _NSIG / 8);
191 /* No need for the attribute anymore. */
192 (void) pthread_attr_destroy (&attr);
194 /* We have to make sure that after fork()ing a new helper thread can
195 be created. */
196 pthread_atfork (NULL, NULL, reset_helper_control);