diff options
author | Jonathan Roelofs <jonathan@codesourcery.com> | 2014-05-06 21:30:56 +0000 |
---|---|---|
committer | Jonathan Roelofs <jonathan@codesourcery.com> | 2014-05-06 21:30:56 +0000 |
commit | c285efad792f81f323c18e7f7674164ad94b4059 (patch) | |
tree | 2bf957338933bedfc220172a30a30ac8e65958d3 | |
parent | 5db9aa48b796e95cf61ac02d31fd7a0c13a98bea (diff) |
On single threaded systems, turn mutexes into nops
http://reviews.llvm.org/D3386
git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@208135 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | src/config.h | 25 | ||||
-rw-r--r-- | src/cxa_exception.cpp | 6 | ||||
-rw-r--r-- | src/cxa_exception_storage.cpp | 14 | ||||
-rw-r--r-- | src/cxa_guard.cpp | 29 | ||||
-rw-r--r-- | src/fallback_malloc.ipp | 14 | ||||
-rw-r--r-- | test/test_exception_storage.cpp | 16 |
6 files changed, 96 insertions, 8 deletions
diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..7289447 --- /dev/null +++ b/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 <unistd.h> + +#if defined(_POSIX_THREADS) && _POSIX_THREADS > 0 +# define LIBCXXABI_SINGLE_THREADED 0 +#else +# define LIBCXXABI_SINGLE_THREADED 1 +#endif + +#endif // LIBCXXABI_CONFIG_H diff --git a/src/cxa_exception.cpp b/src/cxa_exception.cpp index 64040fd..9bc6ad6 100644 --- a/src/cxa_exception.cpp +++ b/src/cxa_exception.cpp @@ -11,13 +11,15 @@ // //===----------------------------------------------------------------------===// +#include "config.h" #include "cxxabi.h" #include <exception> // for std::terminate #include <cstdlib> // for malloc, free #include <cstring> // for memset -#include <pthread.h> - +#if !LIBCXXABI_SINGLE_THREADED +# include <pthread.h> // for fallback_malloc.ipp's mutexes +#endif #include "cxa_exception.hpp" #include "cxa_handlers.hpp" diff --git a/src/cxa_exception_storage.cpp b/src/cxa_exception_storage.cpp index c3ee856..6f902c6 100644 --- a/src/cxa_exception_storage.cpp +++ b/src/cxa_exception_storage.cpp @@ -13,7 +13,19 @@ #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 { diff --git a/src/cxa_guard.cpp b/src/cxa_guard.cpp index e403b64..b22fcbb 100644 --- a/src/cxa_guard.cpp +++ b/src/cxa_guard.cpp @@ -8,8 +8,11 @@ //===----------------------------------------------------------------------===// #include "abort_message.h" +#include "config.h" -#include <pthread.h> +#if !LIBCXXABI_SINGLE_THREADED +# include <pthread.h> +#endif #include <stdint.h> /* @@ -59,8 +62,10 @@ void set_initialized(guard_type* guard_object) { #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 @@ set_lock(uint32_t& x, lock_type y) extern "C" { +#if LIBCXXABI_SINGLE_THREADED +int __cxa_guard_acquire(guard_type* 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) { - char* initialized = (char*)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 @@ void __cxa_guard_abort(guard_type* guard_object) abort_message("__cxa_guard_abort failed to broadcast condition variable"); } +#endif // !LIBCXXABI_SINGLE_THREADED + } // extern "C" } // __cxxabiv1 diff --git a/src/fallback_malloc.ipp b/src/fallback_malloc.ipp index 4f8ce90..a97f3fd 100644 --- a/src/fallback_malloc.ipp +++ b/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 }; diff --git a/test/test_exception_storage.cpp b/test/test_exception_storage.cpp index e2b230a..f1d63fd 100644 --- a/test/test_exception_storage.cpp +++ b/test/test_exception_storage.cpp @@ -7,10 +7,14 @@ // //===----------------------------------------------------------------------===// +#include "../src/config.h" + #include <cstdlib> #include <algorithm> #include <iostream> -#include <pthread.h> +#if !LIBCXXABI_SINGLE_THREADED +# include <pthread.h> +#endif #include <unistd.h> #include "../src/cxa_exception.hpp" @@ -34,10 +38,11 @@ void *thread_code (void *parm) { 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 @@ void print_sizes ( size_t *first, size_t *last ) { 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 @@ int main ( int argc, char *argv [] ) { retVal = 2; } // print_sizes ( thread_globals, thread_globals + NUMTHREADS ); - + +#endif return retVal; } |