Home | History | Annotate | Download | only in tests
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <pthread.h>
      4 #include <unistd.h>
      5 
      6 static void thread_cleanup(void *arg)
      7 {
      8   printf("cleaning up 0x%lx\n", (long)arg);
      9 
     10   return;
     11 }
     12 
     13 static void *thread_main(void *arg)
     14 {
     15   pthread_cleanup_push(thread_cleanup, (void *)0x1234);
     16   pthread_cleanup_push(thread_cleanup, (void *)0x5678);
     17 
     18   if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
     19     {
     20       perror("pthread_setcanceltype");
     21       return NULL;
     22     }
     23 
     24   if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
     25     {
     26       perror("pthread_setcancelstate");
     27       return NULL;
     28     }
     29 
     30   pause();
     31 
     32   pthread_cleanup_pop(0);
     33   pthread_cleanup_pop(0);
     34 
     35   return NULL;
     36 }
     37 
     38 int main(int argc, char **argv)
     39 {
     40   pthread_t tid;
     41   void *result;
     42 
     43   if (pthread_create(&tid, NULL, thread_main, NULL) != 0)
     44     {
     45       perror("pthread_create");
     46       exit(1);
     47     }
     48 
     49   sleep(1);
     50 
     51   if (pthread_cancel(tid) != 0)
     52     {
     53       perror("pthread_cancel");
     54       exit(1);
     55     }
     56 
     57   if (pthread_join(tid, &result) != 0)
     58     {
     59       perror("pthread_join");
     60       exit(1);
     61     }
     62 
     63   printf("result is %s\n", result == PTHREAD_CANCELED ? "correct" : "incorrect");
     64 
     65   exit(0);
     66 }
     67