xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/pending-fork-event-detach.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2021-2023 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   while (!release_forking_thread);
36 
37   if (FORK_FUNCTION () == 0)
38     {
39       /* We create the file in a separate program that we exec: if FORK_FUNCTION
40 	 is vfork, we shouldn't do anything more than an exec.  */
41       execl (TOUCH_FILE_BIN, TOUCH_FILE_BIN, NULL);
42     }
43 
44 }
45 
46 static void *
47 thread_func (void *p)
48 {
49   pthread_barrier_wait (&barrier);
50 
51 #if defined(MAIN_THREAD_FORKS)
52   break_here ();
53 #elif defined(OTHER_THREAD_FORKS)
54   do_fork ();
55 #else
56 # error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
57 #endif
58 }
59 
60 
61 int
62 main (void)
63 {
64   pthread_t thread;
65   int ret;
66 
67   pthread_barrier_init (&barrier, NULL, 2);
68 
69   alarm (30);
70   ret = pthread_create (&thread, NULL, thread_func, NULL);
71   assert (ret == 0);
72 
73   pthread_barrier_wait (&barrier);
74 
75 #if defined(MAIN_THREAD_FORKS)
76   do_fork ();
77 #elif defined(OTHER_THREAD_FORKS)
78   break_here ();
79 #else
80 # error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
81 #endif
82 
83   pthread_join (thread, NULL);
84 
85   return 0;
86 }
87