lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2003-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@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, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
| 22 | #include <semaphore.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | |
| 28 | #define N 10 |
| 29 | static pthread_t th[N]; |
| 30 | |
| 31 | |
| 32 | #define CB(n) \ |
| 33 | static void \ |
| 34 | cb##n (void) \ |
| 35 | { \ |
| 36 | if (th[n] != pthread_self ()) \ |
| 37 | { \ |
| 38 | write (STDOUT_FILENO, "wrong callback\n", 15); \ |
| 39 | _exit (1); \ |
| 40 | } \ |
| 41 | } |
| 42 | CB (0) |
| 43 | CB (1) |
| 44 | CB (2) |
| 45 | CB (3) |
| 46 | CB (4) |
| 47 | CB (5) |
| 48 | CB (6) |
| 49 | CB (7) |
| 50 | CB (8) |
| 51 | CB (9) |
| 52 | static void (*cbs[]) (void) = |
| 53 | { |
| 54 | cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9 |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec"))); |
| 59 | |
| 60 | |
| 61 | static sem_t s; |
| 62 | |
| 63 | |
| 64 | #define THE_SIG SIGUSR1 |
| 65 | static void |
| 66 | handler (int sig) |
| 67 | { |
| 68 | if (sig != THE_SIG) |
| 69 | { |
| 70 | write (STDOUT_FILENO, "wrong signal\n", 13); |
| 71 | _exit (1); |
| 72 | } |
| 73 | |
| 74 | fp (); |
| 75 | |
| 76 | if (sem_post (&s) != 0) |
| 77 | { |
| 78 | write (STDOUT_FILENO, "sem_post failed\n", 16); |
| 79 | _exit (1); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static pthread_barrier_t b; |
| 85 | |
| 86 | #define TOTAL_SIGS 1000 |
| 87 | static int nsigs; |
| 88 | |
| 89 | |
| 90 | static void * |
| 91 | tf (void *arg) |
| 92 | { |
| 93 | fp = arg; |
| 94 | |
| 95 | pthread_barrier_wait (&b); |
| 96 | |
| 97 | pthread_barrier_wait (&b); |
| 98 | |
| 99 | if (nsigs != TOTAL_SIGS) |
| 100 | { |
| 101 | puts ("barrier_wait prematurely returns"); |
| 102 | exit (1); |
| 103 | } |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | int |
| 110 | do_test (void) |
| 111 | { |
| 112 | if (pthread_barrier_init (&b, NULL, N + 1) != 0) |
| 113 | { |
| 114 | puts ("barrier_init failed"); |
| 115 | exit (1); |
| 116 | } |
| 117 | |
| 118 | if (sem_init (&s, 0, 0) != 0) |
| 119 | { |
| 120 | puts ("sem_init failed"); |
| 121 | exit (1); |
| 122 | } |
| 123 | |
| 124 | struct sigaction sa; |
| 125 | sa.sa_handler = handler; |
| 126 | sigemptyset (&sa.sa_mask); |
| 127 | sa.sa_flags = 0; |
| 128 | if (sigaction (THE_SIG, &sa, NULL) != 0) |
| 129 | { |
| 130 | puts ("sigaction failed"); |
| 131 | exit (1); |
| 132 | } |
| 133 | |
| 134 | pthread_attr_t a; |
| 135 | |
| 136 | if (pthread_attr_init (&a) != 0) |
| 137 | { |
| 138 | puts ("attr_init failed"); |
| 139 | exit (1); |
| 140 | } |
| 141 | |
| 142 | if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0) |
| 143 | { |
| 144 | puts ("attr_setstacksize failed"); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | int i; |
| 149 | for (i = 0; i < N; ++i) |
| 150 | if (pthread_create (&th[i], &a, tf, cbs[i]) != 0) |
| 151 | { |
| 152 | puts ("pthread_create failed"); |
| 153 | exit (1); |
| 154 | } |
| 155 | |
| 156 | if (pthread_attr_destroy (&a) != 0) |
| 157 | { |
| 158 | puts ("attr_destroy failed"); |
| 159 | exit (1); |
| 160 | } |
| 161 | |
| 162 | pthread_barrier_wait (&b); |
| 163 | |
| 164 | sigset_t ss; |
| 165 | sigemptyset (&ss); |
| 166 | sigaddset (&ss, THE_SIG); |
| 167 | if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) |
| 168 | { |
| 169 | puts ("pthread_sigmask failed"); |
| 170 | exit (1); |
| 171 | } |
| 172 | |
| 173 | /* Start sending signals. */ |
| 174 | for (i = 0; i < TOTAL_SIGS; ++i) |
| 175 | { |
| 176 | if (kill (getpid (), THE_SIG) != 0) |
| 177 | { |
| 178 | puts ("kill failed"); |
| 179 | exit (1); |
| 180 | } |
| 181 | |
| 182 | if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0) |
| 183 | { |
| 184 | puts ("sem_wait failed"); |
| 185 | exit (1); |
| 186 | } |
| 187 | |
| 188 | ++nsigs; |
| 189 | } |
| 190 | |
| 191 | pthread_barrier_wait (&b); |
| 192 | |
| 193 | for (i = 0; i < N; ++i) |
| 194 | if (pthread_join (th[i], NULL) != 0) |
| 195 | { |
| 196 | puts ("join failed"); |
| 197 | exit (1); |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | #define TEST_FUNCTION do_test () |
| 205 | #include "../test-skeleton.c" |