Back to index

glibc  2.9
tst-cancel.c
Go to the documentation of this file.
00001 /* Tests for cancelation handling.  */
00002 
00003 #include <pthread.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 #include <sys/stat.h>
00009 
00010 int fd;
00011 
00012 pthread_barrier_t bar;
00013 
00014 
00015 #ifdef NOT_YET
00016 static void
00017 cleanup (void *arg)
00018 {
00019   int nr = (int) (long int) arg;
00020   char s[30];
00021   char *cp = stpcpy (s, "cleanup ");
00022   *cp++ = '0' + nr;
00023   *cp++ = '\n';
00024   __libc_lseek (fd, 0, SEEK_END);
00025   __libc_write (fd, s, cp - s);
00026 }
00027 
00028 
00029 static void *
00030 t1 (void *arg)
00031 {
00032   pthread_cleanup_push (cleanup, (void *) (long int) 1);
00033   return NULL;
00034   pthread_cleanup_pop (0);
00035 }
00036 
00037 
00038 static void
00039 inner (int a)
00040 {
00041   pthread_cleanup_push (cleanup, (void *) (long int) a);
00042   if (a)
00043     return;
00044   pthread_cleanup_pop (0);
00045 }
00046 
00047 
00048 static void *
00049 t2 (void *arg)
00050 {
00051   pthread_cleanup_push (cleanup, (void *) (long int) 2);
00052   inner ((int) (long int) arg);
00053   return NULL;
00054   pthread_cleanup_pop (0);
00055 }
00056 #endif
00057 
00058 /* This does not work yet.  */
00059 volatile int cleanupokcnt;
00060 
00061 static void
00062 cleanupok (void *arg)
00063 {
00064   ++cleanupokcnt;
00065 }
00066 
00067 #ifdef NOT_YET
00068 static void *
00069 t3 (void *arg)
00070 {
00071   pthread_cleanup_push (cleanupok, (void *) (long int) 4);
00072   inner ((int) (long int) arg);
00073   pthread_exit (NULL);
00074   pthread_cleanup_pop (0);
00075 }
00076 #endif
00077 
00078 static void
00079 innerok (int a)
00080 {
00081   pthread_cleanup_push (cleanupok, (void *) (long int) a);
00082   pthread_exit (NULL);
00083   pthread_cleanup_pop (0);
00084 }
00085 
00086 
00087 static void *
00088 t4 (void *arg)
00089 {
00090   pthread_cleanup_push (cleanupok, (void *) (long int) 6);
00091   innerok ((int) (long int) arg);
00092   pthread_cleanup_pop (0);
00093   return NULL;
00094 }
00095 
00096 
00097 int
00098 main (int argc, char *argv[])
00099 {
00100   pthread_t td;
00101   int err;
00102   char *tmp;
00103   const char *prefix;
00104   const char template[] = "thtstXXXXXX";
00105   struct stat64 st;
00106   int result = 0;
00107 
00108   prefix = argc > 1 ? argv[1] : "";
00109   tmp = (char *) alloca (strlen (prefix) + sizeof template);
00110   strcpy (stpcpy (tmp, prefix), template);
00111 
00112   fd = mkstemp (tmp);
00113   if (fd == -1)
00114     {
00115       printf ("cannot create temporary file: %m");
00116       exit (1);
00117     }
00118   unlink (tmp);
00119 
00120   err = pthread_barrier_init (&bar, NULL, 2);
00121   if (err != 0 )
00122     {
00123       printf ("cannot create barrier: %s\n", strerror (err));
00124       exit (1);
00125     }
00126 
00127 #ifdef NOT_YET
00128   err = pthread_create (&td, NULL, t1, NULL);
00129   if (err != 0)
00130     {
00131       printf ("cannot create thread t1: %s\n", strerror (err));
00132       exit (1);
00133     }
00134 
00135   err = pthread_join (td, NULL);
00136   if (err != 0)
00137     {
00138       printf ("cannot join thread: %s\n", strerror (err));
00139       exit (1);
00140     }
00141 
00142   err = pthread_create (&td, NULL, t2, (void *) 3);
00143   if (err != 0)
00144     {
00145       printf ("cannot create thread t2: %s\n", strerror (err));
00146       exit (1);
00147     }
00148 
00149   err = pthread_join (td, NULL);
00150   if (err != 0)
00151     {
00152       printf ("cannot join thread: %s\n", strerror (err));
00153       exit (1);
00154     }
00155 
00156   err = pthread_create (&td, NULL, t3, (void *) 5);
00157   if (err != 0)
00158     {
00159       printf ("cannot create thread t3: %s\n", strerror (err));
00160       exit (1);
00161     }
00162 
00163   err = pthread_join (td, NULL);
00164   if (err != 0)
00165     {
00166       printf ("cannot join thread: %s\n", strerror (err));
00167       exit (1);
00168     }
00169 #endif
00170 
00171   err = pthread_create (&td, NULL, t4, (void *) 7);
00172   if (err != 0)
00173     {
00174       printf ("cannot create thread t4: %s\n", strerror (err));
00175       exit (1);
00176     }
00177 
00178   err = pthread_join (td, NULL);
00179   if (err != 0)
00180     {
00181       printf ("cannot join thread: %s\n", strerror (err));
00182       exit (1);
00183     }
00184 
00185   if (fstat64 (fd, &st) < 0)
00186     {
00187       printf ("cannot stat temporary file: %m\n");
00188       result = 1;
00189     }
00190   else if (st.st_size != 0)
00191     {
00192       char buf[512];
00193       puts ("some cleanup handlers ran:");
00194       fflush (stdout);
00195       __lseek (fd, 0, SEEK_SET);
00196       while (1)
00197        {
00198          ssize_t n = read (fd, buf, sizeof buf);
00199          if (n <= 0)
00200            break;
00201          write (STDOUT_FILENO, buf, n);
00202        }
00203       result = 1;
00204     }
00205 
00206   // if (cleanupokcnt != 3)  will be three once t3 runs
00207   if (cleanupokcnt != 2)
00208     {
00209       printf ("cleanupokcnt = %d\n", cleanupokcnt);
00210       result = 1;
00211     }
00212 
00213   return result;
00214 }