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