(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / timer_routines.c
blob23c800f98e4acb3c07d7483acfde97942faf7630
1 /* Copyright (C) 2003, 2004 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 #ifdef __NR_timer_create
31 /* Helper thread to call the user-provided function. */
32 static void *
33 timer_sigev_thread (void *arg)
35 /* The parent thread has all signals blocked. This is a bit
36 surprising for user code, although valid. We unblock all
37 signals. */
38 sigset_t ss;
39 sigemptyset (&ss);
40 INTERNAL_SYSCALL_DECL (err);
41 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
43 struct timer *tk = (struct timer *) arg;
45 /* Call the user-provided function. */
46 tk->thrfunc (tk->sival);
48 return NULL;
52 /* Helper function to support starting threads for SIGEV_THREAD. */
53 static void *
54 timer_helper_thread (void *arg)
56 /* Wait for the SIGTIMER signal and none else. */
57 sigset_t ss;
58 sigemptyset (&ss);
59 sigaddset (&ss, SIGTIMER);
61 /* Endless loop of waiting for signals. The loop is only ended when
62 the thread is canceled. */
63 while (1)
65 siginfo_t si;
67 /* sigwaitinfo cannot be used here, since it deletes
68 SIGCANCEL == SIGTIMER from the set. */
70 int oldtype = LIBC_CANCEL_ASYNC ();
72 /* XXX The size argument hopefully will have to be changed to the
73 real size of the user-level sigset_t. */
74 int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
75 _NSIG / 8);
77 LIBC_CANCEL_RESET (oldtype);
79 if (result > 0)
81 if (si.si_code == SI_TIMER)
83 struct timer *tk = (struct timer *) si.si_ptr;
85 /* That the signal we are waiting for. */
86 pthread_t th;
87 (void) pthread_create (&th, &tk->attr, timer_sigev_thread, tk);
89 else if (si.si_code == SI_TKILL)
90 /* The thread is canceled. */
91 pthread_exit (NULL);
97 /* Control variable for helper thread creation. */
98 pthread_once_t __helper_once attribute_hidden;
101 /* TID of the helper thread. */
102 pid_t __helper_tid attribute_hidden;
105 /* Reset variables so that after a fork a new helper thread gets started. */
106 static void
107 reset_helper_control (void)
109 __helper_once = PTHREAD_ONCE_INIT;
110 __helper_tid = 0;
114 void
115 attribute_hidden
116 __start_helper_thread (void)
118 /* The helper thread needs only very little resources
119 and should go away automatically when canceled. */
120 pthread_attr_t attr;
121 (void) pthread_attr_init (&attr);
122 (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
124 /* Block all signals in the helper thread. To do this thoroughly we
125 temporarily have to block all signals here. The helper can lose
126 wakeups if SIGCANCEL is not blocked throughout, but sigfillset omits
127 it. So, we add it back explicitly here. */
128 sigset_t ss;
129 sigset_t oss;
130 sigfillset (&ss);
131 __sigaddset (&ss, SIGCANCEL);
132 INTERNAL_SYSCALL_DECL (err);
133 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
135 /* Create the helper thread for this timer. */
136 pthread_t th;
137 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
138 if (res == 0)
139 /* We managed to start the helper thread. */
140 __helper_tid = ((struct pthread *) th)->tid;
142 /* Restore the signal mask. */
143 INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
144 _NSIG / 8);
146 /* No need for the attribute anymore. */
147 (void) pthread_attr_destroy (&attr);
149 /* We have to make sure that after fork()ing a new helper thread can
150 be created. */
151 pthread_atfork (NULL, NULL, reset_helper_control);
153 #endif
155 #ifndef __ASSUME_POSIX_TIMERS
156 # include <nptl/sysdeps/pthread/timer_routines.c>
157 #endif