Back to index

glibc  2.9
tst-join5.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2006 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 #include <sys/syscall.h>
00027 
00028 
00029 #define wait_code()                                                  \
00030   do {                                                               \
00031     struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 };                    \
00032     while (syscall (__NR_nanosleep, &ts, &ts) < 0)                          \
00033       /* nothing */;                                                 \
00034   } while (0)
00035 
00036 
00037 #ifdef WAIT_IN_CHILD
00038 static pthread_barrier_t b;
00039 #endif
00040 
00041 
00042 static void *
00043 tf1 (void *arg)
00044 {
00045 #ifdef WAIT_IN_CHILD
00046   int e = pthread_barrier_wait (&b);
00047   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00048     {
00049       printf ("%s: barrier_wait failed\n", __func__);
00050       exit (1);
00051     }
00052 
00053   wait_code ();
00054 #endif
00055 
00056   pthread_join ((pthread_t) arg, NULL);
00057 
00058   exit (42);
00059 }
00060 
00061 
00062 static void *
00063 tf2 (void *arg)
00064 {
00065 #ifdef WAIT_IN_CHILD
00066   int e = pthread_barrier_wait (&b);
00067   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00068     {
00069       printf ("%s: barrier_wait failed\n", __func__);
00070       exit (1);
00071     }
00072 
00073   wait_code ();
00074 #endif
00075   pthread_join ((pthread_t) arg, NULL);
00076 
00077   exit (43);
00078 }
00079 
00080 
00081 static int
00082 do_test (void)
00083 {
00084 #ifdef WAIT_IN_CHILD
00085   if (pthread_barrier_init (&b, NULL, 2) != 0)
00086     {
00087       puts ("barrier_init failed");
00088       return 1;
00089     }
00090 #endif
00091 
00092   pthread_t th;
00093 
00094   int err = pthread_join (pthread_self (), NULL);
00095   if (err == 0)
00096     {
00097       puts ("1st circular join succeeded");
00098       return 1;
00099     }
00100   if (err != EDEADLK)
00101     {
00102       printf ("1st circular join %d, not EDEADLK\n", err);
00103       return 1;
00104     }
00105 
00106   if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
00107     {
00108       puts ("1st create failed");
00109       return 1;
00110     }
00111 
00112 #ifndef WAIT_IN_CHILD
00113   wait_code ();
00114 #endif
00115 
00116   if (pthread_cancel (th) != 0)
00117     {
00118       puts ("cannot cancel 1st thread");
00119       return 1;
00120     }
00121 
00122 #ifdef WAIT_IN_CHILD
00123   int e = pthread_barrier_wait (&b);
00124   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00125     {
00126       printf ("%s: barrier_wait failed\n", __func__);
00127       return 1;
00128     }
00129 #endif
00130 
00131   void *r;
00132   err = pthread_join (th, &r);
00133   if (err != 0)
00134     {
00135       printf ("cannot join 1st thread: %d\n", err);
00136       return 1;
00137     }
00138   if (r != PTHREAD_CANCELED)
00139     {
00140       puts ("1st thread not canceled");
00141       return 1;
00142     }
00143 
00144   err = pthread_join (pthread_self (), NULL);
00145   if (err == 0)
00146     {
00147       puts ("2nd circular join succeeded");
00148       return 1;
00149     }
00150   if (err != EDEADLK)
00151     {
00152       printf ("2nd circular join %d, not EDEADLK\n", err);
00153       return 1;
00154     }
00155 
00156   if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
00157     {
00158       puts ("2nd create failed");
00159       return 1;
00160     }
00161 
00162 #ifndef WAIT_IN_CHILD
00163   wait_code ();
00164 #endif
00165 
00166   if (pthread_cancel (th) != 0)
00167     {
00168       puts ("cannot cancel 2nd thread");
00169       return 1;
00170     }
00171 
00172 #ifdef WAIT_IN_CHILD
00173   e = pthread_barrier_wait (&b);
00174   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00175     {
00176       printf ("%s: barrier_wait failed\n", __func__);
00177       return 1;
00178     }
00179 #endif
00180 
00181   if (pthread_join (th, &r) != 0)
00182     {
00183       puts ("cannot join 2nd thread");
00184       return 1;
00185     }
00186   if (r != PTHREAD_CANCELED)
00187     {
00188       puts ("2nd thread not canceled");
00189       return 1;
00190     }
00191 
00192   err = pthread_join (pthread_self (), NULL);
00193   if (err == 0)
00194     {
00195       puts ("3rd circular join succeeded");
00196       return 1;
00197     }
00198   if (err != EDEADLK)
00199     {
00200       printf ("3rd circular join %d, not EDEADLK\n", err);
00201       return 1;
00202     }
00203 
00204   return 0;
00205 }
00206 
00207 #define TEST_FUNCTION do_test ()
00208 #include "../test-skeleton.c"