scratch_buffer: Suppress truncation warning on 32-bit
[glibc.git] / nptl / tst-signal3.c
blob90b2e71b28aa99fd8524ae5463e8b599a9dc01e7
1 /* Copyright (C) 2002-2015 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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <semaphore.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
28 #ifdef SIGRTMIN
30 /* Number of different signals to use. Also is the number of threads. */
31 # define N 10
32 /* Maximum number of threads in flight at any one time. */
33 # define INFLIGHT 5
34 /* Number of signals sent in total. */
35 # define ROUNDS 10000
38 static int received[N][N];
39 static int nsig[N];
40 static pthread_t th[N];
41 static sem_t sem;
42 static pthread_mutex_t lock[N];
43 static pthread_t th_main;
44 static int sig0;
46 static void
47 handler (int sig)
49 int i;
50 for (i = 0; i < N; ++i)
51 if (pthread_equal (pthread_self (), th[i]))
52 break;
54 if (i == N)
56 if (pthread_equal (pthread_self (), th_main))
57 puts ("signal received by main thread");
58 else
59 printf ("signal received by unknown thread (%lx)\n",
60 (unsigned long int) pthread_self ());
61 exit (1);
64 ++received[i][sig - sig0];
66 sem_post (&sem);
70 static void *
71 tf (void *arg)
73 int idx = (long int) arg;
75 sigset_t ss;
76 sigemptyset (&ss);
78 int i;
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);
85 exit (1);
88 pthread_mutex_lock (&lock[idx]);
90 return NULL;
94 static int
95 do_test (void)
97 /* Block all signals. */
98 sigset_t ss;
99 sigfillset (&ss);
101 th_main = pthread_self ();
103 sig0 = SIGRTMIN;
105 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
107 puts ("1st pthread_sigmask failed");
108 exit (1);
111 /* Install the handler. */
112 int i;
113 for (i = 0; i < N; ++i)
115 struct sigaction sa =
117 .sa_handler = handler,
118 .sa_flags = 0
120 sigfillset (&sa.sa_mask);
122 if (sigaction (sig0 + i, &sa, NULL) != 0)
124 printf ("sigaction for signal %d failed\n", i);
125 exit (1);
129 if (sem_init (&sem, 0, INFLIGHT) != 0)
131 puts ("sem_init failed");
132 exit (1);
135 pthread_attr_t a;
137 if (pthread_attr_init (&a) != 0)
139 puts ("attr_init failed");
140 exit (1);
143 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
145 puts ("attr_setstacksize failed");
146 return 1;
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);
164 exit (1);
168 if (pthread_attr_destroy (&a) != 0)
170 puts ("attr_destroy failed");
171 exit (1);
174 int result = 0;
175 unsigned int r = 42;
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);
183 exit (1);
186 int s = rand_r (&r) % N;
188 kill (pid, sig0 + s);
191 void *status;
192 for (i = 0; i < N; ++i)
194 if (pthread_mutex_unlock (&lock[i]) != 0)
196 printf ("unlock %d failed\n", i);
197 exit (1);
200 if (pthread_join (th[i], &status) != 0)
202 printf ("join %d failed\n", i);
203 result = 1;
205 else if (status != NULL)
207 printf ("%d: result != NULL\n", i);
208 result = 1;
212 int total = 0;
213 for (i = 0; i < N; ++i)
215 int j;
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);
224 result = 1;
228 if (total != ROUNDS)
230 printf ("total number of handled signals is %d, expected %d\n",
231 total, ROUNDS);
232 result = 1;
235 printf ("A total of %d signals sent and received\n", total);
236 for (i = 0; i < N; ++i)
238 printf ("thread %2d:", i);
240 int j;
241 for (j = 0; j <= i; ++j)
243 printf (" %5d", received[i][j]);
244 nsig[j] += received[i][j];
247 putchar ('\n');
251 printf ("\nTotal :");
252 for (i = 0; i < N; ++i)
253 printf (" %5d", nsig[i]);
254 putchar ('\n');
256 return result;
259 # define TIMEOUT 10
260 # define TEST_FUNCTION do_test ()
262 #else
263 # define TEST_FUNCTION 0
264 #endif
266 #include "../test-skeleton.c"