1 /* Copyright (C) 2002 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
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");
134 for (i
= 0; i
< N
; ++i
)
136 if (pthread_mutex_init (&lock
[i
], NULL
) != 0)
138 printf ("mutex_init[%d] failed\n", i
);
141 if (pthread_mutex_lock (&lock
[i
]) != 0)
143 printf ("mutex_lock[%d] failed\n", i
);
146 if (pthread_create (&th
[i
], NULL
, tf
, (void *) (long int) i
) != 0)
148 printf ("create of thread %d failed\n", i
);
155 pid_t pid
= getpid ();
157 for (i
= 0; i
< ROUNDS
; ++i
)
159 if (sem_wait (&sem
) != 0)
161 printf ("sem_wait round %d failed: %m\n", i
);
165 int s
= rand_r (&r
) % N
;
167 kill (pid
, sig0
+ s
);
171 for (i
= 0; i
< N
; ++i
)
173 if (pthread_mutex_unlock (&lock
[i
]) != 0)
175 printf ("unlock %d failed\n", i
);
179 if (pthread_join (th
[i
], &status
) != 0)
181 printf ("join %d failed\n", i
);
184 else if (status
!= NULL
)
186 printf ("%d: result != NULL\n", i
);
192 for (i
= 0; i
< N
; ++i
)
196 for (j
= 0; j
<= i
; ++j
)
197 total
+= received
[i
][j
];
199 for (j
= i
+ 1; j
< N
; ++j
)
200 if (received
[i
][j
] != 0)
202 printf ("thread %d received signal SIGRTMIN+%d\n", i
, j
);
209 printf ("total number of handled signals is %d, expected %d\n",
214 printf ("A total of %d signals sent and received\n", total
);
215 for (i
= 0; i
< N
; ++i
)
217 printf ("thread %2d:", i
);
220 for (j
= 0; j
<= i
; ++j
)
222 printf (" %5d", received
[i
][j
]);
223 nsig
[j
] += received
[i
][j
];
230 printf ("\nTotal :");
231 for (i
= 0; i
< N
; ++i
)
232 printf (" %5d", nsig
[i
]);
239 #define TEST_FUNCTION do_test ()
240 #include "../test-skeleton.c"