aboutsummaryrefslogtreecommitdiff
path: root/nptl/tst-cancel25.c
blob: c6e1c23c02bed64de8418e22357498fc024b8fe0 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <internal-signals.h>


static pthread_barrier_t b;
static pthread_t th2;


static void *
tf2 (void *arg)
{
  sigset_t mask;
  if (pthread_sigmask (SIG_SETMASK, NULL, &mask) != 0)
    {
      puts ("pthread_sigmask failed");
      exit (1);
    }
  if (sigismember (&mask, SIGCANCEL))
    {
      puts ("SIGCANCEL blocked in new thread");
      exit (1);
    }

  /* Sync with the main thread so that we do not test anything else.  */
  int e = pthread_barrier_wait (&b);
  if (e != 0  && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  while (1)
    {
      /* Just a cancelable call.  */
      struct timespec ts = { 10000, 0 };
      nanosleep (&ts, 0);
    }

  return NULL;
}


static void
unwhand (void *arg)
{
  if (pthread_create (&th2, NULL, tf2, NULL) != 0)
    {
      puts ("unwhand: create failed");
      exit (1);
    }
}


static void *
tf (void *arg)
{
  pthread_cleanup_push (unwhand, NULL);

  /* Sync with the main thread so that we do not test anything else.  */
  int e = pthread_barrier_wait (&b);
  if (e != 0  && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  while (1)
    {
      /* Just a cancelable call.  */
      struct timespec ts = { 10000, 0 };
      nanosleep (&ts, 0);
    }

  pthread_cleanup_pop (0);

  return NULL;
}


static int
do_test (void)
{
  if (pthread_barrier_init (&b, NULL, 2) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  pthread_t th1;
  if (pthread_create (&th1, NULL, tf, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  int e = pthread_barrier_wait (&b);
  if (e != 0  && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      return 1;
    }

  /* Make sure tf1 enters nanosleep.  */
  struct timespec ts = { 0, 500000000 };
  while (nanosleep (&ts, &ts) != 0)
    ;

  if (pthread_cancel (th1) != 0)
    {
      puts ("1st cancel failed");
      return 1;
    }

  void *res;
  if (pthread_join (th1, &res) != 0)
    {
      puts ("1st join failed");
      return 1;
    }
  if (res != PTHREAD_CANCELED)
    {
      puts ("1st thread not canceled");
      return 1;
    }

  e = pthread_barrier_wait (&b);
  if (e != 0  && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      return 1;
    }

  /* Make sure tf2 enters nanosleep.  */
  ts.tv_sec = 0;
  ts.tv_nsec = 500000000;
  while (nanosleep (&ts, &ts) != 0)
    ;

  puts ("calling pthread_cancel the second time");
  if (pthread_cancel (th2) != 0)
    {
      puts ("2nd cancel failed");
      return 1;
    }

  puts ("calling pthread_join the second time");
  if (pthread_join (th2, &res) != 0)
    {
      puts ("2nd join failed");
      return 1;
    }
  if (res != PTHREAD_CANCELED)
    {
      puts ("2nd thread not canceled");
      return 1;
    }

  if (pthread_barrier_destroy (&b) != 0)
    {
      puts ("barrier_destroy failed");
      return 0;
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"