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, see
17 <http://www.gnu.org/licenses/>. */
21 #include <semaphore.h>
28 /* Number of different signalss to use. Also is the number of
31 /* Maximum number of threads in flight at any one time. */
33 /* Number of signals sent in total. */
37 static int received[N][N];
39 static pthread_t th[N];
41 static pthread_mutex_t lock[N];
42 static pthread_t th_main;
49 for (i = 0; i < N; ++i)
50 if (pthread_equal (pthread_self (), th[i]))
55 if (pthread_equal (pthread_self (), th_main))
56 puts ("signal received by main thread");
58 printf ("signal received by unknown thread (%lx)\n",
59 (unsigned long int) pthread_self ());
63 ++received[i][sig - sig0];
72 int idx = (long int) arg;
78 for (i = 0; i <= idx; ++i)
79 sigaddset (&ss, sig0 + i);
81 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
83 printf ("thread %d: pthread_sigmask failed\n", i);
87 pthread_mutex_lock (&lock[idx]);
96 /* Block all signals. */
100 th_main = pthread_self ();
104 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
106 puts ("1st pthread_sigmask failed");
110 /* Install the handler. */
112 for (i = 0; i < N; ++i)
114 struct sigaction sa =
116 .sa_handler = handler,
119 sigfillset (&sa.sa_mask);
121 if (sigaction (sig0 + i, &sa, NULL) != 0)
123 printf ("sigaction for signal %d failed\n", i);
128 if (sem_init (&sem, 0, INFLIGHT) != 0)
130 puts ("sem_init failed");
136 if (pthread_attr_init (&a) != 0)
138 puts ("attr_init failed");
142 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
144 puts ("attr_setstacksize failed");
148 for (i = 0; i < N; ++i)
150 if (pthread_mutex_init (&lock[i], NULL) != 0)
152 printf ("mutex_init[%d] failed\n", i);
155 if (pthread_mutex_lock (&lock[i]) != 0)
157 printf ("mutex_lock[%d] failed\n", i);
160 if (pthread_create (&th[i], &a, tf, (void *) (long int) i) != 0)
162 printf ("create of thread %d failed\n", i);
167 if (pthread_attr_destroy (&a) != 0)
169 puts ("attr_destroy failed");
175 pid_t pid = getpid ();
177 for (i = 0; i < ROUNDS; ++i)
179 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
181 printf ("sem_wait round %d failed: %m\n", i);
185 int s = rand_r (&r) % N;
187 kill (pid, sig0 + s);
191 for (i = 0; i < N; ++i)
193 if (pthread_mutex_unlock (&lock[i]) != 0)
195 printf ("unlock %d failed\n", i);
199 if (pthread_join (th[i], &status) != 0)
201 printf ("join %d failed\n", i);
204 else if (status != NULL)
206 printf ("%d: result != NULL\n", i);
212 for (i = 0; i < N; ++i)
216 for (j = 0; j <= i; ++j)
217 total += received[i][j];
219 for (j = i + 1; j < N; ++j)
220 if (received[i][j] != 0)
222 printf ("thread %d received signal SIGRTMIN+%d\n", i, j);
229 printf ("total number of handled signals is %d, expected %d\n",
234 printf ("A total of %d signals sent and received\n", total);
235 for (i = 0; i < N; ++i)
237 printf ("thread %2d:", i);
240 for (j = 0; j <= i; ++j)
242 printf (" %5d", received[i][j]);
243 nsig[j] += received[i][j];
250 printf ("\nTotal :");
251 for (i = 0; i < N; ++i)
252 printf (" %5d", nsig[i]);
259 #define TEST_FUNCTION do_test ()
260 #include "../test-skeleton.c"