1 | /* Tests for cancelation handling. */
|
---|
2 |
|
---|
3 | #include <pthread.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <unistd.h>
|
---|
8 | #include <sys/stat.h>
|
---|
9 |
|
---|
10 | int fd;
|
---|
11 |
|
---|
12 | pthread_barrier_t bar;
|
---|
13 |
|
---|
14 |
|
---|
15 | #ifdef NOT_YET
|
---|
16 | static void
|
---|
17 | cleanup (void *arg)
|
---|
18 | {
|
---|
19 | int nr = (int) (long int) arg;
|
---|
20 | char s[30];
|
---|
21 | char *cp = stpcpy (s, "cleanup ");
|
---|
22 | *cp++ = '0' + nr;
|
---|
23 | *cp++ = '\n';
|
---|
24 | __libc_lseek (fd, 0, SEEK_END);
|
---|
25 | __libc_write (fd, s, cp - s);
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | static void *
|
---|
30 | t1 (void *arg)
|
---|
31 | {
|
---|
32 | pthread_cleanup_push (cleanup, (void *) (long int) 1);
|
---|
33 | return NULL;
|
---|
34 | pthread_cleanup_pop (0);
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | static void
|
---|
39 | inner (int a)
|
---|
40 | {
|
---|
41 | pthread_cleanup_push (cleanup, (void *) (long int) a);
|
---|
42 | if (a)
|
---|
43 | return;
|
---|
44 | pthread_cleanup_pop (0);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | static void *
|
---|
49 | t2 (void *arg)
|
---|
50 | {
|
---|
51 | pthread_cleanup_push (cleanup, (void *) (long int) 2);
|
---|
52 | inner ((int) (long int) arg);
|
---|
53 | return NULL;
|
---|
54 | pthread_cleanup_pop (0);
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | /* This does not work yet. */
|
---|
59 | volatile int cleanupokcnt;
|
---|
60 |
|
---|
61 | static void
|
---|
62 | cleanupok (void *arg)
|
---|
63 | {
|
---|
64 | ++cleanupokcnt;
|
---|
65 | }
|
---|
66 |
|
---|
67 | #ifdef NOT_YET
|
---|
68 | static void *
|
---|
69 | t3 (void *arg)
|
---|
70 | {
|
---|
71 | pthread_cleanup_push (cleanupok, (void *) (long int) 4);
|
---|
72 | inner ((int) (long int) arg);
|
---|
73 | pthread_exit (NULL);
|
---|
74 | pthread_cleanup_pop (0);
|
---|
75 | }
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | static void
|
---|
79 | innerok (int a)
|
---|
80 | {
|
---|
81 | pthread_cleanup_push (cleanupok, (void *) (long int) a);
|
---|
82 | pthread_exit (NULL);
|
---|
83 | pthread_cleanup_pop (0);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | static void *
|
---|
88 | t4 (void *arg)
|
---|
89 | {
|
---|
90 | pthread_cleanup_push (cleanupok, (void *) (long int) 6);
|
---|
91 | innerok ((int) (long int) arg);
|
---|
92 | pthread_cleanup_pop (0);
|
---|
93 | return NULL;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int
|
---|
98 | main (int argc, char *argv[])
|
---|
99 | {
|
---|
100 | pthread_t td;
|
---|
101 | int err;
|
---|
102 | char *tmp;
|
---|
103 | const char *prefix;
|
---|
104 | const char template[] = "thtstXXXXXX";
|
---|
105 | struct stat64 st;
|
---|
106 | int result = 0;
|
---|
107 |
|
---|
108 | prefix = argc > 1 ? argv[1] : "";
|
---|
109 | tmp = (char *) alloca (strlen (prefix) + sizeof template);
|
---|
110 | strcpy (stpcpy (tmp, prefix), template);
|
---|
111 |
|
---|
112 | fd = mkstemp (tmp);
|
---|
113 | if (fd == -1)
|
---|
114 | {
|
---|
115 | printf ("cannot create temporary file: %m");
|
---|
116 | exit (1);
|
---|
117 | }
|
---|
118 | unlink (tmp);
|
---|
119 |
|
---|
120 | err = pthread_barrier_init (&bar, NULL, 2);
|
---|
121 | if (err != 0 )
|
---|
122 | {
|
---|
123 | printf ("cannot create barrier: %s\n", strerror (err));
|
---|
124 | exit (1);
|
---|
125 | }
|
---|
126 |
|
---|
127 | #ifdef NOT_YET
|
---|
128 | err = pthread_create (&td, NULL, t1, NULL);
|
---|
129 | if (err != 0)
|
---|
130 | {
|
---|
131 | printf ("cannot create thread t1: %s\n", strerror (err));
|
---|
132 | exit (1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | err = pthread_join (td, NULL);
|
---|
136 | if (err != 0)
|
---|
137 | {
|
---|
138 | printf ("cannot join thread: %s\n", strerror (err));
|
---|
139 | exit (1);
|
---|
140 | }
|
---|
141 |
|
---|
142 | err = pthread_create (&td, NULL, t2, (void *) 3);
|
---|
143 | if (err != 0)
|
---|
144 | {
|
---|
145 | printf ("cannot create thread t2: %s\n", strerror (err));
|
---|
146 | exit (1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | err = pthread_join (td, NULL);
|
---|
150 | if (err != 0)
|
---|
151 | {
|
---|
152 | printf ("cannot join thread: %s\n", strerror (err));
|
---|
153 | exit (1);
|
---|
154 | }
|
---|
155 |
|
---|
156 | err = pthread_create (&td, NULL, t3, (void *) 5);
|
---|
157 | if (err != 0)
|
---|
158 | {
|
---|
159 | printf ("cannot create thread t3: %s\n", strerror (err));
|
---|
160 | exit (1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | err = pthread_join (td, NULL);
|
---|
164 | if (err != 0)
|
---|
165 | {
|
---|
166 | printf ("cannot join thread: %s\n", strerror (err));
|
---|
167 | exit (1);
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | err = pthread_create (&td, NULL, t4, (void *) 7);
|
---|
172 | if (err != 0)
|
---|
173 | {
|
---|
174 | printf ("cannot create thread t4: %s\n", strerror (err));
|
---|
175 | exit (1);
|
---|
176 | }
|
---|
177 |
|
---|
178 | err = pthread_join (td, NULL);
|
---|
179 | if (err != 0)
|
---|
180 | {
|
---|
181 | printf ("cannot join thread: %s\n", strerror (err));
|
---|
182 | exit (1);
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (fstat64 (fd, &st) < 0)
|
---|
186 | {
|
---|
187 | printf ("cannot stat temporary file: %m\n");
|
---|
188 | result = 1;
|
---|
189 | }
|
---|
190 | else if (st.st_size != 0)
|
---|
191 | {
|
---|
192 | char buf[512];
|
---|
193 | puts ("some cleanup handlers ran:");
|
---|
194 | fflush (stdout);
|
---|
195 | __lseek (fd, 0, SEEK_SET);
|
---|
196 | while (1)
|
---|
197 | {
|
---|
198 | ssize_t n = read (fd, buf, sizeof buf);
|
---|
199 | if (n <= 0)
|
---|
200 | break;
|
---|
201 | write (STDOUT_FILENO, buf, n);
|
---|
202 | }
|
---|
203 | result = 1;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // if (cleanupokcnt != 3) will be three once t3 runs
|
---|
207 | if (cleanupokcnt != 2)
|
---|
208 | {
|
---|
209 | printf ("cleanupokcnt = %d\n", cleanupokcnt);
|
---|
210 | result = 1;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return result;
|
---|
214 | }
|
---|