blob: 9081246928147f5e50133fa28324de442e2ffdae [file] [log] [blame]
Howard Hinnant987afbe2011-12-06 18:01:47 +00001//===-------------------- 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 Roelofs40e98422014-05-06 21:30:56 +000010#include "../src/config.h"
11
Marshall Clow1df50ca2011-07-20 14:53:53 +000012#include <cstdlib>
13#include <algorithm>
14#include <iostream>
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000015#ifndef _LIBCXXABI_HAS_NO_THREADS
Jonathan Roelofs40e98422014-05-06 21:30:56 +000016# include <pthread.h>
17#endif
Howard Hinnant805036c2012-01-28 00:30:38 +000018#include <unistd.h>
Marshall Clow1df50ca2011-07-20 14:53:53 +000019
Howard Hinnant805036c2012-01-28 00:30:38 +000020#include "../src/cxa_exception.hpp"
Marshall Clow1df50ca2011-07-20 14:53:53 +000021
22typedef __cxxabiv1::__cxa_eh_globals globals_t ;
23
24void *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
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000041#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000042#define NUMTHREADS 10
43size_t thread_globals [ NUMTHREADS ] = { 0 };
44pthread_t threads [ NUMTHREADS ];
Jonathan Roelofs40e98422014-05-06 21:30:56 +000045#endif
Marshall Clow1df50ca2011-07-20 14:53:53 +000046
Marshall Clow1df50ca2011-07-20 14:53:53 +000047int main ( int argc, char *argv [] ) {
48 int retVal = 0;
49
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000050#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000051// 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
Marshall Clow1df50ca2011-07-20 14:53:53 +000063 std::sort ( thread_globals, thread_globals + NUMTHREADS );
Howard Hinnantf8d292e2012-01-31 20:01:06 +000064 for ( int i = 1; i < NUMTHREADS; ++i )
65 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
Marshall Clow1df50ca2011-07-20 14:53:53 +000066 std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
67 retVal = 2;
68 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000069#else // _LIBCXXABI_HAS_NO_THREADS
70 size_t thread_globals;
71 // Check that __cxa_get_globals() is not NULL.
72 if (thread_code(&thread_globals) == 0) {
73 retVal = 1;
Marshall Clow1df50ca2011-07-20 14:53:53 +000074 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000075#endif // !_LIBCXXABI_HAS_NO_THREADS
76 return retVal;
77}