1 //===-------------------- test_exception_storage.cpp ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <cstdlib> 12 #include <__threading_support> 13 #include <unistd.h> 14 15 #include "../src/cxa_exception.h" 16 17 typedef __cxxabiv1::__cxa_eh_globals globals_t ; 18 19 void *thread_code (void *parm) { 20 size_t *result = (size_t *) parm; 21 globals_t *glob1, *glob2; 22 23 glob1 = __cxxabiv1::__cxa_get_globals (); 24 if ( NULL == glob1 ) 25 std::printf("Got null result from __cxa_get_globals\n"); 26 27 glob2 = __cxxabiv1::__cxa_get_globals_fast (); 28 if ( glob1 != glob2 ) 29 std::printf("Got different globals!\n"); 30 31 *result = (size_t) glob1; 32 #ifndef _LIBCXXABI_HAS_NO_THREADS 33 sleep ( 1 ); 34 #endif 35 return parm; 36 } 37 38 #ifndef _LIBCXXABI_HAS_NO_THREADS 39 #define NUMTHREADS 10 40 size_t thread_globals [ NUMTHREADS ] = { 0 }; 41 std::__libcpp_thread_t threads [ NUMTHREADS ]; 42 #endif 43 44 int main () { 45 int retVal = 0; 46 47 #ifndef _LIBCXXABI_HAS_NO_THREADS 48 // Make the threads, let them run, and wait for them to finish 49 for ( int i = 0; i < NUMTHREADS; ++i ) 50 std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); 51 for ( int i = 0; i < NUMTHREADS; ++i ) 52 std::__libcpp_thread_join ( &threads [ i ] ); 53 54 for ( int i = 0; i < NUMTHREADS; ++i ) { 55 if ( 0 == thread_globals [ i ] ) { 56 std::printf("Thread #%d had a zero global\n", i); 57 retVal = 1; 58 } 59 } 60 61 std::sort ( thread_globals, thread_globals + NUMTHREADS ); 62 for ( int i = 1; i < NUMTHREADS; ++i ) { 63 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { 64 std::printf("Duplicate thread globals (%d and %d)\n", i-1, i); 65 retVal = 2; 66 } 67 } 68 #else // _LIBCXXABI_HAS_NO_THREADS 69 size_t thread_globals; 70 // Check that __cxa_get_globals() is not NULL. 71 if (thread_code(&thread_globals) == 0) { 72 retVal = 1; 73 } 74 #endif // !_LIBCXXABI_HAS_NO_THREADS 75 return retVal; 76 } 77