On single threaded systems, turn mutexes into nops

http://reviews.llvm.org/D3386

llvm-svn: 208135
This commit is contained in:
Jonathan Roelofs 2014-05-06 21:30:56 +00:00
parent 9c928478f4
commit 40e9842854
6 changed files with 96 additions and 8 deletions

25
libcxxabi/src/config.h Normal file
View File

@ -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

View File

@ -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"

View File

@ -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 {

View File

@ -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

View File

@ -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
};

View File

@ -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;
}