Back to index

glibc  2.9
tst-cancel18.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 #include <unistd.h>
00026 
00027 
00028 static pthread_barrier_t b;
00029 
00030 
00031 /* Cleanup handling test.  */
00032 static int cl_called;
00033 
00034 static void
00035 cl (void *arg)
00036 {
00037   ++cl_called;
00038 }
00039 
00040 
00041 static void *
00042 tf (void *arg)
00043 {
00044   int r = pthread_barrier_wait (&b);
00045   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00046     {
00047       puts ("barrier_wait failed");
00048       exit (1);
00049     }
00050 
00051   pthread_cleanup_push (cl, NULL);
00052 
00053   struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
00054   TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
00055 
00056   pthread_cleanup_pop (0);
00057 
00058   puts ("clock_nanosleep returned");
00059 
00060   exit (1);
00061 }
00062 
00063 
00064 static int
00065 do_test (void)
00066 {
00067   if (pthread_barrier_init (&b, NULL, 2) != 0)
00068     {
00069       puts ("barrier_init failed");
00070       return 1;
00071     }
00072 
00073   pthread_t th;
00074   if (pthread_create (&th, NULL, tf, NULL) != 0)
00075     {
00076       puts ("1st create failed");
00077       return 1;
00078     }
00079 
00080   int r = pthread_barrier_wait (&b);
00081   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00082     {
00083       puts ("barrier_wait failed");
00084       exit (1);
00085     }
00086 
00087   struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
00088   while (nanosleep (&ts, &ts) != 0)
00089     continue;
00090 
00091   puts ("going to cancel in-time");
00092   if (pthread_cancel (th) != 0)
00093     {
00094       puts ("1st cancel failed");
00095       return 1;
00096     }
00097 
00098   void *status;
00099   if (pthread_join (th, &status) != 0)
00100     {
00101       puts ("1st join failed");
00102       return 1;
00103     }
00104   if (status != PTHREAD_CANCELED)
00105     {
00106       puts ("1st thread not canceled");
00107       return 1;
00108     }
00109 
00110   if (cl_called == 0)
00111     {
00112       puts ("cleanup handler not called");
00113       return 1;
00114     }
00115   if (cl_called > 1)
00116     {
00117       puts ("cleanup handler called more than once");
00118       return 1;
00119     }
00120 
00121   puts ("in-time cancellation succeeded");
00122 
00123 
00124   cl_called = 0;
00125 
00126   if (pthread_create (&th, NULL, tf, NULL) != 0)
00127     {
00128       puts ("2nd create failed");
00129       return 1;
00130     }
00131 
00132   puts ("going to cancel early");
00133   if (pthread_cancel (th) != 0)
00134     {
00135       puts ("2nd cancel failed");
00136       return 1;
00137     }
00138 
00139   r = pthread_barrier_wait (&b);
00140   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00141     {
00142       puts ("barrier_wait failed");
00143       exit (1);
00144     }
00145 
00146   if (pthread_join (th, &status) != 0)
00147     {
00148       puts ("2nd join failed");
00149       return 1;
00150     }
00151   if (status != PTHREAD_CANCELED)
00152     {
00153       puts ("2nd thread not canceled");
00154       return 1;
00155     }
00156 
00157   if (cl_called == 0)
00158     {
00159       printf ("cleanup handler not called\n");
00160       return 1;
00161     }
00162   if (cl_called > 1)
00163     {
00164       printf ("cleanup handler called more than once\n");
00165       return 1;
00166     }
00167 
00168   puts ("early cancellation succeeded");
00169 
00170   return 0;
00171 }
00172 
00173 #define TEST_FUNCTION do_test ()
00174 #include "../test-skeleton.c"