1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2018-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/>. */
22 /* This defines the number of threads to spawn. */
25 /* Global barrier type to control synchronization between threads. */
26 static pthread_barrier_t print_barrier
;
28 /* Define global thread identifiers. */
29 static pthread_t threads
[THREADCOUNT
];
31 /* Hold values for each thread at the index supplied to the thread
33 static int thread_ids
[THREADCOUNT
];
35 /* Find the value associated with the calling thread. */
39 for (int tid
= 0; tid
< THREADCOUNT
; ++tid
)
41 if (pthread_equal (threads
[tid
], pthread_self ()))
42 return thread_ids
[tid
];
44 /* Value for the main thread. */
48 /* Return the nth Fibonacci number. */
50 fast_fib (unsigned int n
)
55 for (unsigned int i
= 0; i
< n
; ++i
)
64 /* Encapsulate the synchronization of the threads. Perform a barrier before
65 and after the computation. */
67 thread_function (void *args
)
69 int tid
= *((int *) args
);
70 int status
= pthread_barrier_wait (&print_barrier
);
71 if (status
== PTHREAD_BARRIER_SERIAL_THREAD
)
72 printf ("All threads entering compute region\n");
74 unsigned long result
= fast_fib (100); /* testmarker01 */
75 status
= pthread_barrier_wait (&print_barrier
);
76 if (status
== PTHREAD_BARRIER_SERIAL_THREAD
)
77 printf ("All threads outputting results\n");
79 pthread_barrier_wait (&print_barrier
);
80 printf ("Thread %d Result: %lu\n", tid
, result
);
86 int err
= pthread_barrier_init (&print_barrier
, NULL
, THREADCOUNT
);
89 fprintf (stderr
, "Barrier creation failed\n");
92 /* Create the worker threads (main). */
93 printf ("Spawning worker threads\n");
94 for (int tid
= 0; tid
< THREADCOUNT
; ++tid
)
96 /* Add 2 so the value maps to the debugger's thread identifiers. */
97 thread_ids
[tid
] = tid
+ 2; /* prethreadcreationmarker */
98 err
= pthread_create (&threads
[tid
], NULL
, thread_function
,
99 (void *) &thread_ids
[tid
]);
102 fprintf (stderr
, "Thread creation failed\n");
106 /* Wait for the threads to complete then exit. */
107 for (int tid
= 0; tid
< THREADCOUNT
; ++tid
)
108 pthread_join (threads
[tid
], NULL
);