1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-12-23 12:03:41 +01:00
libunwind-eh_elf/tests/Gtest-concurrent.c

135 lines
3.2 KiB
C
Raw Normal View History

2003-11-20 02:10:03 +01:00
/* libunwind - a platform-independent unwind library
Copyright (C) 2003-2005 Hewlett-Packard Co
2003-11-20 02:10:03 +01:00
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/* Verify that multi-threaded concurrent unwinding works as expected. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <libunwind.h>
#include <limits.h>
2003-11-20 02:10:03 +01:00
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NTHREADS 128
#define panic(args...) \
do { fprintf (stderr, args); ++nerrors; } while (0)
int verbose;
int nerrors;
int got_usr1, got_usr2;
char *sigusr1_sp;
void
handler (int sig)
{
unw_word_t ip;
unw_context_t uc;
unw_cursor_t c;
int ret;
unw_getcontext (&uc);
unw_init_local (&c, &uc);
2003-11-20 02:10:03 +01:00
do
{
unw_get_reg (&c, UNW_REG_IP, &ip);
2003-11-20 02:10:03 +01:00
if (verbose)
printf ("%lx: IP=%lx\n", (long) pthread_self (), (unsigned long) ip);
2003-11-20 02:10:03 +01:00
}
while ((ret = unw_step (&c)) > 0);
2003-11-20 02:10:03 +01:00
if (ret < 0)
panic ("unw_step() returned %d\n", ret);
}
void *
worker (void *arg)
{
signal (SIGUSR1, handler);
if (verbose)
printf ("sending SIGUSR1\n");
pthread_kill (pthread_self (), SIGUSR1);
return NULL;
}
static void
doit (void)
{
pthread_t th[NTHREADS];
pthread_attr_t attr;
2003-11-20 02:10:03 +01:00
int i;
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN + 64*1024);
2003-11-20 02:10:03 +01:00
for (i = 0; i < NTHREADS; ++i)
if (pthread_create (th + i, &attr, worker, NULL))
{
fprintf (stderr, "FAILURE: Failed to create %u threads "
"(after %u threads)\n",
NTHREADS, i);
exit (-1);
}
2003-11-20 02:10:03 +01:00
for (i = 0; i < NTHREADS; ++i)
pthread_join (th[i], NULL);
}
int
main (int argc, char **argv)
{
if (argc > 1)
verbose = 1;
if (verbose)
printf ("Caching: none\n");
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
doit ();
if (verbose)
printf ("Caching: global\n");
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
doit ();
2003-11-20 02:10:03 +01:00
if (verbose)
printf ("Caching: per-thread\n");
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
doit ();
2003-11-20 02:10:03 +01:00
if (nerrors)
{
fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
exit (-1);
}
2003-11-20 02:10:03 +01:00
if (verbose)
printf ("SUCCESS\n");
return 0;
}