test_exception_storage.cpp revision 6953d901c3fd95e27241b305f35a0fc97bd881e6
1//===-------------------- test_exception_storage.cpp ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <cstdlib>
11#include <algorithm>
12#include <iostream>
13#include <pthread.h>
14#include <unistd.h>
15
16#include "../src/cxa_exception.hpp"
17
18typedef __cxxabiv1::__cxa_eh_globals globals_t ;
19
20void *thread_code (void *parm) {
21    size_t *result = (size_t *) parm;
22    globals_t *glob1, *glob2;
23
24    glob1 = __cxxabiv1::__cxa_get_globals ();
25    if ( NULL == glob1 )
26        std::cerr << "Got null result from __cxa_get_globals" << std::endl;
27
28    glob2 = __cxxabiv1::__cxa_get_globals_fast ();
29    if ( glob1 != glob2 )
30        std::cerr << "Got different globals!" << std::endl;
31
32    *result = (size_t) glob1;
33    sleep ( 1 );
34    return parm;
35    }
36
37
38#define NUMTHREADS  10
39size_t      thread_globals [ NUMTHREADS ] = { 0 };
40pthread_t   threads        [ NUMTHREADS ];
41
42void print_sizes ( size_t *first, size_t *last ) {
43    std::cout << "{ " << std::hex;
44    for ( size_t *iter = first; iter != last; ++iter )
45        std::cout << *iter << " ";
46    std::cout << "}" << std::dec << std::endl;
47    }
48
49int main ( int argc, char *argv [] ) {
50    int retVal = 0;
51
52//  Make the threads, let them run, and wait for them to finish
53    for ( int i = 0; i < NUMTHREADS; ++i )
54        pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i));
55    for ( int i = 0; i < NUMTHREADS; ++i )
56        pthread_join ( threads [ i ], NULL );
57
58    for ( int i = 0; i < NUMTHREADS; ++i )
59        if ( 0 == thread_globals [ i ] ) {
60            std::cerr << "Thread #" << i << " had a zero global" << std::endl;
61            retVal = 1;
62            }
63
64//  print_sizes ( thread_globals, thread_globals + NUMTHREADS );
65    std::sort ( thread_globals, thread_globals + NUMTHREADS );
66    for ( int i = 1; i < NUMTHREADS; ++i )
67        if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
68            std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
69            retVal = 2;
70            }
71//  print_sizes ( thread_globals, thread_globals + NUMTHREADS );
72
73    return retVal;
74    }
75