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 "../src/config.h" 11 12 #include <cstdlib> 13 #include <algorithm> 14 #include <iostream> 15 #if !LIBCXXABI_SINGLE_THREADED 16 # include <pthread.h> 17 #endif 18 #include <unistd.h> 19 20 #include "../src/cxa_exception.hpp" 21 22 typedef __cxxabiv1::__cxa_eh_globals globals_t ; 23 24 void *thread_code (void *parm) { 25 size_t *result = (size_t *) parm; 26 globals_t *glob1, *glob2; 27 28 glob1 = __cxxabiv1::__cxa_get_globals (); 29 if ( NULL == glob1 ) { 30 std::cerr << "Got null result from __cxa_get_globals" << std::endl; 31 return 0; 32 } 33 34 glob2 = __cxxabiv1::__cxa_get_globals_fast (); 35 if ( glob1 != glob2 ) { 36 std::cerr << "Got different globals!" << std::endl; 37 return 0; 38 } 39 40 *result = (size_t) glob1; 41 #if !LIBCXXABI_SINGLE_THREADED 42 sleep ( 1 ); 43 #endif 44 return parm; 45 } 46 47 #if !LIBCXXABI_SINGLE_THREADED 48 #define NUMTHREADS 10 49 size_t thread_globals [ NUMTHREADS ] = { 0 }; 50 pthread_t threads [ NUMTHREADS ]; 51 #endif 52 53 void print_sizes ( size_t *first, size_t *last ) { 54 std::cout << "{ " << std::hex; 55 for ( size_t *iter = first; iter != last; ++iter ) 56 std::cout << *iter << " "; 57 std::cout << "}" << std::dec << std::endl; 58 } 59 60 int main ( int argc, char *argv [] ) { 61 int retVal = 0; 62 63 #if LIBCXXABI_SINGLE_THREADED 64 size_t thread_globals; 65 retVal = thread_code(&thread_globals) != &thread_globals; 66 #else 67 // Make the threads, let them run, and wait for them to finish 68 for ( int i = 0; i < NUMTHREADS; ++i ) 69 pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i)); 70 for ( int i = 0; i < NUMTHREADS; ++i ) 71 pthread_join ( threads [ i ], NULL ); 72 73 for ( int i = 0; i < NUMTHREADS; ++i ) 74 if ( 0 == thread_globals [ i ] ) { 75 std::cerr << "Thread #" << i << " had a zero global" << std::endl; 76 retVal = 1; 77 } 78 79 // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); 80 std::sort ( thread_globals, thread_globals + NUMTHREADS ); 81 for ( int i = 1; i < NUMTHREADS; ++i ) 82 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { 83 std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; 84 retVal = 2; 85 } 86 // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); 87 88 #endif 89 return retVal; 90 } 91