Gtest-concurrent.c revision b0406d0a2a97b290a835ab94c13036119a0db23b
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 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 <limits.h> 32#include <pthread.h> 33#include <signal.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <unistd.h> 37 38#define NTHREADS 128 39 40#define panic(args...) \ 41 do { fprintf (stderr, args); ++nerrors; } while (0) 42 43int verbose; 44int nerrors; 45int got_usr1, got_usr2; 46char *sigusr1_sp; 47 48void 49handler (int sig) 50{ 51 unw_word_t ip; 52 unw_context_t uc; 53 unw_cursor_t c; 54 int ret; 55 56 unw_getcontext (&uc); 57 unw_init_local (&c, &uc); 58 do 59 { 60 unw_get_reg (&c, UNW_REG_IP, &ip); 61 if (verbose) 62 printf ("%lx: IP=%lx\n", (long) pthread_self (), (unsigned long) ip); 63 } 64 while ((ret = unw_step (&c)) > 0); 65 66 if (ret < 0) 67 panic ("unw_step() returned %d\n", ret); 68} 69 70void * 71worker (void *arg) 72{ 73 signal (SIGUSR1, handler); 74 75 if (verbose) 76 printf ("sending SIGUSR1\n"); 77 pthread_kill (pthread_self (), SIGUSR1); 78 return NULL; 79} 80 81static void 82doit (void) 83{ 84 pthread_t th[NTHREADS]; 85 pthread_attr_t attr; 86 int i; 87 88 pthread_attr_init (&attr); 89 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN + 64*1024); 90 91 for (i = 0; i < NTHREADS; ++i) 92 if (pthread_create (th + i, &attr, worker, NULL)) 93 { 94 fprintf (stderr, "FAILURE: Failed to create %u threads " 95 "(after %u threads)\n", 96 NTHREADS, i); 97 exit (-1); 98 } 99 100 for (i = 0; i < NTHREADS; ++i) 101 pthread_join (th[i], NULL); 102} 103 104int 105main (int argc, char **argv) 106{ 107 if (argc > 1) 108 verbose = 1; 109 110 if (verbose) 111 printf ("Caching: none\n"); 112 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE); 113 doit (); 114 115 if (verbose) 116 printf ("Caching: global\n"); 117 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL); 118 119 if (verbose) 120 printf ("Caching: per-thread\n"); 121 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD); 122 123 if (nerrors) 124 { 125 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors); 126 exit (-1); 127 } 128 129 if (verbose) 130 printf ("SUCCESS\n"); 131 return 0; 132} 133