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> |
| 13 | #include <pthread.h> |
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 | |
| 37 | |
| 38 | #define NUMTHREADS 10 |
| 39 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
| 40 | pthread_t threads [ NUMTHREADS ]; |
| 41 | |
| 42 | void print_sizes ( size_t *first, size_t *last ) { |
| 43 | std::cout << "{ " << std::hex; |
| 44 | for ( size_t *iter = first; iter != last; ++iter ) |
| 45 | std::cout << *iter << " "; |
| 46 | std::cout << "}" << std::dec << std::endl; |
| 47 | } |
| 48 | |
| 49 | int main ( int argc, char *argv [] ) { |
| 50 | int retVal = 0; |
| 51 | |
| 52 | // Make the threads, let them run, and wait for them to finish |
| 53 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 54 | pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i)); |
| 55 | for ( int i = 0; i < NUMTHREADS; ++i ) |
| 56 | pthread_join ( threads [ i ], NULL ); |
| 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 | |
| 64 | // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); |
| 65 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
| 66 | for ( int i = 1; i < NUMTHREADS; ++i ) { |
| 67 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) |
| 68 | std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl; |
| 69 | retVal = 2; |
| 70 | } |
| 71 | // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); |
| 72 | |
| 73 | return retVal; |
| 74 | } |