1 /* This tests the barrier reset mechanism.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
23 #include <internaltypes.h>
26 static pthread_barrier_t b1
;
27 static pthread_barrier_t b2
;
31 #define ROUNDS_PER_RUN 20
32 #define START ((BARRIER_IN_THRESHOLD / N - ROUNDS_PER_RUN / 2) * N)
41 /* In each run, we execute a number of rounds and initialize the barrier
42 so that we will go over the reset threshold with those rounds. */
43 for (int rounds
= 0; rounds
< ROUNDS_PER_RUN
; rounds
++)
44 pthread_barrier_wait (&b1
);
46 if (pthread_barrier_wait (&b1
) == PTHREAD_BARRIER_SERIAL_THREAD
)
48 pthread_barrier_destroy (&b1
);
49 if (pthread_barrier_init (&b1
, NULL
, N
) != 0)
51 puts ("tf: 1st barrier_init failed");
54 puts ("b1 reinitialized");
55 /* Trigger a reset. */
56 struct pthread_barrier
*bar
= (struct pthread_barrier
*) &b1
;
59 /* We deliberately don't set bar->current_round so that we also
60 test whether the helping for the updates of current_round
64 /* Same as above, just for b2. */
65 for (int rounds
= 0; rounds
< ROUNDS_PER_RUN
; rounds
++)
66 pthread_barrier_wait (&b2
);
68 if (pthread_barrier_wait (&b2
) == PTHREAD_BARRIER_SERIAL_THREAD
)
70 pthread_barrier_destroy (&b2
);
71 if (pthread_barrier_init (&b2
, NULL
, N
) != 0)
73 puts ("tf: 2nd barrier_init failed");
76 puts ("b2 reinitialized");
77 /* Trigger a reset. See above. */
78 struct pthread_barrier
*bar
= (struct pthread_barrier
*) &b2
;
94 if (pthread_attr_init (&at
) != 0)
96 puts ("attr_init failed");
100 if (pthread_attr_setstacksize (&at
, 1 * 1024 * 1024) != 0)
102 puts ("attr_setstacksize failed");
106 if (pthread_barrier_init (&b1
, NULL
, N
) != 0)
108 puts ("1st barrier_init failed");
112 if (pthread_barrier_init (&b2
, NULL
, N
) != 0)
114 puts ("2nd barrier_init failed");
119 for (cnt
= 0; cnt
< N
- 1; ++cnt
)
120 if (pthread_create (&th
[cnt
], &at
, tf
, NULL
) != 0)
122 puts ("pthread_create failed");
126 if (pthread_attr_destroy (&at
) != 0)
128 puts ("attr_destroy failed");
134 for (cnt
= 0; cnt
< N
- 1; ++cnt
)
135 if (pthread_join (th
[cnt
], NULL
) != 0)
137 puts ("pthread_join failed");
144 #define TEST_FUNCTION do_test ()
145 #include "../test-skeleton.c"