aboutsummaryrefslogtreecommitdiff
path: root/nptl/tst-cond22.c
blob: 64f19ea0a55af05788aba392ca14f44d6883c67a (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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>


static pthread_barrier_t b;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;


static void
cl (void *arg)
{
  pthread_mutex_unlock (&m);
}


static void *
tf (void *arg)
{
  if (pthread_mutex_lock (&m) != 0)
    {
      printf ("%s: mutex_lock failed\n", __func__);
      exit (1);
    }
  int e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      printf ("%s: barrier_wait failed\n", __func__);
      exit (1);
    }
  pthread_cleanup_push (cl, NULL);
  /* We have to loop here because the cancellation might come after
     the cond_wait call left the cancelable area and is then waiting
     on the mutex.  In this case the beginning of the second cond_wait
     call will cause the cancellation to happen.  */
  do
    if (pthread_cond_wait (&c, &m) != 0)
      {
	printf ("%s: cond_wait failed\n", __func__);
	exit (1);
      }
  while (arg == NULL);
  pthread_cleanup_pop (0);
  if (pthread_mutex_unlock (&m) != 0)
    {
      printf ("%s: mutex_unlock failed\n", __func__);
      exit (1);
    }
  return NULL;
}


static int
do_test (void)
{
  int status = 0;

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

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("1st create failed");
      return 1;
    }
  int e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("1st barrier_wait failed");
      return 1;
    }
  if (pthread_mutex_lock (&m) != 0)
    {
      puts ("1st mutex_lock failed");
      return 1;
    }
  if (pthread_cond_signal (&c) != 0)
    {
      puts ("1st cond_signal failed");
      return 1;
    }
  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      return 1;
    }
  if (pthread_mutex_unlock (&m) != 0)
    {
      puts ("1st mutex_unlock failed");
      return 1;
    }
  void *res;
  if (pthread_join (th, &res) != 0)
    {
      puts ("1st join failed");
      return 1;
    }
  if (res != PTHREAD_CANCELED)
    {
      puts ("first thread not canceled");
      status = 1;
    }

  printf ("cond = { %llu, %llu, %u/%u/%u, %u/%u/%u, %u, %u }\n",
	  c.__data.__wseq, c.__data.__g1_start,
	  c.__data.__g_signals[0], c.__data.__g_refs[0], c.__data.__g_size[0],
	  c.__data.__g_signals[1], c.__data.__g_refs[1], c.__data.__g_size[1],
	  c.__data.__g1_orig_size, c.__data.__wrefs);

  if (pthread_create (&th, NULL, tf, (void *) 1l) != 0)
    {
      puts ("2nd create failed");
      return 1;
    }
  e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("2nd barrier_wait failed");
      return 1;
    }
  if (pthread_mutex_lock (&m) != 0)
    {
      puts ("2nd mutex_lock failed");
      return 1;
    }
  if (pthread_cond_signal (&c) != 0)
    {
      puts ("2nd cond_signal failed");
      return 1;
    }
  if (pthread_mutex_unlock (&m) != 0)
    {
      puts ("2nd mutex_unlock failed");
      return 1;
    }
  if (pthread_join (th, &res) != 0)
    {
      puts ("2nd join failed");
      return 1;
    }
  if (res != NULL)
    {
      puts ("2nd thread canceled");
      status = 1;
    }

  printf ("cond = { %llu, %llu, %u/%u/%u, %u/%u/%u, %u, %u }\n",
	  c.__data.__wseq, c.__data.__g1_start,
	  c.__data.__g_signals[0], c.__data.__g_refs[0], c.__data.__g_size[0],
	  c.__data.__g_signals[1], c.__data.__g_refs[1], c.__data.__g_size[1],
	  c.__data.__g1_orig_size, c.__data.__wrefs);

  return status;
}

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