1/* Copyright (C) 2003-2020 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 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <setjmp.h>
21#include <signal.h>
22#include <stdbool.h>
23#include <sysdep-cancel.h>
24#include <nptl/pthreadP.h>
25#include "kernel-posix-timers.h"
26
27
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
34struct thread_start_data
35{
36 void (*thrfunc) (sigval_t);
37 sigval_t sival;
38};
39
40
41/* Helper thread to call the user-provided function. */
42static void *
43timer_sigev_thread (void *arg)
44{
45 __libc_signal_unblock_sigtimer (NULL);
46
47 struct thread_start_data *td = (struct thread_start_data *) arg;
48 void (*thrfunc) (sigval_t) = td->thrfunc;
49 sigval_t sival = td->sival;
50
51 /* The TD object was allocated in timer_helper_thread. */
52 free (td);
53
54 /* Call the user-provided function. */
55 thrfunc (sival);
56
57 return NULL;
58}
59
60
61/* Helper function to support starting threads for SIGEV_THREAD. */
62static void *
63timer_helper_thread (void *arg)
64{
65 /* Endless loop of waiting for signals. The loop is only ended when
66 the thread is canceled. */
67 while (1)
68 {
69 siginfo_t si;
70
71 while (sigwaitinfo (&sigtimer_set, &si) < 0);
72 if (si.si_code == SI_TIMER)
73 {
74 struct timer *tk = (struct timer *) si.si_ptr;
75
76 /* Check the timer is still used and will not go away
77 while we are reading the values here. */
78 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
79
80 struct timer *runp = __active_timer_sigev_thread;
81 while (runp != NULL)
82 if (runp == tk)
83 break;
84 else
85 runp = runp->next;
86
87 if (runp != NULL)
88 {
89 struct thread_start_data *td = malloc (sizeof (*td));
90
91 /* There is not much we can do if the allocation fails. */
92 if (td != NULL)
93 {
94 /* This is the signal we are waiting for. */
95 td->thrfunc = tk->thrfunc;
96 td->sival = tk->sival;
97
98 pthread_t th;
99 pthread_create (&th, &tk->attr, timer_sigev_thread, td);
100 }
101 }
102
103 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
104 }
105 else if (si.si_code == SI_TKILL)
106 /* The thread is canceled. */
107 pthread_exit (NULL);
108 }
109}
110
111
112/* Control variable for helper thread creation. */
113pthread_once_t __helper_once attribute_hidden;
114
115
116/* TID of the helper thread. */
117pid_t __helper_tid attribute_hidden;
118
119
120/* Reset variables so that after a fork a new helper thread gets started. */
121static void
122reset_helper_control (void)
123{
124 __helper_once = PTHREAD_ONCE_INIT;
125 __helper_tid = 0;
126}
127
128
129void
130attribute_hidden
131__start_helper_thread (void)
132{
133 /* The helper thread needs only very little resources
134 and should go away automatically when canceled. */
135 pthread_attr_t attr;
136 (void) pthread_attr_init (&attr);
137 (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
138
139 /* Block all signals in the helper thread but SIGSETXID. */
140 sigset_t ss;
141 __sigfillset (&ss);
142 __sigdelset (&ss, SIGSETXID);
143 int res = __pthread_attr_setsigmask_internal (&attr, &ss);
144 if (res != 0)
145 {
146 pthread_attr_destroy (&attr);
147 return;
148 }
149
150 /* Create the helper thread for this timer. */
151 pthread_t th;
152 res = pthread_create (&th, &attr, timer_helper_thread, NULL);
153 if (res == 0)
154 /* We managed to start the helper thread. */
155 __helper_tid = ((struct pthread *) th)->tid;
156
157 /* No need for the attribute anymore. */
158 (void) pthread_attr_destroy (&attr);
159
160 /* We have to make sure that after fork()ing a new helper thread can
161 be created. */
162 pthread_atfork (NULL, NULL, reset_helper_control);
163}
164