1 /* Copyright (C) 2003, 2004, 2005, 2006, 2007 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 /* List of active SIGEV_THREAD timers. */
31 struct timer
*__active_timer_sigev_thread
;
32 /* Lock for the __active_timer_sigev_thread. */
33 pthread_mutex_t __active_timer_sigev_thread_lock
= PTHREAD_MUTEX_INITIALIZER
;
36 struct thread_start_data
38 void (*thrfunc
) (sigval_t
);
43 #ifdef __NR_timer_create
44 /* Helper thread to call the user-provided function. */
46 timer_sigev_thread (void *arg
)
48 /* The parent thread has all signals blocked. This is a bit
49 surprising for user code, although valid. We unblock all
53 INTERNAL_SYSCALL_DECL (err
);
54 INTERNAL_SYSCALL (rt_sigprocmask
, err
, 4, SIG_SETMASK
, &ss
, NULL
, _NSIG
/ 8);
56 struct thread_start_data
*td
= (struct thread_start_data
*) arg
;
58 void (*thrfunc
) (sigval_t
) = td
->thrfunc
;
59 sigval_t sival
= td
->sival
;
61 /* The TD object was allocated in timer_helper_thread. */
64 /* Call the user-provided function. */
71 /* Helper function to support starting threads for SIGEV_THREAD. */
73 timer_helper_thread (void *arg
)
75 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
79 __sigaddset (&ss
, SIGTIMER
);
81 /* Endless loop of waiting for signals. The loop is only ended when
82 the thread is canceled. */
87 /* sigwaitinfo cannot be used here, since it deletes
88 SIGCANCEL == SIGTIMER from the set. */
90 int oldtype
= LIBC_CANCEL_ASYNC ();
92 /* XXX The size argument hopefully will have to be changed to the
93 real size of the user-level sigset_t. */
94 int result
= INLINE_SYSCALL (rt_sigtimedwait
, 4, &ss
, &si
, NULL
,
97 LIBC_CANCEL_RESET (oldtype
);
101 if (si
.si_code
== SI_TIMER
)
103 struct timer
*tk
= (struct timer
*) si
.si_ptr
;
105 /* Check the timer is still used and will not go away
106 while we are reading the values here. */
107 pthread_mutex_lock (&__active_timer_sigev_thread_lock
);
109 struct timer
*runp
= __active_timer_sigev_thread
;
118 struct thread_start_data
*td
= malloc (sizeof (*td
));
120 /* There is not much we can do if the allocation fails. */
123 /* This is the signal we are waiting for. */
124 td
->thrfunc
= tk
->thrfunc
;
125 td
->sival
= tk
->sival
;
128 (void) pthread_create (&th
, &tk
->attr
,
129 timer_sigev_thread
, td
);
133 pthread_mutex_unlock (&__active_timer_sigev_thread_lock
);
135 else if (si
.si_code
== SI_TKILL
)
136 /* The thread is canceled. */
143 /* Control variable for helper thread creation. */
144 pthread_once_t __helper_once attribute_hidden
;
147 /* TID of the helper thread. */
148 pid_t __helper_tid attribute_hidden
;
151 /* Reset variables so that after a fork a new helper thread gets started. */
153 reset_helper_control (void)
155 __helper_once
= PTHREAD_ONCE_INIT
;
162 __start_helper_thread (void)
164 /* The helper thread needs only very little resources
165 and should go away automatically when canceled. */
167 (void) pthread_attr_init (&attr
);
168 (void) pthread_attr_setstacksize (&attr
, PTHREAD_STACK_MIN
);
170 /* Block all signals in the helper thread but SIGSETXID. To do this
171 thoroughly we temporarily have to block all signals here. The
172 helper can lose wakeups if SIGCANCEL is not blocked throughout,
173 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
178 __sigaddset (&ss
, SIGCANCEL
);
179 INTERNAL_SYSCALL_DECL (err
);
180 INTERNAL_SYSCALL (rt_sigprocmask
, err
, 4, SIG_SETMASK
, &ss
, &oss
, _NSIG
/ 8);
182 /* Create the helper thread for this timer. */
184 int res
= pthread_create (&th
, &attr
, timer_helper_thread
, NULL
);
186 /* We managed to start the helper thread. */
187 __helper_tid
= ((struct pthread
*) th
)->tid
;
189 /* Restore the signal mask. */
190 INTERNAL_SYSCALL (rt_sigprocmask
, err
, 4, SIG_SETMASK
, &oss
, NULL
,
193 /* No need for the attribute anymore. */
194 (void) pthread_attr_destroy (&attr
);
196 /* We have to make sure that after fork()ing a new helper thread can
198 pthread_atfork (NULL
, NULL
, reset_helper_control
);
202 #ifndef __ASSUME_POSIX_TIMERS
203 # include <nptl/sysdeps/pthread/timer_routines.c>