Remove obsolete test from gdb.cp/var-tag.exp
[binutils-gdb.git] / gdb / testsuite / gdb.threads / infcall-from-bp-cond-other-thread-event.c
blob38a5bc284dfb9e44d8cc3bbe100eef533ea21740
1 /* Copyright 2022-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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 <unistd.h>
20 #include <stdlib.h>
21 #include <sched.h>
23 #define NUM_THREADS 2
25 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
27 /* Some global variables to poke, just for something to do. */
28 volatile int global_var_0 = 0;
29 volatile int global_var_1 = 0;
31 /* This flag is updated from GDB. */
32 volatile int raise_signal = 0;
34 /* Implement the breakpoint condition function. Release the other thread
35 and try to give the other thread a chance to run. Then return ANSWER. */
36 int
37 condition_core_func (int answer)
39 /* This unlock should release the other thread. */
40 if (pthread_mutex_unlock (&mutex) != 0)
41 abort ();
43 /* And this yield and sleep should (hopefully) give the other thread a
44 chance to run. This isn't guaranteed of course, but once the other
45 thread does run it should hit a breakpoint, which GDB should
46 (temporarily) ignore, so there's no easy way for us to know the other
47 thread has done what it needs to, thus, yielding and sleeping is the
48 best we can do. */
49 sched_yield ();
50 sleep (2);
52 return answer;
55 void
56 stop_marker ()
58 int a = 100; /* Final breakpoint here. */
61 /* A breakpoint condition function that always returns true. */
62 int
63 condition_true_func ()
65 return condition_core_func (1);
68 /* A breakpoint condition function that always returns false. */
69 int
70 condition_false_func ()
72 return condition_core_func (0);
75 void *
76 worker_func (void *arg)
78 volatile int *ptr = 0;
79 int tid = *((int *) arg);
81 switch (tid)
83 case 0:
84 global_var_0 = 11; /* First thread breakpoint. */
85 break;
87 case 1:
88 if (pthread_mutex_lock (&mutex) != 0)
89 abort ();
90 if (raise_signal)
91 global_var_1 = *ptr; /* Signal here. */
92 else
93 global_var_1 = 99; /* Other thread breakpoint. */
94 break;
96 default:
97 abort ();
100 return NULL;
104 main ()
106 pthread_t threads[NUM_THREADS];
107 int args[NUM_THREADS];
109 /* Set an alarm, just in case the test deadlocks. */
110 alarm (300);
112 /* We want the mutex to start locked. */
113 if (pthread_mutex_lock (&mutex) != 0)
114 abort ();
116 for (int i = 0; i < NUM_THREADS; i++)
118 args[i] = i;
119 pthread_create (&threads[i], NULL, worker_func, &args[i]);
122 for (int i = 0; i < NUM_THREADS; i++)
124 void *retval;
125 pthread_join (threads[i], &retval);
128 /* Unlock once we're done, just for cleanliness. */
129 if (pthread_mutex_unlock (&mutex) != 0)
130 abort ();
132 stop_marker ();
134 return 0;