| 1 | /* Copyright 2022-2024 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of GDB. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 17 | |
| 18 | #include <pthread.h> |
| 19 | #include <unistd.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sched.h> |
| 22 | |
| 23 | #define NUM_THREADS 2 |
| 24 | |
| 25 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 26 | |
| 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; |
| 30 | |
| 31 | /* This flag is updated from GDB. */ |
| 32 | volatile int raise_signal = 0; |
| 33 | |
| 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) |
| 38 | { |
| 39 | /* This unlock should release the other thread. */ |
| 40 | if (pthread_mutex_unlock (&mutex) != 0) |
| 41 | abort (); |
| 42 | |
| 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); |
| 51 | |
| 52 | return answer; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | stop_marker () |
| 57 | { |
| 58 | int a = 100; /* Final breakpoint here. */ |
| 59 | } |
| 60 | |
| 61 | /* A breakpoint condition function that always returns true. */ |
| 62 | int |
| 63 | condition_true_func () |
| 64 | { |
| 65 | return condition_core_func (1); |
| 66 | } |
| 67 | |
| 68 | /* A breakpoint condition function that always returns false. */ |
| 69 | int |
| 70 | condition_false_func () |
| 71 | { |
| 72 | return condition_core_func (0); |
| 73 | } |
| 74 | |
| 75 | void * |
| 76 | worker_func (void *arg) |
| 77 | { |
| 78 | volatile int *ptr = 0; |
| 79 | int tid = *((int *) arg); |
| 80 | |
| 81 | switch (tid) |
| 82 | { |
| 83 | case 0: |
| 84 | global_var_0 = 11; /* First thread breakpoint. */ |
| 85 | break; |
| 86 | |
| 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; |
| 95 | |
| 96 | default: |
| 97 | abort (); |
| 98 | } |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | main () |
| 105 | { |
| 106 | pthread_t threads[NUM_THREADS]; |
| 107 | int args[NUM_THREADS]; |
| 108 | |
| 109 | /* Set an alarm, just in case the test deadlocks. */ |
| 110 | alarm (300); |
| 111 | |
| 112 | /* We want the mutex to start locked. */ |
| 113 | if (pthread_mutex_lock (&mutex) != 0) |
| 114 | abort (); |
| 115 | |
| 116 | for (int i = 0; i < NUM_THREADS; i++) |
| 117 | { |
| 118 | args[i] = i; |
| 119 | pthread_create (&threads[i], NULL, worker_func, &args[i]); |
| 120 | } |
| 121 | |
| 122 | for (int i = 0; i < NUM_THREADS; i++) |
| 123 | { |
| 124 | void *retval; |
| 125 | pthread_join (threads[i], &retval); |
| 126 | } |
| 127 | |
| 128 | /* Unlock once we're done, just for cleanliness. */ |
| 129 | if (pthread_mutex_unlock (&mutex) != 0) |
| 130 | abort (); |
| 131 | |
| 132 | stop_marker (); |
| 133 | |
| 134 | return 0; |
| 135 | } |