]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/watch_thread_num.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / watch_thread_num.c
CommitLineData
37e4754d
LM
1/* This testcase is part of GDB, the GNU debugger.
2
1d506c26 3 Copyright 2002-2024 Free Software Foundation, Inc.
37e4754d 4
d53a7b30
JB
5 Copyright 1992, 1993, 1994, 1995, 1999, 2002, 2003, 2007, 2008, 2009
6 Free Software Foundation, Inc.
7
37e4754d
LM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
d53a7b30 10 the Free Software Foundation; either version 3 of the License, or
37e4754d
LM
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
d53a7b30 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
37e4754d
LM
20
21 This file is copied from schedlock.c. */
22
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <pthread.h>
27
28void *thread_function (void *arg); /* Pointer to function executed by each thread */
29
a7b796db
PA
30static pthread_barrier_t threads_started_barrier;
31
1a3da2cd
AB
32static pthread_barrier_t threads_started_barrier2;
33
a7b796db
PA
34#define NUM 15
35
36static int num_threads = NUM;
37e4754d
LM
37
38static unsigned int shared_var = 1;
39
40int main () {
41 int res;
42 pthread_t threads[NUM];
43 void *thread_result;
44 long i;
45
169a2871
TV
46 alarm (180);
47
a7b796db
PA
48 pthread_barrier_init (&threads_started_barrier, NULL, NUM + 1);
49
1a3da2cd
AB
50 pthread_barrier_init (&threads_started_barrier2, NULL, 2);
51
37e4754d
LM
52 for (i = 0; i < NUM; i++)
53 {
54 res = pthread_create (&threads[i],
55 NULL,
56 thread_function,
57 (void *) i);
58 }
59
a7b796db
PA
60 pthread_barrier_wait (&threads_started_barrier);
61
1a3da2cd
AB
62 pthread_barrier_wait (&threads_started_barrier2); /* all threads started */
63
64 pthread_join (threads[0], NULL);
65
169a2871
TV
66 /* first child thread exited */
67
68 while (1)
69 sleep (1);
37e4754d
LM
70
71 exit (EXIT_SUCCESS);
72}
73
a7b796db
PA
74void
75loop (void)
76{
77}
78
37e4754d
LM
79void *thread_function (void *arg) {
80 int my_number = (long) arg;
a7b796db
PA
81
82 pthread_barrier_wait (&threads_started_barrier);
83
1a3da2cd 84 if (my_number > 0)
37e4754d 85 {
1a3da2cd
AB
86 /* Don't run forever. Run just short of it :) */
87 while (shared_var > 0)
88 {
89 shared_var++;
90 usleep (1); /* Loop increment. */
91 loop ();
92 }
37e4754d 93 }
1a3da2cd
AB
94 else
95 pthread_barrier_wait (&threads_started_barrier2);
37e4754d
LM
96
97 pthread_exit (NULL);
98}
99