test_exception_storage.pass.cpp (2338B)
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 <__threading_support> 14 #include <unistd.h> 15 16 #include "../src/cxa_exception.hpp" 17 18 typedef __cxxabiv1::__cxa_eh_globals globals_t ; 19 20 void *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 #ifndef _LIBCXXABI_HAS_NO_THREADS 38 #define NUMTHREADS 10 39 size_t thread_globals [ NUMTHREADS ] = { 0 }; 40 std::__libcpp_thread_t threads [ NUMTHREADS ]; 41 #endif 42 43 int main () { 44 int retVal = 0; 45 46 #ifndef _LIBCXXABI_HAS_NO_THREADS 47 // Make the threads, let them run, and wait for them to finish 48 for ( int i = 0; i < NUMTHREADS; ++i ) 49 std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); 50 for ( int i = 0; i < NUMTHREADS; ++i ) 51 std::__libcpp_thread_join ( &threads [ i ] ); 52 53 for ( int i = 0; i < NUMTHREADS; ++i ) 54 if ( 0 == thread_globals [ i ] ) { 55 std::cerr << "Thread #" << i << " had a zero global" << std::endl; 56 retVal = 1; 57 } 58 59 std::sort ( thread_globals, thread_globals + NUMTHREADS ); 60 for ( int i = 1; i < NUMTHREADS; ++i ) 61 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { 62 std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; 63 retVal = 2; 64 } 65 #else // _LIBCXXABI_HAS_NO_THREADS 66 size_t thread_globals; 67 // Check that __cxa_get_globals() is not NULL. 68 if (thread_code(&thread_globals) == 0) { 69 retVal = 1; 70 } 71 #endif // !_LIBCXXABI_HAS_NO_THREADS 72 return retVal; 73 }