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 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 10 | #include <cstdlib> |
| 11 | #include <algorithm> |
| 12 | #include <iostream> |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 13 | #include <__threading_support> |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 16 | #include "../src/cxa_exception.hpp" |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 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 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 37 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 38 | #define NUMTHREADS 10 |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 39 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 40 | std::__libcpp_thread_t threads [ NUMTHREADS ]; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 41 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 42 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 43 | int main () { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 44 | int retVal = 0; |
| 45 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 46 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 47 | // Make the threads, let them run, and wait for them to finish |
| 48 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 49 | std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 50 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 51 | std::__libcpp_thread_join ( &threads [ i ] ); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 59 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
Howard Hinnant | f8d292e | 2012-01-31 20:01:06 +0000 | [diff] [blame] | 60 | for ( int i = 1; i < NUMTHREADS; ++i ) |
| 61 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 62 | std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; |
| 63 | retVal = 2; |
| 64 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 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; |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 70 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 71 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
| 72 | return retVal; |
| 73 | } |