Test attaching to a program that constantly spawns short-lived threads
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / attach-many-short-lived-threads.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2014-2015 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 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 pthread_t main_thread;
27 pthread_attr_t detached_attr;
28 pthread_attr_t joinable_attr;
29
30 /* Number of threads we'll create of each variant
31 (joinable/detached). */
32 int n_threads = 50;
33
34 /* Mutex used to hold creating detached threads. */
35 pthread_mutex_t dthrds_create_mutex;
36
37 /* Wrapper for pthread_create. */
38
39 void
40 create_thread (pthread_attr_t *attr,
41 void *(*start_routine) (void *), void *arg)
42 {
43 pthread_t child;
44 int rc;
45
46 while ((rc = pthread_create (&child, attr, start_routine, arg)) != 0)
47 {
48 fprintf (stderr, "unexpected error from pthread_create: %s (%d)\n",
49 strerror (rc), rc);
50 sleep (1);
51 }
52 }
53
54 void
55 break_fn (void)
56 {
57 }
58
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.) */
62
63 struct thread_arg
64 {
65 pthread_t parent;
66 };
67
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
71 thread_arg object. */
72
73 void *
74 joinable_fn (void *arg)
75 {
76 struct thread_arg *p = arg;
77
78 pthread_setname_np (pthread_self (), "joinable");
79
80 if (p->parent != main_thread)
81 assert (pthread_join (p->parent, NULL) == 0);
82
83 p->parent = pthread_self ();
84
85 create_thread (&joinable_attr, joinable_fn, p);
86
87 break_fn ();
88
89 return NULL;
90 }
91
92 /* Entry point for detached threads. */
93
94 void *
95 detached_fn (void *arg)
96 {
97 pthread_setname_np (pthread_self (), "detached");
98
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);
102
103 create_thread (&detached_attr, detached_fn, NULL);
104
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
108 breakpoint). */
109 break_fn ();
110
111 pthread_mutex_unlock (&dthrds_create_mutex);
112
113 return NULL;
114 }
115
116 int
117 main (int argc, char *argv[])
118 {
119 int i;
120
121 if (argc > 1)
122 n_threads = atoi (argv[1]);
123
124 pthread_mutex_init (&dthrds_create_mutex, NULL);
125
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);
130
131 main_thread = pthread_self ();
132
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)
137 {
138 struct thread_arg *p;
139
140 p = malloc (sizeof *p);
141 p->parent = main_thread;
142 create_thread (&joinable_attr, joinable_fn, p);
143
144 create_thread (&detached_attr, detached_fn, NULL);
145 }
146
147 /* Long enough for all the attach/detach sequences done by the .exp
148 file. */
149 sleep (180);
150 return 0;
151 }
This page took 0.034082 seconds and 5 git commands to generate.