Home | History | Annotate | Download | only in test
      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 
     10 #include "../src/config.h"
     11 
     12 #include <cstdlib>
     13 #include <algorithm>
     14 #include <iostream>
     15 #include <__threading_support>
     16 #include <unistd.h>
     17 
     18 #include "../src/cxa_exception.hpp"
     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 
     39 #ifndef _LIBCXXABI_HAS_NO_THREADS
     40 #define NUMTHREADS  10
     41 size_t                 thread_globals [ NUMTHREADS ] = { 0 };
     42 std::__libcpp_thread_t   threads        [ NUMTHREADS ];
     43 #endif
     44 
     45 int main () {
     46     int retVal = 0;
     47 
     48 #ifndef _LIBCXXABI_HAS_NO_THREADS
     49 //  Make the threads, let them run, and wait for them to finish
     50     for ( int i = 0; i < NUMTHREADS; ++i )
     51         std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i));
     52     for ( int i = 0; i < NUMTHREADS; ++i )
     53         std::__libcpp_thread_join ( &threads [ i ] );
     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 
     61     std::sort ( thread_globals, thread_globals + NUMTHREADS );
     62     for ( int i = 1; i < NUMTHREADS; ++i )
     63         if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
     64             std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
     65             retVal = 2;
     66             }
     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;
     72     }
     73 #endif // !_LIBCXXABI_HAS_NO_THREADS
     74     return retVal;
     75 }
     76