Back to index

glibc  2.9
tst-cleanup4.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 Jakub Jelinek <jakub@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 <pthread.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 
00025 /* LinuxThreads pthread_cleanup_{push,pop} helpers.  */
00026 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
00027                                    void (*__routine) (void *),
00028                                    void *__arg);
00029 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer,
00030                                   int __execute);
00031 
00032 static int fds[2];
00033 static pthread_barrier_t b2;
00034 static int global;
00035 
00036 /* Defined in tst-cleanup4aux.c, never compiled with -fexceptions.  */
00037 extern void fn5 (void);
00038 extern void fn7 (void);
00039 extern void fn9 (void);
00040 
00041 void
00042 clh (void *arg)
00043 {
00044   int val = (long int) arg;
00045 
00046   printf ("clh (%d)\n", val);
00047 
00048   global *= val;
00049   global += val;
00050 }
00051 
00052 
00053 static __attribute__((noinline)) void
00054 fn_read (void)
00055 {
00056   int r = pthread_barrier_wait (&b2);
00057   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00058     {
00059       printf ("%s: barrier_wait failed\n", __FUNCTION__);
00060       exit (1);
00061     }
00062 
00063   char c;
00064   read (fds[0], &c, 1);
00065 }
00066 
00067 
00068 __attribute__((noinline)) void
00069 fn0 (void)
00070 {
00071   pthread_cleanup_push (clh, (void *) 1l);
00072 
00073   fn_read ();
00074 
00075   pthread_cleanup_pop (1);
00076 }
00077 
00078 
00079 __attribute__((noinline)) void
00080 fn1 (void)
00081 {
00082   /* This is the old LinuxThreads pthread_cleanup_{push,pop}.  */
00083   struct _pthread_cleanup_buffer b;
00084   _pthread_cleanup_push (&b, clh, (void *) 2l);
00085 
00086   fn0 ();
00087 
00088   _pthread_cleanup_pop (&b, 1);
00089 }
00090 
00091 
00092 static __attribute__((noinline)) void
00093 fn2 (void)
00094 {
00095   pthread_cleanup_push (clh, (void *) 3l);
00096 
00097   fn1 ();
00098 
00099   pthread_cleanup_pop (1);
00100 }
00101 
00102 
00103 static void *
00104 tf (void *a)
00105 {
00106   switch ((long) a)
00107     {
00108     case 0:
00109       fn2 ();
00110       break;
00111     case 1:
00112       fn5 ();
00113       break;
00114     case 2:
00115       fn7 ();
00116       break;
00117     case 3:
00118       fn9 ();
00119       break;
00120     }
00121 
00122   return NULL;
00123 }
00124 
00125 
00126 int
00127 do_test (void)
00128 {
00129   int result = 0;
00130 
00131   if (pipe (fds) != 0)
00132     {
00133       puts ("pipe failed");
00134       exit (1);
00135     }
00136 
00137   if (pthread_barrier_init (&b2, NULL, 2) != 0)
00138     {
00139       puts ("b2 init failed");
00140       exit (1);
00141     }
00142 
00143   const int expect[] =
00144     {
00145       15,     /* 1 2 3 */
00146       276,    /* 1 4 5 6 */
00147       120,    /* 1 7 8 */
00148       460     /* 1 2 9 10 */
00149     };
00150 
00151   long i;
00152   for (i = 0; i < 4; ++i)
00153     {
00154       global = 0;
00155 
00156       printf ("test %ld\n", i);
00157 
00158       pthread_t th;
00159       if (pthread_create (&th, NULL, tf, (void *) i) != 0)
00160        {
00161          puts ("create failed");
00162          exit (1);
00163        }
00164 
00165       int e = pthread_barrier_wait (&b2);
00166       if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00167        {
00168          printf ("%s: barrier_wait failed\n", __FUNCTION__);
00169          exit (1);
00170        }
00171 
00172       pthread_cancel (th);
00173 
00174       void *r;
00175       if ((e = pthread_join (th, &r)) != 0)
00176        {
00177          printf ("join failed: %d\n", e);
00178          _exit (1);
00179        }
00180 
00181       if (r != PTHREAD_CANCELED)
00182        {
00183          puts ("thread not canceled");
00184          exit (1);
00185        }
00186 
00187       if (global != expect[i])
00188        {
00189          printf ("global = %d, expected %d\n", global, expect[i]);
00190          result = 1;
00191        }
00192     }
00193 
00194   return result;
00195 }
00196 
00197 #define TEST_FUNCTION do_test ()
00198 #include "../test-skeleton.c"