]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/timer_routines.c
Force building with -fno-common
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / timer_routines.c
CommitLineData
2b778ceb 1/* Copyright (C) 2003-2021 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>
3c79234c 24#include <pthreadP.h>
09402f5b
UD
25#include "kernel-posix-timers.h"
26
27
f160a450 28/* List of active SIGEV_THREAD timers. */
30639e79 29struct timer *__timer_active_sigev_thread;
f160a450 30
2c16cb88 31/* Lock for _timer_active_sigev_thread. */
30639e79 32pthread_mutex_t __timer_active_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
f160a450 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{
27d83441 45 __libc_signal_unblock_sigtimer (NULL);
fa0e226b 46
a6375d11 47 struct thread_start_data *td = (struct thread_start_data *) arg;
a6375d11
UD
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);
09402f5b
UD
53
54 /* Call the user-provided function. */
a6375d11 55 thrfunc (sival);
09402f5b
UD
56
57 return NULL;
58}
59
60
61/* Helper function to support starting threads for SIGEV_THREAD. */
2c16cb88 62static _Noreturn void *
5f5004df 63timer_helper_thread (void *arg)
09402f5b 64{
09402f5b
UD
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
2c16cb88 71 while (__sigwaitinfo (&sigtimer_set, &si) < 0);
27d83441 72 if (si.si_code == SI_TIMER)
09402f5b 73 {
27d83441
AZ
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. */
2c16cb88 78 __pthread_mutex_lock (&__timer_active_sigev_thread_lock);
a6375d11 79
2c16cb88 80 struct timer *runp = __timer_active_sigev_thread;
27d83441
AZ
81 while (runp != NULL)
82 if (runp == tk)
83 break;
84 else
85 runp = runp->next;
a6375d11 86
27d83441
AZ
87 if (runp != NULL)
88 {
89 struct thread_start_data *td = malloc (sizeof (*td));
f160a450 90
27d83441
AZ
91 /* There is not much we can do if the allocation fails. */
92 if (td != NULL)
f160a450 93 {
27d83441
AZ
94 /* This is the signal we are waiting for. */
95 td->thrfunc = tk->thrfunc;
96 td->sival = tk->sival;
f160a450 97
27d83441 98 pthread_t th;
2c16cb88 99 __pthread_create (&th, &tk->attr, timer_sigev_thread, td);
27d83441 100 }
e7608d77 101 }
27d83441 102
2c16cb88 103 __pthread_mutex_unlock (&__timer_active_sigev_thread_lock);
09402f5b
UD
104 }
105 }
106}
107
5f5004df
UD
108
109/* Control variable for helper thread creation. */
7c241325 110pthread_once_t __timer_helper_once = PTHREAD_ONCE_INIT;
5f5004df
UD
111
112
113/* TID of the helper thread. */
7c241325 114pid_t __timer_helper_tid;
5f5004df
UD
115
116
117/* Reset variables so that after a fork a new helper thread gets started. */
2c16cb88
FW
118void
119__timer_fork_subprocess (void)
5f5004df 120{
2c16cb88
FW
121 __timer_helper_once = PTHREAD_ONCE_INIT;
122 __timer_helper_tid = 0;
5f5004df
UD
123}
124
125
126void
2c16cb88 127__timer_start_helper_thread (void)
5f5004df
UD
128{
129 /* The helper thread needs only very little resources
130 and should go away automatically when canceled. */
131 pthread_attr_t attr;
2c16cb88
FW
132 __pthread_attr_init (&attr);
133 __pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
5f5004df 134
ba9f6ee9 135 /* Block all signals in the helper thread but SIGSETXID. */
fa0e226b 136 sigset_t ss;
ba9f6ee9
FW
137 __sigfillset (&ss);
138 __sigdelset (&ss, SIGSETXID);
139 int res = __pthread_attr_setsigmask_internal (&attr, &ss);
140 if (res != 0)
141 {
2c16cb88 142 __pthread_attr_destroy (&attr);
ba9f6ee9
FW
143 return;
144 }
fa0e226b 145
5f5004df
UD
146 /* Create the helper thread for this timer. */
147 pthread_t th;
2c16cb88 148 res = __pthread_create (&th, &attr, timer_helper_thread, NULL);
5f5004df
UD
149 if (res == 0)
150 /* We managed to start the helper thread. */
2c16cb88 151 __timer_helper_tid = ((struct pthread *) th)->tid;
5f5004df
UD
152
153 /* No need for the attribute anymore. */
2c16cb88 154 __pthread_attr_destroy (&attr);
5f5004df 155}