Skip to content

Instantly share code, notes, and snippets.

@EricWF
Last active September 6, 2016 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricWF/0edfd18c22cd50a5f34fd11674f4eda9 to your computer and use it in GitHub Desktop.
Save EricWF/0edfd18c22cd50a5f34fd11674f4eda9 to your computer and use it in GitHub Desktop.
diff --git a/include/__threading_support b/include/__threading_support
index 427fb9c..b71be4e 100644
--- a/include/__threading_support
+++ b/include/__threading_support
@@ -1,272 +1,243 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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 _LIBCPP_THREADING_SUPPORT
#define _LIBCPP_THREADING_SUPPORT
#include <__config>
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
#pragma GCC system_header
#endif
#ifndef _LIBCPP_HAS_NO_THREADS
-#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
+#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) && \
+ (!defined(__has_include) || __has_include(<__external_threading>))
+#include<__external_threading>
+#else
#include <pthread.h>
#include <sched.h>
#endif
-#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
+#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
+#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
+#else
+#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
+#endif
_LIBCPP_BEGIN_NAMESPACE_STD
// Mutex
-#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
typedef pthread_mutex_t __libcpp_mutex_t;
+#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_mutex_lock(__libcpp_mutex_t* __m);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_mutex_trylock(__libcpp_mutex_t* __m);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_mutex_unlock(__libcpp_mutex_t* __m);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_mutex_destroy(__libcpp_mutex_t* __m);
+
+// Condition variable
+typedef pthread_cond_t __libcpp_condvar_t;
+#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
+
+// Thread id
+typedef pthread_t __libcpp_thread_id;
+_LIBCPP_THREAD_ABI_VISIBILITY
+bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
+_LIBCPP_THREAD_ABI_VISIBILITY
+bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
+
+// Thread
+typedef pthread_t __libcpp_thread_t;
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg);
+_LIBCPP_THREAD_ABI_VISIBILITY
+__libcpp_thread_id __libcpp_thread_get_current_id();
+_LIBCPP_THREAD_ABI_VISIBILITY
+__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_thread_join(__libcpp_thread_t* __t);
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_thread_detach(__libcpp_thread_t* __t);
+_LIBCPP_THREAD_ABI_VISIBILITY
+void __libcpp_thread_yield();
+
+// Thread local storage
+typedef pthread_key_t __libcpp_tl_key;
+_LIBCPP_THREAD_ABI_VISIBILITY
+int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*));
+_LIBCPP_THREAD_ABI_VISIBILITY
+void* __libcpp_tl_get(__libcpp_tl_key __key);
+_LIBCPP_THREAD_ABI_VISIBILITY
+void __libcpp_tl_set(__libcpp_tl_key __key, void* __p);
+
+#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) \
+ || defined(_LIBCPP_BUILDING_EXTERNAL_THREADS)
-inline _LIBCPP_ALWAYS_INLINE
+// Mutex
int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m)
{
pthread_mutexattr_t attr;
int __ec = pthread_mutexattr_init(&attr);
if (__ec) return __ec;
__ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if (__ec)
{
pthread_mutexattr_destroy(&attr);
return __ec;
}
__ec = pthread_mutex_init(__m, &attr);
if (__ec)
{
pthread_mutexattr_destroy(&attr);
return __ec;
}
__ec = pthread_mutexattr_destroy(&attr);
if (__ec)
{
pthread_mutex_destroy(__m);
return __ec;
}
return 0;
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_mutex_lock(__libcpp_mutex_t* __m)
{
return pthread_mutex_lock(__m);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_mutex_trylock(__libcpp_mutex_t* __m)
{
return pthread_mutex_trylock(__m);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_mutex_unlock(__libcpp_mutex_t* __m)
{
return pthread_mutex_unlock(__m);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_mutex_destroy(__libcpp_mutex_t* __m)
{
return pthread_mutex_destroy(__m);
}
// Condition variable
-#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
-typedef pthread_cond_t __libcpp_condvar_t;
-
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_condvar_signal(__libcpp_condvar_t* __cv)
{
return pthread_cond_signal(__cv);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv)
{
return pthread_cond_broadcast(__cv);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m)
{
return pthread_cond_wait(__cv, __m);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts)
{
return pthread_cond_timedwait(__cv, __m, __ts);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv)
{
return pthread_cond_destroy(__cv);
}
// Thread id
-typedef pthread_t __libcpp_thread_id;
// Returns non-zero if the thread ids are equal, otherwise 0
-inline _LIBCPP_ALWAYS_INLINE
bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
{
return pthread_equal(t1, t2) != 0;
}
// Returns non-zero if t1 < t2, otherwise 0
-inline _LIBCPP_ALWAYS_INLINE
bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
{
return t1 < t2;
}
// Thread
-typedef pthread_t __libcpp_thread_t;
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg)
{
return pthread_create(__t, 0, __func, __arg);
}
-inline _LIBCPP_ALWAYS_INLINE
__libcpp_thread_id __libcpp_thread_get_current_id()
{
return pthread_self();
}
-inline _LIBCPP_ALWAYS_INLINE
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t)
{
return *__t;
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_thread_join(__libcpp_thread_t* __t)
{
return pthread_join(*__t, 0);
}
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_thread_detach(__libcpp_thread_t* __t)
{
return pthread_detach(*__t);
}
-inline _LIBCPP_ALWAYS_INLINE
void __libcpp_thread_yield()
{
sched_yield();
}
// Thread local storage
-typedef pthread_key_t __libcpp_tl_key;
-inline _LIBCPP_ALWAYS_INLINE
int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*))
{
return pthread_key_create(__key, __at_exit);
}
-inline _LIBCPP_ALWAYS_INLINE
void* __libcpp_tl_get(__libcpp_tl_key __key)
{
return pthread_getspecific(__key);
}
-inline _LIBCPP_ALWAYS_INLINE
void __libcpp_tl_set(__libcpp_tl_key __key, void* __p)
{
pthread_setspecific(__key, __p);
}
-_LIBCPP_END_NAMESPACE_STD
-
-#elif defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
-#if !defined(__has_include) || __has_include(<__external_threading>)
-/* Delegate everything to the external header. We expect libc++ vendors
- (with non-pthread threading systems) to provide this header.
-
- For compile-time integration of libc++ with a particular thread
- implementation, library vendors should provide definitions for the
- thread-api functions below - the definitions should be marked as
- _LIBCPP_ALWAYS_INLINE so that there won't be an additional cost of a
- function call.
-
- For link-time integration of libc++ with a thread implementation,
- this header should only declare the function prototypes - with the
- implementation deferred to some external library. */
- #include<__external_threading>
-#else
-/* By default, we use a pthread-based externalized threading api - this is
- to allow easy testing of the link-time external threading. The thread
- implementation lives in test/support/external_threads.cpp which is
- compiled into a separate library and linked when running the tests. */
-#include <pthread.h>
-#include <time.h>
-#include <stdint.h>
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-// Mutex
-typedef pthread_mutex_t __libcpp_mutex_t;
-#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
-int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m);
-int __libcpp_mutex_lock(__libcpp_mutex_t* __m);
-int __libcpp_mutex_trylock(__libcpp_mutex_t* __m);
-int __libcpp_mutex_unlock(__libcpp_mutex_t* __m);
-int __libcpp_mutex_destroy(__libcpp_mutex_t* __m);
-
-// Condition variable
-typedef pthread_cond_t __libcpp_condvar_t;
-#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
-int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
-int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
-int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
-int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts);
-int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
-
-// Thread id
-typedef pthread_t __libcpp_thread_id;
-bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
-bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
-
-// Thread
-typedef pthread_t __libcpp_thread_t;
-int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg);
-__libcpp_thread_id __libcpp_thread_get_current_id();
-__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t);
-int __libcpp_thread_join(__libcpp_thread_t* __t);
-int __libcpp_thread_detach(__libcpp_thread_t* __t);
-void __libcpp_thread_yield();
-
-// Thread local storage
-typedef pthread_key_t __libcpp_tl_key;
-int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*));
-void* __libcpp_tl_get(__libcpp_tl_key __key);
-void __libcpp_tl_set(__libcpp_tl_key __key, void* __p);
+#endif // _LIBCPP_HAS_THREAD_API_PTHREAD
_LIBCPP_END_NAMESPACE_STD
-#endif
-
-#else // !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
- #error "No thread API selected."
-#endif
#endif // _LIBCPP_HAS_NO_THREADS
#endif // _LIBCPP_THREADING_SUPPORT
diff --git a/test/support/external_threads.cpp b/test/support/external_threads.cpp
index 4099a2b..5c5b191 100644
--- a/test/support/external_threads.cpp
+++ b/test/support/external_threads.cpp
@@ -1,172 +1,11 @@
//===----------------------------------------------------------------------===//
//
// 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 SUPPORT_EXTERNAL_THREADS
-#define SUPPORT_EXTERNAL_THREADS
+#define _LIBCPP_BUILDING_EXTERNAL_THREADS
#include <__threading_support>
-#include <pthread.h>
-#include <stdlib.h>
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-//-- Mutex --//
-
-int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m)
-{
- // Initialize the allocated pthread_mutex_t object as a recursive mutex
- pthread_mutexattr_t attr;
- int __ec = pthread_mutexattr_init(&attr);
- if (__ec)
- goto fail;
-
- __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- if (__ec)
- {
- pthread_mutexattr_destroy(&attr);
- goto fail;
- }
-
- __ec = pthread_mutex_init(__m, &attr);
- if (__ec)
- {
- pthread_mutexattr_destroy(&attr);
- goto fail;
- }
-
- __ec = pthread_mutexattr_destroy(&attr);
- if (__ec)
- {
- pthread_mutex_destroy(__m);
- goto fail;
- }
-
- return 0;
-
-fail:
- return __ec;
-}
-
-int __libcpp_mutex_lock(__libcpp_mutex_t* __m)
-{
- return pthread_mutex_lock(__m);
-}
-
-int __libcpp_mutex_trylock(__libcpp_mutex_t* __m)
-{
- return pthread_mutex_trylock(__m);
-}
-
-int __libcpp_mutex_unlock(__libcpp_mutex_t* __m)
-{
- return pthread_mutex_unlock(__m);
-}
-
-int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) {
- return pthread_mutex_destroy(__m);
-}
-
-//-- Condition variable --//
-
-int __libcpp_condvar_signal(__libcpp_condvar_t* __cv)
-{
- return pthread_cond_signal(__cv);
-}
-
-int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv)
-{
- return pthread_cond_broadcast(__cv);
-}
-
-int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m)
-{
- return pthread_cond_wait(__cv, __m);
-}
-
-int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts)
-{
- return pthread_cond_timedwait(__cv, __m, __ts);
-}
-
-int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv)
-{
- return pthread_cond_destroy(__cv);
-}
-
-//-- Thread --//
-
-bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) {
- return pthread_equal(t1, t2) != 0;
-}
-
-bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) {
- return t1 < t2;
-}
-
-int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg)
-{
- return pthread_create(__t, 0, __func, __arg);
-}
-
-__libcpp_thread_id __libcpp_thread_get_current_id()
-{
- return static_cast<__libcpp_thread_id>(pthread_self());
-}
-
-__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t)
-{
- return *__t;
-}
-
-int __libcpp_thread_join(__libcpp_thread_t* __t)
-{
- // Must return non-zero if the internal state has already been invalidated
- // This can happen for example, if std::thread::join() has been called
- // before.
- if (*__t == 0)
- return -1;
-
- return pthread_join(*__t, 0);
-}
-
-int __libcpp_thread_detach(__libcpp_thread_t* __t)
-{
- // Must return non-zero if the internal state has already been invalidated
- // This can happen for example, if std::thread::detach() has been called
- // before.
- if (*__t == 0)
- return -1;
-
- return pthread_detach(*__t);
-}
-
-void __libcpp_thread_yield()
-{
- sched_yield();
-}
-
-//-- TLS --//
-
-int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*))
-{
- return pthread_key_create(__key, __at_exit);
-}
-
-void* __libcpp_tl_get(__libcpp_tl_key __key)
-{
- return pthread_getspecific(__key);
-}
-
-void __libcpp_tl_set(__libcpp_tl_key __key, void* __p)
-{
- pthread_setspecific(__key, __p);
-}
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // SUPPORT_EXTERNAL_THREADS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment