1 //===----------------------------------------------------------------------===// 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 #include "test_macros.h" 18 19 typedef __cxxabiv1::__cxa_eh_globals globals_t ; 20 21 void *thread_code (void *parm) { 22 size_t *result = (size_t *) parm; 23 globals_t *glob1, *glob2; 24 25 glob1 = __cxxabiv1::__cxa_get_globals (); 26 if ( NULL == glob1 ) 27 std::printf("Got null result from __cxa_get_globals\n"); 28 29 glob2 = __cxxabiv1::__cxa_get_globals_fast (); 30 if ( glob1 != glob2 ) 31 std::printf("Got different globals!\n"); 32 33 *result = (size_t) glob1; 34 #ifndef TEST_HAS_NO_THREADS 35 sleep ( 1 ); 36 #endif 37 return parm; 38 } 39 40 #ifndef TEST_HAS_NO_THREADS 41 #define NUMTHREADS 10 42 size_t thread_globals [ NUMTHREADS ] = { 0 }; 43 std::__libcpp_thread_t threads [ NUMTHREADS ]; 44 #endif 45 46 int main() { 47 #ifndef TEST_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 int retVal = 0; 55 for ( int i = 0; i < NUMTHREADS; ++i ) { 56 if ( 0 == thread_globals [ i ] ) { 57 std::printf("Thread #%d had a zero global\n", i); 58 retVal = 1; 59 } 60 } 61 62 std::sort ( thread_globals, thread_globals + NUMTHREADS ); 63 for ( int i = 1; i < NUMTHREADS; ++i ) { 64 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { 65 std::printf("Duplicate thread globals (%d and %d)\n", i-1, i); 66 retVal = 2; 67 } 68 } 69 return retVal; 70 #else // TEST_HAS_NO_THREADS 71 size_t thread_globals; 72 thread_code(&thread_globals); 73 // Check that __cxa_get_globals() is not NULL. 74 return (thread_globals == 0) ? 1 : 0; 75 #endif // !TEST_HAS_NO_THREADS 76 } 77