Howard Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 1 | //===-------------------- test_exception_storage.cpp ----------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // 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 Hinnant | 987afbe | 2011-12-06 18:01:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 9 | #include <algorithm> |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 10 | #include <cstdio> |
| 11 | #include <cstdlib> |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 12 | #include <__threading_support> |
Howard Hinnant | 805036c | 2012-01-28 00:30:38 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 14 | |
Nico Weber | 086048d | 2019-08-12 19:11:23 +0000 | [diff] [blame] | 15 | #include "../src/cxa_exception.h" |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 16 | |
| 17 | typedef __cxxabiv1::__cxa_eh_globals globals_t ; |
| 18 | |
| 19 | void *thread_code (void *parm) { |
| 20 | size_t *result = (size_t *) parm; |
| 21 | globals_t *glob1, *glob2; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 22 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 23 | glob1 = __cxxabiv1::__cxa_get_globals (); |
| 24 | if ( NULL == glob1 ) |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 25 | std::printf("Got null result from __cxa_get_globals\n"); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 26 | |
| 27 | glob2 = __cxxabiv1::__cxa_get_globals_fast (); |
| 28 | if ( glob1 != glob2 ) |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 29 | std::printf("Got different globals!\n"); |
| 30 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 31 | *result = (size_t) glob1; |
Hafiz Abid Qadeer | 272279a | 2020-10-21 20:56:24 +0100 | [diff] [blame] | 32 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 33 | sleep ( 1 ); |
Hafiz Abid Qadeer | 272279a | 2020-10-21 20:56:24 +0100 | [diff] [blame] | 34 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 35 | return parm; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 36 | } |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 37 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 38 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 39 | #define NUMTHREADS 10 |
Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 40 | size_t thread_globals [ NUMTHREADS ] = { 0 }; |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 41 | std::__libcpp_thread_t threads [ NUMTHREADS ]; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 42 | #endif |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 43 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 44 | int main () { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 45 | int retVal = 0; |
| 46 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 47 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 48 | // Make the threads, let them run, and wait for them to finish |
| 49 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 50 | std::__libcpp_thread_create ( threads + i, thread_code, (void *) (thread_globals + i)); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 51 | for ( int i = 0; i < NUMTHREADS; ++i ) |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 52 | std::__libcpp_thread_join ( &threads [ i ] ); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 53 | |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 54 | for ( int i = 0; i < NUMTHREADS; ++i ) { |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 55 | if ( 0 == thread_globals [ i ] ) { |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 56 | std::printf("Thread #%d had a zero global\n", i); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 57 | retVal = 1; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 61 | std::sort ( thread_globals, thread_globals + NUMTHREADS ); |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 62 | for ( int i = 1; i < NUMTHREADS; ++i ) { |
Howard Hinnant | f8d292e | 2012-01-31 20:01:06 +0000 | [diff] [blame] | 63 | if ( thread_globals [ i - 1 ] == thread_globals [ i ] ) { |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 64 | std::printf("Duplicate thread globals (%d and %d)\n", i-1, i); |
Marshall Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 65 | retVal = 2; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 68 | #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 Clow | 1df50ca | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 73 | } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 74 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
| 75 | return retVal; |
| 76 | } |