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/>. */
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. */
37 condition_core_func (int answer
)
39 /* This unlock should release the other thread. */
40 if (pthread_mutex_unlock (&mutex
) != 0)
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
58 int a
= 100; /* Final breakpoint here. */
61 /* A breakpoint condition function that always returns true. */
63 condition_true_func ()
65 return condition_core_func (1);
68 /* A breakpoint condition function that always returns false. */
70 condition_false_func ()
72 return condition_core_func (0);
76 worker_func (void *arg
)
78 volatile int *ptr
= 0;
79 int tid
= *((int *) arg
);
84 global_var_0
= 11; /* First thread breakpoint. */
88 if (pthread_mutex_lock (&mutex
) != 0)
91 global_var_1
= *ptr
; /* Signal here. */
93 global_var_1
= 99; /* Other thread breakpoint. */
106 pthread_t threads
[NUM_THREADS
];
107 int args
[NUM_THREADS
];
109 /* Set an alarm, just in case the test deadlocks. */
112 /* We want the mutex to start locked. */
113 if (pthread_mutex_lock (&mutex
) != 0)
116 for (int i
= 0; i
< NUM_THREADS
; i
++)
119 pthread_create (&threads
[i
], NULL
, worker_func
, &args
[i
]);
122 for (int i
= 0; i
< NUM_THREADS
; i
++)
125 pthread_join (threads
[i
], &retval
);
128 /* Unlock once we're done, just for cleanliness. */
129 if (pthread_mutex_unlock (&mutex
) != 0)