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