2005-04-27 Ulrich Drepper <drepper@redhat.com>
[glibc/history.git] / linuxthreads / tst-cancel.c
blob59c6f1f511805499618f9d60312655214454c595
1 /* Tests for cancelation handling. */
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>
10 int fd;
12 pthread_barrier_t bar;
15 #ifdef NOT_YET
16 static void
17 cleanup (void *arg)
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);
29 static void *
30 t1 (void *arg)
32 pthread_cleanup_push (cleanup, (void *) (long int) 1);
33 return NULL;
34 pthread_cleanup_pop (0);
38 static void
39 inner (int a)
41 pthread_cleanup_push (cleanup, (void *) (long int) a);
42 if (a)
43 return;
44 pthread_cleanup_pop (0);
48 static void *
49 t2 (void *arg)
51 pthread_cleanup_push (cleanup, (void *) (long int) 2);
52 inner ((int) (long int) arg);
53 return NULL;
54 pthread_cleanup_pop (0);
56 #endif
58 /* This does not work yet. */
59 volatile int cleanupokcnt;
61 static void
62 cleanupok (void *arg)
64 ++cleanupokcnt;
67 #ifdef NOT_YET
68 static void *
69 t3 (void *arg)
71 pthread_cleanup_push (cleanupok, (void *) (long int) 4);
72 inner ((int) (long int) arg);
73 pthread_exit (NULL);
74 pthread_cleanup_pop (0);
76 #endif
78 static void
79 innerok (int a)
81 pthread_cleanup_push (cleanupok, (void *) (long int) a);
82 pthread_exit (NULL);
83 pthread_cleanup_pop (0);
87 static void *
88 t4 (void *arg)
90 pthread_cleanup_push (cleanupok, (void *) (long int) 6);
91 innerok ((int) (long int) arg);
92 pthread_cleanup_pop (0);
93 return NULL;
97 int
98 main (int argc, char *argv[])
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;
108 prefix = argc > 1 ? argv[1] : "";
109 tmp = (char *) alloca (strlen (prefix) + sizeof template);
110 strcpy (stpcpy (tmp, prefix), template);
112 fd = mkstemp (tmp);
113 if (fd == -1)
115 printf ("cannot create temporary file: %m");
116 exit (1);
118 unlink (tmp);
120 err = pthread_barrier_init (&bar, NULL, 2);
121 if (err != 0 )
123 printf ("cannot create barrier: %s\n", strerror (err));
124 exit (1);
127 #ifdef NOT_YET
128 err = pthread_create (&td, NULL, t1, NULL);
129 if (err != 0)
131 printf ("cannot create thread t1: %s\n", strerror (err));
132 exit (1);
135 err = pthread_join (td, NULL);
136 if (err != 0)
138 printf ("cannot join thread: %s\n", strerror (err));
139 exit (1);
142 err = pthread_create (&td, NULL, t2, (void *) 3);
143 if (err != 0)
145 printf ("cannot create thread t2: %s\n", strerror (err));
146 exit (1);
149 err = pthread_join (td, NULL);
150 if (err != 0)
152 printf ("cannot join thread: %s\n", strerror (err));
153 exit (1);
156 err = pthread_create (&td, NULL, t3, (void *) 5);
157 if (err != 0)
159 printf ("cannot create thread t3: %s\n", strerror (err));
160 exit (1);
163 err = pthread_join (td, NULL);
164 if (err != 0)
166 printf ("cannot join thread: %s\n", strerror (err));
167 exit (1);
169 #endif
171 err = pthread_create (&td, NULL, t4, (void *) 7);
172 if (err != 0)
174 printf ("cannot create thread t4: %s\n", strerror (err));
175 exit (1);
178 err = pthread_join (td, NULL);
179 if (err != 0)
181 printf ("cannot join thread: %s\n", strerror (err));
182 exit (1);
185 if (fstat64 (fd, &st) < 0)
187 printf ("cannot stat temporary file: %m\n");
188 result = 1;
190 else if (st.st_size != 0)
192 char buf[512];
193 puts ("some cleanup handlers ran:");
194 fflush (stdout);
195 __lseek (fd, 0, SEEK_SET);
196 while (1)
198 ssize_t n = read (fd, buf, sizeof buf);
199 if (n <= 0)
200 break;
201 write (STDOUT_FILENO, buf, n);
203 result = 1;
206 // if (cleanupokcnt != 3) will be three once t3 runs
207 if (cleanupokcnt != 2)
209 printf ("cleanupokcnt = %d\n", cleanupokcnt);
210 result = 1;
213 return result;