Initial import
[external/libunwind.git] / tests / Gtest-concurrent.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-2005 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23
24 /*  Verify that multi-threaded concurrent unwinding works as expected.  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "compiler.h"
31
32 #include <libunwind.h>
33 #include <limits.h>
34 #include <pthread.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #define NTHREADS        128
41
42 #define panic(args...)                                          \
43         do { fprintf (stderr, args); ++nerrors; } while (0)
44
45 int verbose;
46 int nerrors;
47 int got_usr1, got_usr2;
48 char *sigusr1_sp;
49
50 void
51 handler (int sig UNUSED)
52 {
53   unw_word_t ip;
54   unw_context_t uc;
55   unw_cursor_t c;
56   int ret;
57
58   unw_getcontext (&uc);
59   unw_init_local (&c, &uc);
60   do
61     {
62       unw_get_reg (&c, UNW_REG_IP, &ip);
63       if (verbose)
64         printf ("%lx: IP=%lx\n", (long) pthread_self (), (unsigned long) ip);
65     }
66   while ((ret = unw_step (&c)) > 0);
67
68   if (ret < 0)
69     panic ("unw_step() returned %d\n", ret);
70 }
71
72 void *
73 worker (void *arg UNUSED)
74 {
75   signal (SIGUSR1, handler);
76
77   if (verbose)
78     printf ("sending SIGUSR1\n");
79   pthread_kill (pthread_self (), SIGUSR1);
80   return NULL;
81 }
82
83 static void
84 doit (void)
85 {
86   pthread_t th[NTHREADS];
87   pthread_attr_t attr;
88   int i;
89
90   pthread_attr_init (&attr);
91   pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN + 64*1024);
92
93   for (i = 0; i < NTHREADS; ++i)
94     if (pthread_create (th + i, &attr, worker, NULL))
95       {
96         fprintf (stderr, "FAILURE: Failed to create %u threads "
97                  "(after %u threads)\n",
98                  NTHREADS, i);
99         exit (-1);
100       }
101
102   for (i = 0; i < NTHREADS; ++i)
103     pthread_join (th[i], NULL);
104 }
105
106 int
107 main (int argc, char **argv UNUSED)
108 {
109   if (argc > 1)
110     verbose = 1;
111
112   if (verbose)
113     printf ("Caching: none\n");
114   unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
115   doit ();
116
117   if (verbose)
118     printf ("Caching: global\n");
119   unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
120   doit ();
121
122   if (verbose)
123     printf ("Caching: per-thread\n");
124   unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
125   doit ();
126
127   if (nerrors)
128     {
129       fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
130       exit (-1);
131     }
132
133   if (verbose)
134     printf ("SUCCESS\n");
135   return 0;
136 }