summary refs log tree commit diff
path: root/linuxthreads/tst-cancel.c
blob: 84b8197e42db9dcfad9d651e2dccfdf9737fb52a (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Tests for cancelation handling.  */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

int fd;

pthread_barrier_t bar;


static void
cleanup (void *arg)
{
  int nr = (int) (long int) arg;
  char s[30];
  char *cp = stpcpy (s, "cleanup ");
  *cp++ = '0' + nr;
  *cp++ = '\n';
  __libc_lseek (fd, 0, SEEK_END);
  __libc_write (fd, s, cp - s);
}


volatile int cleanupokcnt;

static void
cleanupok (void *arg)
{
  ++cleanupokcnt;
}


static void *
t1 (void *arg)
{
  pthread_cleanup_push (cleanup, (void *) (long int) 1);
  return NULL;
  pthread_cleanup_pop (0);
}


static void
inner (int a)
{
  pthread_cleanup_push (cleanup, (void *) (long int) a);
  if (a)
    return;
  pthread_cleanup_pop (0);
}


static void *
t2 (void *arg)
{
  pthread_cleanup_push (cleanup, (void *) (long int) 2);
  inner ((int) (long int) arg);
  return NULL;
  pthread_cleanup_pop (0);
}


static void
innerok (int a)
{
  pthread_cleanup_push (cleanup, (void *) (long int) a);
  if (a)
    return;
  pthread_cleanup_pop (0);
}


static void *
t3 (void *arg)
{
  pthread_cleanup_push (cleanupok, (void *) (long int) 4);
  innerok ((int) (long int) arg);
  pthread_exit (NULL);
  pthread_cleanup_pop (0);
}


int
main (int argc, char *argv[])
{
  pthread_t td;
  int err;
  char *tmp;
  const char *path;
  const char template[] = "thtstXXXXXX";
  struct stat64 st;
  int result = 0;

  path = argc > 1 ? argv[1] : "";
  tmp = (char *) alloca (strlen (path) + sizeof template);
  strcpy (stpcpy (tmp, path), template);

  fd = mkstemp (tmp);
  if (fd == -1)
    {
      printf ("cannot create temporary file: %m");
      exit (1);
    }
  unlink (tmp);

  err = pthread_barrier_init (&bar, NULL, 2);
  if (err != 0 )
    {
      printf ("cannot create barrier: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_create (&td, NULL, t1, NULL);
  if (err != 0)
    {
      printf ("cannot create thread t1: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_join (td, NULL);
  if (err != 0)
    {
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_create (&td, NULL, t2, (void *) 3);
  if (err != 0)
    {
      printf ("cannot create thread t2: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_join (td, NULL);
  if (err != 0)
    {
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_create (&td, NULL, t3, (void *) 5);
  if (err != 0)
    {
      printf ("cannot create thread t3: %s\n", strerror (err));
      exit (1);
    }

  err = pthread_join (td, NULL);
  if (err != 0)
    {
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
    }

  if (fstat64 (fd, &st) < 0)
    {
      printf ("cannot stat temporary file: %m\n");
      result = 1;
    }
  else if (st.st_size != 0)
    {
      char buf[512];
      puts ("some cleanup handlers ran:");
      fflush (stdout);
      __lseek (fd, 0, SEEK_SET);
      while (1)
	{
	  ssize_t n = read (fd, buf, sizeof buf);
	  if (n <= 0)
	    break;
	  write (STDOUT_FILENO, buf, n);
	}
      result = 1;
    }

  if (cleanupokcnt != 1)
    {
      printf ("cleanupokcnt = %d\n", cleanupokcnt);
      result = 1;
    }

  return result;
}