blob: 6456285b49a86182b348cc4cc61848f51e0474d1 [file] [log] [blame]
#ifndef LIBCPP_EXTERNAL_THREADING_STARBOARD_
#define LIBCPP_EXTERNAL_THREADING_STARBOARD_
#include <__config>
#include <chrono>
#include "starboard/common/recursive_mutex.h"
#include "starboard/condition_variable.h"
#include "starboard/mutex.h"
#include "starboard/once.h"
#include "starboard/thread.h"
#include "starboard/thread_types.h"
#include "starboard/time.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 SbMutex __libcpp_mutex_t;
typedef starboard::RecursiveMutex __libcpp_recursive_mutex_t;
#define _LIBCPP_MUTEX_INITIALIZER SB_MUTEX_INITIALIZER
typedef SbConditionVariable __libcpp_condvar_t;
#define _LIBCPP_CONDVAR_INITIALIZER SB_CONDITION_VARIABLE_INITIALIZER
typedef SbOnceControl __libcpp_exec_once_flag;
#define _LIBCPP_EXEC_ONCE_INITIALIZER SB_ONCE_INITIALIZER
typedef SbThread __libcpp_thread_t;
typedef SbThread __libcpp_thread_id;
typedef SbThreadLocalKey __libcpp_tls_key;
#define _LIBCPP_NULL_THREAD kSbThreadInvalid
// 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 SbMutexAcquire(__m); }
bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) {
return (SbMutexAcquireTry(__m) == kSbMutexAcquired);
}
int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) {
if (SbMutexRelease(__m)) {
return 0;
}
return 1;
}
int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) {
if (SbMutexDestroy(__m)) {
return 0;
}
return 1;
}
int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) {
if (SbConditionVariableSignal(__cv)) {
return 0;
}
return 1;
}
int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) {
if (SbConditionVariableBroadcast(__cv)) {
return 0;
}
return 1;
}
int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) {
return SbConditionVariableWait(__cv, __m);
}
int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m,
timespec* __ts) {
// Convert the duration provided by __ts to a Starboard time. The conversion
// is from seconds (10^1) and nanoseconds (10^-9) to microseconds (10^-6).
const SbTime duration = __ts->tv_sec * 1000000 + __ts->tv_nsec / 1000;
return SbConditionVariableWaitTimed(__cv, __m, duration);
}
int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) {
if (SbConditionVariableDestroy(__cv)) {
return 0;
}
return 1;
}
int __libcpp_execute_once(__libcpp_exec_once_flag* flag,
void (*init_routine)(void)) {
if (SbOnce(flag, init_routine)) {
return 0;
}
return 1;
}
bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) {
return SbThreadIsEqual(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 !SbThreadIsValid(*__t);
}
int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*),
void* __arg) {
*__t = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true, "",
__func, __arg);
if (*__t == kSbThreadInvalid) {
return 1;
}
return 0;
}
__libcpp_thread_id __libcpp_thread_get_current_id() { return SbThreadGetCurrent(); }
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) {
return *__t;
}
int __libcpp_thread_join(__libcpp_thread_t* __t) {
if (SbThreadJoin(*__t, nullptr)) {
return 0;
}
return 1;
}
int __libcpp_thread_detach(__libcpp_thread_t* __t) {
SbThreadDetach(*__t);
return 0;
}
void __libcpp_thread_yield() { SbThreadYield(); }
void __libcpp_thread_sleep_for(const std::chrono::nanoseconds& __ns) {
// Convert nanoseconds (10^-9) to microseconds (10^-6).
const SbTime duration = __ns.count() / 1000;
SbThreadSleep(duration);
}
int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) {
const SbThreadLocalKey __k = SbThreadCreateLocalKey(__at_exit);
if (__k != kSbThreadLocalKeyInvalid) {
*__key = __k;
return 0;
}
return 1;
}
void* __libcpp_tls_get(__libcpp_tls_key __key) {
return SbThreadGetLocalValue(__key);
}
int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) {
if (SbThreadSetLocalValue(__key, __p)) {
return 0;
}
return 1;
}
#endif // defined(_LIBCPP_HAS_EXTERNAL_THREAD_API)
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // LIBCPP_EXTERNAL_THREADING_STARBOARD_