blob: bf423667b7734cc9224911330f7ec0843c3547e9 [file] [log] [blame]
Howard Hinnant987afbe2011-12-06 18:01:47 +00001//===-------------------- test_exception_storage.cpp ----------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant987afbe2011-12-06 18:01:47 +00006//
7//===----------------------------------------------------------------------===//
8
Marshall Clow1df50ca2011-07-20 14:53:53 +00009#include <algorithm>
Louis Dionnecc69d212020-10-13 15:47:31 -040010#include <cstdio>
11#include <cstdlib>
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000012#include <__threading_support>
Howard Hinnant805036c2012-01-28 00:30:38 +000013#include <unistd.h>
Marshall Clow1df50ca2011-07-20 14:53:53 +000014
Nico Weber086048d2019-08-12 19:11:23 +000015#include "../src/cxa_exception.h"
Marshall Clow1df50ca2011-07-20 14:53:53 +000016
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;
Louis Dionnecc69d212020-10-13 15:47:31 -040022
Marshall Clow1df50ca2011-07-20 14:53:53 +000023 glob1 = __cxxabiv1::__cxa_get_globals ();
24 if ( NULL == glob1 )
Louis Dionnecc69d212020-10-13 15:47:31 -040025 std::printf("Got null result from __cxa_get_globals\n");
Marshall Clow1df50ca2011-07-20 14:53:53 +000026
27 glob2 = __cxxabiv1::__cxa_get_globals_fast ();
28 if ( glob1 != glob2 )
Louis Dionnecc69d212020-10-13 15:47:31 -040029 std::printf("Got different globals!\n");
30
Marshall Clow1df50ca2011-07-20 14:53:53 +000031 *result = (size_t) glob1;
Hafiz Abid Qadeer272279a2020-10-21 20:56:24 +010032#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000033 sleep ( 1 );
Hafiz Abid Qadeer272279a2020-10-21 20:56:24 +010034#endif
Marshall Clow1df50ca2011-07-20 14:53:53 +000035 return parm;
Louis Dionnecc69d212020-10-13 15:47:31 -040036}
Marshall Clow1df50ca2011-07-20 14:53:53 +000037
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000038#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000039#define NUMTHREADS 10
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +000040size_t thread_globals [ NUMTHREADS ] = { 0 };
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000041std::__libcpp_thread_t threads [ NUMTHREADS ];
Jonathan Roelofs40e98422014-05-06 21:30:56 +000042#endif
Marshall Clow1df50ca2011-07-20 14:53:53 +000043
Eric Fiseliera140cba2016-12-24 00:37:13 +000044int main () {
Marshall Clow1df50ca2011-07-20 14:53:53 +000045 int retVal = 0;
46
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000047#ifndef _LIBCXXABI_HAS_NO_THREADS
Marshall Clow1df50ca2011-07-20 14:53:53 +000048// Make the threads, let them run, and wait for them to finish
49 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000050 std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i));
Marshall Clow1df50ca2011-07-20 14:53:53 +000051 for ( int i = 0; i < NUMTHREADS; ++i )
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000052 std::__libcpp_thread_join ( &threads [ i ] );
Marshall Clow1df50ca2011-07-20 14:53:53 +000053
Louis Dionnecc69d212020-10-13 15:47:31 -040054 for ( int i = 0; i < NUMTHREADS; ++i ) {
Marshall Clow1df50ca2011-07-20 14:53:53 +000055 if ( 0 == thread_globals [ i ] ) {
Louis Dionnecc69d212020-10-13 15:47:31 -040056 std::printf("Thread #%d had a zero global\n", i);
Marshall Clow1df50ca2011-07-20 14:53:53 +000057 retVal = 1;
Louis Dionnecc69d212020-10-13 15:47:31 -040058 }
59 }
60
Marshall Clow1df50ca2011-07-20 14:53:53 +000061 std::sort ( thread_globals, thread_globals + NUMTHREADS );
Louis Dionnecc69d212020-10-13 15:47:31 -040062 for ( int i = 1; i < NUMTHREADS; ++i ) {
Howard Hinnantf8d292e2012-01-31 20:01:06 +000063 if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) {
Louis Dionnecc69d212020-10-13 15:47:31 -040064 std::printf("Duplicate thread globals (%d and %d)\n", i-1, i);
Marshall Clow1df50ca2011-07-20 14:53:53 +000065 retVal = 2;
Louis Dionnecc69d212020-10-13 15:47:31 -040066 }
67 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000068#else // _LIBCXXABI_HAS_NO_THREADS
69 size_t thread_globals;
70 // Check that __cxa_get_globals() is not NULL.
71 if (thread_code(&thread_globals) == 0) {
72 retVal = 1;
Marshall Clow1df50ca2011-07-20 14:53:53 +000073 }
Asiri Rathnayake7c98baa2016-09-21 09:09:32 +000074#endif // !_LIBCXXABI_HAS_NO_THREADS
75 return retVal;
76}