1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <semaphore.h>
29 /* Number of different signalss to use. Also is the number of
32 /* Maximum number of threads in flight at any one time. */
34 /* Number of signals sent in total. */
38 static int received[N][N];
40 static pthread_t th[N];
42 static pthread_mutex_t lock[N];
43 static pthread_t th_main;
50 for (i = 0; i < N; ++i)
51 if (pthread_equal (pthread_self (), th[i]))
56 if (pthread_equal (pthread_self (), th_main))
57 puts ("signal received by main thread");
59 printf ("signal received by unknown thread (%lx)\n",
60 (unsigned long int) pthread_self ());
64 ++received[i][sig - sig0];
73 int idx = (long int) arg;
79 for (i = 0; i <= idx; ++i)
80 sigaddset (&ss, sig0 + i);
82 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
84 printf ("thread %d: pthread_sigmask failed\n", i);
88 pthread_mutex_lock (&lock[idx]);
97 /* Block all signals. */
101 th_main = pthread_self ();
105 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
107 puts ("1st pthread_sigmask failed");
111 /* Install the handler. */
113 for (i = 0; i < N; ++i)
115 struct sigaction sa =
117 .sa_handler = handler,
120 sigfillset (&sa.sa_mask);
122 if (sigaction (sig0 + i, &sa, NULL) != 0)
124 printf ("sigaction for signal %d failed\n", i);
129 if (sem_init (&sem, 0, INFLIGHT) != 0)
131 puts ("sem_init failed");
137 if (pthread_attr_init (&a) != 0)
139 puts ("attr_init failed");
143 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
145 puts ("attr_setstacksize failed");
149 for (i = 0; i < N; ++i)
151 if (pthread_mutex_init (&lock[i], NULL) != 0)
153 printf ("mutex_init[%d] failed\n", i);
156 if (pthread_mutex_lock (&lock[i]) != 0)
158 printf ("mutex_lock[%d] failed\n", i);
161 if (pthread_create (&th[i], &a, tf, (void *) (long int) i) != 0)
163 printf ("create of thread %d failed\n", i);
168 if (pthread_attr_destroy (&a) != 0)
170 puts ("attr_destroy failed");
176 pid_t pid = getpid ();
178 for (i = 0; i < ROUNDS; ++i)
180 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
182 printf ("sem_wait round %d failed: %m\n", i);
186 int s = rand_r (&r) % N;
188 kill (pid, sig0 + s);
192 for (i = 0; i < N; ++i)
194 if (pthread_mutex_unlock (&lock[i]) != 0)
196 printf ("unlock %d failed\n", i);
200 if (pthread_join (th[i], &status) != 0)
202 printf ("join %d failed\n", i);
205 else if (status != NULL)
207 printf ("%d: result != NULL\n", i);
213 for (i = 0; i < N; ++i)
217 for (j = 0; j <= i; ++j)
218 total += received[i][j];
220 for (j = i + 1; j < N; ++j)
221 if (received[i][j] != 0)
223 printf ("thread %d received signal SIGRTMIN+%d\n", i, j);
230 printf ("total number of handled signals is %d, expected %d\n",
235 printf ("A total of %d signals sent and received\n", total);
236 for (i = 0; i < N; ++i)
238 printf ("thread %2d:", i);
241 for (j = 0; j <= i; ++j)
243 printf (" %5d", received[i][j]);
244 nsig[j] += received[i][j];
251 printf ("\nTotal :");
252 for (i = 0; i < N; ++i)
253 printf (" %5d", nsig[i]);
260 #define TEST_FUNCTION do_test ()
261 #include "../test-skeleton.c"