Howard Hinnant | b9f2cc8 | 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 | |
Eric Fiselier | 66c8647 | 2017-02-17 04:26:22 +0000 | [diff] [blame] | 10 | // FIXME: cxa_exception.hpp directly references `std::unexpected` and friends. |
| 11 | // This breaks this test when compiled in C++17. For now fix this by manually |
| 12 | // re-enabling the STL functions. |
| 13 | #define _LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 14 | |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 15 | #include <cstdlib> |
| 16 | #include <algorithm> |
| 17 | #include <iostream> |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 18 | #include <__threading_support> |
Howard Hinnant | 27b00d8 | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 20 | |
Howard Hinnant | 27b00d8 | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 21 | #include "../src/cxa_exception.hpp" |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 22 | |
| 23 | typedef __cxxabiv1::__cxa_eh_globals globals_t ; |
| 24 | |
| 25 | void *thread_code (void *parm) { |
| 26 | size_t *result = (size_t *) parm; |
| 27 | globals_t *glob1, *glob2; |
| 28 | |
| 29 | glob1 = __cxxabiv1::__cxa_get_globals (); |
| 30 | if ( NULL == glob1 ) |
| 31 | std::cerr << "Got null result from __cxa_get_globals" << std::endl; |
| 32 | |
| 33 | glob2 = __cxxabiv1::__cxa_get_globals_fast (); |
| 34 | if ( glob1 != glob2 ) |
| 35 | std::cerr << "Got different globals!" << std::endl; |
| 36 | |
| 37 | *result = (size_t) glob1; |
| 38 | sleep ( 1 ); |
| 39 | return parm; |
| 40 | } |
| 41 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 42 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 43 | #define NUMTHREADS 10 |
Asiri Rathnayake | 5180673 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 44 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 45 | std::__libcpp_thread_t threads [ NUMTHREADS ]; |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 46 | #endif |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 47 | |
Eric Fiselier | 08bf03c | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 48 | int main () { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 49 | int retVal = 0; |
| 50 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 51 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 52 | // Make the threads, let them run, and wait for them to finish |
| 53 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 54 | std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 55 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 56 | std::__libcpp_thread_join ( &threads [ i ] ); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 57 | |
| 58 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 59 | if ( 0 == thread_globals [ i ] ) { |
| 60 | std::cerr << "Thread #" << i << " had a zero global" << std::endl; |
| 61 | retVal = 1; |
| 62 | } |
| 63 | |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 64 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
Howard Hinnant | 6953d90 | 2012-01-31 20:01:06 +0000 | [diff] [blame] | 65 | for ( int i = 1; i < NUMTHREADS; ++i ) |
| 66 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 67 | std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; |
| 68 | retVal = 2; |
| 69 | } |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 70 | #else // _LIBCXXABI_HAS_NO_THREADS |
| 71 | size_t thread_globals; |
| 72 | // Check that __cxa_get_globals() is not NULL. |
| 73 | if (thread_code(&thread_globals) == 0) { |
| 74 | retVal = 1; |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 75 | } |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 76 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
| 77 | return retVal; |
| 78 | } |