]>
Commit | Line | Data |
---|---|---|
757bf54b KB |
1 | /* This testcase is part of GDB, the GNU debugger. |
2 | ||
e2882c85 | 3 | Copyright 2017-2018 Free Software Foundation, Inc. |
757bf54b KB |
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 <memory.h> | |
21 | ||
22 | #define NTHR 3 | |
23 | #define NBOGUSTHR 2 | |
24 | ||
25 | int thr_data[NTHR]; | |
26 | ||
27 | /* Thread handles for each thread plus some "bogus" threads. */ | |
28 | pthread_t thrs[NTHR + NBOGUSTHR]; | |
29 | ||
30 | /* The thread children will meet at this barrier. */ | |
31 | pthread_barrier_t c_barrier; | |
32 | ||
33 | /* The main thread and child thread will meet at this barrier. */ | |
34 | pthread_barrier_t mc_barrier; | |
35 | ||
36 | void | |
37 | do_something (int n) | |
38 | { | |
39 | } | |
40 | ||
41 | void * | |
42 | do_work (void *data) | |
43 | { | |
44 | int num = * (int *) data; | |
45 | ||
46 | /* As the child threads are created, they'll meet the main thread | |
47 | at this barrier. We do this to ensure that threads end up in | |
48 | GDB's thread list in the order in which they were created. Having | |
49 | this ordering makes it easier to write the test. */ | |
50 | pthread_barrier_wait (&mc_barrier); | |
51 | ||
52 | /* All of the child threads will meet at this barrier before proceeding. | |
53 | This ensures that all threads will be active (not exited) and in | |
54 | roughly the same state when the first one hits the breakpoint in | |
55 | do_something(). */ | |
56 | pthread_barrier_wait (&c_barrier); | |
57 | ||
58 | do_something (num); | |
59 | ||
60 | pthread_exit (NULL); | |
61 | } | |
62 | ||
63 | void | |
64 | after_mc_barrier (void) | |
65 | { | |
66 | } | |
67 | ||
68 | int | |
69 | main (int argc, char **argv) | |
70 | { | |
71 | int i; | |
72 | ||
73 | pthread_barrier_init (&c_barrier, NULL, NTHR - 1); | |
74 | pthread_barrier_init (&mc_barrier, NULL, 2); | |
75 | ||
76 | thrs[0] = pthread_self (); | |
77 | thr_data[0] = 1; | |
78 | ||
79 | /* Create two bogus thread handles. */ | |
80 | memset (&thrs[NTHR], 0, sizeof (pthread_t)); | |
81 | memset (&thrs[NTHR + 1], 0xaa, sizeof (pthread_t)); | |
82 | ||
83 | for (i = 1; i < NTHR; i++) | |
84 | { | |
85 | thr_data[i] = i + 1; | |
86 | ||
87 | pthread_create (&thrs[i], NULL, do_work, &thr_data[i]); | |
88 | pthread_barrier_wait (&mc_barrier); | |
89 | after_mc_barrier (); | |
90 | } | |
91 | ||
92 | for (i = 1; i < NTHR; i++) | |
93 | pthread_join (thrs[i], NULL); | |
94 | } |