OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / nptl / sysdeps / unix / sysv / linux / timer_routines.c
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.
4
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.
9
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.
14
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
17    not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <setjmp.h>
21 #include <signal.h>
22 #include <stdbool.h>
23 #include <sysdep.h>
24 #include <bits/kernel-features.h>
25 #include <pthreadP.h>
26 #include "kernel-posix-timers.h"
27
28
29 /* List of active SIGEV_THREAD timers.  */
30 struct timer *__active_timer_sigev_thread;
31 /* Lock for the __active_timer_sigev_thread.  */
32 pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
33
34
35 struct thread_start_data
36 {
37   void (*thrfunc) (sigval_t);
38   sigval_t sival;
39 };
40
41
42 #ifdef __NR_timer_create
43 /* Helper thread to call the user-provided function.  */
44 static void *
45 timer_sigev_thread (void *arg)
46 {
47   /* The parent thread has all signals blocked.  This is a bit
48      surprising for user code, although valid.  We unblock all
49      signals.  */
50   sigset_t ss;
51   __sigemptyset (&ss);
52   INTERNAL_SYSCALL_DECL (err);
53   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
54
55   struct thread_start_data *td = (struct thread_start_data *) arg;
56
57   void (*thrfunc) (sigval_t) = td->thrfunc;
58   sigval_t sival = td->sival;
59
60   /* The TD object was allocated in timer_helper_thread.  */
61   free (td);
62
63   /* Call the user-provided function.  */
64   thrfunc (sival);
65
66   return NULL;
67 }
68
69
70 /* Helper function to support starting threads for SIGEV_THREAD.  */
71 static attribute_noreturn void *
72 timer_helper_thread (void *arg)
73 {
74   /* Wait for the SIGTIMER signal, allowing the setXid signal, and
75      none else.  */
76   sigset_t ss;
77   __sigemptyset (&ss);
78   __sigaddset (&ss, SIGTIMER);
79
80   /* Endless loop of waiting for signals.  The loop is only ended when
81      the thread is canceled.  */
82   while (1)
83     {
84       siginfo_t si;
85
86       /* sigwaitinfo cannot be used here, since it deletes
87          SIGCANCEL == SIGTIMER from the set.  */
88
89       int oldtype = LIBC_CANCEL_ASYNC ();
90
91       /* XXX The size argument hopefully will have to be changed to the
92          real size of the user-level sigset_t.  */
93       int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
94                                    _NSIG / 8);
95
96       LIBC_CANCEL_RESET (oldtype);
97
98       if (result > 0)
99         {
100           if (si.si_code == SI_TIMER)
101             {
102               struct timer *tk = (struct timer *) si.si_ptr;
103
104               /* Check the timer is still used and will not go away
105                  while we are reading the values here.  */
106               pthread_mutex_lock (&__active_timer_sigev_thread_lock);
107
108               struct timer *runp = __active_timer_sigev_thread;
109               while (runp != NULL)
110                 if (runp == tk)
111                   break;
112                 else
113                   runp = runp->next;
114
115               if (runp != NULL)
116                 {
117                   struct thread_start_data *td = malloc (sizeof (*td));
118
119                   /* There is not much we can do if the allocation fails.  */
120                   if (td != NULL)
121                     {
122                       /* This is the signal we are waiting for.  */
123                       td->thrfunc = tk->thrfunc;
124                       td->sival = tk->sival;
125
126                       pthread_t th;
127                       (void) pthread_create (&th, &tk->attr,
128                                              timer_sigev_thread, td);
129                     }
130                 }
131
132               pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
133             }
134           else if (si.si_code == SI_TKILL)
135             /* The thread is canceled.  */
136             pthread_exit (NULL);
137         }
138     }
139 }
140
141
142 /* Control variable for helper thread creation.  */
143 pthread_once_t __helper_once attribute_hidden;
144
145
146 /* TID of the helper thread.  */
147 pid_t __helper_tid attribute_hidden;
148
149
150 /* Reset variables so that after a fork a new helper thread gets started.  */
151 static void
152 reset_helper_control (void)
153 {
154   __helper_once = PTHREAD_ONCE_INIT;
155   __helper_tid = 0;
156 }
157
158
159 void
160 attribute_hidden
161 __start_helper_thread (void)
162 {
163   /* The helper thread needs only very little resources
164      and should go away automatically when canceled.  */
165   pthread_attr_t attr;
166   (void) pthread_attr_init (&attr);
167   (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
168
169   /* Block all signals in the helper thread but SIGSETXID.  To do this
170      thoroughly we temporarily have to block all signals here.  The
171      helper can lose wakeups if SIGCANCEL is not blocked throughout,
172      but sigfillset omits it SIGSETXID.  So, we add SIGCANCEL back
173      explicitly here.  */
174   sigset_t ss;
175   sigset_t oss;
176   sigfillset (&ss);
177   __sigaddset (&ss, SIGCANCEL);
178   INTERNAL_SYSCALL_DECL (err);
179   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
180
181   /* Create the helper thread for this timer.  */
182   pthread_t th;
183   int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
184   if (res == 0)
185     /* We managed to start the helper thread.  */
186     __helper_tid = ((struct pthread *) th)->tid;
187
188   /* Restore the signal mask.  */
189   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
190                     _NSIG / 8);
191
192   /* No need for the attribute anymore.  */
193   (void) pthread_attr_destroy (&attr);
194
195   /* We have to make sure that after fork()ing a new helper thread can
196      be created.  */
197   pthread_atfork (NULL, NULL, reset_helper_control);
198 }
199 #endif
200
201 #ifndef __ASSUME_POSIX_TIMERS
202 # include <nptl/sysdeps/pthread/timer_routines.c>
203 #endif