[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / queue-signal.c
blob9351248b1f0f8d469532431963b27266b8ccc8b7
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2014-2024 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/>. */
18 #include <pthread.h>
19 #include <signal.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 /* Used to individually advance each thread to the desired stopping point. */
24 int ready;
26 sig_atomic_t sigusr1_received;
27 sig_atomic_t sigusr2_received;
28 sig_atomic_t sigabrt_received;
30 /* Number of threads currently running. */
31 int thread_count;
32 pthread_mutex_t thread_count_mutex;
33 pthread_cond_t thread_count_condvar;
35 static void
36 incr_thread_count (void)
38 pthread_mutex_lock (&thread_count_mutex);
39 ++thread_count;
40 pthread_cond_signal (&thread_count_condvar);
41 pthread_mutex_unlock (&thread_count_mutex);
44 static void
45 sigusr1_handler (int sig)
47 sigusr1_received = 1;
50 static void
51 sigusr2_handler (int sig)
53 sigusr2_received = 1;
56 static void
57 sigabrt_handler (int sig)
59 sigabrt_received = 1;
62 static void *
63 sigusr1_thread_function (void *unused)
65 incr_thread_count ();
66 while (!ready)
67 usleep (100);
68 pthread_kill (pthread_self (), SIGUSR1);
71 static void *
72 sigusr2_thread_function (void *unused)
74 incr_thread_count ();
75 while (!ready)
76 usleep (100);
77 /* pthread_kill (pthread_self (), SIGUSR2); - manually injected by gdb */
80 /* Wait until all threads are at a point where a backtrace will
81 show the thread entry point function. */
83 static void
84 wait_all_threads_running (int nr_threads)
86 pthread_mutex_lock (&thread_count_mutex);
88 while (1)
90 if (thread_count == nr_threads)
92 pthread_mutex_unlock (&thread_count_mutex);
93 return;
95 pthread_cond_wait (&thread_count_condvar, &thread_count_mutex);
99 static void
100 all_threads_running (void)
102 while (!ready)
103 usleep (100);
106 static void
107 all_threads_done (void)
112 main ()
114 pthread_t sigusr1_thread, sigusr2_thread;
116 /* Protect against running forever. */
117 alarm (60);
119 signal (SIGUSR1, sigusr1_handler);
120 signal (SIGUSR2, sigusr2_handler);
121 signal (SIGABRT, sigabrt_handler);
123 /* Don't let any thread advance past initialization. */
124 ready = 0;
126 pthread_mutex_init (&thread_count_mutex, NULL);
127 pthread_cond_init (&thread_count_condvar, NULL);
129 #define NR_THREADS 2
130 pthread_create (&sigusr1_thread, NULL, sigusr1_thread_function, NULL);
131 pthread_create (&sigusr2_thread, NULL, sigusr2_thread_function, NULL);
132 wait_all_threads_running (NR_THREADS);
133 all_threads_running ();
135 pthread_kill (pthread_self (), SIGABRT);
137 pthread_join (sigusr1_thread, NULL);
138 pthread_join (sigusr2_thread, NULL);
139 all_threads_done ();
141 return 0;