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/>. */
32 pthread_mutex_t thread_count_mutex
;
34 pthread_cond_t thread_count_condvar
;
37 incr_thread_count (void)
39 pthread_mutex_lock (&thread_count_mutex
);
41 if (thread_count
== NR_THREADS
)
42 pthread_cond_signal (&thread_count_condvar
);
43 pthread_mutex_unlock (&thread_count_mutex
);
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
);
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
);
69 forever_pthread (void *unused
)
80 /* Wait until all threads are running. */
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
);
91 pthread_cond_wait (&thread_count_condvar
, &thread_count_mutex
);
92 if (thread_count
== NR_THREADS
)
94 pthread_mutex_unlock (&thread_count_mutex
);
97 pthread_mutex_unlock (&thread_count_mutex
);
98 printf ("failed waiting for all threads to start\n");
102 /* Called when all threads are running.
103 Easy place for a breakpoint. */
106 all_threads_running (void)
113 pthread_t forever
[NR_THREADS
];
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 ();