test_exception_storage.cpp revision b9f2cc816dc3e5369507db9509bea6d10b524a94
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 <cstdlib>
11#include <algorithm>
12#include <iostream>
13#include <pthread.h>
14
15#include "cxa_exception.hpp"
16
17typedef __cxxabiv1::__cxa_eh_globals globals_t ;
18
19void *thread_code (void *parm) {
20    size_t *result = (size_t *) parm;
21    globals_t *glob1, *glob2;
22
23    glob1 = __cxxabiv1::__cxa_get_globals ();
24    if ( NULL == glob1 )
25        std::cerr << "Got null result from __cxa_get_globals" << std::endl;
26
27    glob2 = __cxxabiv1::__cxa_get_globals_fast ();
28    if ( glob1 != glob2 )
29        std::cerr << "Got different globals!" << std::endl;
30
31    *result = (size_t) glob1;
32    sleep ( 1 );
33    return parm;
34    }
35
36
37#define NUMTHREADS  10
38size_t      thread_globals [ NUMTHREADS ] = { 0 };
39pthread_t   threads        [ NUMTHREADS ];
40
41void print_sizes ( size_t *first, size_t *last ) {
42    std::cout << "{ " << std::hex;
43    for ( size_t *iter = first; iter != last; ++iter )
44        std::cout << *iter << " ";
45    std::cout << "}" << std::dec << std::endl;
46    }
47
48int main ( int argc, char *argv [] ) {
49    int retVal = 0;
50
51//  Make the threads, let them run, and wait for them to finish
52    for ( int i = 0; i < NUMTHREADS; ++i )
53        pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i));
54    for ( int i = 0; i < NUMTHREADS; ++i )
55        pthread_join ( threads [ i ], NULL );
56
57    for ( int i = 0; i < NUMTHREADS; ++i )
58        if ( 0 == thread_globals [ i ] ) {
59            std::cerr << "Thread #" << i << " had a zero global" << std::endl;
60            retVal = 1;
61            }
62
63//  print_sizes ( thread_globals, thread_globals + NUMTHREADS );
64    std::sort ( thread_globals, thread_globals + NUMTHREADS );
65    for ( int i = 1; i < NUMTHREADS; ++i ) {
66        if ( thread_globals [ i - 1 ] == thread_globals [ i ] )
67            std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
68            retVal = 2;
69            }
70//  print_sizes ( thread_globals, thread_globals + NUMTHREADS );
71
72    return retVal;
73    }
74