1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2014-2015 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
);
117 main (int argc
, char *argv
[])
122 n_threads
= atoi (argv
[1]);
124 pthread_mutex_init (&dthrds_create_mutex
, NULL
);
126 pthread_attr_init (&detached_attr
);
127 pthread_attr_setdetachstate (&detached_attr
, PTHREAD_CREATE_DETACHED
);
128 pthread_attr_init (&joinable_attr
);
129 pthread_attr_setdetachstate (&joinable_attr
, PTHREAD_CREATE_JOINABLE
);
131 main_thread
= pthread_self ();
133 /* Spawn the initial set of test threads. Some threads are
134 joinable, others are detached. This exercises different code
135 paths in the runtime. */
136 for (i
= 0; i
< n_threads
; ++i
)
138 struct thread_arg
*p
;
140 p
= malloc (sizeof *p
);
141 p
->parent
= main_thread
;
142 create_thread (&joinable_attr
, joinable_fn
, p
);
144 create_thread (&detached_attr
, detached_fn
, NULL
);
147 /* Long enough for all the attach/detach sequences done by the .exp