Fix sysdeps/unix/sysv/linux/arm/libc-do-syscall.S warning.
[glibc.git] / nptl / tst-signal3.c
blobd76ab5e3bd062bb7639a140f11d1c078115cabcf
1 /* Copyright (C) 2002-2014 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 /* Number of different signalss to use. Also is the number of
29 threads. */
30 #define N 10
31 /* Maximum number of threads in flight at any one time. */
32 #define INFLIGHT 5
33 /* Number of signals sent in total. */
34 #define ROUNDS 10000
37 static int received[N][N];
38 static int nsig[N];
39 static pthread_t th[N];
40 static sem_t sem;
41 static pthread_mutex_t lock[N];
42 static pthread_t th_main;
43 static int sig0;
45 static void
46 handler (int sig)
48 int i;
49 for (i = 0; i < N; ++i)
50 if (pthread_equal (pthread_self (), th[i]))
51 break;
53 if (i == N)
55 if (pthread_equal (pthread_self (), th_main))
56 puts ("signal received by main thread");
57 else
58 printf ("signal received by unknown thread (%lx)\n",
59 (unsigned long int) pthread_self ());
60 exit (1);
63 ++received[i][sig - sig0];
65 sem_post (&sem);
69 static void *
70 tf (void *arg)
72 int idx = (long int) arg;
74 sigset_t ss;
75 sigemptyset (&ss);
77 int i;
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);
84 exit (1);
87 pthread_mutex_lock (&lock[idx]);
89 return NULL;
93 static int
94 do_test (void)
96 /* Block all signals. */
97 sigset_t ss;
98 sigfillset (&ss);
100 th_main = pthread_self ();
102 sig0 = SIGRTMIN;
104 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
106 puts ("1st pthread_sigmask failed");
107 exit (1);
110 /* Install the handler. */
111 int i;
112 for (i = 0; i < N; ++i)
114 struct sigaction sa =
116 .sa_handler = handler,
117 .sa_flags = 0
119 sigfillset (&sa.sa_mask);
121 if (sigaction (sig0 + i, &sa, NULL) != 0)
123 printf ("sigaction for signal %d failed\n", i);
124 exit (1);
128 if (sem_init (&sem, 0, INFLIGHT) != 0)
130 puts ("sem_init failed");
131 exit (1);
134 pthread_attr_t a;
136 if (pthread_attr_init (&a) != 0)
138 puts ("attr_init failed");
139 exit (1);
142 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
144 puts ("attr_setstacksize failed");
145 return 1;
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);
163 exit (1);
167 if (pthread_attr_destroy (&a) != 0)
169 puts ("attr_destroy failed");
170 exit (1);
173 int result = 0;
174 unsigned int r = 42;
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);
182 exit (1);
185 int s = rand_r (&r) % N;
187 kill (pid, sig0 + s);
190 void *status;
191 for (i = 0; i < N; ++i)
193 if (pthread_mutex_unlock (&lock[i]) != 0)
195 printf ("unlock %d failed\n", i);
196 exit (1);
199 if (pthread_join (th[i], &status) != 0)
201 printf ("join %d failed\n", i);
202 result = 1;
204 else if (status != NULL)
206 printf ("%d: result != NULL\n", i);
207 result = 1;
211 int total = 0;
212 for (i = 0; i < N; ++i)
214 int j;
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);
223 result = 1;
227 if (total != ROUNDS)
229 printf ("total number of handled signals is %d, expected %d\n",
230 total, ROUNDS);
231 result = 1;
234 printf ("A total of %d signals sent and received\n", total);
235 for (i = 0; i < N; ++i)
237 printf ("thread %2d:", i);
239 int j;
240 for (j = 0; j <= i; ++j)
242 printf (" %5d", received[i][j]);
243 nsig[j] += received[i][j];
246 putchar ('\n');
250 printf ("\nTotal :");
251 for (i = 0; i < N; ++i)
252 printf (" %5d", nsig[i]);
253 putchar ('\n');
255 return result;
258 #define TIMEOUT 10
259 #define TEST_FUNCTION do_test ()
260 #include "../test-skeleton.c"