1192800Strasz/* Compiler options:
2192800Strasz#progos: linux
3192800Strasz#cc: additional_flags=-pthread
4192800Strasz#output: abbb ok\n
5192800Strasz
6192800Strasz   Testing a signal handler corner case.  */
7192800Strasz
8192800Strasz#include <stddef.h>
9192800Strasz#include <stdlib.h>
10192800Strasz#include <stdio.h>
11192800Strasz#include <unistd.h>
12192800Strasz#include <signal.h>
13192800Strasz#include <pthread.h>
14192804Strasz
15192804Straszstatic void *
16192804Straszprocess (void *arg)
17192804Strasz{
18192804Strasz  write (2, "a", 1);
19192804Strasz  write (2, "b", 1);
20192804Strasz  write (2, "b", 1);
21192804Strasz  write (2, "b", 1);
22192804Strasz  return NULL;
23192804Strasz}
24192804Strasz
25192800Straszint ok = 0;
26192800Straszvolatile int done = 0;
27192800Strasz
28192800Straszvoid
29192800Straszsigusr1 (int signum)
30192800Strasz{
31192800Strasz  if (signum != SIGUSR1 || !ok)
32192800Strasz    abort ();
33192800Strasz  done = 1;
34192800Strasz}
35192800Strasz
36192800Straszint
37192800Straszmain (void)
38192800Strasz{
39192800Strasz  int retcode;
40  pthread_t th_a;
41  void *retval;
42  sigset_t sigs;
43
44  if (sigemptyset (&sigs) != 0)
45    abort ();
46
47  retcode = pthread_create (&th_a, NULL, process, NULL);
48  if (retcode != 0)
49    abort ();
50
51  if (signal (SIGUSR1, sigusr1) != SIG_DFL)
52    abort ();
53  if (pthread_sigmask (SIG_BLOCK, NULL, &sigs) != 0
54      || sigaddset (&sigs, SIGUSR1) != 0
55      || pthread_sigmask (SIG_BLOCK, &sigs, NULL) != 0)
56    abort ();
57  if (pthread_kill (pthread_self (), SIGUSR1) != 0
58      || sched_yield () != 0
59      || sched_yield () != 0
60      || sched_yield () != 0)
61    abort ();
62
63  ok = 1;
64  if (pthread_sigmask (SIG_UNBLOCK, NULL, &sigs) != 0
65      || sigaddset (&sigs, SIGUSR1) != 0
66      || pthread_sigmask (SIG_UNBLOCK, &sigs, NULL) != 0)
67    abort ();
68
69  if (!done)
70    abort ();
71
72  retcode = pthread_join (th_a, &retval);
73  if (retcode != 0)
74    abort ();
75  fprintf (stderr, " ok\n");
76  return 0;
77}
78