Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 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 | |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 10 | #include "../src/config.h" |
| 11 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 12 | #include <cstdlib> |
| 13 | #include <algorithm> |
| 14 | #include <iostream> |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 15 | #include "../src/threading_support.h" |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 17 | |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 18 | #include "../src/cxa_exception.hpp" |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 19 | |
| 20 | typedef __cxxabiv1::__cxa_eh_globals globals_t ; |
| 21 | |
| 22 | void *thread_code (void *parm) { |
| 23 | size_t *result = (size_t *) parm; |
| 24 | globals_t *glob1, *glob2; |
| 25 | |
| 26 | glob1 = __cxxabiv1::__cxa_get_globals (); |
| 27 | if ( NULL == glob1 ) |
| 28 | std::cerr << "Got null result from __cxa_get_globals" << std::endl; |
| 29 | |
| 30 | glob2 = __cxxabiv1::__cxa_get_globals_fast (); |
| 31 | if ( glob1 != glob2 ) |
| 32 | std::cerr << "Got different globals!" << std::endl; |
| 33 | |
| 34 | *result = (size_t) glob1; |
| 35 | sleep ( 1 ); |
| 36 | return parm; |
| 37 | } |
| 38 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 39 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 40 | #define NUMTHREADS 10 |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 41 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
| 42 | __libcxxabi_thread_t threads [ NUMTHREADS ]; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 43 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 44 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame^] | 45 | int main () { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 46 | int retVal = 0; |
| 47 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 48 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 49 | // Make the threads, let them run, and wait for them to finish |
| 50 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 51 | __libcxxabi_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 52 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 53 | __libcxxabi_thread_join ( &threads [ i ] ); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 54 | |
| 55 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 56 | if ( 0 == thread_globals [ i ] ) { |
| 57 | std::cerr << "Thread #" << i << " had a zero global" << std::endl; |
| 58 | retVal = 1; |
| 59 | } |
| 60 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 61 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
Howard Hinnant | f8d292e | 2012-01-31 20:01:06 +0000 | [diff] [blame] | 62 | for ( int i = 1; i < NUMTHREADS; ++i ) |
| 63 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 64 | std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; |
| 65 | retVal = 2; |
| 66 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 67 | #else // _LIBCXXABI_HAS_NO_THREADS |
| 68 | size_t thread_globals; |
| 69 | // Check that __cxa_get_globals() is not NULL. |
| 70 | if (thread_code(&thread_globals) == 0) { |
| 71 | retVal = 1; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 72 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 73 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
| 74 | return retVal; |
| 75 | } |