Back to index

glibc  2.9
tst-cancel1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <pthread.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 
00025 
00026 static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
00027 static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
00028 
00029 static int cntr;
00030 
00031 
00032 static void
00033 cleanup (void *arg)
00034 {
00035   if (arg != (void *) 42l)
00036     cntr = 42;
00037   else
00038     cntr = 1;
00039 }
00040 
00041 
00042 static void *
00043 tf (void *arg)
00044 {
00045   int err;
00046 
00047   pthread_cleanup_push (cleanup, (void *) 42l);
00048 
00049   err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00050   if (err != 0)
00051     {
00052       printf ("setcanceltype failed: %s\n", strerror (err));
00053       exit (1);
00054     }
00055 
00056   err = pthread_mutex_unlock (&m2);
00057   if (err != 0)
00058     {
00059       printf ("child: mutex_unlock failed: %s\n", strerror (err));
00060       exit (1);
00061     }
00062 
00063   err = pthread_mutex_lock (&m1);
00064   if (err != 0)
00065     {
00066       printf ("child: 1st mutex_lock failed: %s\n", strerror (err));
00067       exit (1);
00068     }
00069 
00070   /* We should never come here.  */
00071 
00072   pthread_cleanup_pop (0);
00073 
00074   return NULL;
00075 }
00076 
00077 
00078 static int
00079 do_test (void)
00080 {
00081   int err;
00082   pthread_t th;
00083   int result = 0;
00084   void *retval;
00085 
00086   /* Get the mutexes.  */
00087   err = pthread_mutex_lock (&m1);
00088   if (err != 0)
00089     {
00090       printf ("parent: 1st mutex_lock failed: %s\n", strerror (err));
00091       return 1;
00092     }
00093   err = pthread_mutex_lock (&m2);
00094   if (err != 0)
00095     {
00096       printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err));
00097       return 1;
00098     }
00099 
00100   err = pthread_create (&th, NULL, tf, NULL);
00101   if (err != 0)
00102     {
00103       printf ("create failed: %s\n", strerror (err));
00104       return 1;
00105     }
00106 
00107   err = pthread_mutex_lock (&m2);
00108   if (err != 0)
00109     {
00110       printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err));
00111       return 1;
00112     }
00113 
00114   err = pthread_cancel (th);
00115   if (err != 0)
00116     {
00117       printf ("cancel failed: %s\n", strerror (err));
00118       return 1;
00119     }
00120 
00121   err = pthread_join (th, &retval);
00122   if (err != 0)
00123     {
00124       printf ("join failed: %s\n", strerror (err));
00125       return 1;
00126     }
00127 
00128   if (retval != PTHREAD_CANCELED)
00129     {
00130       printf ("wrong return value: %p\n", retval);
00131       result = 1;
00132     }
00133 
00134   if (cntr == 42)
00135     {
00136       puts ("cleanup handler called with wrong argument");
00137       result = 1;
00138     }
00139   else if (cntr != 1)
00140     {
00141       puts ("cleanup handling not called");
00142       result = 1;
00143     }
00144 
00145   return result;
00146 }
00147 
00148 
00149 #define TEST_FUNCTION do_test ()
00150 #include "../test-skeleton.c"