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. */
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
);
37 #ifdef __NR_timer_create
38 /* Helper thread to call the user-provided function. */
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
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. */
58 /* Call the user-provided function. */
65 /* Helper function to support starting threads for SIGEV_THREAD. */
67 timer_helper_thread (void *arg
)
69 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
73 __sigaddset (&ss
, SIGTIMER
);
75 /* Endless loop of waiting for signals. The loop is only ended when
76 the thread is canceled. */
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
,
91 LIBC_CANCEL_RESET (oldtype
);
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. */
103 /* That is the signal we are waiting for. */
104 td
->thrfunc
= tk
->thrfunc
;
105 td
->sival
= tk
->sival
;
108 (void) pthread_create (&th
, &tk
->attr
, timer_sigev_thread
,
112 else if (si
.si_code
== SI_TKILL
)
113 /* The thread is canceled. */
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. */
130 reset_helper_control (void)
132 __helper_once
= PTHREAD_ONCE_INIT
;
139 __start_helper_thread (void)
141 /* The helper thread needs only very little resources
142 and should go away automatically when canceled. */
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
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. */
161 int res
= pthread_create (&th
, &attr
, timer_helper_thread
, NULL
);
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
,
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
175 pthread_atfork (NULL
, NULL
, reset_helper_control
);
179 #ifndef __ASSUME_POSIX_TIMERS
180 # include <nptl/sysdeps/pthread/timer_routines.c>