1 /* Test case for hand function calls interrupted by a signal in another thread.
3 Copyright 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 pthread_t threads
[NR_THREADS
];
33 /* Number of threads currently running. */
36 pthread_mutex_t thread_count_mutex
;
38 pthread_cond_t thread_count_condvar
;
40 sig_atomic_t sigabrt_received
;
43 incr_thread_count (void)
45 pthread_mutex_lock (&thread_count_mutex
);
47 if (thread_count
== NR_THREADS
)
48 pthread_cond_signal (&thread_count_condvar
);
49 pthread_mutex_unlock (&thread_count_mutex
);
53 cond_wait (pthread_cond_t
*cond
, pthread_mutex_t
*mut
)
55 pthread_mutex_lock (mut
);
56 pthread_cond_wait (cond
, mut
);
57 pthread_mutex_unlock (mut
);
66 pthread_mutex_init (&mut
, NULL
);
67 pthread_cond_init (&cond
, NULL
);
69 /* Wait for a condition that will never be signaled, so we effectively
70 block the thread here. */
71 cond_wait (&cond
, &mut
);
75 thread_entry (void *unused
)
82 sigabrt_handler (int signo
)
87 /* Helper to test a hand-call being "interrupted" by a signal on another
91 hand_call_with_signal (void)
93 const struct timespec ts
= { 0, 10000000 }; /* 0.01 sec */
96 pthread_kill (threads
[0], SIGABRT
);
97 while (! sigabrt_received
)
98 nanosleep (&ts
, NULL
);
101 /* Wait until all threads are running. */
104 wait_all_threads_running (void)
106 pthread_mutex_lock (&thread_count_mutex
);
107 if (thread_count
== NR_THREADS
)
109 pthread_mutex_unlock (&thread_count_mutex
);
112 pthread_cond_wait (&thread_count_condvar
, &thread_count_mutex
);
113 if (thread_count
== NR_THREADS
)
115 pthread_mutex_unlock (&thread_count_mutex
);
118 pthread_mutex_unlock (&thread_count_mutex
);
119 printf ("failed waiting for all threads to start\n");
123 /* Called when all threads are running.
124 Easy place for a breakpoint. */
127 all_threads_running (void)
136 signal (SIGABRT
, sigabrt_handler
);
138 pthread_mutex_init (&thread_count_mutex
, NULL
);
139 pthread_cond_init (&thread_count_condvar
, NULL
);
141 for (i
= 0; i
< NR_THREADS
; ++i
)
142 pthread_create (&threads
[i
], NULL
, thread_entry
, NULL
);
144 wait_all_threads_running ();
145 all_threads_running ();