[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / multiple-successive-infcall.c
blob7ba702a33a7f8cc0f8ccef3dab1307ee957f23b9
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/>. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <pthread.h>
22 /* This defines the number of threads to spawn. */
23 #define THREADCOUNT 4
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
32 on creation. */
33 static int thread_ids[THREADCOUNT];
35 /* Find the value associated with the calling thread. */
36 static int
37 get_value ()
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. */
45 return 1;
48 /* Return the nth Fibonacci number. */
49 static unsigned long
50 fast_fib (unsigned int n)
52 int a = 0;
53 int b = 1;
54 int t;
55 for (unsigned int i = 0; i < n; ++i)
57 t = b;
58 b = a + b;
59 a = t;
61 return a;
64 /* Encapsulate the synchronization of the threads. Perform a barrier before
65 and after the computation. */
66 static void *
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);
83 int
84 main (void)
86 int err = pthread_barrier_init (&print_barrier, NULL, THREADCOUNT);
87 if (err != 0)
89 fprintf (stderr, "Barrier creation failed\n");
90 return EXIT_FAILURE;
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]);
100 if (err != 0)
102 fprintf (stderr, "Thread creation failed\n");
103 return EXIT_FAILURE;
106 /* Wait for the threads to complete then exit. */
107 for (int tid = 0; tid < THREADCOUNT; ++tid)
108 pthread_join (threads[tid], NULL);
110 return EXIT_SUCCESS;