]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/timer_routines.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / timer_routines.c
CommitLineData
04277e02 1/* Copyright (C) 2003-2019 Free Software Foundation, Inc.
09402f5b
UD
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
59ba27a6 16 License along with the GNU C Library; see the file COPYING.LIB. If
5a82c748 17 not, see <https://www.gnu.org/licenses/>. */
09402f5b 18
d7ba1313 19#include <errno.h>
09402f5b
UD
20#include <setjmp.h>
21#include <signal.h>
22#include <stdbool.h>
c6bb095e 23#include <sysdep-cancel.h>
5f5004df 24#include <nptl/pthreadP.h>
09402f5b
UD
25#include "kernel-posix-timers.h"
26
27
f160a450
UD
28/* List of active SIGEV_THREAD timers. */
29struct timer *__active_timer_sigev_thread;
30/* Lock for the __active_timer_sigev_thread. */
31pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
32
33
a6375d11
UD
34struct thread_start_data
35{
36 void (*thrfunc) (sigval_t);
37 sigval_t sival;
38};
39
40
09402f5b
UD
41/* Helper thread to call the user-provided function. */
42static void *
43timer_sigev_thread (void *arg)
44{
fa0e226b
UD
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);
52
a6375d11
UD
53 struct thread_start_data *td = (struct thread_start_data *) arg;
54
55 void (*thrfunc) (sigval_t) = td->thrfunc;
56 sigval_t sival = td->sival;
57
58 /* The TD object was allocated in timer_helper_thread. */
59 free (td);
09402f5b
UD
60
61 /* Call the user-provided function. */
a6375d11 62 thrfunc (sival);
09402f5b
UD
63
64 return NULL;
65}
66
67
68/* Helper function to support starting threads for SIGEV_THREAD. */
5f5004df
UD
69static void *
70timer_helper_thread (void *arg)
09402f5b 71{
fd4af664
UD
72 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
73 none else. */
09402f5b 74 sigset_t ss;
09402f5b 75 sigemptyset (&ss);
fd4af664 76 __sigaddset (&ss, SIGTIMER);
09402f5b
UD
77
78 /* Endless loop of waiting for signals. The loop is only ended when
79 the thread is canceled. */
80 while (1)
81 {
82 siginfo_t si;
83
d7ba1313
UD
84 /* sigwaitinfo cannot be used here, since it deletes
85 SIGCANCEL == SIGTIMER from the set. */
86
d7ba1313
UD
87 /* XXX The size argument hopefully will have to be changed to the
88 real size of the user-level sigset_t. */
c6bb095e 89 int result = SYSCALL_CANCEL (rt_sigtimedwait, &ss, &si, NULL, _NSIG / 8);
d7ba1313
UD
90
91 if (result > 0)
09402f5b 92 {
e7608d77
UD
93 if (si.si_code == SI_TIMER)
94 {
95 struct timer *tk = (struct timer *) si.si_ptr;
a6375d11 96
f160a450
UD
97 /* Check the timer is still used and will not go away
98 while we are reading the values here. */
99 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
a6375d11 100
f160a450
UD
101 struct timer *runp = __active_timer_sigev_thread;
102 while (runp != NULL)
103 if (runp == tk)
104 break;
105 else
106 runp = runp->next;
107
108 if (runp != NULL)
109 {
110 struct thread_start_data *td = malloc (sizeof (*td));
111
112 /* There is not much we can do if the allocation fails. */
113 if (td != NULL)
114 {
115 /* This is the signal we are waiting for. */
116 td->thrfunc = tk->thrfunc;
117 td->sival = tk->sival;
118
119 pthread_t th;
120 (void) pthread_create (&th, &tk->attr,
121 timer_sigev_thread, td);
122 }
a6375d11 123 }
f160a450
UD
124
125 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
e7608d77
UD
126 }
127 else if (si.si_code == SI_TKILL)
128 /* The thread is canceled. */
129 pthread_exit (NULL);
09402f5b
UD
130 }
131 }
132}
133
5f5004df
UD
134
135/* Control variable for helper thread creation. */
136pthread_once_t __helper_once attribute_hidden;
137
138
139/* TID of the helper thread. */
140pid_t __helper_tid attribute_hidden;
141
142
143/* Reset variables so that after a fork a new helper thread gets started. */
144static void
145reset_helper_control (void)
146{
147 __helper_once = PTHREAD_ONCE_INIT;
148 __helper_tid = 0;
149}
150
151
152void
153attribute_hidden
154__start_helper_thread (void)
155{
156 /* The helper thread needs only very little resources
157 and should go away automatically when canceled. */
158 pthread_attr_t attr;
159 (void) pthread_attr_init (&attr);
2c1094bd 160 (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
5f5004df 161
fd4af664
UD
162 /* Block all signals in the helper thread but SIGSETXID. To do this
163 thoroughly we temporarily have to block all signals here. The
164 helper can lose wakeups if SIGCANCEL is not blocked throughout,
8558d715
UD
165 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
166 explicitly here. */
fa0e226b
UD
167 sigset_t ss;
168 sigset_t oss;
169 sigfillset (&ss);
0086214c 170 __sigaddset (&ss, SIGCANCEL);
fa0e226b
UD
171 INTERNAL_SYSCALL_DECL (err);
172 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
173
5f5004df
UD
174 /* Create the helper thread for this timer. */
175 pthread_t th;
176 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
177 if (res == 0)
178 /* We managed to start the helper thread. */
179 __helper_tid = ((struct pthread *) th)->tid;
180
fa0e226b
UD
181 /* Restore the signal mask. */
182 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
183 _NSIG / 8);
184
5f5004df
UD
185 /* No need for the attribute anymore. */
186 (void) pthread_attr_destroy (&attr);
187
188 /* We have to make sure that after fork()ing a new helper thread can
189 be created. */
190 pthread_atfork (NULL, NULL, reset_helper_control);
191}