summaryrefslogtreecommitdiff
path: root/sim/testsuite/sim/cris/c/thread5.c
blob: 494251fdd39616bdfb7f51c5fc7913b777d7cb77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Compiler options:
#notarget: cris*-*-elf
#cc: additional_flags=-pthread
#output: abbb ok\n

   Testing a signal handler corner case.  */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

static void *
process (void *arg)
{
  write (2, "a", 1);
  write (2, "b", 1);
  write (2, "b", 1);
  write (2, "b", 1);
  return NULL;
}

int ok = 0;
volatile int done = 0;

void
sigusr1 (int signum)
{
  if (signum != SIGUSR1 || !ok)
    abort ();
  done = 1;
}

int
main (void)
{
  int retcode;
  pthread_t th_a;
  void *retval;
  sigset_t sigs;

  if (sigemptyset (&sigs) != 0)
    abort ();

  retcode = pthread_create (&th_a, NULL, process, NULL);
  if (retcode != 0)
    abort ();

  if (signal (SIGUSR1, sigusr1) != SIG_DFL)
    abort ();
  if (pthread_sigmask (SIG_BLOCK, NULL, &sigs) != 0
      || sigaddset (&sigs, SIGUSR1) != 0
      || pthread_sigmask (SIG_BLOCK, &sigs, NULL) != 0)
    abort ();
  if (pthread_kill (pthread_self (), SIGUSR1) != 0
      || sched_yield () != 0
      || sched_yield () != 0
      || sched_yield () != 0)
    abort ();

  ok = 1;
  if (pthread_sigmask (SIG_UNBLOCK, NULL, &sigs) != 0
      || sigaddset (&sigs, SIGUSR1) != 0
      || pthread_sigmask (SIG_UNBLOCK, &sigs, NULL) != 0)
    abort ();

  if (!done)
    abort ();

  retcode = pthread_join (th_a, &retval);
  if (retcode != 0)
    abort ();
  fprintf (stderr, " ok\n");
  return 0;
}