1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2014-2024 Free Software Foundation, Inc.
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/>. */
26 pthread_t main_thread
;
27 pthread_attr_t detached_attr
;
28 pthread_attr_t joinable_attr
;
30 /* Number of threads we'll create of each variant
31 (joinable/detached). */
34 /* Mutex used to hold creating detached threads. */
35 pthread_mutex_t dthrds_create_mutex
;
37 /* Wrapper for pthread_create. */
40 create_thread (pthread_attr_t
*attr
,
41 void *(*start_routine
) (void *), void *arg
)
46 while ((rc
= pthread_create (&child
, attr
, start_routine
, arg
)) != 0)
48 fprintf (stderr
, "unexpected error from pthread_create: %s (%d)\n",
59 /* Data passed to joinable threads on creation. This is allocated on
60 the heap and ownership transferred from parent to child. (We do
61 this because it's not portable to cast pthread_t to pointer.) */
68 /* Entry point for joinable threads. These threads first join their
69 parent before spawning a new child (and exiting). The parent's tid
70 is passed as pthread_create argument, encapsulated in a struct
74 joinable_fn (void *arg
)
76 struct thread_arg
*p
= arg
;
78 pthread_setname_np (pthread_self (), "joinable");
80 if (p
->parent
!= main_thread
)
81 assert (pthread_join (p
->parent
, NULL
) == 0);
83 p
->parent
= pthread_self ();
85 create_thread (&joinable_attr
, joinable_fn
, p
);
92 /* Entry point for detached threads. */
95 detached_fn (void *arg
)
97 pthread_setname_np (pthread_self (), "detached");
99 /* This should throttle threads a bit in case we manage to spawn
100 threads faster than they exit. */
101 pthread_mutex_lock (&dthrds_create_mutex
);
103 create_thread (&detached_attr
, detached_fn
, NULL
);
105 /* Note this is called before the mutex is unlocked otherwise in
106 non-stop mode, when the breakpoint is hit we'd keep spawning more
107 threads forever while the old threads stay alive (stopped in the
111 pthread_mutex_unlock (&dthrds_create_mutex
);
116 /* Allow for as much timeout as DejaGnu wants, plus a bit of
118 #define SECONDS (TIMEOUT + 20)
120 /* We'll exit after this many seconds. */
121 unsigned int seconds_left
= SECONDS
;
123 /* GDB sets this whenever it's about to start a new detach/attach
124 sequence. We react by resetting the seconds left counter. */
125 volatile int again
= 0;
128 main (int argc
, char *argv
[])
133 n_threads
= atoi (argv
[1]);
135 pthread_mutex_init (&dthrds_create_mutex
, NULL
);
137 pthread_attr_init (&detached_attr
);
138 pthread_attr_setdetachstate (&detached_attr
, PTHREAD_CREATE_DETACHED
);
139 pthread_attr_init (&joinable_attr
);
140 pthread_attr_setdetachstate (&joinable_attr
, PTHREAD_CREATE_JOINABLE
);
142 main_thread
= pthread_self ();
144 /* Spawn the initial set of test threads. Some threads are
145 joinable, others are detached. This exercises different code
146 paths in the runtime. */
147 for (i
= 0; i
< n_threads
; ++i
)
149 struct thread_arg
*p
;
151 p
= malloc (sizeof *p
);
152 p
->parent
= main_thread
;
153 create_thread (&joinable_attr
, joinable_fn
, p
);
155 create_thread (&detached_attr
, detached_fn
, NULL
);
158 /* Exit after a while if GDB is gone/crashes. But wait long enough
159 for one attach/detach sequence done by the .exp file. */
160 while (--seconds_left
> 0)
166 /* GDB should be reattaching soon. Restart the timer. */
168 seconds_left
= SECONDS
;
172 printf ("timeout, exiting\n");