Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / timer_routines.c
blob4401a8b3d0e75a16e2b937f2eb00dd492d69e9b6
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.
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 <nptl/pthreadP.h>
25 #include "kernel-posix-timers.h"
28 /* List of active SIGEV_THREAD timers. */
29 struct timer *__active_timer_sigev_thread;
30 /* Lock for the __active_timer_sigev_thread. */
31 pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
34 struct thread_start_data
36 void (*thrfunc) (sigval_t);
37 sigval_t sival;
41 /* Helper thread to call the user-provided function. */
42 static void *
43 timer_sigev_thread (void *arg)
45 /* The parent thread has all signals blocked. This is a bit
46 surprising for user code, although valid. We unblock all
47 signals. */
48 sigset_t ss;
49 sigemptyset (&ss);
50 INTERNAL_SYSCALL_DECL (err);
51 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
53 struct thread_start_data *td = (struct thread_start_data *) arg;
55 void (*thrfunc) (sigval_t) = td->thrfunc;
56 sigval_t sival = td->sival;
58 /* The TD object was allocated in timer_helper_thread. */
59 free (td);
61 /* Call the user-provided function. */
62 thrfunc (sival);
64 return NULL;
68 /* Helper function to support starting threads for SIGEV_THREAD. */
69 static void *
70 timer_helper_thread (void *arg)
72 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
73 none else. */
74 sigset_t ss;
75 sigemptyset (&ss);
76 __sigaddset (&ss, SIGTIMER);
78 /* Endless loop of waiting for signals. The loop is only ended when
79 the thread is canceled. */
80 while (1)
82 siginfo_t si;
84 /* sigwaitinfo cannot be used here, since it deletes
85 SIGCANCEL == SIGTIMER from the set. */
87 int oldtype = LIBC_CANCEL_ASYNC ();
89 /* XXX The size argument hopefully will have to be changed to the
90 real size of the user-level sigset_t. */
91 int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
92 _NSIG / 8);
94 LIBC_CANCEL_RESET (oldtype);
96 if (result > 0)
98 if (si.si_code == SI_TIMER)
100 struct timer *tk = (struct timer *) si.si_ptr;
102 /* Check the timer is still used and will not go away
103 while we are reading the values here. */
104 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
106 struct timer *runp = __active_timer_sigev_thread;
107 while (runp != NULL)
108 if (runp == tk)
109 break;
110 else
111 runp = runp->next;
113 if (runp != NULL)
115 struct thread_start_data *td = malloc (sizeof (*td));
117 /* There is not much we can do if the allocation fails. */
118 if (td != NULL)
120 /* This is the signal we are waiting for. */
121 td->thrfunc = tk->thrfunc;
122 td->sival = tk->sival;
124 pthread_t th;
125 (void) pthread_create (&th, &tk->attr,
126 timer_sigev_thread, td);
130 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
132 else if (si.si_code == SI_TKILL)
133 /* The thread is canceled. */
134 pthread_exit (NULL);
140 /* Control variable for helper thread creation. */
141 pthread_once_t __helper_once attribute_hidden;
144 /* TID of the helper thread. */
145 pid_t __helper_tid attribute_hidden;
148 /* Reset variables so that after a fork a new helper thread gets started. */
149 static void
150 reset_helper_control (void)
152 __helper_once = PTHREAD_ONCE_INIT;
153 __helper_tid = 0;
157 void
158 attribute_hidden
159 __start_helper_thread (void)
161 /* The helper thread needs only very little resources
162 and should go away automatically when canceled. */
163 pthread_attr_t attr;
164 (void) pthread_attr_init (&attr);
165 (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
167 /* Block all signals in the helper thread but SIGSETXID. To do this
168 thoroughly we temporarily have to block all signals here. The
169 helper can lose wakeups if SIGCANCEL is not blocked throughout,
170 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
171 explicitly here. */
172 sigset_t ss;
173 sigset_t oss;
174 sigfillset (&ss);
175 __sigaddset (&ss, SIGCANCEL);
176 INTERNAL_SYSCALL_DECL (err);
177 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
179 /* Create the helper thread for this timer. */
180 pthread_t th;
181 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
182 if (res == 0)
183 /* We managed to start the helper thread. */
184 __helper_tid = ((struct pthread *) th)->tid;
186 /* Restore the signal mask. */
187 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
188 _NSIG / 8);
190 /* No need for the attribute anymore. */
191 (void) pthread_attr_destroy (&attr);
193 /* We have to make sure that after fork()ing a new helper thread can
194 be created. */
195 pthread_atfork (NULL, NULL, reset_helper_control);