blob: 49c9b5d4963e3feb0fd67ab491300caab7d7b63d [file] [log] [blame]
Howard Hinnantb9f2cc82011-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
Eric Fiselier66c86472017-02-17 04:26:22 +000010// FIXME: cxa_exception.hpp directly references `std::unexpected` and friends.
11// This breaks this test when compiled in C++17. For now fix this by manually
12// re-enabling the STL functions.
13#define _LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000014
Marshall Clow61b898e2011-07-20 14:53:53 +000015#include <cstdlib>
16#include <algorithm>
17#include <iostream>
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000018#include <__threading_support>
Howard Hinnant27b00d82012-01-28 00:30:38 +000019#include <unistd.h>
Marshall Clow61b898e2011-07-20 14:53:53 +000020
Howard Hinnant27b00d82012-01-28 00:30:38 +000021#include "../src/cxa_exception.hpp"
Marshall Clow61b898e2011-07-20 14:53:53 +000022
23typedef __cxxabiv1::__cxa_eh_globals globals_t ;
24
25void *thread_code (void *parm) {
26 size_t *result = (size_t *) parm;
27 globals_t *glob1, *glob2;
28
29 glob1 = __cxxabiv1::__cxa_get_globals ();
30 if ( NULL == glob1 )
31 std::cerr << "Got null result from __cxa_get_globals" << std::endl;
32
33 glob2 = __cxxabiv1::__cxa_get_globals_fast ();
34 if ( glob1 != glob2 )
35 std::cerr << "Got different globals!" << std::endl;
36
37 *result = (size_t) glob1;
38 sleep ( 1 );
39 return parm;
40 }
41
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000042#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow61b898e2011-07-20 14:53:53 +000043#define NUMTHREADS 10
Asiri Rathnayake51806732016-10-13 15:05:19 +000044size_t thread_globals [ NUMTHREADS ] = { 0 };
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000045std::__libcpp_thread_t threads [ NUMTHREADS ];
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000046#endif
Marshall Clow61b898e2011-07-20 14:53:53 +000047
Eric Fiselier08bf03c2016-12-24 00:37:13 +000048int main () {
Marshall Clow61b898e2011-07-20 14:53:53 +000049 int retVal = 0;
50
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000051#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow61b898e2011-07-20 14:53:53 +000052// Make the threads, let them run, and wait for them to finish
53 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000054 std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i));
Marshall Clow61b898e2011-07-20 14:53:53 +000055 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000056 std::__libcpp_thread_join ( &threads [ i ] );
Marshall Clow61b898e2011-07-20 14:53:53 +000057
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
Marshall Clow61b898e2011-07-20 14:53:53 +000064 std::sort ( thread_globals, thread_globals + NUMTHREADS );
Howard Hinnant6953d902012-01-31 20:01:06 +000065 for ( int i = 1; i < NUMTHREADS; ++i )
66 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
Marshall Clow61b898e2011-07-20 14:53:53 +000067 std::cerr << "Duplicate thread globals (" << i-1 << " and " << i << ")" << std::endl;
68 retVal = 2;
69 }
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000070#else // _LIBCXXABI_HAS_NO_THREADS
71 size_t thread_globals;
72 // Check that __cxa_get_globals() is not NULL.
73 if (thread_code(&thread_globals) == 0) {
74 retVal = 1;
Marshall Clow61b898e2011-07-20 14:53:53 +000075 }
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000076#endif // !_LIBCXXABI_HAS_NO_THREADS
77 return retVal;
78}