/[glibc-bsd]/trunk/glibc-ports/kfreebsd/linuxthreads/timer_routines.c
ViewVC logotype

Contents of /trunk/glibc-ports/kfreebsd/linuxthreads/timer_routines.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4187 - (show annotations) (download)
Tue Apr 10 10:13:35 2012 UTC (14 months, 1 week ago) by rmh
File MIME type: text/plain
File size: 6271 byte(s)
Revert commit 4170, based on advice from Petr Salinger:

While in general it is a correct change, the situation inside library implementing libc/libpthread/librt is slightly different.

linuxthreads/signals.c:

int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)

   /* Don't allow __pthread_sig_restart to be unmasked.
      Don't allow __pthread_sig_cancel to be masked. */


At least inside __start_helper_thread __sigprocmask() should be used, othewise:

kfreebsd/linuxthreads/timer_routines.c:

 /* Block all signals in the helper thread but SIGSETXID.  To do this
    thoroughly we temporarily have to block all signals here.  The
    helper can lose wakeups if SIGCANCEL is not blocked throughout,
    but sigfillset omits it SIGSETXID.  So, we add SIGCANCEL back
    explicitly here.  */

1 /* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2012 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <errno.h>
21 #include <setjmp.h>
22 #include <signal.h>
23 #include <stdbool.h>
24 #include <sysdep.h>
25 #include <sys/_types.h> /* __lwpid_t */
26 #include <atomic.h>
27 #include <kernel-features.h>
28 #include <linuxthreads/internals.h> /* LIBC_CANCEL_ASYNC */
29 #include <semaphore.h>
30 #include "kernel-posix-timers.h"
31
32 /* NPTL/Linux simply casts "timer_t" to "struct timer *", but on
33 kFreeBSD timer_t may not be large enough to hold a pointer.
34 So we store the pointers here... (sigh) */
35 struct timer *__all_timers[TIMER_MAX];
36
37 /* List of active SIGEV_THREAD timers. */
38 struct timer *__active_timer_sigev_thread;
39 /* Lock for the __active_timer_sigev_thread. */
40 pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
41
42
43 struct thread_start_data
44 {
45 void (*thrfunc) (sigval_t);
46 sigval_t sival;
47 };
48
49 /* TID of the helper thread. */
50 __lwpid_t __helper_tid attribute_hidden;
51 sem_t __helper_tid_semaphore attribute_hidden;
52
53 #ifdef SYS_ktimer_create
54 /* Helper thread to call the user-provided function. */
55 static void *
56 timer_sigev_thread (void *arg)
57 {
58 /* The parent thread has all signals blocked. This is a bit
59 surprising for user code, although valid. We unblock all
60 signals. */
61 sigset_t ss;
62 sigemptyset (&ss);
63 sigprocmask (SIG_SETMASK, &ss, NULL);
64
65 struct thread_start_data *td = (struct thread_start_data *) arg;
66
67 void (*thrfunc) (sigval_t) = td->thrfunc;
68 sigval_t sival = td->sival;
69
70 /* The TD object was allocated in timer_helper_thread. */
71 free (td);
72
73 /* Call the user-provided function. */
74 thrfunc (sival);
75
76 return NULL;
77 }
78
79
80 /* Helper function to support starting threads for SIGEV_THREAD. */
81 static void *
82 timer_helper_thread (void *arg)
83 {
84 /* Wait for the SIGTIMER signal, allowing the setXid signal, and
85 none else. */
86 sigset_t ss;
87 sigemptyset (&ss);
88 __sigaddset (&ss, SIGTIMER);
89
90 syscall (SYS_thr_self, &__helper_tid);
91 sem_post (&__helper_tid_semaphore);
92
93 /* Endless loop of waiting for signals. The loop is only ended when
94 the thread is canceled. */
95 while (1)
96 {
97 siginfo_t si;
98
99 /* sigwaitinfo cannot be used here, since it deletes
100 SIGCANCEL == SIGTIMER from the set. */
101
102 int oldtype = LIBC_CANCEL_ASYNC ();
103
104 /* XXX The size argument hopefully will have to be changed to the
105 real size of the user-level sigset_t. */
106 int result = sigtimedwait (&ss, &si, NULL);
107
108 LIBC_CANCEL_RESET (oldtype);
109
110 if (result > 0)
111 {
112 if (si.si_code == SI_TIMER)
113 {
114 struct timer *tk = (struct timer *) si.si_value.sival_ptr;
115
116 /* Check the timer is still used and will not go away
117 while we are reading the values here. */
118 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
119
120 struct timer *runp = __active_timer_sigev_thread;
121 while (runp != NULL)
122 if (runp == tk)
123 break;
124 else
125 runp = runp->next;
126
127 if (runp != NULL)
128 {
129 struct thread_start_data *td = malloc (sizeof (*td));
130
131 /* There is not much we can do if the allocation fails. */
132 if (td != NULL)
133 {
134 /* This is the signal we are waiting for. */
135 td->thrfunc = tk->thrfunc;
136 td->sival = tk->sival;
137
138 pthread_t th;
139 (void) pthread_create (&th, &tk->attr,
140 timer_sigev_thread, td);
141 }
142 }
143
144 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
145 }
146 else if (si.si_code == SI_LWP
147 /* Backward compatibility (see rev 211732 in -CURRENT). */
148 || si.si_code == SI_USER)
149 /* The thread is canceled. */
150 pthread_exit (NULL);
151 }
152 }
153 }
154
155
156 /* Control variable for helper thread creation. */
157 pthread_once_t __helper_once attribute_hidden;
158
159
160 /* Reset variables so that after a fork a new helper thread gets started. */
161 static void
162 reset_helper_control (void)
163 {
164 __helper_once = PTHREAD_ONCE_INIT;
165 __helper_tid = 0;
166 sem_destroy (&__helper_tid_semaphore);
167 }
168
169
170 void
171 attribute_hidden
172 __start_helper_thread (void)
173 {
174 /* The helper thread needs only very little resources
175 and should go away automatically when canceled. */
176 pthread_attr_t attr;
177 (void) pthread_attr_init (&attr);
178 (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
179
180 /* Block all signals in the helper thread but SIGSETXID. To do this
181 thoroughly we temporarily have to block all signals here. The
182 helper can lose wakeups if SIGCANCEL is not blocked throughout,
183 but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
184 explicitly here. */
185 sigset_t ss;
186 sigset_t oss;
187 sigfillset (&ss);
188 __sigaddset (&ss, SIGCANCEL);
189 sigprocmask (SIG_SETMASK, &ss, &oss);
190
191 sem_init (&__helper_tid_semaphore, 0, 0);
192
193 /* Create the helper thread for this timer. */
194 pthread_t th;
195 int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
196 if (res == 0)
197 /* We managed to start the helper thread. */
198 sem_wait (&__helper_tid_semaphore);
199
200 /* Restore the signal mask. */
201 sigprocmask (SIG_SETMASK, &oss, NULL);
202
203 /* No need for the attribute anymore. */
204 (void) pthread_attr_destroy (&attr);
205
206 /* We have to make sure that after fork()ing a new helper thread can
207 be created. */
208 pthread_atfork (NULL, NULL, reset_helper_control);
209 }
210 #endif
211
212 #ifndef __ASSUME_POSIX_TIMERS
213 # include <linuxthreads/sysdeps/pthread/timer_routines.c>
214 #endif

  ViewVC Help
Powered by ViewVC 1.1.5