[libcxxabi] r208135 - On single threaded systems, turn mutexes into nops

Jonathan Roelofs jonathan at codesourcery.com
Tue May 6 14:30:56 PDT 2014


Author: jroelofs
Date: Tue May  6 16:30:56 2014
New Revision: 208135

URL: http://llvm.org/viewvc/llvm-project?rev=208135&view=rev
Log:
On single threaded systems, turn mutexes into nops

http://reviews.llvm.org/D3386

Added:
    libcxxabi/trunk/src/config.h
Modified:
    libcxxabi/trunk/src/cxa_exception.cpp
    libcxxabi/trunk/src/cxa_exception_storage.cpp
    libcxxabi/trunk/src/cxa_guard.cpp
    libcxxabi/trunk/src/fallback_malloc.ipp
    libcxxabi/trunk/test/test_exception_storage.cpp

Added: libcxxabi/trunk/src/config.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/config.h?rev=208135&view=auto
==============================================================================
--- libcxxabi/trunk/src/config.h (added)
+++ libcxxabi/trunk/src/config.h Tue May  6 16:30:56 2014
@@ -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

Modified: libcxxabi/trunk/src/cxa_exception.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/cxa_exception.cpp?rev=208135&r1=208134&r2=208135&view=diff
==============================================================================
--- libcxxabi/trunk/src/cxa_exception.cpp (original)
+++ libcxxabi/trunk/src/cxa_exception.cpp Tue May  6 16:30:56 2014
@@ -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"
 

Modified: libcxxabi/trunk/src/cxa_exception_storage.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/cxa_exception_storage.cpp?rev=208135&r1=208134&r2=208135&view=diff
==============================================================================
--- libcxxabi/trunk/src/cxa_exception_storage.cpp (original)
+++ libcxxabi/trunk/src/cxa_exception_storage.cpp Tue May  6 16:30:56 2014
@@ -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 {
 

Modified: libcxxabi/trunk/src/cxa_guard.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/cxa_guard.cpp?rev=208135&r1=208134&r2=208135&view=diff
==============================================================================
--- libcxxabi/trunk/src/cxa_guard.cpp (original)
+++ libcxxabi/trunk/src/cxa_guard.cpp Tue May  6 16:30:56 2014
@@ -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_o
 
 #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
         abort_message("__cxa_guard_abort failed to broadcast condition variable");
 }
 
+#endif // !LIBCXXABI_SINGLE_THREADED
+
 }  // extern "C"
 
 }  // __cxxabiv1

Modified: libcxxabi/trunk/src/fallback_malloc.ipp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/fallback_malloc.ipp?rev=208135&r1=208134&r2=208135&view=diff
==============================================================================
--- libcxxabi/trunk/src/fallback_malloc.ipp (original)
+++ libcxxabi/trunk/src/fallback_malloc.ipp Tue May  6 16:30:56 2014
@@ -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
     };
 
         

Modified: libcxxabi/trunk/test/test_exception_storage.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/test/test_exception_storage.cpp?rev=208135&r1=208134&r2=208135&view=diff
==============================================================================
--- libcxxabi/trunk/test/test_exception_storage.cpp (original)
+++ libcxxabi/trunk/test/test_exception_storage.cpp Tue May  6 16:30:56 2014
@@ -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
 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;
     }





More information about the cfe-commits mailing list