| #ifndef LIBCPP_EXTERNAL_THREADING_STARBOARD_ |
| #define LIBCPP_EXTERNAL_THREADING_STARBOARD_ |
| |
| #include <__chrono/duration.h> |
| #include <__config> |
| #include <pthread.h> |
| #include <sched.h> |
| #include <unistd.h> |
| |
| #include "starboard/common/recursive_mutex.h" |
| #include "starboard/thread.h" |
| |
| _LIBCPP_PUSH_MACROS |
| #include <__undef_macros> |
| |
| // Controls the visibility of the declared functions, allowing us to hide them |
| // from the ABI and prevent redefinitions of the same function. |
| #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY |
| |
| #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis) |
| #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) |
| #else |
| #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| #endif |
| |
| _LIBCPP_BEGIN_NAMESPACE_STD |
| |
| typedef pthread_mutex_t __libcpp_mutex_t; |
| |
| typedef starboard::RecursiveMutex __libcpp_recursive_mutex_t; |
| #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| |
| typedef pthread_cond_t __libcpp_condvar_t; |
| |
| #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
| |
| typedef pthread_once_t __libcpp_exec_once_flag; |
| #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT |
| |
| typedef pthread_t __libcpp_thread_t; |
| typedef pthread_t __libcpp_thread_id; |
| typedef pthread_key_t __libcpp_tls_key; |
| #define _LIBCPP_NULL_THREAD 0 |
| |
| // The calling-convention for the thread-local storage destructor. This is |
| // mostly used on Windows platforms, and documentation can be found at |
| // https://docs.microsoft.com/en-us/cpp/cpp/stdcall. |
| #define _LIBCPP_TLS_DESTRUCTOR_CC |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| int __libcpp_mutex_lock(__libcpp_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| int __libcpp_mutex_unlock(__libcpp_mutex_t *__m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_mutex_destroy(__libcpp_mutex_t *__m); |
| |
| _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 _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| 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); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_execute_once(__libcpp_exec_once_flag *flag, |
| void (*init_routine)(void)); |
| |
| _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); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| bool __libcpp_thread_isnull(const __libcpp_thread_t *__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(); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_tls_create(__libcpp_tls_key* __key, |
| void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*)); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| void *__libcpp_tls_get(__libcpp_tls_key __key); |
| |
| _LIBCPP_THREAD_ABI_VISIBILITY |
| int __libcpp_tls_set(__libcpp_tls_key __key, void *__p); |
| |
| #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
| |
| // No further initialization required other than simply creating an instance of |
| // starboard::RecursiveMutex since its constructor initializes it. |
| int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) {} |
| |
| int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) { |
| __m->Acquire(); |
| return 0; |
| } |
| |
| bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) { |
| if (__m->AcquireTry()) { |
| return 0; |
| } |
| return 1; |
| } |
| |
| int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) { |
| __m->Release(); |
| return 0; |
| } |
| |
| // Similar situation to the constructor. |
| int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) {} |
| |
| int __libcpp_mutex_lock(__libcpp_mutex_t* __m) { |
| return pthread_mutex_lock(__m); |
| } |
| |
| bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) { |
| return pthread_mutex_trylock(__m) == 0; |
| } |
| |
| 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); |
| } |
| |
| 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); |
| } |
| |
| int __libcpp_execute_once(__libcpp_exec_once_flag* flag, |
| void (*init_routine)(void)) { |
| if (pthread_once(flag, init_routine) == 0) { |
| return 0; |
| } |
| return 1; |
| } |
| |
| bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) { |
| return pthread_equal(t1, t2); |
| } |
| |
| bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) { |
| return t1 < t2; |
| } |
| |
| bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) { |
| return *__t == 0; |
| } |
| |
| int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), |
| void* __arg) { |
| return pthread_create(__t, nullptr, __func, __arg); |
| } |
| |
| __libcpp_thread_id __libcpp_thread_get_current_id() { return pthread_self(); } |
| |
| __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) { |
| return *__t; |
| } |
| |
| int __libcpp_thread_join(__libcpp_thread_t* __t) { |
| return pthread_join(*__t, nullptr); |
| } |
| |
| int __libcpp_thread_detach(__libcpp_thread_t* __t) { |
| pthread_detach(*__t); |
| return 0; |
| } |
| |
| void __libcpp_thread_yield() { sched_yield(); } |
| |
| void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) { |
| // Convert nanoseconds (10^-9) to microseconds (10^-6). |
| const int64_t duration = __ns.count() / 1000; |
| usleep(duration); |
| } |
| |
| int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { |
| pthread_key_t __k = 0; |
| int res = pthread_key_create(&__k,__at_exit); |
| |
| if (res == 0) { |
| *__key = __k; |
| return 0; |
| } |
| return 1; |
| } |
| |
| void* __libcpp_tls_get(__libcpp_tls_key __key) { |
| return pthread_getspecific(__key); |
| } |
| |
| int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) { |
| if (pthread_setspecific(__key, __p) !=0) { |
| return 0; |
| } |
| return 1; |
| } |
| |
| typedef ::timespec __libcpp_timespec_t; |
| |
| #endif // defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
| |
| _LIBCPP_END_NAMESPACE_STD |
| |
| _LIBCPP_POP_MACROS |
| |
| #endif // LIBCPP_EXTERNAL_THREADING_STARBOARD_ |