1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2020-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
25 /* The number of threads to create. */
28 /* Counter accessed from threads to ensure that all threads have been
29 started. Is initialised to THREAD_COUNT and each thread decrements it
33 /* Lock guarding COUNTER. */
34 pthread_mutex_t counter_mutex
= PTHREAD_MUTEX_INITIALIZER
;
36 /* Is initialised with our pid, GDB will read this. */
39 /* Just somewhere to put a breakpoint. */
46 /* Thread safe decrement of the COUNTER global. */
50 if (pthread_mutex_lock (&counter_mutex
) != 0)
53 if (pthread_mutex_unlock (&counter_mutex
) != 0)
57 /* Thread safe read of the COUNTER global. */
63 if (pthread_mutex_lock (&counter_mutex
) != 0)
66 if (pthread_mutex_unlock (&counter_mutex
) != 0)
72 #if defined DO_EXIT_TEST
74 /* Thread entry point. ARG is a pointer to a single integer, the ID for
75 this thread numbered 1 to THREAD_COUNT (a global). */
77 thread_worker_exiting (void *arg
)
85 if (id
!= thread_count
)
89 /* All threads except the last one will wait here while the test is
90 carried out. Don't wait forever though, just in case the test
92 for (i
= 0; i
< 60; ++i
)
97 /* The last thread waits here until all other threads have been
99 while (read_counter () > 0)
102 /* Hit the breakpoint so GDB can stop. */
105 /* And exit all threads. */
112 #define thread_worker thread_worker_exiting
114 #elif defined DO_SIGNAL_TEST
116 /* Thread entry point. ARG is a pointer to a single integer, the ID for
117 this thread numbered 1 to THREAD_COUNT (a global). */
119 thread_worker_signalling (void *arg
)
125 decrement_counter ();
127 if (id
== thread_count
)
129 /* The last thread waits here until all other threads have been
131 while (read_counter () > 0)
134 /* Hit the breakpoint so GDB can stop. */
138 /* All threads wait here while the testsuite sends us a signal. Don't
139 block forever though, just in case the test goes wrong. */
140 for (i
= 0; i
< 60; ++i
)
146 #define thread_worker thread_worker_signalling
150 #error "Compile with DO_EXIT_TEST or DO_SIGNAL_TEST defined"
163 int i
, max
= thread_count
;
165 /* Put the pid somewhere easy for GDB to read. */
166 global_pid
= getpid ();
168 /* Space to hold all of the thread_info objects. */
169 struct thread_info
*info
= malloc (sizeof (struct thread_info
) * max
);
173 /* Initialise the counter. Don't do this under lock as we only have the
174 main thread at this point. */
175 counter
= thread_count
;
177 /* Create all of the threads. */
178 for (i
= 0; i
< max
; ++i
)
180 struct thread_info
*thr
= &info
[i
];
182 if (pthread_create (&thr
->thread
, NULL
, thread_worker
, &thr
->id
) != 0)
186 /* Gather in all of the threads. This never completes, as the
187 final thread created will exit the process, and all of the other
188 threads block forever. Still, it gives the main thread something to
190 for (i
= 0; i
< max
; ++i
)
192 struct thread_info
*thr
= &info
[i
];
193 if (pthread_join (thr
->thread
, NULL
) != 0)
199 /* Return non-zero. We should never get here, but if we do make sure we
200 indicate something has gone wrong. */