Index: include/__cxxabi_config.h =================================================================== --- include/__cxxabi_config.h +++ include/__cxxabi_config.h @@ -33,6 +33,16 @@ #define _LIBCXXABI_FUNC_VIS __declspec(dllimport) #define _LIBCXXABI_TYPE_VIS __declspec(dllimport) #endif + #if defined(_MSC_VER) && !defined(__clang__) + // Using Microsoft Visual C++ compiler + #define _LIBCXXABI_INLINE_VISIBILITY __forceinline + #else + #if __has_attribute(__internal_linkage__) + #define _LIBCXXABI_INLINE_VISIBILITY __attribute__ ((__internal_linkage__, __always_inline__)) + #else + #define _LIBCXXABI_INLINE_VISIBILITY __attribute__ ((__always_inline__)) + #endif + #endif #else #define _LIBCXXABI_HIDDEN __attribute__((__visibility__("hidden"))) #define _LIBCXXABI_DATA_VIS __attribute__((__visibility__("default"))) @@ -42,6 +52,11 @@ #else #define _LIBCXXABI_TYPE_VIS __attribute__((__visibility__("default"))) #endif + #if __has_attribute(__internal_linkage__) + #define _LIBCXXABI_INLINE_VISIBILITY __attribute__ ((__internal_linkage__, __always_inline__)) + #else + #define _LIBCXXABI_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__)) + #endif #endif #endif // ____CXXABI_CONFIG_H Index: src/config.h =================================================================== --- src/config.h +++ src/config.h @@ -16,6 +16,35 @@ #include +#define _LIBCXXABI_STRINGIFY_EXPANDED(_value_) #_value_ +#define _LIBCXXABI_STRINGIFY(_value_) _LIBCXXABI_STRINGIFY_EXPANDED(_value_) + +#if defined(__GNUC__) || defined(__clang__) +#define _LIBCXXABI_PRAGMA(_parameter_) _Pragma(#_parameter_) +#define _LIBCXXABI_WARNING(_message_) _LIBCXXABI_PRAGMA(GCC warning _message_) +#elif defined(_MSC_VER) +#define _LIBCXXABI_WARNING(_message_) \ + __pragma(message(__FILE__ "(" _LIBCXXABI_STRINGIFY(__LINE__) "): warning: " _message_)) +#else +#define _LIBCXXABI_WARNING(_message_) +#endif + +// Deduce a threading api based on the platform if one has not been explicitly +// set at build time. +#if !defined(_LIBCXXABI_HAS_NO_THREADS) && \ + !defined(_LIBCXXABI_HAS_THREAD_API_PTHREAD) +# if defined(__FreeBSD__) || \ + defined(__NetBSD__) || \ + defined(__linux__) || \ + defined(__APPLE__) || \ + defined(__CloudABI__) || \ + defined(__sun__) +# define _LIBCXXABI_HAS_THREAD_API_PTHREAD +# else +# error "No thread API" +# endif +#endif + // Set this in the CXXFLAGS when you need it, because otherwise we'd have to // #if !defined(__linux__) && !defined(__APPLE__) && ... // and so-on for *every* platform. Index: src/cxa_exception.cpp =================================================================== --- src/cxa_exception.cpp +++ src/cxa_exception.cpp @@ -12,14 +12,12 @@ //===----------------------------------------------------------------------===// #include "config.h" +#include "threading_support.h" #include "cxxabi.h" #include // for std::terminate #include // for malloc, free #include // for memset -#ifndef _LIBCXXABI_HAS_NO_THREADS -# 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 @@ -14,6 +14,7 @@ #include "cxa_exception.hpp" #include "config.h" +#include "threading_support.h" #if defined(_LIBCXXABI_HAS_NO_THREADS) @@ -44,7 +45,6 @@ #else -#include #include // for calloc, free #include "abort_message.h" @@ -54,18 +54,18 @@ namespace __cxxabiv1 { namespace { - pthread_key_t key_; - pthread_once_t flag_ = PTHREAD_ONCE_INIT; + __libcxxabi_tls_key key_; + __libcxxabi_exec_once_flag flag_ = _LIBCXXABI_EXEC_ONCE_INITIALIZER; void destruct_ (void *p) { std::free ( p ); - if ( 0 != ::pthread_setspecific ( key_, NULL ) ) + if ( 0 != __libcxxabi_tls_set ( key_, NULL ) ) abort_message("cannot zero out thread value for __cxa_get_globals()"); } void construct_ () { - if ( 0 != pthread_key_create ( &key_, destruct_ ) ) - abort_message("cannot create pthread key for __cxa_get_globals()"); + if ( 0 != __libcxxabi_tls_create ( &key_, destruct_ ) ) + abort_message("cannot create thread specific key for __cxa_get_globals()"); } } @@ -80,8 +80,8 @@ (std::calloc (1, sizeof (__cxa_eh_globals))); if ( NULL == retVal ) abort_message("cannot allocate __cxa_eh_globals"); - if ( 0 != pthread_setspecific ( key_, retVal ) ) - abort_message("pthread_setspecific failure in __cxa_get_globals()"); + if ( 0 != __libcxxabi_tls_set ( key_, retVal ) ) + abort_message("__libcxxabi_tls_set failure in __cxa_get_globals()"); } return retVal; } @@ -92,10 +92,10 @@ // libc++abi. __cxa_eh_globals * __cxa_get_globals_fast () { // First time through, create the key. - if (0 != pthread_once(&flag_, construct_)) - abort_message("pthread_once failure in __cxa_get_globals_fast()"); + if (0 != __libcxxabi_execute_once(&flag_, construct_)) + abort_message("execute once failure in __cxa_get_globals_fast()"); // static int init = construct_(); - return static_cast<__cxa_eh_globals*>(::pthread_getspecific(key_)); + return static_cast<__cxa_eh_globals*>(__libcxxabi_tls_get(key_)); } } Index: src/cxa_guard.cpp =================================================================== --- src/cxa_guard.cpp +++ src/cxa_guard.cpp @@ -11,10 +11,8 @@ #include "abort_message.h" #include "config.h" +#include "threading_support.h" -#ifndef _LIBCXXABI_HAS_NO_THREADS -# include -#endif #include /* @@ -22,9 +20,9 @@ which will turn around and try to call __cxa_guard_acquire reentrantly. For this reason, the headers of this file are as restricted as possible. Previous implementations of this code for __APPLE__ have used - pthread_mutex_lock and the abort_message utility without problem. This - implementation also uses pthread_cond_wait which has tested to not be a - problem. + __libcxxabi_mutex_lock and the abort_message utility without problem. This + implementation also uses __libcxxabi_condvar_wait which has tested + to not be a problem. */ namespace __cxxabiv1 @@ -69,8 +67,8 @@ #endif #ifndef _LIBCXXABI_HAS_NO_THREADS -pthread_mutex_t guard_mut = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t guard_cv = PTHREAD_COND_INITIALIZER; +__libcxxabi_mutex_t guard_mut = _LIBCXXABI_MUTEX_INITIALIZER; +__libcxxabi_condvar_t guard_cv = _LIBCXXABI_CONDVAR_INITIALIZER; #endif #if defined(__APPLE__) && !defined(__arm__) @@ -175,7 +173,7 @@ #ifndef _LIBCXXABI_HAS_NO_THREADS _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) { char* initialized = (char*)guard_object; - if (pthread_mutex_lock(&guard_mut)) + if (__libcxxabi_mutex_lock(&guard_mut)) abort_message("__cxa_guard_acquire failed to acquire mutex"); int result = *initialized == 0; if (result) @@ -190,7 +188,7 @@ abort_message("__cxa_guard_acquire detected deadlock"); do { - if (pthread_cond_wait(&guard_cv, &guard_mut)) + if (__libcxxabi_condvar_wait(&guard_cv, &guard_mut)) abort_message("__cxa_guard_acquire condition variable wait failed"); lock = get_lock(*guard_object); } while (lock); @@ -202,36 +200,36 @@ set_lock(*guard_object, id); #else // !__APPLE__ || __arm__ while (get_lock(*guard_object)) - if (pthread_cond_wait(&guard_cv, &guard_mut)) + if (__libcxxabi_condvar_wait(&guard_cv, &guard_mut)) abort_message("__cxa_guard_acquire condition variable wait failed"); result = *initialized == 0; if (result) set_lock(*guard_object, true); #endif // !__APPLE__ || __arm__ } - if (pthread_mutex_unlock(&guard_mut)) + if (__libcxxabi_mutex_unlock(&guard_mut)) abort_message("__cxa_guard_acquire failed to release mutex"); return result; } _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) { - if (pthread_mutex_lock(&guard_mut)) + if (__libcxxabi_mutex_lock(&guard_mut)) abort_message("__cxa_guard_release failed to acquire mutex"); *guard_object = 0; set_initialized(guard_object); - if (pthread_mutex_unlock(&guard_mut)) + if (__libcxxabi_mutex_unlock(&guard_mut)) abort_message("__cxa_guard_release failed to release mutex"); - if (pthread_cond_broadcast(&guard_cv)) + if (__libcxxabi_condvar_broadcast(&guard_cv)) abort_message("__cxa_guard_release failed to broadcast condition variable"); } _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) { - if (pthread_mutex_lock(&guard_mut)) + if (__libcxxabi_mutex_lock(&guard_mut)) abort_message("__cxa_guard_abort failed to acquire mutex"); *guard_object = 0; - if (pthread_mutex_unlock(&guard_mut)) + if (__libcxxabi_mutex_unlock(&guard_mut)) abort_message("__cxa_guard_abort failed to release mutex"); - if (pthread_cond_broadcast(&guard_cv)) + if (__libcxxabi_condvar_broadcast(&guard_cv)) abort_message("__cxa_guard_abort failed to broadcast condition variable"); } Index: src/fallback_malloc.ipp =================================================================== --- src/fallback_malloc.ipp +++ src/fallback_malloc.ipp @@ -27,7 +27,7 @@ // When POSIX threads are not available, make the mutex operations a nop #ifndef _LIBCXXABI_HAS_NO_THREADS -static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; +static __libcxxabi_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; #else static void * heap_mutex = 0; #endif @@ -35,8 +35,8 @@ class mutexor { public: #ifndef _LIBCXXABI_HAS_NO_THREADS - mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } - ~mutexor () { pthread_mutex_unlock ( mtx_ ); } + mutexor ( __libcxxabi_mutex_t *m ) : mtx_(m) { __libcxxabi_mutex_lock ( mtx_ ); } + ~mutexor () { __libcxxabi_mutex_unlock ( mtx_ ); } #else mutexor ( void * ) {} ~mutexor () {} @@ -45,7 +45,7 @@ mutexor ( const mutexor &rhs ); mutexor & operator = ( const mutexor &rhs ); #ifndef _LIBCXXABI_HAS_NO_THREADS - pthread_mutex_t *mtx_; + __libcxxabi_mutex_t *mtx_; #endif }; Index: src/threading_support.h =================================================================== --- /dev/null +++ src/threading_support.h @@ -0,0 +1,82 @@ +//===------------------------ threading_support.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. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCXXABI_THREADING_SUPPORT_H +#define _LIBCXXABI_THREADING_SUPPORT_H + +#include "__cxxabi_config.h" +#include "config.h" + +#ifndef _LIBCXXABI_HAS_NO_THREADS + +#if defined(_LIBCXXABI_HAS_THREAD_API_PTHREAD) +#include + +#define _LIBCXXABI_THREAD_ABI_VISIBILITY inline _LIBCXXABI_INLINE_VISIBILITY + +// Mutex +typedef pthread_mutex_t __libcxxabi_mutex_t; +#define _LIBCXXABI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_mutex_lock(__libcxxabi_mutex_t *mutex) { + return pthread_mutex_lock(mutex); +} + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_mutex_unlock(__libcxxabi_mutex_t *mutex) { + return pthread_mutex_unlock(mutex); +} + +// Condition variable +typedef pthread_cond_t __libcxxabi_condvar_t; +#define _LIBCXXABI_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_condvar_wait(__libcxxabi_condvar_t *cv, + __libcxxabi_mutex_t *mutex) { + return pthread_cond_wait(cv, mutex); +} + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_condvar_broadcast(__libcxxabi_condvar_t *cv) { + return pthread_cond_broadcast(cv); +} + +// Execute once +typedef pthread_once_t __libcxxabi_exec_once_flag; +#define _LIBCXXABI_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_execute_once(__libcxxabi_exec_once_flag *flag, + void (*init_routine)(void)) { + return pthread_once(flag, init_routine); +} + +// TLS +typedef pthread_key_t __libcxxabi_tls_key; + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_tls_create(__libcxxabi_tls_key *key, + void (*destructor)(void *)) { + return pthread_key_create(key, destructor); +} + +_LIBCXXABI_THREAD_ABI_VISIBILITY +void *__libcxxabi_tls_get(__libcxxabi_tls_key key) { + return pthread_getspecific(key); +} + +_LIBCXXABI_THREAD_ABI_VISIBILITY +int __libcxxabi_tls_set(__libcxxabi_tls_key key, void *value) { + return pthread_setspecific(key, value); +} +#endif // _LIBCXXABI_HAS_THREAD_API_PTHREAD +#endif // !_LIBCXXABI_HAS_NO_THREADS +#endif // _LIBCXXABI_THREADING_SUPPORT_H Index: test/test_fallback_malloc.pass.cpp =================================================================== --- test/test_fallback_malloc.pass.cpp +++ test/test_fallback_malloc.pass.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include "../src/threading_support.h" typedef std::deque container;