blob: 13a4a60c797c0e05a0924280cc2ccab33cedcaff [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 Rathnayake6d3ea682016-10-13 15:05:19 +000015#include "../src/threading_support.h"
Howard Hinnant805036c2012-01-28 00:30:38 +000016#include <unistd.h>
Marshall Clow1df50ca2011-07-20 14:53:53 +000017
Howard Hinnant805036c2012-01-28 00:30:38 +000018#include "../src/cxa_exception.hpp"
Marshall Clow1df50ca2011-07-20 14:53:53 +000019
20typedef __cxxabiv1::__cxa_eh_globals globals_t ;
21
22void *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
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000039#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000040#define NUMTHREADS 10
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000041size_t thread_globals [ NUMTHREADS ] = { 0 };
42__libcxxabi_thread_t threads [ NUMTHREADS ];
Jonathan Roelofs40e98422014-05-06 21:30:56 +000043#endif
Marshall Clow1df50ca2011-07-20 14:53:53 +000044
Eric Fiseliera140cba2016-12-24 00:37:13 +000045int main () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000046 int retVal = 0;
47
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000048#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000049// Make the threads, let them run, and wait for them to finish
50 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000051 __libcxxabi_thread_create ( threads + i, thread_code, (void *) (thread_globals + i));
Marshall Clow1df50ca2011-07-20 14:53:53 +000052 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000053 __libcxxabi_thread_join ( &threads [ i ] );
Marshall Clow1df50ca2011-07-20 14:53:53 +000054
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
Marshall Clow1df50ca2011-07-20 14:53:53 +000061 std::sort ( thread_globals, thread_globals + NUMTHREADS );
Howard Hinnantf8d292e2012-01-31 20:01:06 +000062 for ( int i = 1; i < NUMTHREADS; ++i )
63 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
Marshall Clow1df50ca2011-07-20 14:53:53 +000064 std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
65 retVal = 2;
66 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000067#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;
Marshall Clow1df50ca2011-07-20 14:53:53 +000072 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000073#endif // !_LIBCXXABI_HAS_NO_THREADS
74 return retVal;
75}