lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2003. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | /* LinuxThreads pthread_cleanup_{push,pop} helpers. */ |
| 26 | extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 27 | void (*__routine) (void *), |
| 28 | void *__arg); |
| 29 | extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 30 | int __execute); |
| 31 | |
| 32 | static int fds[2]; |
| 33 | static pthread_barrier_t b2; |
| 34 | static int global; |
| 35 | |
| 36 | /* Defined in tst-cleanup4aux.c, never compiled with -fexceptions. */ |
| 37 | extern void fn5 (void); |
| 38 | extern void fn7 (void); |
| 39 | extern void fn9 (void); |
| 40 | |
| 41 | void |
| 42 | clh (void *arg) |
| 43 | { |
| 44 | int val = (long int) arg; |
| 45 | |
| 46 | printf ("clh (%d)\n", val); |
| 47 | |
| 48 | global *= val; |
| 49 | global += val; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static __attribute__((noinline)) void |
| 54 | fn_read (void) |
| 55 | { |
| 56 | int r = pthread_barrier_wait (&b2); |
| 57 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) |
| 58 | { |
| 59 | printf ("%s: barrier_wait failed\n", __FUNCTION__); |
| 60 | exit (1); |
| 61 | } |
| 62 | |
| 63 | char c; |
| 64 | read (fds[0], &c, 1); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | __attribute__((noinline)) void |
| 69 | fn0 (void) |
| 70 | { |
| 71 | pthread_cleanup_push (clh, (void *) 1l); |
| 72 | |
| 73 | fn_read (); |
| 74 | |
| 75 | pthread_cleanup_pop (1); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | __attribute__((noinline)) void |
| 80 | fn1 (void) |
| 81 | { |
| 82 | /* This is the old LinuxThreads pthread_cleanup_{push,pop}. */ |
| 83 | struct _pthread_cleanup_buffer b; |
| 84 | _pthread_cleanup_push (&b, clh, (void *) 2l); |
| 85 | |
| 86 | fn0 (); |
| 87 | |
| 88 | _pthread_cleanup_pop (&b, 1); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static __attribute__((noinline)) void |
| 93 | fn2 (void) |
| 94 | { |
| 95 | pthread_cleanup_push (clh, (void *) 3l); |
| 96 | |
| 97 | fn1 (); |
| 98 | |
| 99 | pthread_cleanup_pop (1); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static void * |
| 104 | tf (void *a) |
| 105 | { |
| 106 | switch ((long) a) |
| 107 | { |
| 108 | case 0: |
| 109 | fn2 (); |
| 110 | break; |
| 111 | case 1: |
| 112 | fn5 (); |
| 113 | break; |
| 114 | case 2: |
| 115 | fn7 (); |
| 116 | break; |
| 117 | case 3: |
| 118 | fn9 (); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | int |
| 127 | do_test (void) |
| 128 | { |
| 129 | int result = 0; |
| 130 | |
| 131 | if (pipe (fds) != 0) |
| 132 | { |
| 133 | puts ("pipe failed"); |
| 134 | exit (1); |
| 135 | } |
| 136 | |
| 137 | if (pthread_barrier_init (&b2, NULL, 2) != 0) |
| 138 | { |
| 139 | puts ("b2 init failed"); |
| 140 | exit (1); |
| 141 | } |
| 142 | |
| 143 | const int expect[] = |
| 144 | { |
| 145 | 15, /* 1 2 3 */ |
| 146 | 276, /* 1 4 5 6 */ |
| 147 | 120, /* 1 7 8 */ |
| 148 | 460 /* 1 2 9 10 */ |
| 149 | }; |
| 150 | |
| 151 | long i; |
| 152 | for (i = 0; i < 4; ++i) |
| 153 | { |
| 154 | global = 0; |
| 155 | |
| 156 | printf ("test %ld\n", i); |
| 157 | |
| 158 | pthread_t th; |
| 159 | if (pthread_create (&th, NULL, tf, (void *) i) != 0) |
| 160 | { |
| 161 | puts ("create failed"); |
| 162 | exit (1); |
| 163 | } |
| 164 | |
| 165 | int e = pthread_barrier_wait (&b2); |
| 166 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 167 | { |
| 168 | printf ("%s: barrier_wait failed\n", __FUNCTION__); |
| 169 | exit (1); |
| 170 | } |
| 171 | |
| 172 | pthread_cancel (th); |
| 173 | |
| 174 | void *r; |
| 175 | if ((e = pthread_join (th, &r)) != 0) |
| 176 | { |
| 177 | printf ("join failed: %d\n", e); |
| 178 | _exit (1); |
| 179 | } |
| 180 | |
| 181 | if (r != PTHREAD_CANCELED) |
| 182 | { |
| 183 | puts ("thread not canceled"); |
| 184 | exit (1); |
| 185 | } |
| 186 | |
| 187 | if (global != expect[i]) |
| 188 | { |
| 189 | printf ("global = %d, expected %d\n", global, expect[i]); |
| 190 | result = 1; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | #define TEST_FUNCTION do_test () |
| 198 | #include "../test-skeleton.c" |