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. */
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. */
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
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
);
52 /* Helper function to support starting threads for SIGEV_THREAD. */
54 timer_helper_thread (void *arg
)
56 /* Wait for the SIGTIMER signal and none else. */
59 sigaddset (&ss
, SIGTIMER
);
61 /* Endless loop of waiting for signals. The loop is only ended when
62 the thread is canceled. */
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
,
77 LIBC_CANCEL_RESET (oldtype
);
81 if (si
.si_code
== SI_TIMER
)
83 struct timer
*tk
= (struct timer
*) si
.si_ptr
;
85 /* That the signal we are waiting for. */
87 (void) pthread_create (&th
, &tk
->attr
, timer_sigev_thread
, tk
);
89 else if (si
.si_code
== SI_TKILL
)
90 /* The thread is canceled. */
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. */
107 reset_helper_control (void)
109 __helper_once
= PTHREAD_ONCE_INIT
;
116 __start_helper_thread (void)
118 /* The helper thread needs only very little resources
119 and should go away automatically when canceled. */
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. */
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. */
137 int res
= pthread_create (&th
, &attr
, timer_helper_thread
, NULL
);
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
,
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
151 pthread_atfork (NULL
, NULL
, reset_helper_control
);
155 #ifndef __ASSUME_POSIX_TIMERS
156 # include <nptl/sysdeps/pthread/timer_routines.c>