* gdb.threads/sigthread.c: Use barriers to ensure that
[external/binutils.git] / gdb / testsuite / gdb.threads / sigthread.c
1 /* Test case for C-c sent to threads with pending signals.  Before I
2    even get there, creating a thread and sending it a signal before it
3    has a chance to run leads to an internal error in GDB.  We need to
4    record that there's a pending SIGSTOP, so that we'll ignore it
5    later, and pass the current signal back to the thread.
6
7    The fork/vfork case has similar trouble but that's even harder
8    to get around.  We may need to send a SIGCONT to cancel out the
9    SIGSTOP.  Different kernels may do different things if the thread
10    is stopped by ptrace and sent a SIGSTOP.  */
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <pthread.h>
16 #include <signal.h>
17
18 /* Loop long enough for GDB to send a few signals of its own, but
19    don't hang around eating CPU forever if something goes wrong during
20    testing.  */
21 #define NSIGS 10000000
22
23 pthread_barrier_t barrier;
24
25 void
26 handler (int sig)
27 {
28   ;
29 }
30
31 pthread_t main_thread;
32 pthread_t child_thread, child_thread_two;
33
34 void *
35 child_two (void *arg)
36 {
37   int i;
38
39   pthread_barrier_wait (&barrier);
40
41   for (i = 0; i < NSIGS; i++)
42     pthread_kill (child_thread, SIGUSR1);
43 }
44
45 void *
46 thread_function (void *arg)
47 {
48   int i;
49
50   pthread_barrier_wait (&barrier);
51
52   for (i = 0; i < NSIGS; i++)
53     pthread_kill (child_thread_two, SIGUSR2);
54 }
55
56 int main()
57 {
58   int i;
59
60   signal (SIGUSR1, handler);
61   signal (SIGUSR2, handler);
62
63   pthread_barrier_init (&barrier, NULL, 3);
64
65   main_thread = pthread_self ();
66   pthread_create (&child_thread, NULL, thread_function, NULL);
67   pthread_create (&child_thread_two, NULL, child_two, NULL);
68
69   pthread_barrier_wait (&barrier);
70
71   for (i = 0; i < NSIGS; i++)
72     pthread_kill (child_thread_two, SIGUSR1);
73
74   pthread_join (child_thread, NULL);
75
76   exit (0);
77 }