[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / hand-call-in-threads.c
blob777e4fbfa47c7a5101e23007408bb8c391e58af5
1 /* Test case for hand function calls in multi-threaded program.
3 Copyright 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <unistd.h>
26 #ifndef NR_THREADS
27 #define NR_THREADS 4
28 #endif
30 int thread_count;
32 pthread_mutex_t thread_count_mutex;
34 pthread_cond_t thread_count_condvar;
36 void
37 incr_thread_count (void)
39 pthread_mutex_lock (&thread_count_mutex);
40 ++thread_count;
41 if (thread_count == NR_THREADS)
42 pthread_cond_signal (&thread_count_condvar);
43 pthread_mutex_unlock (&thread_count_mutex);
46 void
47 cond_wait (pthread_cond_t *cond, pthread_mutex_t *mut)
49 pthread_mutex_lock (mut);
50 pthread_cond_wait (cond, mut);
51 pthread_mutex_unlock (mut);
54 void
55 noreturn (void)
57 pthread_mutex_t mut;
58 pthread_cond_t cond;
60 pthread_mutex_init (&mut, NULL);
61 pthread_cond_init (&cond, NULL);
63 /* Wait for a condition that will never be signaled, so we effectively
64 block the thread here. */
65 cond_wait (&cond, &mut);
68 void *
69 forever_pthread (void *unused)
71 incr_thread_count ();
72 noreturn ();
75 void
76 hand_call (void)
80 /* Wait until all threads are running. */
82 void
83 wait_all_threads_running (void)
85 pthread_mutex_lock (&thread_count_mutex);
86 if (thread_count == NR_THREADS)
88 pthread_mutex_unlock (&thread_count_mutex);
89 return;
91 pthread_cond_wait (&thread_count_condvar, &thread_count_mutex);
92 if (thread_count == NR_THREADS)
94 pthread_mutex_unlock (&thread_count_mutex);
95 return;
97 pthread_mutex_unlock (&thread_count_mutex);
98 printf ("failed waiting for all threads to start\n");
99 abort ();
102 /* Called when all threads are running.
103 Easy place for a breakpoint. */
105 void
106 all_threads_running (void)
111 main (void)
113 pthread_t forever[NR_THREADS];
114 int i;
116 pthread_mutex_init (&thread_count_mutex, NULL);
117 pthread_cond_init (&thread_count_condvar, NULL);
119 for (i = 0; i < NR_THREADS; ++i)
120 pthread_create (&forever[i], NULL, forever_pthread, NULL);
122 wait_all_threads_running ();
123 all_threads_running ();
125 return 0;