2.5-18.1
[glibc.git] / nptl / sysdeps / unix / sysv / linux / timer_routines.c
bloba5eb44225115ddaf2f6be43152dfa54e44f3a27f
1 /* Copyright (C) 2003, 2004, 2005, 2006 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <setjmp.h>
22 #include <signal.h>
23 #include <stdbool.h>
24 #include <sysdep.h>
25 #include <kernel-features.h>
26 #include <nptl/pthreadP.h>
27 #include "kernel-posix-timers.h"
30 struct thread_start_data
32 void (*thrfunc) (sigval_t);
33 sigval_t sival;
37 #ifdef __NR_timer_create
38 /* Helper thread to call the user-provided function. */
39 static void *
40 timer_sigev_thread (void *arg)
42 /* The parent thread has all signals blocked. This is a bit
43 surprising for user code, although valid. We unblock all
44 signals. */
45 sigset_t ss;
46 sigemptyset (&ss);
47 INTERNAL_SYSCALL_DECL (err);
48 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
50 struct thread_start_data *td = (struct thread_start_data *) arg;
52 void (*thrfunc) (sigval_t) = td->thrfunc;
53 sigval_t sival = td->sival;
55 /* The TD object was allocated in timer_helper_thread. */
56 free (td);
58 /* Call the user-provided function. */
59 thrfunc (sival);
61 return NULL;
65 /* Helper function to support starting threads for SIGEV_THREAD. */
66 static void *
67 timer_helper_thread (void *arg)
69 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
70 none else. */
71 sigset_t ss;
72 sigemptyset (&ss);
73 __sigaddset (&ss, SIGTIMER);
75 /* Endless loop of waiting for signals. The loop is only ended when
76 the thread is canceled. */
77 while (1)
79 siginfo_t si;
81 /* sigwaitinfo cannot be used here, since it deletes
82 SIGCANCEL == SIGTIMER from the set. */
84 int oldtype = LIBC_CANCEL_ASYNC ();
86 /* XXX The size argument hopefully will have to be changed to the
87 real size of the user-level sigset_t. */
88 int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
89 _NSIG / 8);
91 LIBC_CANCEL_RESET (oldtype);
93 if (result > 0)
95 if (si.si_code == SI_TIMER)
97 struct timer *tk = (struct timer *) si.si_ptr;
98 struct thread_start_data *td = malloc (sizeof (*td));
100 /* There is not much we can do if the allocation fails. */
101 if (td != NULL)
103 /* That is the signal we are waiting for. */
104 td->thrfunc = tk->thrfunc;
105 td->sival = tk->sival;
107 pthread_t th;
108 (void) pthread_create (&th, &tk->attr, timer_sigev_thread,
109 td);
112 else if (si.si_code == SI_TKILL)
113 /* The thread is canceled. */
114 pthread_exit (NULL);
120 /* Control variable for helper thread creation. */
121 pthread_once_t __helper_once attribute_hidden;
124 /* TID of the helper thread. */
125 pid_t __helper_tid attribute_hidden;
128 /* Reset variables so that after a fork a new helper thread gets started. */
129 static void
130 reset_helper_control (void)
132 __helper_once = PTHREAD_ONCE_INIT;
133 __helper_tid = 0;
137 void
138 attribute_hidden
139 __start_helper_thread (void)
141 /* The helper thread needs only very little resources
142 and should go away automatically when canceled. */
143 pthread_attr_t attr;
144 (void) pthread_attr_init (&attr);
145 (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
147 /* Block all signals in the helper thread but SIGSETXID. To do this
148 thoroughly we temporarily have to block all signals here. The
149 helper can lose wakeups if SIGCANCEL is not blocked throughout,
150 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
151 explicitly here. */
152 sigset_t ss;
153 sigset_t oss;
154 sigfillset (&ss);
155 __sigaddset (&ss, SIGCANCEL);
156 INTERNAL_SYSCALL_DECL (err);
157 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
159 /* Create the helper thread for this timer. */
160 pthread_t th;
161 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
162 if (res == 0)
163 /* We managed to start the helper thread. */
164 __helper_tid = ((struct pthread *) th)->tid;
166 /* Restore the signal mask. */
167 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
168 _NSIG / 8);
170 /* No need for the attribute anymore. */
171 (void) pthread_attr_destroy (&attr);
173 /* We have to make sure that after fork()ing a new helper thread can
174 be created. */
175 pthread_atfork (NULL, NULL, reset_helper_control);
177 #endif
179 #ifndef __ASSUME_POSIX_TIMERS
180 # include <nptl/sysdeps/pthread/timer_routines.c>
181 #endif