gdb: move clearing of tp->pending_follow to follow_fork_inferior
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pending-fork-event.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2021 Free Software Foundation, Inc.
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 <assert.h>
21
22 static volatile int release_forking_thread = 0;
23 static int x;
24 static pthread_barrier_t barrier;
25
26 static void
27 break_here (void)
28 {
29 x++;
30 }
31
32 static void
33 do_fork (void)
34 {
35 pthread_barrier_wait (&barrier);
36
37 while (!release_forking_thread);
38
39 if (FORK_FUNCTION () == 0)
40 _exit (0);
41
42 }
43
44 static void *
45 thread_func (void *p)
46 {
47 #if defined(MAIN_THREAD_FORKS)
48 break_here ();
49 #elif defined(OTHER_THREAD_FORKS)
50 do_fork ();
51 #else
52 # error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
53 #endif
54 }
55
56
57 int
58 main (void)
59 {
60 pthread_t thread;
61 int ret;
62
63 pthread_barrier_init (&barrier, NULL, 2);
64
65 alarm (30);
66 ret = pthread_create (&thread, NULL, thread_func, NULL);
67 assert (ret == 0);
68
69 pthread_barrier_wait (&barrier);
70
71 #if defined(MAIN_THREAD_FORKS)
72 do_fork ();
73 #elif defined(OTHER_THREAD_FORKS)
74 break_here ();
75 #else
76 # error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
77 #endif
78
79 pthread_join (thread, NULL);
80
81 return 0;
82 }