Index: src/config.h =================================================================== --- src/config.h +++ src/config.h @@ -0,0 +1,25 @@ +//===----------------------------- config.h -------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +// +// Defines macros used within the libc++abi project. +// +//===----------------------------------------------------------------------===// + + +#ifndef LIBCXXABI_CONFIG_H +#define LIBCXXABI_CONFIG_H + +#include + +#if defined(_POSIX_THREADS) && _POSIX_THREADS > 0 +# define LIBCXXABI_SINGLE_THREADED 0 +#else +# define LIBCXXABI_SINGLE_THREADED 1 +#endif + +#endif // LIBCXXABI_CONFIG_H Index: src/cxa_exception.cpp =================================================================== --- src/cxa_exception.cpp +++ src/cxa_exception.cpp @@ -11,13 +11,15 @@ // //===----------------------------------------------------------------------===// +#include "config.h" #include "cxxabi.h" #include // for std::terminate #include // for malloc, free #include // for memset -#include - +#if !LIBCXXABI_SINGLE_THREADED +# include // for fallback_malloc.ipp's mutexes +#endif #include "cxa_exception.hpp" #include "cxa_handlers.hpp" Index: src/cxa_exception_storage.cpp =================================================================== --- src/cxa_exception_storage.cpp +++ src/cxa_exception_storage.cpp @@ -13,10 +13,22 @@ #include "cxa_exception.hpp" -#ifdef HAS_THREAD_LOCAL +#include "config.h" +#if LIBCXXABI_SINGLE_THREADED + namespace __cxxabiv1 { +extern "C" { + static __cxa_eh_globals eh_globals; + __cxa_eh_globals *__cxa_get_globals() { return &eh_globals; } + __cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; } + } +} +#elif defined(HAS_THREAD_LOCAL) + +namespace __cxxabiv1 { + namespace { __cxa_eh_globals * __globals () { static thread_local __cxa_eh_globals eh_globals; Index: src/cxa_guard.cpp =================================================================== --- src/cxa_guard.cpp +++ src/cxa_guard.cpp @@ -8,8 +8,11 @@ //===----------------------------------------------------------------------===// #include "abort_message.h" +#include "config.h" -#include +#if !LIBCXXABI_SINGLE_THREADED +# include +#endif #include /* @@ -59,8 +62,10 @@ #endif +#if !LIBCXXABI_SINGLE_THREADED pthread_mutex_t guard_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t guard_cv = PTHREAD_COND_INITIALIZER; +#endif #if defined(__APPLE__) && !defined(__arm__) @@ -161,9 +166,27 @@ extern "C" { +#if LIBCXXABI_SINGLE_THREADED int __cxa_guard_acquire(guard_type* guard_object) { - char* initialized = (char*)guard_object; + return !is_initialized(guard_object); +} + +void __cxa_guard_release(guard_type* guard_object) +{ + *guard_object = 0; + set_initialized(guard_object); +} + +void __cxa_guard_abort(guard_type* guard_object) +{ + *guard_object = 0; +} + +#else // !LIBCXXABI_SINGLE_THREADED + +int __cxa_guard_acquire(guard_type* guard_object) +{ if (pthread_mutex_lock(&guard_mut)) abort_message("__cxa_guard_acquire failed to acquire mutex"); int result = *initialized == 0; @@ -226,6 +249,8 @@ abort_message("__cxa_guard_abort failed to broadcast condition variable"); } +#endif // !LIBCXXABI_SINGLE_THREADED + } // extern "C" } // __cxxabiv1 Index: src/fallback_malloc.ipp =================================================================== --- src/fallback_malloc.ipp +++ src/fallback_malloc.ipp @@ -11,6 +11,8 @@ // //===----------------------------------------------------------------------===// +#include "config.h" + // A small, simple heap manager based (loosely) on // the startup heap manager from FreeBSD, optimized for space. // @@ -23,16 +25,28 @@ namespace { +// When POSIX threads are not available, make the mutex operations a nop +#if LIBCXXABI_SINGLE_THREADED +static void * heap_mutex = 0; +#else static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif class mutexor { public: +#if LIBCXXABI_SINGLE_THREADED + mutexor ( void * ) {} + ~mutexor () {} +#else mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } ~mutexor () { pthread_mutex_unlock ( mtx_ ); } +#endif private: mutexor ( const mutexor &rhs ); mutexor & operator = ( const mutexor &rhs ); +#if !LIBCXXABI_SINGLE_THREADED pthread_mutex_t *mtx_; +#endif }; Index: test/test_exception_storage.cpp =================================================================== --- test/test_exception_storage.cpp +++ test/test_exception_storage.cpp @@ -7,10 +7,14 @@ // //===----------------------------------------------------------------------===// +#include "../src/config.h" + #include #include #include -#include +#if !LIBCXXABI_SINGLE_THREADED +# include +#endif #include #include "../src/cxa_exception.hpp" @@ -34,10 +38,11 @@ return parm; } - +#if !LIBCXXABI_SINGLE_THREADED #define NUMTHREADS 10 size_t thread_globals [ NUMTHREADS ] = { 0 }; pthread_t threads [ NUMTHREADS ]; +#endif void print_sizes ( size_t *first, size_t *last ) { std::cout << "{ " << std::hex; @@ -49,6 +54,10 @@ int main ( int argc, char *argv [] ) { int retVal = 0; +#if LIBCXXABI_SINGLE_THREADED + size_t thread_globals; + retVal = thread_code(&thread_globals) != 0; +#else // Make the threads, let them run, and wait for them to finish for ( int i = 0; i < NUMTHREADS; ++i ) pthread_create( threads + i, NULL, thread_code, (void *) (thread_globals + i)); @@ -69,6 +78,7 @@ retVal = 2; } // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); - + +#endif return retVal; }