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> |
Jonathan Roelofs | 3b7f085 | 2014-09-05 17:46:40 +0000 | [diff] [blame] | 15 | #if !LIBCXXABI_HAS_NO_THREADS |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 16 | # include <pthread.h> |
| 17 | #endif |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 19 | |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 20 | #include "../src/cxa_exception.hpp" |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 21 | |
| 22 | typedef __cxxabiv1::__cxa_eh_globals globals_t ; |
| 23 | |
| 24 | void *thread_code (void *parm) { |
| 25 | size_t *result = (size_t *) parm; |
| 26 | globals_t *glob1, *glob2; |
| 27 | |
| 28 | glob1 = __cxxabiv1::__cxa_get_globals (); |
| 29 | if ( NULL == glob1 ) |
| 30 | std::cerr << "Got null result from __cxa_get_globals" << std::endl; |
| 31 | |
| 32 | glob2 = __cxxabiv1::__cxa_get_globals_fast (); |
| 33 | if ( glob1 != glob2 ) |
| 34 | std::cerr << "Got different globals!" << std::endl; |
| 35 | |
| 36 | *result = (size_t) glob1; |
| 37 | sleep ( 1 ); |
| 38 | return parm; |
| 39 | } |
| 40 | |
Jonathan Roelofs | 3b7f085 | 2014-09-05 17:46:40 +0000 | [diff] [blame] | 41 | #if !LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 42 | #define NUMTHREADS 10 |
| 43 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
| 44 | pthread_t threads [ NUMTHREADS ]; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 45 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 46 | |
| 47 | void print_sizes ( size_t *first, size_t *last ) { |
| 48 | std::cout << "{ " << std::hex; |
| 49 | for ( size_t *iter = first; iter != last; ++iter ) |
| 50 | std::cout << *iter << " "; |
| 51 | std::cout << "}" << std::dec << std::endl; |
| 52 | } |
| 53 | |
| 54 | int main ( int argc, char *argv [] ) { |
| 55 | int retVal = 0; |
| 56 | |
Jonathan Roelofs | 3b7f085 | 2014-09-05 17:46:40 +0000 | [diff] [blame] | 57 | #if LIBCXXABI_HAS_NO_THREADS |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 58 | size_t thread_globals; |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 +0000 | [diff] [blame] | 59 | // Check that __cxa_get_globals() is not NULL. |
| 60 | if (thread_code(&thread_globals) == 0) { |
| 61 | retVal = 1; |
| 62 | } |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 63 | #else |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 64 | // Make the threads, let them run, and wait for them to finish |
| 65 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 66 | pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i)); |
| 67 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 68 | pthread_join ( threads [ i ], NULL ); |
| 69 | |
| 70 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 71 | if ( 0 == thread_globals [ i ] ) { |
| 72 | std::cerr << "Thread #" << i << " had a zero global" << std::endl; |
| 73 | retVal = 1; |
| 74 | } |
| 75 | |
| 76 | // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); |
| 77 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
Howard Hinnant | f8d292e | 2012-01-31 20:01:06 +0000 | [diff] [blame] | 78 | for ( int i = 1; i < NUMTHREADS; ++i ) |
| 79 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 80 | std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; |
| 81 | retVal = 2; |
| 82 | } |
| 83 | // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 84 | |
| 85 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 86 | return retVal; |
| 87 | } |