Back to index

glibc  2.9
tst-cancel7.c
Go to the documentation of this file.
00001 /* Test for pthread cancellation of mutex blocks.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 
00026 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00027 pthread_barrier_t b;
00028 int value = 0;
00029 
00030 static void *
00031 tf (void *arg)
00032 {
00033   int r = pthread_barrier_wait (&b);
00034   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00035     {
00036       printf ("%s: barrier_wait failed\n", __FUNCTION__);
00037       exit (1);
00038     }
00039 
00040   pthread_mutex_lock (&lock);
00041   ++value;
00042   pthread_testcancel ();
00043   ++value;
00044   pthread_mutex_unlock (&lock);
00045   return NULL;
00046 }
00047 
00048 
00049 static int
00050 do_test (void)
00051 {
00052   pthread_mutex_lock (&lock);
00053 
00054   if (pthread_barrier_init (&b, NULL, 2) != 0)
00055     {
00056       puts ("barrier init failed");
00057       return 1;
00058     }
00059 
00060   pthread_t th;
00061   if (pthread_create (&th, NULL, tf, NULL) != 0)
00062     {
00063       puts ("pthread_create failed");
00064       return 1;
00065     }
00066 
00067   int r = pthread_barrier_wait (&b);
00068   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00069     {
00070       printf ("%s: barrier_wait failed\n", __FUNCTION__);
00071       exit (1);
00072     }
00073 
00074   if (pthread_cancel (th) != 0)
00075     {
00076       puts ("pthread_cancel failed");
00077       return 1;
00078     }
00079 
00080   pthread_mutex_unlock (&lock);
00081 
00082   void *status;
00083   if (pthread_join (th, &status) != 0)
00084     {
00085       puts ("join failed");
00086       return 1;
00087     }
00088 
00089   if (status != PTHREAD_CANCELED)
00090     {
00091       puts ("thread not canceled");
00092       return 1;
00093     }
00094 
00095   if (value == 0)
00096     {
00097       puts ("thread cancelled in the pthread_mutex_lock call");
00098       return 1;
00099     }
00100 
00101   if (value != 1)
00102     {
00103       puts ("thread not cancelled in pthread_testcancel call");
00104       return 1;
00105     }
00106 
00107   return 0;
00108 }
00109 
00110 #define TEST_FUNCTION do_test ()
00111 #include "../test-skeleton.c"