diff --git a/base/observer_list_threadsafe.cc b/base/observer_list_threadsafe.cc index e3022514a65b..b05b11cc6f0f 100644 --- a/base/observer_list_threadsafe.cc +++ b/base/observer_list_threadsafe.cc @@ -18,21 +18,22 @@ namespace internal { namespace { ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } } void ObserverListThreadSafeBase::EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } -const SbThreadLocalKey ObserverListThreadSafeBase::GetThreadLocalKey() { +const pthread_key_t ObserverListThreadSafeBase::GetThreadLocalKey() { + EnsureThreadLocalKeyInited(); return s_thread_local_key; } diff --git a/base/observer_list_threadsafe.h b/base/observer_list_threadsafe.h index cb5e5fff6cdc..a6ac666faa60 100644 --- a/base/observer_list_threadsafe.h +++ b/base/observer_list_threadsafe.h @@ -26,7 +26,7 @@ #include "third_party/abseil-cpp/absl/base/attributes.h" #if defined(STARBOARD) -#include "starboard/thread.h" +#include #endif /////////////////////////////////////////////////////////////////////////////// @@ -94,7 +94,7 @@ class BASE_EXPORT ObserverListThreadSafeBase #if defined(STARBOARD) static void EnsureThreadLocalKeyInited(); - static const SbThreadLocalKey GetThreadLocalKey(); + static const pthread_key_t GetThreadLocalKey(); #endif virtual ~ObserverListThreadSafeBase() = default; @@ -156,7 +156,7 @@ class ObserverListThreadSafe : public internal::ObserverListThreadSafeBase { // |lock_|). if (policy_ == ObserverListPolicy::ALL) { #if defined(STARBOARD) - void* current_notification_void = SbThreadGetLocalValue(GetThreadLocalKey()); + void* current_notification_void = pthread_getspecific(GetThreadLocalKey()); if (current_notification_void) { if (const NotificationDataBase* const current_notification = static_cast(current_notification_void); @@ -276,8 +276,8 @@ class ObserverListThreadSafe : public internal::ObserverListThreadSafeBase { // important to save the previous value to restore it later. #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - void* scoped_reset_value = SbThreadGetLocalValue(GetThreadLocalKey()); - SbThreadSetLocalValue(GetThreadLocalKey(), const_cast(¬ification)); + void* scoped_reset_value = pthread_getspecific(GetThreadLocalKey()); + pthread_setspecific(GetThreadLocalKey(), const_cast(¬ification)); #else const AutoReset resetter_( &GetCurrentNotification(), ¬ification); @@ -287,7 +287,7 @@ class ObserverListThreadSafe : public internal::ObserverListThreadSafeBase { notification.method.Run(observer); #if defined(STARBOARD) - SbThreadSetLocalValue(GetThreadLocalKey(), scoped_reset_value); + pthread_setspecific(GetThreadLocalKey(), scoped_reset_value); #endif } diff --git a/base/run_loop.cc b/base/run_loop.cc index 644366bb8dcc..d5a7c6adeb33 100644 --- a/base/run_loop.cc +++ b/base/run_loop.cc @@ -27,39 +27,41 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_delegate_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_delegate_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_delegate_key = 0; void InitThreadLocalDelegateKey() { - s_thread_local_delegate_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_delegate_key)); + pthread_key_create(&s_thread_local_delegate_key , NULL); + DCHECK(s_thread_local_delegate_key); } void EnsureThreadLocalDelegateKeyInited() { pthread_once(&s_once_delegate_flag, InitThreadLocalDelegateKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_delegate_key)); + DCHECK(s_thread_local_delegate_key); } RunLoop::Delegate* GetDelegate() { + EnsureThreadLocalDelegateKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_delegate_key)); + pthread_getspecific(s_thread_local_delegate_key)); } ABSL_CONST_INIT pthread_once_t s_once_timeout_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_timeout_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_timeout_key = 0; void InitThreadLocalTimeoutKey() { - s_thread_local_timeout_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_timeout_key)); + pthread_key_create(&s_thread_local_timeout_key, NULL); + DCHECK(s_thread_local_timeout_key); } void EnsureThreadLocalTimeoutKeyInited() { pthread_once(&s_once_timeout_flag, InitThreadLocalTimeoutKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_timeout_key)); + DCHECK(s_thread_local_timeout_key); } const RunLoop::RunLoopTimeout* GetRunLoopTimeout() { + EnsureThreadLocalTimeoutKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_timeout_key)); + pthread_getspecific(s_thread_local_timeout_key)); } #else ABSL_CONST_INIT thread_local RunLoop::Delegate* delegate = nullptr; @@ -103,7 +105,7 @@ RunLoop::Delegate::~Delegate() { #if defined(STARBOARD) DCHECK_EQ(this, GetDelegate()); EnsureThreadLocalDelegateKeyInited(); - SbThreadSetLocalValue(s_thread_local_delegate_key, nullptr); + pthread_setspecific(s_thread_local_delegate_key, nullptr); #else DCHECK_EQ(this, delegate); delegate = nullptr; @@ -138,7 +140,7 @@ void RunLoop::RegisterDelegateForCurrentThread(Delegate* new_delegate) { "MessageLoop/TaskEnvironment on a thread that already had one?"; #if defined(STARBOARD) EnsureThreadLocalDelegateKeyInited(); - SbThreadSetLocalValue(s_thread_local_delegate_key, new_delegate); + pthread_setspecific(s_thread_local_delegate_key, new_delegate); new_delegate->bound_ = true; #else delegate = new_delegate; @@ -392,7 +394,7 @@ RunLoop::RunLoopTimeout::~RunLoopTimeout() = default; void RunLoop::SetTimeoutForCurrentThread(const RunLoopTimeout* timeout) { #if defined(STARBOARD) EnsureThreadLocalTimeoutKeyInited(); - SbThreadSetLocalValue(s_thread_local_timeout_key, const_cast(timeout)); + pthread_setspecific(s_thread_local_timeout_key, const_cast(timeout)); #else run_loop_timeout = timeout; #endif diff --git a/base/sampling_heap_profiler/sampling_heap_profiler.cc b/base/sampling_heap_profiler/sampling_heap_profiler.cc index f192f817e6c6..ce5d12721ef1 100644 --- a/base/sampling_heap_profiler/sampling_heap_profiler.cc +++ b/base/sampling_heap_profiler/sampling_heap_profiler.cc @@ -97,22 +97,22 @@ const char* GetAndLeakThreadName() { const char* UpdateAndGetThreadName(const char* name) { #if defined(STARBOARD) static pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; - static SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; + static pthread_key_t s_thread_local_key = 0; auto InitThreadLocalKey = [](){ - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); }; pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); - const char* thread_name = static_cast(SbThreadGetLocalValue(s_thread_local_key)); + const char* thread_name = static_cast(pthread_getspecific(s_thread_local_key)); if (name) - SbThreadSetLocalValue(s_thread_local_key, const_cast(name)); + pthread_setspecific(s_thread_local_key, const_cast(name)); else if (!thread_name) - SbThreadSetLocalValue(s_thread_local_key, const_cast(GetAndLeakThreadName())); - return static_cast(SbThreadGetLocalValue(s_thread_local_key)); + pthread_setspecific(s_thread_local_key, const_cast(GetAndLeakThreadName())); + return static_cast(pthread_getspecific(s_thread_local_key)); #else static thread_local const char* thread_name; if (name) diff --git a/base/sequence_token.cc b/base/sequence_token.cc index 66347e2df076..e920fa090cfd 100644 --- a/base/sequence_token.cc +++ b/base/sequence_token.cc @@ -25,69 +25,66 @@ base::AtomicSequenceNumber g_task_token_generator; #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_sequence_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_sequence_key = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_sequence_key = 0; void InitThreadLocalSequenceKey() { - s_thread_local_sequence_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_sequence_key)); + pthread_key_create(&s_thread_local_sequence_key, NULL); + DCHECK(s_thread_local_sequence_key); } void EnsureThreadLocalSequenceKeyInited() { pthread_once(&s_once_sequence_flag, InitThreadLocalSequenceKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_sequence_key)); + DCHECK(s_thread_local_sequence_key); } ABSL_CONST_INIT pthread_once_t s_once_set_sequence_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_sequence_set_for_thread = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_sequence_set_for_thread = 0; void InitThreadLocalSequenceBoolKey() { - s_thread_local_sequence_set_for_thread = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_sequence_set_for_thread)); + pthread_key_create(&s_thread_local_sequence_set_for_thread, NULL); + DCHECK(s_thread_local_sequence_set_for_thread); } void EnsureThreadLocalSequenceBoolKeyInited() { pthread_once(&s_once_set_sequence_flag, InitThreadLocalSequenceBoolKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_sequence_set_for_thread)); + DCHECK(s_thread_local_sequence_set_for_thread); } bool IsSequenceSetForThread() { + EnsureThreadLocalSequenceBoolKeyInited(); void* set_for_thread = - SbThreadGetLocalValue(s_thread_local_sequence_set_for_thread); + pthread_getspecific(s_thread_local_sequence_set_for_thread); return !!set_for_thread ? reinterpret_cast(set_for_thread) != 0 : false; } ABSL_CONST_INIT pthread_once_t s_once_task_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_task_key = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_task_key = 0; void InitThreadLocalTaskKey() { - s_thread_local_task_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_key)); + pthread_key_create(&s_thread_local_task_key , NULL); + DCHECK(s_thread_local_task_key); } void EnsureThreadLocalTaskKeyInited() { pthread_once(&s_once_task_flag, InitThreadLocalTaskKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_key)); + DCHECK(s_thread_local_task_key); } ABSL_CONST_INIT pthread_once_t s_once_set_task_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_task_set_for_thread = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_task_set_for_thread = 0; void InitThreadLocalTaskBoolKey() { - s_thread_local_task_set_for_thread = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_set_for_thread)); + pthread_key_create(&s_thread_local_task_set_for_thread, NULL); + DCHECK(s_thread_local_task_set_for_thread); } void EnsureThreadLocalTaskBoolKeyInited() { pthread_once(&s_once_set_task_flag, InitThreadLocalTaskBoolKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_set_for_thread)); + DCHECK(s_thread_local_task_set_for_thread); } bool IsTaskSetForThread() { - void* set_for_thread = - SbThreadGetLocalValue(s_thread_local_task_set_for_thread); + EnsureThreadLocalTaskBoolKeyInited(); + void* set_for_thread = pthread_getspecific(s_thread_local_task_set_for_thread); return !!set_for_thread ? reinterpret_cast(set_for_thread) != 0 : false; } @@ -121,8 +118,9 @@ SequenceToken SequenceToken::Create() { SequenceToken SequenceToken::GetForCurrentThread() { #if defined(STARBOARD) if (IsSequenceSetForThread()) { + EnsureThreadLocalSequenceKeyInited(); int token = static_cast(reinterpret_cast( - SbThreadGetLocalValue(s_thread_local_sequence_key))); + pthread_getspecific(s_thread_local_sequence_key))); return SequenceToken(token); } else { return SequenceToken(); @@ -151,8 +149,9 @@ TaskToken TaskToken::Create() { TaskToken TaskToken::GetForCurrentThread() { #if defined(STARBOARD) if (IsTaskSetForThread()) { + EnsureThreadLocalTaskKeyInited(); int token = static_cast(reinterpret_cast( - SbThreadGetLocalValue(s_thread_local_task_key))); + pthread_getspecific(s_thread_local_task_key))); return TaskToken(token); } else { return TaskToken(); @@ -171,7 +170,7 @@ ScopedSetSequenceTokenForCurrentThread::ScopedSetSequenceTokenForCurrentThread( DCHECK(!sequence_reset_token.IsValid()); scoped_sequence_reset_value_ = reinterpret_cast( static_cast(sequence_reset_token.GetToken())); - SbThreadSetLocalValue(s_thread_local_sequence_key, + pthread_setspecific(s_thread_local_sequence_key, reinterpret_cast( static_cast(sequence_token.GetToken()))); @@ -180,15 +179,15 @@ ScopedSetSequenceTokenForCurrentThread::ScopedSetSequenceTokenForCurrentThread( DCHECK(!task_reset_token.IsValid()); scoped_task_reset_value_ = reinterpret_cast( static_cast(task_reset_token.GetToken())); - SbThreadSetLocalValue(s_thread_local_task_key, + pthread_setspecific(s_thread_local_task_key, reinterpret_cast(static_cast( TaskToken::Create().GetToken()))); EnsureThreadLocalSequenceBoolKeyInited(); - SbThreadSetLocalValue(s_thread_local_sequence_set_for_thread, + pthread_setspecific(s_thread_local_sequence_set_for_thread, reinterpret_cast(static_cast(true))); EnsureThreadLocalTaskBoolKeyInited(); - SbThreadSetLocalValue(s_thread_local_task_set_for_thread, + pthread_setspecific(s_thread_local_task_set_for_thread, reinterpret_cast(static_cast(true))); } #else @@ -209,9 +208,12 @@ ScopedSetSequenceTokenForCurrentThread::ScopedSetSequenceTokenForCurrentThread( #if defined(STARBOARD) ScopedSetSequenceTokenForCurrentThread:: ~ScopedSetSequenceTokenForCurrentThread() { - SbThreadSetLocalValue(s_thread_local_sequence_key, + EnsureThreadLocalSequenceKeyInited(); + pthread_setspecific(s_thread_local_sequence_key, scoped_sequence_reset_value_); - SbThreadSetLocalValue(s_thread_local_task_key, scoped_task_reset_value_); + + EnsureThreadLocalTaskKeyInited(); + pthread_setspecific(s_thread_local_task_key, scoped_task_reset_value_); } #else ScopedSetSequenceTokenForCurrentThread:: diff --git a/base/task/common/scoped_defer_task_posting.cc b/base/task/common/scoped_defer_task_posting.cc index 4bcc3d5a33e9..6784bb5de235 100644 --- a/base/task/common/scoped_defer_task_posting.cc +++ b/base/task/common/scoped_defer_task_posting.cc @@ -20,21 +20,22 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } ScopedDeferTaskPosting* GetScopedDeferTaskPosting() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else // Holds a thread-local pointer to the current scope or null when no @@ -83,7 +84,7 @@ bool ScopedDeferTaskPosting::Set(ScopedDeferTaskPosting* scope) { return false; #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, scope); + pthread_setspecific(s_thread_local_key, scope); #else scoped_defer_task_posting = scope; #endif diff --git a/base/task/common/task_annotator.cc b/base/task/common/task_annotator.cc index fe37d95869f9..09bcf64a2f56 100644 --- a/base/task/common/task_annotator.cc +++ b/base/task/common/task_annotator.cc @@ -41,50 +41,48 @@ TaskAnnotator::ObserverForTesting* g_task_annotator_observer = nullptr; #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_task_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_task_key = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_task_key = 0; void InitThreadLocalTaskKey() { - s_thread_local_task_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_key)); + pthread_key_create(&s_thread_local_task_key, NULL); + DCHECK(s_thread_local_task_key); } void EnsureThreadLocalTaskKeyInited() { pthread_once(&s_once_task_flag, InitThreadLocalTaskKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_task_key)); + DCHECK(s_thread_local_task_key); } PendingTask* GetCurrentPendingTask() { + EnsureThreadLocalTaskKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_task_key)); + pthread_getspecific(s_thread_local_task_key)); } ABSL_CONST_INIT pthread_once_t s_once_hash_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_hash_key = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_hash_key = 0; void InitThreadLocalHashKey() { - s_thread_local_hash_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_hash_key)); + pthread_key_create(&s_thread_local_hash_key , NULL); + DCHECK(s_thread_local_hash_key); } void EnsureThreadLocalHashKeyInited() { pthread_once(&s_once_hash_flag, InitThreadLocalHashKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_hash_key)); + DCHECK(s_thread_local_hash_key); } ABSL_CONST_INIT pthread_once_t s_once_tracker_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_tracker_key = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_tracker_key = 0; void InitThreadLocalTrackerKey() { - s_thread_local_tracker_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_tracker_key)); + pthread_key_create(&s_thread_local_tracker_key , NULL); + DCHECK(s_thread_local_tracker_key); } void EnsureThreadLocalTrackerKeyInited() { pthread_once(&s_once_tracker_flag, InitThreadLocalTrackerKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_tracker_key)); + DCHECK(s_thread_local_tracker_key); } #else // The PendingTask currently in progress on each thread. Used to allow creating @@ -106,8 +104,9 @@ ABSL_CONST_INIT thread_local TaskAnnotator::LongTaskTracker* // variable accesses, once the MSAN workaround is not necessary. TaskAnnotator::ScopedSetIpcHash* GetCurrentScopedIpcHash() { #if defined(STARBOARD) + EnsureThreadLocalHashKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_hash_key)); + pthread_getspecific(s_thread_local_hash_key)); #else // Workaround false-positive MSAN use-of-uninitialized-value on // thread_local storage for loaded libraries: @@ -121,8 +120,9 @@ TaskAnnotator::ScopedSetIpcHash* GetCurrentScopedIpcHash() { TaskAnnotator::LongTaskTracker* GetCurrentLongTaskTracker() { #if defined(STARBOARD) + EnsureThreadLocalTrackerKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_tracker_key)); + pthread_getspecific(s_thread_local_tracker_key)); #else // Workaround false-positive MSAN use-of-uninitialized-value on // thread_local storage for loaded libraries: @@ -249,8 +249,8 @@ void TaskAnnotator::RunTaskImpl(PendingTask& pending_task) { { #if defined(STARBOARD) EnsureThreadLocalTaskKeyInited(); - void* reset_to = SbThreadGetLocalValue(s_thread_local_task_key); - SbThreadSetLocalValue(s_thread_local_task_key, &pending_task); + void* reset_to = pthread_getspecific(s_thread_local_task_key); + pthread_setspecific(s_thread_local_task_key, &pending_task); #else const AutoReset resetter(¤t_pending_task, &pending_task); @@ -282,7 +282,7 @@ void TaskAnnotator::RunTaskImpl(PendingTask& pending_task) { #endif #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_task_key, reset_to); + pthread_setspecific(s_thread_local_task_key, reset_to); #endif } @@ -371,8 +371,8 @@ TaskAnnotator::ScopedSetIpcHash::ScopedSetIpcHash( ipc_interface_name_(ipc_interface_name) { #if defined(STARBOARD) EnsureThreadLocalHashKeyInited(); - scoped_reset_value_ = SbThreadGetLocalValue(s_thread_local_hash_key); - SbThreadSetLocalValue(s_thread_local_hash_key, this); + scoped_reset_value_ = pthread_getspecific(s_thread_local_hash_key); + pthread_setspecific(s_thread_local_hash_key, this); #endif } @@ -390,7 +390,8 @@ uint32_t TaskAnnotator::ScopedSetIpcHash::MD5HashMetricName( TaskAnnotator::ScopedSetIpcHash::~ScopedSetIpcHash() { DCHECK_EQ(this, GetCurrentScopedIpcHash()); #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_hash_key, scoped_reset_value_); + EnsureThreadLocalHashKeyInited(); + pthread_setspecific(s_thread_local_hash_key, scoped_reset_value_); #endif } @@ -407,8 +408,8 @@ TaskAnnotator::LongTaskTracker::LongTaskTracker(const TickClock* tick_clock, task_annotator_(task_annotator) { #if defined(STARBOARD) EnsureThreadLocalTrackerKeyInited(); - scoped_reset_value_ = SbThreadGetLocalValue(s_thread_local_tracker_key); - SbThreadSetLocalValue(s_thread_local_tracker_key, this); + scoped_reset_value_ = pthread_getspecific(s_thread_local_tracker_key); + pthread_setspecific(s_thread_local_tracker_key, this); #endif TRACE_EVENT_CATEGORY_GROUP_ENABLED("scheduler.long_tasks", &is_tracing_); @@ -420,7 +421,8 @@ TaskAnnotator::LongTaskTracker::LongTaskTracker(const TickClock* tick_clock, TaskAnnotator::LongTaskTracker::~LongTaskTracker() { DCHECK_EQ(this, GetCurrentLongTaskTracker()); #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_tracker_key, scoped_reset_value_); + EnsureThreadLocalTrackerKeyInited(); + pthread_setspecific(s_thread_local_tracker_key, scoped_reset_value_); #endif if (!is_tracing_) diff --git a/base/task/scoped_set_task_priority_for_current_thread.cc b/base/task/scoped_set_task_priority_for_current_thread.cc index 4218fff4c734..2f2325f4573c 100644 --- a/base/task/scoped_set_task_priority_for_current_thread.cc +++ b/base/task/scoped_set_task_priority_for_current_thread.cc @@ -21,33 +21,33 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } ABSL_CONST_INIT pthread_once_t s_once_set_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_set_for_thread = - kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_set_for_thread = 0; void InitThreadLocalBoolKey() { - s_thread_local_set_for_thread = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_set_for_thread)); + pthread_key_create(&s_thread_local_set_for_thread, NULL); + DCHECK(s_thread_local_set_for_thread); } void EnsureThreadLocalBoolKeyInited() { pthread_once(&s_once_set_flag, InitThreadLocalBoolKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_set_for_thread)); + DCHECK(s_thread_local_set_for_thread); } bool IsValueSetForThread() { - void* set_for_thread = SbThreadGetLocalValue(s_thread_local_set_for_thread); + EnsureThreadLocalBoolKeyInited(); + void* set_for_thread = pthread_getspecific(s_thread_local_set_for_thread); return !!set_for_thread ? reinterpret_cast(set_for_thread) != 0 : false; } @@ -66,11 +66,11 @@ ScopedSetTaskPriorityForCurrentThread::ScopedSetTaskPriorityForCurrentThread( scoped_reset_value_ = reinterpret_cast(static_cast( static_cast(GetTaskPriorityForCurrentThread()))); - SbThreadSetLocalValue(s_thread_local_key, + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast( static_cast(priority)))); EnsureThreadLocalBoolKeyInited(); - SbThreadSetLocalValue(s_thread_local_set_for_thread, + pthread_setspecific(s_thread_local_set_for_thread, reinterpret_cast(static_cast(true))); } #else @@ -82,7 +82,8 @@ ScopedSetTaskPriorityForCurrentThread::ScopedSetTaskPriorityForCurrentThread( #if defined(STARBOARD) ScopedSetTaskPriorityForCurrentThread:: ~ScopedSetTaskPriorityForCurrentThread() { - SbThreadSetLocalValue(s_thread_local_key, scoped_reset_value_); + EnsureThreadLocalKeyInited(); + pthread_setspecific(s_thread_local_key, scoped_reset_value_); } #else ScopedSetTaskPriorityForCurrentThread:: @@ -91,8 +92,9 @@ ScopedSetTaskPriorityForCurrentThread:: TaskPriority GetTaskPriorityForCurrentThread() { #if defined(STARBOARD) + EnsureThreadLocalKeyInited(); void* task_priority_for_current_thread = - SbThreadGetLocalValue(s_thread_local_key); + pthread_getspecific(s_thread_local_key); return IsValueSetForThread() ? static_cast(static_cast( reinterpret_cast( task_priority_for_current_thread))) diff --git a/base/task/sequence_manager/sequence_manager_impl.cc b/base/task/sequence_manager/sequence_manager_impl.cc index 627b98103fdd..38bc86eed645 100644 --- a/base/task/sequence_manager/sequence_manager_impl.cc +++ b/base/task/sequence_manager/sequence_manager_impl.cc @@ -51,21 +51,22 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key, NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } internal::SequenceManagerImpl* GetThreadLocalSequenceManager() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else ABSL_CONST_INIT thread_local internal::SequenceManagerImpl* @@ -268,7 +269,7 @@ SequenceManagerImpl::~SequenceManagerImpl() { DCHECK_EQ(this, GetCurrent()); #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_setspecific(s_thread_local_key, nullptr); #else thread_local_sequence_manager = nullptr; #endif @@ -398,7 +399,7 @@ void SequenceManagerImpl::CompleteInitializationOnBoundThread() { << "Can't register a second SequenceManagerImpl on the same thread."; #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, this); + pthread_setspecific(s_thread_local_key, this); #else thread_local_sequence_manager = this; #endif diff --git a/base/task/sequenced_task_runner.cc b/base/task/sequenced_task_runner.cc index 61e3ad08deac..1b6ce9cfcd9b 100644 --- a/base/task/sequenced_task_runner.cc +++ b/base/task/sequenced_task_runner.cc @@ -24,21 +24,22 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key, NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } SequencedTaskRunner::CurrentDefaultHandle* GetCurrentDefaultHandle() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else ABSL_CONST_INIT thread_local SequencedTaskRunner::CurrentDefaultHandle* @@ -135,7 +136,7 @@ SequencedTaskRunner::CurrentDefaultHandle::CurrentDefaultHandle( task_runner_(std::move(task_runner)) { #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, this); + pthread_setspecific(s_thread_local_key, this); #endif DCHECK(task_runner_->RunsTasksInCurrentSequence()); } @@ -144,7 +145,7 @@ SequencedTaskRunner::CurrentDefaultHandle::~CurrentDefaultHandle() { DCHECK(task_runner_->RunsTasksInCurrentSequence()); #if defined(STARBOARD) auto current_default_handle = GetCurrentDefaultHandle(); - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_setspecific(s_thread_local_key, nullptr); #endif DCHECK_EQ(current_default_handle, this); } diff --git a/base/task/single_thread_task_runner.cc b/base/task/single_thread_task_runner.cc index b6e4e27e6f72..cf49208519e8 100644 --- a/base/task/single_thread_task_runner.cc +++ b/base/task/single_thread_task_runner.cc @@ -29,16 +29,16 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } #else ABSL_CONST_INIT thread_local SingleThreadTaskRunner::CurrentDefaultHandle* @@ -49,8 +49,9 @@ ABSL_CONST_INIT thread_local SingleThreadTaskRunner::CurrentDefaultHandle* // variable accesses, once the MSAN workaround is not necessary. SingleThreadTaskRunner::CurrentDefaultHandle* GetCurrentDefaultHandle() { #if defined(STARBOARD) + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); #else // Workaround false-positive MSAN use-of-uninitialized-value on // thread_local storage for loaded libraries: @@ -97,7 +98,7 @@ SingleThreadTaskRunner::CurrentDefaultHandle::CurrentDefaultHandle( sequenced_task_runner_current_default_(task_runner_) { #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, this); + pthread_setspecific(s_thread_local_key, this); #endif DCHECK(task_runner_->BelongsToCurrentThread()); } @@ -106,7 +107,7 @@ SingleThreadTaskRunner::CurrentDefaultHandle::~CurrentDefaultHandle() { DCHECK(task_runner_->BelongsToCurrentThread()); DCHECK_EQ(GetCurrentDefaultHandle(), this); #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_setspecific(s_thread_local_key, nullptr); #endif } diff --git a/base/task/thread_pool/task_tracker.cc b/base/task/thread_pool/task_tracker.cc index 550afc7ce818..1e89a093b953 100644 --- a/base/task/thread_pool/task_tracker.cc +++ b/base/task/thread_pool/task_tracker.cc @@ -129,20 +129,21 @@ auto EmitThreadPoolTraceEventMetadata(perfetto::EventContext& ctx, #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } bool GetFizzleBlockShutdownTasks() { - void* fizzle_block_shutdown_tasks = SbThreadGetLocalValue(s_thread_local_key); + EnsureThreadLocalKeyInited(); + void* fizzle_block_shutdown_tasks = pthread_getspecific(s_thread_local_key); return !!fizzle_block_shutdown_tasks ? reinterpret_cast(fizzle_block_shutdown_tasks) != 0 : false; } #else @@ -451,7 +452,7 @@ bool TaskTracker::IsShutdownComplete() const { void TaskTracker::BeginFizzlingBlockShutdownTasks() { #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(true))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(true))); #else fizzle_block_shutdown_tasks = true; #endif @@ -460,7 +461,7 @@ void TaskTracker::BeginFizzlingBlockShutdownTasks() { void TaskTracker::EndFizzlingBlockShutdownTasks() { #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(false))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(false))); #else fizzle_block_shutdown_tasks = false; #endif diff --git a/base/task/thread_pool/thread_group.cc b/base/task/thread_pool/thread_group.cc index 57a15a5406ba..6005239ba3db 100644 --- a/base/task/thread_pool/thread_group.cc +++ b/base/task/thread_pool/thread_group.cc @@ -33,21 +33,22 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } const ThreadGroup* GetCurrentThreadGroup() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else // ThreadGroup that owns the current thread, if any. @@ -106,7 +107,7 @@ void ThreadGroup::BindToCurrentThread() { DCHECK(!CurrentThreadHasGroup()); #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, this); + pthread_setspecific(s_thread_local_key, this); #else current_thread_group = this; #endif @@ -116,7 +117,7 @@ void ThreadGroup::UnbindFromCurrentThread() { DCHECK(IsBoundToCurrentThread()); #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_setspecific(s_thread_local_key, nullptr); #else current_thread_group = nullptr; #endif diff --git a/base/threading/hang_watcher.cc b/base/threading/hang_watcher.cc index a508abf1c397..a918b80853cb 100644 --- a/base/threading/hang_watcher.cc +++ b/base/threading/hang_watcher.cc @@ -54,21 +54,22 @@ HangWatcher* g_instance = nullptr; #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } internal::HangWatchState* GetHangWatchState() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else ABSL_CONST_INIT thread_local internal::HangWatchState* hang_watch_state = @@ -1197,7 +1198,7 @@ HangWatchState::HangWatchState(HangWatcher::ThreadType thread_type) #if defined(STARBOARD) : thread_type_(thread_type) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, this); + pthread_setspecific(s_thread_local_key, this); #else : resetter_(&hang_watch_state, this, nullptr), thread_type_(thread_type) { #endif @@ -1226,7 +1227,8 @@ HangWatchState::~HangWatchState() { #endif #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_key, nullptr); + EnsureThreadLocalKeyInited(); + pthread_setspecific(s_thread_local_key, nullptr); #endif } diff --git a/base/threading/platform_thread.cc b/base/threading/platform_thread.cc index 3fb1c78e1a92..065886da6b6a 100644 --- a/base/threading/platform_thread.cc +++ b/base/threading/platform_thread.cc @@ -15,7 +15,6 @@ #include #include "base/check_op.h" -#include "starboard/thread.h" #endif namespace base { @@ -24,21 +23,23 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } ThreadType* GetThreadType() { + // TODO: investigate further + if (s_thread_local_key == 0) return nullptr; return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else ABSL_CONST_INIT thread_local ThreadType current_thread_type = @@ -92,7 +93,7 @@ void SetCurrentThreadType(ThreadType thread_type, SetCurrentThreadTypeImpl(thread_type, pump_type_hint); #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, &thread_type); + pthread_setspecific(s_thread_local_key, &thread_type); #else current_thread_type = thread_type; #endif diff --git a/base/threading/scoped_blocking_call.cc b/base/threading/scoped_blocking_call.cc index 99f6dba2ea1a..0ec3f074d891 100644 --- a/base/threading/scoped_blocking_call.cc +++ b/base/threading/scoped_blocking_call.cc @@ -34,20 +34,21 @@ namespace { #if DCHECK_IS_ON() #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } bool GetConstructionInProgress() { - void* construction_in_progress = SbThreadGetLocalValue(s_thread_local_key); + EnsureThreadLocalKeyInited(); + void* construction_in_progress = pthread_getspecific(s_thread_local_key); return !!construction_in_progress ? reinterpret_cast(construction_in_progress) != 0 : false; } #else @@ -68,7 +69,7 @@ ScopedBlockingCall::ScopedBlockingCall(const Location& from_here, #if DCHECK_IS_ON() #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(true))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(true))); #else const AutoReset resetter(&construction_in_progress, true, false); #endif @@ -82,7 +83,7 @@ ScopedBlockingCall::ScopedBlockingCall(const Location& from_here, }); #if DCHECK_IS_ON() && defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(false))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(false))); #endif } @@ -101,7 +102,7 @@ ScopedBlockingCallWithBaseSyncPrimitives:: #if DCHECK_IS_ON() #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(true))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(true))); #else const AutoReset resetter(&construction_in_progress, true, false); #endif @@ -118,7 +119,7 @@ ScopedBlockingCallWithBaseSyncPrimitives:: }); #if DCHECK_IS_ON() && defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(false))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(false))); #endif } diff --git a/base/threading/scoped_blocking_call_internal.cc b/base/threading/scoped_blocking_call_internal.cc index 9febe4ce6489..3fcb96654612 100644 --- a/base/threading/scoped_blocking_call_internal.cc +++ b/base/threading/scoped_blocking_call_internal.cc @@ -35,28 +35,28 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_observer_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_observer_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_observer_key = 0; ABSL_CONST_INIT pthread_once_t s_once_call_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_call_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_call_key = 0; void InitThreadLocalObserverKey() { - s_thread_local_observer_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_observer_key)); + pthread_key_create(&s_thread_local_observer_key , NULL); + DCHECK(s_thread_local_observer_key); } void InitThreadLocalCallKey() { - s_thread_local_call_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_call_key)); + pthread_key_create(&s_thread_local_call_key , NULL); + DCHECK(s_thread_local_call_key); } void EnsureThreadLocalObserverKeyInited() { pthread_once(&s_once_observer_flag, InitThreadLocalObserverKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_observer_key)); + DCHECK(s_thread_local_observer_key); } void EnsureThreadLocalCallKeyInited() { pthread_once(&s_once_call_flag, InitThreadLocalCallKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_call_key)); + DCHECK(s_thread_local_call_key); } #else ABSL_CONST_INIT thread_local BlockingObserver* blocking_observer = nullptr; @@ -70,8 +70,9 @@ ABSL_CONST_INIT thread_local UncheckedScopedBlockingCall* // variable accesses, once the MSAN workaround is not necessary. BlockingObserver* GetBlockingObserver() { #if defined(STARBOARD) + EnsureThreadLocalObserverKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_observer_key)); + pthread_getspecific(s_thread_local_observer_key)); #else // Workaround false-positive MSAN use-of-uninitialized-value on // thread_local storage for loaded libraries: @@ -83,8 +84,9 @@ BlockingObserver* GetBlockingObserver() { } UncheckedScopedBlockingCall* GetLastScopedBlockingCall() { #if defined(STARBOARD) + EnsureThreadLocalCallKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_call_key)); + pthread_getspecific(s_thread_local_call_key)); #else // Workaround false-positive MSAN use-of-uninitialized-value on // thread_local storage for loaded libraries: @@ -112,7 +114,7 @@ void SetBlockingObserverForCurrentThread( DCHECK(!GetBlockingObserver()); #if defined(STARBOARD) EnsureThreadLocalObserverKeyInited(); - SbThreadSetLocalValue(s_thread_local_observer_key, new_blocking_observer); + pthread_setspecific(s_thread_local_observer_key, new_blocking_observer); #else blocking_observer = new_blocking_observer; #endif @@ -121,7 +123,7 @@ void SetBlockingObserverForCurrentThread( void ClearBlockingObserverForCurrentThread() { #if defined(STARBOARD) EnsureThreadLocalObserverKeyInited(); - SbThreadSetLocalValue(s_thread_local_observer_key, nullptr); + pthread_setspecific(s_thread_local_observer_key, nullptr); #else blocking_observer = nullptr; #endif @@ -381,8 +383,8 @@ UncheckedScopedBlockingCall::UncheckedScopedBlockingCall( previous_scoped_blocking_call_->is_will_block_)) { #if defined(STARBOARD) EnsureThreadLocalCallKeyInited(); - reset_to_ = SbThreadGetLocalValue(s_thread_local_call_key); - SbThreadSetLocalValue(s_thread_local_call_key, this); + reset_to_ = pthread_getspecific(s_thread_local_call_key); + pthread_setspecific(s_thread_local_call_key, this); #endif // Only monitor non-nested ScopedBlockingCall(MAY_BLOCK) calls on foreground @@ -420,7 +422,8 @@ UncheckedScopedBlockingCall::~UncheckedScopedBlockingCall() { blocking_observer_->BlockingEnded(); #if defined(STARBOARD) - SbThreadSetLocalValue(s_thread_local_call_key, reset_to_); + EnsureThreadLocalCallKeyInited(); + pthread_setspecific(s_thread_local_call_key, reset_to_); #endif } diff --git a/base/threading/sequence_local_storage_map.cc b/base/threading/sequence_local_storage_map.cc index ef6d357238b5..497fe8cc7a1d 100644 --- a/base/threading/sequence_local_storage_map.cc +++ b/base/threading/sequence_local_storage_map.cc @@ -24,21 +24,22 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } SequenceLocalStorageMap* GetCurrentSequenceLocalStorage() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } #else ABSL_CONST_INIT thread_local SequenceLocalStorageMap* @@ -135,7 +136,7 @@ ScopedSetSequenceLocalStorageMapForCurrentThread:: #if defined(STARBOARD) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, sequence_local_storage); + pthread_setspecific(s_thread_local_key, sequence_local_storage); } #else : resetter_(¤t_sequence_local_storage, @@ -146,7 +147,8 @@ ScopedSetSequenceLocalStorageMapForCurrentThread:: #if defined(STARBOARD) ScopedSetSequenceLocalStorageMapForCurrentThread:: ~ScopedSetSequenceLocalStorageMapForCurrentThread() { - SbThreadSetLocalValue(s_thread_local_key, nullptr); + EnsureThreadLocalKeyInited(); + pthread_setspecific(s_thread_local_key, nullptr); } #else ScopedSetSequenceLocalStorageMapForCurrentThread:: diff --git a/base/threading/thread.cc b/base/threading/thread.cc index 1d0f30d6e54b..4dd1a80abb4f 100644 --- a/base/threading/thread.cc +++ b/base/threading/thread.cc @@ -52,20 +52,21 @@ namespace { #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } bool GetWasQuitProperly() { - void* was_quit_properly = SbThreadGetLocalValue(s_thread_local_key); + EnsureThreadLocalKeyInited(); + void* was_quit_properly = pthread_getspecific(s_thread_local_key); return !!was_quit_properly ? reinterpret_cast(was_quit_properly) != 0 : false; } #else @@ -374,7 +375,7 @@ void Thread::SetThreadWasQuitProperly(bool flag) { #if DCHECK_IS_ON() #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, reinterpret_cast(static_cast(flag))); + pthread_setspecific(s_thread_local_key, reinterpret_cast(static_cast(flag))); #else was_quit_properly = flag; #endif diff --git a/base/threading/thread_id_name_manager.cc b/base/threading/thread_id_name_manager.cc index 89196014d0e1..93eedadb5616 100644 --- a/base/threading/thread_id_name_manager.cc +++ b/base/threading/thread_id_name_manager.cc @@ -30,21 +30,22 @@ static std::string* g_default_name; #if defined(STARBOARD) ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } const char* GetThreadName() { + EnsureThreadLocalKeyInited(); const char* thread_name = static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); return !!thread_name ? thread_name : kDefaultName; } #else @@ -110,7 +111,7 @@ void ThreadIdNameManager::SetName(const std::string& name) { #if defined(STARBOARD) EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, const_cast(leaked_str->c_str())); + pthread_setspecific(s_thread_local_key, const_cast(leaked_str->c_str())); #else thread_name = leaked_str->c_str(); #endif diff --git a/base/threading/thread_local_storage.h b/base/threading/thread_local_storage.h index c8c87c1706da..dfe83837b12c 100644 --- a/base/threading/thread_local_storage.h +++ b/base/threading/thread_local_storage.h @@ -11,7 +11,7 @@ #include "build/build_config.h" #if defined(STARBOARD) -#include "starboard/thread.h" +#include #elif BUILDFLAG(IS_WIN) #include "base/win/windows_types.h" #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) @@ -41,14 +41,10 @@ class ThreadLocalStorageTestInternal; // * ThreadLocalStorage::StaticSlot/Slot for more direct control of the slot. class BASE_EXPORT PlatformThreadLocalStorage { public: -#if defined(STARBOARD) - typedef SbThreadLocalKey TLSKey; - static constexpr SbThreadLocalKey TLS_KEY_OUT_OF_INDEXES = - kSbThreadLocalKeyInvalid; -#elif BUILDFLAG(IS_WIN) +#if BUILDFLAG(IS_WIN) && !defined(STARBOARD) typedef unsigned long TLSKey; enum : unsigned { TLS_KEY_OUT_OF_INDEXES = TLS_OUT_OF_INDEXES }; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || defined(STARBOARD) typedef pthread_key_t TLSKey; // The following is a "reserved key" which is used in our generic Chromium // ThreadLocalStorage implementation. We expect that an OS will not return @@ -71,11 +67,10 @@ class BASE_EXPORT PlatformThreadLocalStorage { static void FreeTLS(TLSKey key); static void SetTLSValue(TLSKey key, void* value); static void* GetTLSValue(TLSKey key) { -#if defined(STARBOARD) - return SbThreadGetLocalValue(key); -#elif BUILDFLAG(IS_WIN) + return pthread_getspecific(key); +#if BUILDFLAG(IS_WIN) && !defined(STARBOARD) return TlsGetValue(key); -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || defined(STARBOARD) return pthread_getspecific(key); #endif } diff --git a/base/threading/thread_local_storage_starboard.cc b/base/threading/thread_local_storage_starboard.cc index 03e68a8ed991..816257f94335 100644 --- a/base/threading/thread_local_storage_starboard.cc +++ b/base/threading/thread_local_storage_starboard.cc @@ -22,9 +22,8 @@ namespace internal { // static bool PlatformThreadLocalStorage::AllocTLS(TLSKey* key) { - *key = SbThreadCreateLocalKey( - base::internal::PlatformThreadLocalStorage::OnThreadExit); - if (!SbThreadIsValidLocalKey(*key)) { + pthread_key_create(key, base::internal::PlatformThreadLocalStorage::OnThreadExit); + if (!(*key)) { NOTREACHED(); return false; } @@ -34,12 +33,12 @@ bool PlatformThreadLocalStorage::AllocTLS(TLSKey* key) { // static void PlatformThreadLocalStorage::FreeTLS(TLSKey key) { - SbThreadDestroyLocalKey(key); + pthread_key_delete(key); } // static void PlatformThreadLocalStorage::SetTLSValue(TLSKey key, void* value) { - if (!SbThreadSetLocalValue(key, value)) { + if (pthread_setspecific(key, value) != 0) { NOTREACHED(); } } diff --git a/glimp/egl/error.cc b/glimp/egl/error.cc index 3e4686280233..4f88dd979820 100644 --- a/glimp/egl/error.cc +++ b/glimp/egl/error.cc @@ -25,16 +25,16 @@ namespace egl { namespace { pthread_once_t s_error_once_control = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_error_tls_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_error_tls_key = 0; void InitializeError() { - s_error_tls_key = SbThreadCreateLocalKey(NULL); + pthread_key_create(&s_error_tls_key, NULL); } } // namespace EGLint GetError() { pthread_once(&s_error_once_control, &InitializeError); - void* local_value = SbThreadGetLocalValue(s_error_tls_key); + void* local_value = pthread_getspecific(s_error_tls_key); if (local_value == NULL) { // The EGL error has never been set. In this case, return EGL_SUCCESS as // that is the initial value for eglGetError(). @@ -47,7 +47,7 @@ EGLint GetError() { void SetError(EGLint error) { pthread_once(&s_error_once_control, &InitializeError); - SbThreadSetLocalValue(s_error_tls_key, reinterpret_cast(error)); + pthread_setspecific(s_error_tls_key, reinterpret_cast(error)); } } // namespace egl diff --git a/glimp/gles/context.cc b/glimp/gles/context.cc index e96e425ce9a4..33bda203b0aa 100644 --- a/glimp/gles/context.cc +++ b/glimp/gles/context.cc @@ -41,13 +41,13 @@ namespace { std::atomic_int s_context_id_counter_(0); pthread_once_t s_tls_current_context_key_once_control = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_tls_current_context_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_tls_current_context_key = 0; void InitializeThreadLocalKey() { - s_tls_current_context_key = SbThreadCreateLocalKey(NULL); + pthread_key_create(&s_tls_current_context_key, NULL); } -SbThreadLocalKey GetThreadLocalKey() { +pthread_key_t GetThreadLocalKey() { pthread_once(&s_tls_current_context_key_once_control, &InitializeThreadLocalKey); return s_tls_current_context_key; @@ -82,7 +82,7 @@ Context::Context(std::unique_ptr context_impl, } Context* Context::GetTLSCurrentContext() { - return reinterpret_cast(SbThreadGetLocalValue(GetThreadLocalKey())); + return reinterpret_cast(pthread_getspecific(GetThreadLocalKey())); } bool Context::SetTLSCurrentContext(Context* context, @@ -107,8 +107,7 @@ bool Context::SetTLSCurrentContext(Context* context, if (existing_context) { existing_context->ReleaseContext(); } - SbThreadSetLocalValue(GetThreadLocalKey(), - reinterpret_cast(context)); + pthread_setspecific(GetThreadLocalKey(), reinterpret_cast(context)); } context->MakeCurrent(draw, read); @@ -119,7 +118,7 @@ void Context::ReleaseTLSCurrentContext() { Context* existing_context = GetTLSCurrentContext(); if (existing_context) { existing_context->ReleaseContext(); - SbThreadSetLocalValue(GetThreadLocalKey(), NULL); + pthread_setspecific(GetThreadLocalKey(), NULL); } } diff --git a/net/third_party/quiche/src/quiche/common/quiche_random.cc b/net/third_party/quiche/src/quiche/common/quiche_random.cc index 98659b3968b9..ab4f72532a5d 100644 --- a/net/third_party/quiche/src/quiche/common/quiche_random.cc +++ b/net/third_party/quiche/src/quiche/common/quiche_random.cc @@ -40,17 +40,17 @@ void ThreadLocalDestructor(void* value) { uint64_t Xoshiro256PlusPlus() { static pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; - static SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; + static pthread_key_t s_thread_local_key = 0; auto InitThreadLocalKey = [](){ - s_thread_local_key = SbThreadCreateLocalKey(ThreadLocalDestructor); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , ThreadLocalDestructor); + DCHECK(s_thread_local_key); }; pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); - uint64_t* rng_state = static_cast(SbThreadGetLocalValue(s_thread_local_key)); + uint64_t* rng_state = static_cast(pthread_getspecific(s_thread_local_key)); if (!rng_state) { rng_state = new uint64_t[4]{ Xoshiro256InitializeRngStateMember(), @@ -69,7 +69,7 @@ uint64_t Xoshiro256PlusPlus() { rng_state[0] ^= rng_state[3]; rng_state[2] ^= t; rng_state[3] = Xoshiro256PlusPlusRotLeft(rng_state[3], 45); - SbThreadSetLocalValue(s_thread_local_key, rng_state); + pthread_setspecific(s_thread_local_key, rng_state); return result; } diff --git a/net/third_party/quiche/src/quiche/quic/core/quic_connection_context.cc b/net/third_party/quiche/src/quiche/quic/core/quic_connection_context.cc index 99ec6af62ba4..c727de28dea9 100644 --- a/net/third_party/quiche/src/quiche/quic/core/quic_connection_context.cc +++ b/net/third_party/quiche/src/quiche/quic/core/quic_connection_context.cc @@ -20,21 +20,22 @@ namespace quic { namespace { ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +ABSL_CONST_INIT pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key, NULL); + DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + DCHECK(s_thread_local_key); } QuicConnectionContext* GetQuicConnectionContext() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } } // namespace @@ -58,7 +59,7 @@ QuicConnectionContextSwitcher::QuicConnectionContextSwitcher( QuicConnectionContext* new_context) : old_context_(QuicConnectionContext::Current()) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, new_context); + pthread_setspecific(s_thread_local_key, new_context); if (new_context && new_context->tracer) { new_context->tracer->Activate(); } @@ -69,7 +70,9 @@ QuicConnectionContextSwitcher::~QuicConnectionContextSwitcher() { if (current && current->tracer) { current->tracer->Deactivate(); } - SbThreadSetLocalValue(s_thread_local_key, old_context_); + + EnsureThreadLocalKeyInited(); + pthread_setspecific(s_thread_local_key, old_context_); } #else // defined(STARBOARD) diff --git a/starboard/CHANGELOG.md b/starboard/CHANGELOG.md index 3b10d9d4d0cd..3e4dbddcab14 100644 --- a/starboard/CHANGELOG.md +++ b/starboard/CHANGELOG.md @@ -9,6 +9,9 @@ since the version previous to it. ## Version 16 +### Deprecated the `SbThreadLocalKey` APIs. +Replaced the `SbThreadLocalKey` with the POSIX `pthread_key_t`. + ### Deprecated `SbOnce` Replaced the `SbOnce` with the POSIX `pthread_once`. diff --git a/starboard/android/shared/jni_env_ext.cc b/starboard/android/shared/jni_env_ext.cc index 98a65fdc49f5..358cddb7cba5 100644 --- a/starboard/android/shared/jni_env_ext.cc +++ b/starboard/android/shared/jni_env_ext.cc @@ -24,7 +24,7 @@ namespace { -SbThreadLocalKey g_tls_key = kSbThreadLocalKeyInvalid; +pthread_key_t g_tls_key = 0; JavaVM* g_vm = NULL; jobject g_application_class_loader = NULL; jobject g_starboard_bridge = NULL; @@ -44,8 +44,8 @@ namespace shared { // static void JniEnvExt::Initialize(JniEnvExt* env, jobject starboard_bridge) { - SB_DCHECK(g_tls_key == kSbThreadLocalKeyInvalid); - g_tls_key = SbThreadCreateLocalKey(Destroy); + SB_DCHECK(g_tls_key == 0); + pthread_key_create(&g_tls_key, Destroy); SB_DCHECK(g_vm == NULL); env->GetJavaVM(&g_vm); @@ -65,9 +65,9 @@ void JniEnvExt::OnThreadShutdown() { // We must call DetachCurrentThread() before exiting, if we have ever // previously called AttachCurrentThread() on it. // http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html - if (SbThreadGetLocalValue(g_tls_key)) { + if (pthread_getspecific(g_tls_key)) { g_vm->DetachCurrentThread(); - SbThreadSetLocalValue(g_tls_key, NULL); + pthread_setspecific(g_tls_key, NULL); } } @@ -80,7 +80,7 @@ JniEnvExt* JniEnvExt::Get() { JavaVMAttachArgs args{JNI_VERSION_1_6, thread_name, NULL}; g_vm->AttachCurrentThread(&env, &args); // We don't use the value, but any non-NULL means we have to detach. - SbThreadSetLocalValue(g_tls_key, env); + pthread_setspecific(g_tls_key, env); } // The downcast is safe since we only add methods, not fields. return static_cast(env); diff --git a/starboard/android/shared/video_max_video_input_size.cc b/starboard/android/shared/video_max_video_input_size.cc index d2f666a053af..0bf7c399cf23 100644 --- a/starboard/android/shared/video_max_video_input_size.cc +++ b/starboard/android/shared/video_max_video_input_size.cc @@ -24,29 +24,29 @@ namespace android { namespace shared { pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key, NULL); + SB_DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + SB_DCHECK(s_thread_local_key); } int GetMaxVideoInputSizeForCurrentThread() { EnsureThreadLocalKeyInited(); // If the key is not valid or there is no value associated // with the key, it returns 0. - return reinterpret_cast(SbThreadGetLocalValue(s_thread_local_key)); + return reinterpret_cast(pthread_getspecific(s_thread_local_key)); } void SetMaxVideoInputSizeForCurrentThread(int max_video_input_size) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, - reinterpret_cast(max_video_input_size)); + pthread_setspecific(s_thread_local_key, + reinterpret_cast(max_video_input_size)); } } // namespace shared diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index a975d2880c50..32d00b3449d5 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -382,12 +382,20 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbSystemSymbolize); REGISTER_SYMBOL(SbThreadContextGetPointer); REGISTER_SYMBOL(SbThreadCreate); + +#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbThreadCreateLocalKey); REGISTER_SYMBOL(SbThreadDestroyLocalKey); +#endif // SB_API_VERSION < 16 + REGISTER_SYMBOL(SbThreadDetach); REGISTER_SYMBOL(SbThreadGetCurrent); REGISTER_SYMBOL(SbThreadGetId); + +#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbThreadGetLocalValue); +#endif // SB_API_VERSION < 16 + REGISTER_SYMBOL(SbThreadGetName); REGISTER_SYMBOL(SbThreadIsEqual); REGISTER_SYMBOL(SbThreadJoin); @@ -396,7 +404,11 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbThreadSamplerFreeze); REGISTER_SYMBOL(SbThreadSamplerIsSupported); REGISTER_SYMBOL(SbThreadSamplerThaw); + +#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbThreadSetLocalValue); +#endif // SB_API_VERSION < 16 + REGISTER_SYMBOL(SbThreadSetName); #if SB_API_VERSION < 16 REGISTER_SYMBOL(SbThreadSleep); diff --git a/starboard/nplb/thread_local_value_test.cc b/starboard/nplb/thread_local_value_test.cc index 3b4ce10e3b70..c76f8989f4d5 100644 --- a/starboard/nplb/thread_local_value_test.cc +++ b/starboard/nplb/thread_local_value_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/configuration_constants.h" #include "starboard/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -189,3 +191,5 @@ TEST(SbThreadLocalValueTest, SunnyDayMany) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/pthread/thread_create_local_key.cc b/starboard/shared/pthread/thread_create_local_key.cc index 4885d3ff5c84..59112c1310d0 100644 --- a/starboard/shared/pthread/thread_create_local_key.cc +++ b/starboard/shared/pthread/thread_create_local_key.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -26,3 +28,5 @@ SbThreadLocalKey SbThreadCreateLocalKey(SbThreadLocalDestructor destructor) { } return key; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/pthread/thread_destroy_local_key.cc b/starboard/shared/pthread/thread_destroy_local_key.cc index 74a14685c057..264bd10e2b4b 100644 --- a/starboard/shared/pthread/thread_destroy_local_key.cc +++ b/starboard/shared/pthread/thread_destroy_local_key.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -26,3 +28,5 @@ void SbThreadDestroyLocalKey(SbThreadLocalKey key) { pthread_key_delete(key->key); delete key; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/pthread/thread_get_local_value.cc b/starboard/shared/pthread/thread_get_local_value.cc index b1fc9f9a178d..21a79180dbc3 100644 --- a/starboard/shared/pthread/thread_get_local_value.cc +++ b/starboard/shared/pthread/thread_get_local_value.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -25,3 +27,5 @@ void* SbThreadGetLocalValue(SbThreadLocalKey key) { return pthread_getspecific(key->key); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/pthread/thread_local_key_internal.h b/starboard/shared/pthread/thread_local_key_internal.h index dfc193b14575..adb74f3882ba 100644 --- a/starboard/shared/pthread/thread_local_key_internal.h +++ b/starboard/shared/pthread/thread_local_key_internal.h @@ -15,6 +15,8 @@ #ifndef STARBOARD_SHARED_PTHREAD_THREAD_LOCAL_KEY_INTERNAL_H_ #define STARBOARD_SHARED_PTHREAD_THREAD_LOCAL_KEY_INTERNAL_H_ +#if SB_API_VERSION < 16 + #include #include "starboard/shared/internal_only.h" @@ -26,4 +28,5 @@ struct SbThreadLocalKeyPrivate { pthread_key_t key; }; +#endif // SB_API_VERSION < 16 #endif // STARBOARD_SHARED_PTHREAD_THREAD_LOCAL_KEY_INTERNAL_H_ diff --git a/starboard/shared/pthread/thread_set_local_value.cc b/starboard/shared/pthread/thread_set_local_value.cc index 148c18845012..1261d4c83c37 100644 --- a/starboard/shared/pthread/thread_set_local_value.cc +++ b/starboard/shared/pthread/thread_set_local_value.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -29,3 +31,5 @@ bool SbThreadSetLocalValue(SbThreadLocalKey key, void* value) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/starboard/net_log.cc b/starboard/shared/starboard/net_log.cc index 57194a3a8295..632fdc847221 100644 --- a/starboard/shared/starboard/net_log.cc +++ b/starboard/shared/starboard/net_log.cc @@ -14,6 +14,7 @@ #include "starboard/shared/starboard/net_log.h" +#include #include #include @@ -389,21 +390,21 @@ class NetLogServer { class ThreadLocalBoolean { public: - ThreadLocalBoolean() : slot_(SbThreadCreateLocalKey(NULL)) {} - ~ThreadLocalBoolean() { SbThreadDestroyLocalKey(slot_); } + ThreadLocalBoolean() { pthread_key_create(&slot_, NULL); } + ~ThreadLocalBoolean() { pthread_key_delete(slot_); } void Set(bool val) { void* ptr = val ? reinterpret_cast(0x1) : nullptr; - SbThreadSetLocalValue(slot_, ptr); + pthread_setspecific(slot_, ptr); } bool Get() const { - void* ptr = SbThreadGetLocalValue(slot_); + void* ptr = pthread_getspecific(slot_); return ptr != nullptr; } private: - SbThreadLocalKey slot_; + pthread_key_t slot_; ThreadLocalBoolean(const ThreadLocalBoolean&) = delete; void operator=(const ThreadLocalBoolean&) = delete; diff --git a/starboard/shared/starboard/player/job_queue.cc b/starboard/shared/starboard/player/job_queue.cc index baba61c340d5..59b7c4b9f4e7 100644 --- a/starboard/shared/starboard/player/job_queue.cc +++ b/starboard/shared/starboard/player/job_queue.cc @@ -29,21 +29,21 @@ namespace player { namespace { pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + int res = pthread_key_create(&s_thread_local_key, NULL); + SB_DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + SB_DCHECK(s_thread_local_key); } JobQueue* GetCurrentThreadJobQueue() { EnsureThreadLocalKeyInited(); - return static_cast(SbThreadGetLocalValue(s_thread_local_key)); + return static_cast(pthread_getspecific(s_thread_local_key)); } void SetCurrentThreadJobQueue(JobQueue* job_queue) { @@ -51,14 +51,14 @@ void SetCurrentThreadJobQueue(JobQueue* job_queue) { SB_DCHECK(GetCurrentThreadJobQueue() == NULL); EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, job_queue); + pthread_setspecific(s_thread_local_key, job_queue); } void ResetCurrentThreadJobQueue() { SB_DCHECK(GetCurrentThreadJobQueue()); EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, NULL); + pthread_setspecific(s_thread_local_key, NULL); } } // namespace diff --git a/starboard/shared/starboard/thread_create_local_key.cc b/starboard/shared/starboard/thread_create_local_key.cc index 211e9474c32b..a1f7d69008f3 100644 --- a/starboard/shared/starboard/thread_create_local_key.cc +++ b/starboard/shared/starboard/thread_create_local_key.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include "starboard/shared/starboard/thread_local_storage_internal.h" @@ -19,3 +21,5 @@ SbThreadLocalKey SbThreadCreateLocalKey(SbThreadLocalDestructor destructor) { return starboard::shared::TLSKeyManager::Get()->CreateKey(destructor); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/starboard/thread_destroy_local_key.cc b/starboard/shared/starboard/thread_destroy_local_key.cc index 48ead9a65508..f4a194625e31 100644 --- a/starboard/shared/starboard/thread_destroy_local_key.cc +++ b/starboard/shared/starboard/thread_destroy_local_key.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include "starboard/shared/starboard/thread_local_storage_internal.h" @@ -19,3 +21,4 @@ void SbThreadDestroyLocalKey(SbThreadLocalKey key) { starboard::shared::TLSKeyManager::Get()->DestroyKey(key); } +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/starboard/thread_get_local_value.cc b/starboard/shared/starboard/thread_get_local_value.cc index aa557ab91b81..7d51b6fb37d8 100644 --- a/starboard/shared/starboard/thread_get_local_value.cc +++ b/starboard/shared/starboard/thread_get_local_value.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include "starboard/shared/starboard/thread_local_storage_internal.h" @@ -19,3 +21,4 @@ void* SbThreadGetLocalValue(SbThreadLocalKey key) { return starboard::shared::TLSKeyManager::Get()->GetLocalValue(key); } +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/starboard/thread_local_storage_internal.h b/starboard/shared/starboard/thread_local_storage_internal.h index 13e988a18f20..1964af0c737a 100644 --- a/starboard/shared/starboard/thread_local_storage_internal.h +++ b/starboard/shared/starboard/thread_local_storage_internal.h @@ -15,6 +15,8 @@ #ifndef STARBOARD_SHARED_STARBOARD_THREAD_LOCAL_STORAGE_INTERNAL_H_ #define STARBOARD_SHARED_STARBOARD_THREAD_LOCAL_STORAGE_INTERNAL_H_ +#if SB_API_VERSION < 16 + #include #include "starboard/common/mutex.h" @@ -78,4 +80,5 @@ class TLSKeyManager { } // namespace shared } // namespace starboard +#endif // SB_API_VERSION < 16 #endif // STARBOARD_SHARED_STARBOARD_THREAD_LOCAL_STORAGE_INTERNAL_H_ diff --git a/starboard/shared/stub/thread_create_local_key.cc b/starboard/shared/stub/thread_create_local_key.cc index 6974ccb643e7..dd9324efa7e7 100644 --- a/starboard/shared/stub/thread_create_local_key.cc +++ b/starboard/shared/stub/thread_create_local_key.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" SbThreadLocalKey SbThreadCreateLocalKey(SbThreadLocalDestructor destructor) { return kSbThreadLocalKeyInvalid; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/thread_destroy_local_key.cc b/starboard/shared/stub/thread_destroy_local_key.cc index f5e36559318c..055adc5a2aae 100644 --- a/starboard/shared/stub/thread_destroy_local_key.cc +++ b/starboard/shared/stub/thread_destroy_local_key.cc @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" void SbThreadDestroyLocalKey(SbThreadLocalKey key) {} + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/thread_get_local_value.cc b/starboard/shared/stub/thread_get_local_value.cc index 103c5bed5103..ac0e867197e9 100644 --- a/starboard/shared/stub/thread_get_local_value.cc +++ b/starboard/shared/stub/thread_get_local_value.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" void* SbThreadGetLocalValue(SbThreadLocalKey key) { return NULL; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/thread_set_local_value.cc b/starboard/shared/stub/thread_set_local_value.cc index 8288928e1043..a716c543a81d 100644 --- a/starboard/shared/stub/thread_set_local_value.cc +++ b/starboard/shared/stub/thread_set_local_value.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" bool SbThreadSetLocalValue(SbThreadLocalKey key, void* value) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/win32/posix_emu/include/pthread.h b/starboard/shared/win32/posix_emu/include/pthread.h index 46546c0bb616..ea0743946b45 100644 --- a/starboard/shared/win32/posix_emu/include/pthread.h +++ b/starboard/shared/win32/posix_emu/include/pthread.h @@ -15,25 +15,43 @@ #ifndef STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_PTHREAD_H_ #define STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_PTHREAD_H_ +#include #include -#include -#include "starboard/shared/win32/posix_emu/include/remove_problematic_windows_macros.h" +#ifdef __cplusplus +#define _INITIALIZER \ + {} +#else +#define _INITIALIZER \ + { 0 } +#endif -#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT -#define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT -#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT +#define PTHREAD_MUTEX_INITIALIZER _INITIALIZER +#define PTHREAD_COND_INITIALIZER _INITIALIZER +#define PTHREAD_ONCE_INIT _INITIALIZER #ifdef __cplusplus extern "C" { #endif -typedef SRWLOCK pthread_mutex_t; +typedef union pthread_cond_t { + uint8_t buffer[8]; + void* ptr; +} pthread_cond_t; + +typedef union pthread_mutex_t { + uint8_t buffer[8]; + void* ptr; +} pthread_mutex_t; + +typedef union pthread_once_t { + uint8_t buffer[8]; + void* ptr; +} pthread_once_t; + typedef unsigned int pthread_mutexattr_t; -typedef void* pthread_key_t; -typedef CONDITION_VARIABLE pthread_cond_t; +typedef uintptr_t pthread_key_t; typedef unsigned int pthread_condattr_t; -typedef INIT_ONCE pthread_once_t; typedef void* pthread_t; typedef void* pthread_attr_t; diff --git a/starboard/shared/win32/posix_emu/include/remove_problematic_windows_macros.h b/starboard/shared/win32/posix_emu/include/remove_problematic_windows_macros.h index d36bc56fdd50..41f8d891eb03 100644 --- a/starboard/shared/win32/posix_emu/include/remove_problematic_windows_macros.h +++ b/starboard/shared/win32/posix_emu/include/remove_problematic_windows_macros.h @@ -23,3 +23,4 @@ #undef DeleteFile // fileapi.h #undef GetCurrentTime // winbase.h; b/324981660 #undef PostMessage // winuser.h +#undef ReplaceFile // winbase.h diff --git a/starboard/shared/win32/posix_emu/pthread.cc b/starboard/shared/win32/posix_emu/pthread.cc index 8f507d9c1ba4..97e1e460134a 100644 --- a/starboard/shared/win32/posix_emu/pthread.cc +++ b/starboard/shared/win32/posix_emu/pthread.cc @@ -15,6 +15,8 @@ #include #include #include +#include + #include #include "starboard/common/log.h" @@ -29,6 +31,7 @@ using starboard::shared::win32::GetCurrentSbThreadPrivate; using starboard::shared::win32::GetThreadSubsystemSingleton; using starboard::shared::win32::SbThreadPrivate; using starboard::shared::win32::ThreadCreateInfo; +using starboard::shared::win32::ThreadSetLocalValue; using starboard::shared::win32::ThreadSubsystemSingleton; using starboard::shared::win32::TlsInternalFree; using starboard::shared::win32::TlsInternalGetValue; @@ -47,10 +50,11 @@ int pthread_mutex_destroy(pthread_mutex_t* mutex) { int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* mutex_attr) { + static_assert(sizeof(SRWLOCK) == sizeof(mutex->buffer)); if (!mutex) { return EINVAL; } - InitializeSRWLock(mutex); + InitializeSRWLock(reinterpret_cast(mutex->buffer)); return 0; } @@ -58,7 +62,7 @@ int pthread_mutex_lock(pthread_mutex_t* mutex) { if (!mutex) { return EINVAL; } - AcquireSRWLockExclusive(mutex); + AcquireSRWLockExclusive(reinterpret_cast(mutex->buffer)); return 0; } @@ -66,7 +70,7 @@ int pthread_mutex_unlock(pthread_mutex_t* mutex) { if (!mutex) { return EINVAL; } - ReleaseSRWLockExclusive(mutex); + ReleaseSRWLockExclusive(reinterpret_cast(mutex->buffer)); return 0; } @@ -74,7 +78,8 @@ int pthread_mutex_trylock(pthread_mutex_t* mutex) { if (!mutex) { return EINVAL; } - bool result = TryAcquireSRWLockExclusive(mutex); + bool result = + TryAcquireSRWLockExclusive(reinterpret_cast(mutex->buffer)); return result ? 0 : EBUSY; } @@ -82,7 +87,7 @@ int pthread_cond_broadcast(pthread_cond_t* cond) { if (!cond) { return -1; } - WakeAllConditionVariable(cond); + WakeAllConditionVariable(reinterpret_cast(cond->buffer)); return 0; } @@ -91,10 +96,12 @@ int pthread_cond_destroy(pthread_cond_t* cond) { } int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) { + static_assert(sizeof(CONDITION_VARIABLE) == sizeof(cond->buffer)); if (!cond) { return -1; } - InitializeConditionVariable(cond); + InitializeConditionVariable( + reinterpret_cast(cond->buffer)); return 0; } @@ -102,7 +109,7 @@ int pthread_cond_signal(pthread_cond_t* cond) { if (!cond) { return -1; } - WakeConditionVariable(cond); + WakeConditionVariable(reinterpret_cast(cond->buffer)); return -0; } @@ -116,7 +123,9 @@ int pthread_cond_timedwait(pthread_cond_t* cond, int64_t now_ms = starboard::CurrentMonotonicTime() / 1000; int64_t timeout_duration_ms = t->tv_sec * 1000 + t->tv_nsec / 1000000; timeout_duration_ms -= now_ms; - bool result = SleepConditionVariableSRW(cond, mutex, timeout_duration_ms, 0); + bool result = SleepConditionVariableSRW( + reinterpret_cast(cond->buffer), + reinterpret_cast(mutex->buffer), timeout_duration_ms, 0); if (result) { return 0; @@ -133,7 +142,9 @@ int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { return -1; } - if (SleepConditionVariableSRW(cond, mutex, INFINITE, 0)) { + if (SleepConditionVariableSRW( + reinterpret_cast(cond->buffer), + reinterpret_cast(mutex->buffer), INFINITE, 0)) { return 0; } return -1; @@ -168,10 +179,12 @@ static BOOL CALLBACK OnceTrampoline(PINIT_ONCE once_control, } int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { + static_assert(sizeof(INIT_ONCE) == sizeof(once_control->buffer)); if (!once_control || !init_routine) { return -1; } - return InitOnceExecuteOnce(once_control, OnceTrampoline, init_routine, NULL) + return InitOnceExecuteOnce(reinterpret_cast(once_control->buffer), + OnceTrampoline, init_routine, NULL) ? 0 : -1; } @@ -181,7 +194,7 @@ static unsigned ThreadTrampoline(void* thread_create_info_context) { static_cast(thread_create_info_context)); ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); - SbThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); + ThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); SbThreadSetName(info->name_.c_str()); void* result = info->entry_point_(info->user_context_); @@ -278,7 +291,8 @@ int pthread_equal(pthread_t t1, pthread_t t2) { int pthread_key_create(pthread_key_t* key, void (*dtor)(void*)) { ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); - *key = SbThreadCreateLocalKeyInternal(dtor, singleton); + *key = reinterpret_cast( + SbThreadCreateLocalKeyInternal(dtor, singleton)); return 0; } @@ -288,8 +302,8 @@ int pthread_key_delete(pthread_key_t key) { } // To match pthreads, the thread local pointer for the key is set to null // so that a supplied destructor doesn't run. - SbThreadSetLocalValue(reinterpret_cast(key), nullptr); - DWORD tls_index = static_cast(key)->tls_index; + ThreadSetLocalValue(reinterpret_cast(key), nullptr); + DWORD tls_index = reinterpret_cast(key)->tls_index; ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); SbMutexAcquire(&singleton->mutex_); @@ -297,7 +311,7 @@ int pthread_key_delete(pthread_key_t key) { SbMutexRelease(&singleton->mutex_); TlsInternalFree(tls_index); - free(key); + free(reinterpret_cast(key)); return 0; } @@ -305,7 +319,7 @@ void* pthread_getspecific(pthread_key_t key) { if (!key) { return NULL; } - DWORD tls_index = static_cast(key)->tls_index; + DWORD tls_index = reinterpret_cast(key)->tls_index; return TlsInternalGetValue(tls_index); } @@ -313,7 +327,7 @@ int pthread_setspecific(pthread_key_t key, const void* value) { if (!key) { return -1; } - DWORD tls_index = static_cast(key)->tls_index; + DWORD tls_index = reinterpret_cast(key)->tls_index; return TlsInternalSetValue(tls_index, const_cast(value)) ? 0 : -1; } diff --git a/starboard/shared/win32/thread_create.cc b/starboard/shared/win32/thread_create.cc index 49db041245aa..b7ac621c5d9d 100644 --- a/starboard/shared/win32/thread_create.cc +++ b/starboard/shared/win32/thread_create.cc @@ -29,6 +29,8 @@ using sbwin32::DebugLogWinError; using sbwin32::GetThreadSubsystemSingleton; using sbwin32::SbThreadPrivate; using sbwin32::ThreadCreateInfo; +using sbwin32::ThreadGetLocalValue; +using sbwin32::ThreadSetLocalValue; using sbwin32::ThreadSubsystemSingleton; using sbwin32::wchar_tToUTF8; @@ -47,11 +49,11 @@ int RunThreadLocalDestructors(ThreadSubsystemSingleton* singleton) { continue; } auto key = curr_it->second; - void* entry = SbThreadGetLocalValue(key); + void* entry = ThreadGetLocalValue(reinterpret_cast(key)); if (!entry) { continue; } - SbThreadSetLocalValue(key, nullptr); + ThreadSetLocalValue(reinterpret_cast(key), nullptr); ++num_destructors_called; curr_it->second->destructor(entry); } @@ -66,7 +68,7 @@ int CountTlsObjectsRemaining(ThreadSubsystemSingleton* singleton) { continue; } auto key = it->second; - void* entry = SbThreadGetLocalValue(key); + void* entry = ThreadGetLocalValue(reinterpret_cast(key)); if (!entry) { continue; } @@ -110,7 +112,7 @@ unsigned ThreadTrampoline(void* thread_create_info_context) { static_cast(thread_create_info_context)); ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); - SbThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); + ThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); SbThreadSetName(info->name_.c_str()); void* result = info->entry_point_(info->user_context_); diff --git a/starboard/shared/win32/thread_destroy_local_key.cc b/starboard/shared/win32/thread_destroy_local_key.cc index 9cf7a84e2fb9..d03267928199 100644 --- a/starboard/shared/win32/thread_destroy_local_key.cc +++ b/starboard/shared/win32/thread_destroy_local_key.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -41,3 +43,4 @@ void SbThreadDestroyLocalKey(SbThreadLocalKey key) { TlsInternalFree(tls_index); free(key); } +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/win32/thread_get_local_value.cc b/starboard/shared/win32/thread_get_local_value.cc index d36c3bb3f466..4bffe5b802c4 100644 --- a/starboard/shared/win32/thread_get_local_value.cc +++ b/starboard/shared/win32/thread_get_local_value.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -28,3 +30,4 @@ void* SbThreadGetLocalValue(SbThreadLocalKey key) { DWORD tls_index = static_cast(key)->tls_index; return TlsInternalGetValue(tls_index); } +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/win32/thread_private.cc b/starboard/shared/win32/thread_private.cc index 665ee98102e2..d537477be8f4 100644 --- a/starboard/shared/win32/thread_private.cc +++ b/starboard/shared/win32/thread_private.cc @@ -19,11 +19,13 @@ #include "starboard/common/log.h" #include "starboard/common/once.h" +#include "starboard/shared/win32/thread_local_internal.h" #include "starboard/shared/win32/thread_private.h" using starboard::shared::win32::GetThreadSubsystemSingleton; using starboard::shared::win32::SbThreadPrivate; using starboard::shared::win32::ThreadSubsystemSingleton; +using starboard::shared::win32::TlsInternalSetValue; namespace starboard { namespace shared { @@ -32,6 +34,22 @@ namespace win32 { SB_ONCE_INITIALIZE_FUNCTION(ThreadSubsystemSingleton, GetThreadSubsystemSingleton); +bool ThreadSetLocalValue(SbThreadLocalKey key, void* value) { + if (!SbThreadIsValidLocalKey(key)) { + return false; + } + DWORD tls_index = reinterpret_cast(key)->tls_index; + return TlsInternalSetValue(tls_index, value); +} + +void* ThreadGetLocalValue(SbThreadLocalKey key) { + if (!SbThreadIsValidLocalKey(key)) { + return nullptr; + } + DWORD tls_index = reinterpret_cast(key)->tls_index; + return TlsInternalGetValue(tls_index); +} + void RegisterMainThread() { std::unique_ptr thread_private(new SbThreadPrivate()); @@ -47,18 +65,17 @@ void RegisterMainThread() { thread_private->handle_ = handle; thread_private->wait_for_join_ = false; - SbThreadSetLocalValue(GetThreadSubsystemSingleton()->thread_private_key_, - thread_private.release()); + ThreadSetLocalValue(GetThreadSubsystemSingleton()->thread_private_key_, + thread_private.release()); } SbThreadPrivate* GetCurrentSbThreadPrivate() { - SbThreadPrivate* sb_thread_private = - static_cast(SbThreadGetLocalValue( - GetThreadSubsystemSingleton()->thread_private_key_)); + SbThreadPrivate* sb_thread_private = static_cast( + ThreadGetLocalValue(GetThreadSubsystemSingleton()->thread_private_key_)); if (sb_thread_private == nullptr) { // We are likely on a thread we did not create, so TLS needs to be setup. RegisterMainThread(); - sb_thread_private = static_cast(SbThreadGetLocalValue( + sb_thread_private = static_cast(ThreadGetLocalValue( GetThreadSubsystemSingleton()->thread_private_key_)); // TODO: Clean up TLS storage for threads we do not create. } diff --git a/starboard/shared/win32/thread_private.h b/starboard/shared/win32/thread_private.h index 38b57dc12791..d5e62c4d0d5a 100644 --- a/starboard/shared/win32/thread_private.h +++ b/starboard/shared/win32/thread_private.h @@ -26,17 +26,30 @@ #include "starboard/shared/internal_only.h" #include "starboard/thread.h" +#define kSbThreadLocalKeyInvalid (SbThreadLocalKey) NULL + struct SbThreadLocalKeyPrivate { DWORD tls_index; SbThreadLocalDestructor destructor; }; +typedef SbThreadLocalKeyPrivate* SbThreadLocalKey; + +static inline bool SbThreadIsValidLocalKey(SbThreadLocalKey key) { + return key != kSbThreadLocalKeyInvalid; +} + namespace starboard { namespace shared { namespace win32 { class ThreadSubsystemSingleton; +SbThreadLocalKey ThreadCreateLocalKey(SbThreadLocalDestructor destructor); +void ThreadDestroyLocalKey(SbThreadLocalKey key); +void* ThreadGetLocalValue(SbThreadLocalKey key); +bool ThreadSetLocalValue(SbThreadLocalKey key, void* value); + // Creates a SbThreadLocalKey given a ThreadSubsystemSingleton. Used // to create the first SbThreadLocalKey. SbThreadLocalKey SbThreadCreateLocalKeyInternal( diff --git a/starboard/shared/win32/thread_set_local_value.cc b/starboard/shared/win32/thread_set_local_value.cc index f857db03464c..e69e198adbaf 100644 --- a/starboard/shared/win32/thread_set_local_value.cc +++ b/starboard/shared/win32/thread_set_local_value.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/thread.h" #include @@ -28,3 +30,4 @@ bool SbThreadSetLocalValue(SbThreadLocalKey key, void* value) { DWORD tls_index = static_cast(key)->tls_index; return TlsInternalSetValue(tls_index, value); } +#endif // SB_API_VERSION < 16 diff --git a/starboard/thread.h b/starboard/thread.h index d8974f93fa2c..4b0349cc2394 100644 --- a/starboard/thread.h +++ b/starboard/thread.h @@ -94,11 +94,13 @@ typedef void (*SbThreadLocalDestructor)(void* value); // may have specific rules about how it must be used. typedef int32_t SbThreadAffinity; +#if SB_API_VERSION < 16 // Private structure representing a thread-local key. typedef struct SbThreadLocalKeyPrivate SbThreadLocalKeyPrivate; // A handle to a thread-local key. typedef SbThreadLocalKeyPrivate* SbThreadLocalKey; +#endif // Well-defined constant value to mean "no thread ID." #define kSbThreadInvalidId (SbThreadId)0 @@ -106,8 +108,10 @@ typedef SbThreadLocalKeyPrivate* SbThreadLocalKey; // Well-defined constant value to mean "no affinity." #define kSbThreadNoAffinity (SbThreadAffinity) kSbInvalidInt +#if SB_API_VERSION < 16 // Well-defined constant value to mean "no thread local key." #define kSbThreadLocalKeyInvalid (SbThreadLocalKey) NULL +#endif // Returns whether the given thread handle is valid. static inline bool SbThreadIsValid(SbThread thread) { @@ -129,10 +133,12 @@ static inline bool SbThreadIsValidAffinity(SbThreadAffinity affinity) { return affinity != kSbThreadNoAffinity; } +#if SB_API_VERSION < 16 // Returns whether the given thread local variable key is valid. static inline bool SbThreadIsValidLocalKey(SbThreadLocalKey key) { return key != kSbThreadLocalKeyInvalid; } +#endif // Creates a new thread, which starts immediately. // - If the function succeeds, the return value is a handle to the newly @@ -193,9 +199,7 @@ SB_EXPORT void SbThreadDetach(SbThread thread); #if SB_API_VERSION < 16 // Yields the currently executing thread, so another thread has a chance to run. SB_EXPORT void SbThreadYield(); -#endif -#if SB_API_VERSION < 16 // Sleeps the currently executing thread. // // |duration|: The minimum amount of time, in microseconds, that the currently @@ -225,6 +229,7 @@ SB_EXPORT void SbThreadGetName(char* buffer, int buffer_size); // |name|: The name to assign to the thread. SB_EXPORT void SbThreadSetName(const char* name); +#if SB_API_VERSION < 16 // Creates and returns a new, unique key for thread local data. If the function // does not succeed, the function returns |kSbThreadLocalKeyInvalid|. // @@ -259,6 +264,7 @@ SB_EXPORT void* SbThreadGetLocalValue(SbThreadLocalKey key); // |key|: The key for which to set the key value. // |value|: The new pointer-sized key value. SB_EXPORT bool SbThreadSetLocalValue(SbThreadLocalKey key, void* value); +#endif // Returns whether |thread| is the current thread. // diff --git a/third_party/boringssl/src/crypto/thread_starboard.cc b/third_party/boringssl/src/crypto/thread_starboard.cc index bfc49c833c8e..7aa41203a749 100644 --- a/third_party/boringssl/src/crypto/thread_starboard.cc +++ b/third_party/boringssl/src/crypto/thread_starboard.cc @@ -33,7 +33,7 @@ struct ThreadLocalEntry { }; // One thread local key will point to an array of ThreadLocalEntry. -SbThreadLocalKey g_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t g_thread_local_key = 0; // Control the creation of the global thread local key. pthread_once_t g_thread_local_once_control = PTHREAD_ONCE_INIT; @@ -51,7 +51,7 @@ void ThreadLocalDestructor(void* value) { } void ThreadLocalInit() { - g_thread_local_key = SbThreadCreateLocalKey(&ThreadLocalDestructor); + pthread_key_create(&g_thread_local_key, &ThreadLocalDestructor); } void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) { @@ -184,12 +184,12 @@ void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX* lock) { void* CRYPTO_get_thread_local(thread_local_data_t index) { pthread_once(&g_thread_local_once_control, &ThreadLocalInit); - if (!SbThreadIsValidLocalKey(g_thread_local_key)) { + if (!g_thread_local_key) { return nullptr; } ThreadLocalEntry* thread_locals = static_cast( - SbThreadGetLocalValue(g_thread_local_key)); + pthread_getspecific(g_thread_local_key)); if (thread_locals == nullptr) { return nullptr; } @@ -200,13 +200,13 @@ void* CRYPTO_get_thread_local(thread_local_data_t index) { int CRYPTO_set_thread_local(thread_local_data_t index, void *value, thread_local_destructor_t destructor) { pthread_once(&g_thread_local_once_control, &ThreadLocalInit); - if (!SbThreadIsValidLocalKey(g_thread_local_key)) { + if (!g_thread_local_key) { destructor(value); return 0; } ThreadLocalEntry* thread_locals = static_cast( - SbThreadGetLocalValue(g_thread_local_key)); + pthread_getspecific(g_thread_local_key)); if (thread_locals == nullptr) { size_t thread_locals_size = sizeof(ThreadLocalEntry) * NUM_OPENSSL_THREAD_LOCALS; @@ -217,7 +217,7 @@ int CRYPTO_set_thread_local(thread_local_data_t index, void *value, return 0; } OPENSSL_memset(thread_locals, 0, thread_locals_size); - if (!SbThreadSetLocalValue(g_thread_local_key, thread_locals)) { + if (pthread_setspecific(g_thread_local_key, thread_locals) !=0 ) { OPENSSL_free(thread_locals); destructor(value); return 0; diff --git a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h index 79b5a729e195..b6f3dbca6545 100644 --- a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h +++ b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h @@ -573,7 +573,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; GTEST_OS_HAIKU || GTEST_OS_GNU_HURD) #endif // GTEST_HAS_PTHREAD -#if GTEST_HAS_PTHREAD +#if GTEST_HAS_PTHREAD || GTEST_OS_STARBOARD // gtest-port.h guarantees to #include when GTEST_HAS_PTHREAD is // true. #include // NOLINT @@ -1270,16 +1270,15 @@ template class ThreadLocal { public: ThreadLocal() { - key_ = SbThreadCreateLocalKey( - [](void* value) { delete static_cast(value); }); - SB_DCHECK(key_ != kSbThreadLocalKeyInvalid); + int res = pthread_key_create(&key_, [](void* value) { delete static_cast(value); }); + SB_DCHECK(res == 0); } explicit ThreadLocal(const T& value) : ThreadLocal() { default_value_ = value; set(value); } ~ThreadLocal() { - SbThreadDestroyLocalKey(key_); + pthread_key_delete(key_); } T* pointer() { return GetOrCreateValue(); } const T* pointer() const { return GetOrCreateValue(); } @@ -1287,18 +1286,18 @@ class ThreadLocal { void set(const T& value) { *GetOrCreateValue() = value; } private: T* GetOrCreateValue() const { - T* ptr = static_cast(SbThreadGetLocalValue(key_)); + T* ptr = static_cast(pthread_getspecific(key_)); if (ptr) { return ptr; } else { T* new_value = new T(default_value_); - bool is_set = SbThreadSetLocalValue(key_, new_value); - SB_CHECK(is_set); + int res = pthread_setspecific(key_, new_value); + SB_CHECK(res == 0); return new_value; } } T default_value_; - SbThreadLocalKey key_; + pthread_key_t key_; }; #else // GTEST_OS_STARBOARD diff --git a/third_party/libjpeg-turbo/turbojpeg.c b/third_party/libjpeg-turbo/turbojpeg.c index ee6f5ea36ed0..99876e981f1c 100644 --- a/third_party/libjpeg-turbo/turbojpeg.c +++ b/third_party/libjpeg-turbo/turbojpeg.c @@ -64,19 +64,19 @@ extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *, /* Error handling (based on example in example.txt) */ #if defined(STARBOARD) -static SbThreadLocalKey g_err_key = kSbThreadLocalKeyInvalid; +static pthread_key_t g_err_key = 0; static pthread_once_t g_err_once = PTHREAD_ONCE_INIT; void initialize_err_key(void) { - g_err_key = SbThreadCreateLocalKey(free); + pthread_key_create(&g_err_key , free); - SB_DCHECK(SbThreadIsValidLocalKey(g_err_key)); + SB_DCHECK(g_err_key); } char* errStr() { pthread_once(&g_err_once, initialize_err_key); - char* value = (char*)(SbThreadGetLocalValue(g_err_key)); + char* value = (char*)(pthread_getspecific(g_err_key)); if (value) { return value; @@ -85,7 +85,8 @@ char* errStr() { value = malloc(sizeof(char) * JMSG_LENGTH_MAX); SB_DCHECK(value); - SB_DCHECK(SbThreadSetLocalValue(g_err_key, value)); + int result = pthread_setspecific(g_err_key, value); + SB_DCHECK(result == 0); memset(value, 0, JMSG_LENGTH_MAX); strcpy(value, "No error"); diff --git a/third_party/llvm-project/libcxx/include/__external_threading b/third_party/llvm-project/libcxx/include/__external_threading index 795a29341fec..42e3e7c5a302 100644 --- a/third_party/llvm-project/libcxx/include/__external_threading +++ b/third_party/llvm-project/libcxx/include/__external_threading @@ -39,7 +39,7 @@ typedef pthread_once_t __libcpp_exec_once_flag; typedef SbThread __libcpp_thread_t; typedef SbThread __libcpp_thread_id; -typedef SbThreadLocalKey __libcpp_tls_key; +typedef pthread_key_t __libcpp_tls_key; #define _LIBCPP_NULL_THREAD kSbThreadInvalid // The calling-convention for the thread-local storage destructor. This is @@ -271,9 +271,10 @@ void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) { } int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { - const SbThreadLocalKey __k = SbThreadCreateLocalKey(__at_exit); + pthread_key_t __k = 0; + pthread_key_create(&__k,__at_exit); - if (__k != kSbThreadLocalKeyInvalid) { + if (__k) { *__key = __k; return 0; } @@ -281,11 +282,11 @@ int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { } void* __libcpp_tls_get(__libcpp_tls_key __key) { - return SbThreadGetLocalValue(__key); + return pthread_getspecific(__key); } int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) { - if (SbThreadSetLocalValue(__key, __p)) { + if (pthread_setspecific(__key, __p) !=0) { return 0; } return 1; diff --git a/third_party/musl/src/starboard/errno/__errno_location.c b/third_party/musl/src/starboard/errno/__errno_location.c index 977c8c9b501a..7990d70701ae 100644 --- a/third_party/musl/src/starboard/errno/__errno_location.c +++ b/third_party/musl/src/starboard/errno/__errno_location.c @@ -6,12 +6,12 @@ #include "starboard/thread.h" #include "../pthread/pthread.h" -static SbThreadLocalKey g_errno_key = kSbThreadLocalKeyInvalid; +static pthread_key_t g_errno_key = 0; static pthread_once_t g_errno_once = PTHREAD_ONCE_INIT; void initialize_errno_key(void) { - g_errno_key = SbThreadCreateLocalKey(free); - SB_DCHECK(g_errno_key != kSbThreadLocalKeyInvalid); + pthread_key_create(&g_errno_key , free); + SB_DCHECK(g_errno_key); } // __errno_location() provides every thread with its own copy of |errno|. @@ -23,9 +23,10 @@ void initialize_errno_key(void) { // This function does not take much effect in musl, use SbSystemGetLastError to set // errno instead. int *__errno_location(void) { - SB_DCHECK(pthread_once(&g_errno_once, &initialize_errno_key) == 0); + int result = pthread_once(&g_errno_once, &initialize_errno_key); + SB_DCHECK(result == 0); - int* value = (int*)SbThreadGetLocalValue(g_errno_key); + int* value = (int*)pthread_getspecific(g_errno_key); if (value) { return value; @@ -34,7 +35,8 @@ int *__errno_location(void) { value = (int*)malloc(sizeof(int)); SB_DCHECK(value); - SB_DCHECK(SbThreadSetLocalValue(g_errno_key, value)); + result = pthread_setspecific(g_errno_key, value); + SB_DCHECK(result == 0); *value = 0; diff --git a/third_party/musl/src/starboard/pthread/pthread.h b/third_party/musl/src/starboard/pthread/pthread.h index 60acecf66c61..559bff63fab0 100644 --- a/third_party/musl/src/starboard/pthread/pthread.h +++ b/third_party/musl/src/starboard/pthread/pthread.h @@ -58,7 +58,7 @@ typedef union pthread_mutexattr_t { void* ptr; } pthread_mutexattr_t; -typedef void* pthread_key_t; +typedef uintptr_t pthread_key_t; #ifdef __cplusplus #define PTHREAD_COND_INITIALIZER \ @@ -111,7 +111,7 @@ typedef union pthread_attr_t { } pthread_attr_t; typedef int clockid_t; -typedef void* pthread_t; +typedef uintptr_t pthread_t; // Max size of the pthread_once_t type. #define MUSL_PTHREAD_ONCE_MAX_SIZE 64 diff --git a/third_party/protobuf/src/google/protobuf/stubs/mutex.h b/third_party/protobuf/src/google/protobuf/stubs/mutex.h index cc092e054a11..7ef1cb691635 100644 --- a/third_party/protobuf/src/google/protobuf/stubs/mutex.h +++ b/third_party/protobuf/src/google/protobuf/stubs/mutex.h @@ -31,12 +31,8 @@ #define GOOGLE_PROTOBUF_STUBS_MUTEX_H_ #ifdef GOOGLE_PROTOBUF_NO_THREADLOCAL -#if defined(STARBOARD) -#include "starboard/thread.h" -#else #include #endif -#endif #include @@ -112,32 +108,16 @@ template class ThreadLocalStorage { public: ThreadLocalStorage() { -#if defined(STARBOARD) - key_ = SbThreadCreateLocalKey(&ThreadLocalStorage::Delete); -#else pthread_key_create(&key_, &ThreadLocalStorage::Delete); -#endif } ~ThreadLocalStorage() { -#if defined(STARBOARD) - SbThreadDestroyLocalKey(key_); -#else pthread_key_delete(key_); -#endif } T* Get() { -#if defined(STARBOARD) - T* result = static_cast(SbThreadGetLocalValue(key_)); -#else T* result = static_cast(pthread_getspecific(key_)); -#endif if (result == NULL) { result = new T(); -#if defined(STARBOARD) - SbThreadSetLocalValue(key_, result); -#else pthread_setspecific(key_, result); -#endif } return result; } @@ -145,11 +125,7 @@ class ThreadLocalStorage { static void Delete(void* value) { delete static_cast(value); } -#if defined(STARBOARD) - SbThreadLocalKey key_; -#else pthread_key_t key_; -#endif GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage); }; diff --git a/third_party/skia/src/sksl/SkSLPool.cpp b/third_party/skia/src/sksl/SkSLPool.cpp index 7effa40b0d60..dae215ec9560 100644 --- a/third_party/skia/src/sksl/SkSLPool.cpp +++ b/third_party/skia/src/sksl/SkSLPool.cpp @@ -31,27 +31,28 @@ static void set_thread_local_memory_pool(MemoryPool* memPool) { #else namespace { pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(nullptr); - SkASSERT(SbThreadIsValidLocalKey(s_thread_local_key)); - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_key_create(&s_thread_local_key , nullptr); + SkASSERT(s_thread_local_key); + pthread_setspecific(s_thread_local_key, nullptr); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - SkASSERT(SbThreadIsValidLocalKey(s_thread_local_key)); + SkASSERT(s_thread_local_key); } } // namespace static MemoryPool* get_thread_local_memory_pool() { - return static_cast(SbThreadGetLocalValue(s_thread_local_key)); + EnsureThreadLocalKeyInited(); + return static_cast(pthread_getspecific(s_thread_local_key)); } static void set_thread_local_memory_pool(MemoryPool* memPool) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, memPool); + pthread_setspecific(s_thread_local_key, memPool); } #endif diff --git a/third_party/skia/src/sksl/SkSLThreadContext.cpp b/third_party/skia/src/sksl/SkSLThreadContext.cpp index 63d3404580b4..6b9883fe69a6 100644 --- a/third_party/skia/src/sksl/SkSLThreadContext.cpp +++ b/third_party/skia/src/sksl/SkSLThreadContext.cpp @@ -214,29 +214,32 @@ namespace { ThreadContext* instance = nullptr; pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(nullptr); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); - SbThreadSetLocalValue(s_thread_local_key, nullptr); + pthread_key_create(&s_thread_local_key , nullptr); + SB_DCHECK(s_thread_local_key); + pthread_setspecific(s_thread_local_key, nullptr); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + SB_DCHECK(s_thread_local_key); } } // namespace -bool ThreadContext::IsActive() { return SbThreadGetLocalValue(s_thread_local_key) != nullptr; } +bool ThreadContext::IsActive() { + EnsureThreadLocalKeyInited(); + return pthread_getspecific(s_thread_local_key) != nullptr; +} ThreadContext& ThreadContext::Instance() { return *instance; } void ThreadContext::SetInstance(std::unique_ptr newInstance) { EnsureThreadLocalKeyInited(); delete instance; - SbThreadSetLocalValue(s_thread_local_key, static_cast(newInstance.release())); - instance = static_cast(SbThreadGetLocalValue(s_thread_local_key)); + pthread_setspecific(s_thread_local_key, static_cast(newInstance.release())); + instance = static_cast(pthread_getspecific(s_thread_local_key)); } #endif diff --git a/v8/src/base/platform/platform-starboard.cc b/v8/src/base/platform/platform-starboard.cc index ecab056bc21b..c9eebae2068b 100644 --- a/v8/src/base/platform/platform-starboard.cc +++ b/v8/src/base/platform/platform-starboard.cc @@ -7,6 +7,7 @@ // apps in the livingroom. #include +#include #include #include "src/base/lazy-instance.h" @@ -407,19 +408,21 @@ bool Thread::Start() { void Thread::Join() { SbThreadJoin(data_->thread_, nullptr); } Thread::LocalStorageKey Thread::CreateThreadLocalKey() { - return SbThreadCreateLocalKey(nullptr); + pthread_key_t key = 0; + pthread_key_create(&key, nullptr); + return key; } void Thread::DeleteThreadLocalKey(LocalStorageKey key) { - SbThreadDestroyLocalKey(key); + pthread_key_delete(key); } void* Thread::GetThreadLocal(LocalStorageKey key) { - return SbThreadGetLocalValue(key); + return pthread_getspecific(key); } void Thread::SetThreadLocal(LocalStorageKey key, void* value) { - bool result = SbThreadSetLocalValue(key, value); + bool result = pthread_setspecific(key, value) == 0; DCHECK(result); } diff --git a/v8/src/base/platform/platform.h b/v8/src/base/platform/platform.h index 042e4428cd31..8a3f1b65dc59 100644 --- a/v8/src/base/platform/platform.h +++ b/v8/src/base/platform/platform.h @@ -333,7 +333,7 @@ class V8_BASE_EXPORT Thread { public: // Opaque data type for thread-local storage keys. #if V8_OS_STARBOARD - using LocalStorageKey = SbThreadLocalKey; + using LocalStorageKey = uintptr_t; #else using LocalStorageKey = int32_t; #endif diff --git a/v8/src/heap/heap-write-barrier.cc b/v8/src/heap/heap-write-barrier.cc index 6f69b3d7e0a8..bef53db7bb4c 100644 --- a/v8/src/heap/heap-write-barrier.cc +++ b/v8/src/heap/heap-write-barrier.cc @@ -41,31 +41,33 @@ namespace internal { namespace { pthread_once_t s_once_flag = PTHREAD_ONCE_INIT; -SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid; +pthread_key_t s_thread_local_key = 0; void InitThreadLocalKey() { - s_thread_local_key = SbThreadCreateLocalKey(NULL); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + pthread_key_create(&s_thread_local_key , NULL); + SB_DCHECK(s_thread_local_key); } void EnsureThreadLocalKeyInited() { pthread_once(&s_once_flag, InitThreadLocalKey); - SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key)); + SB_DCHECK(s_thread_local_key); } MarkingBarrier* GetMarkingBarrier() { + EnsureThreadLocalKeyInited(); return static_cast( - SbThreadGetLocalValue(s_thread_local_key)); + pthread_getspecific(s_thread_local_key)); } } // namespace void WriteBarrier::SetForThread(MarkingBarrier* marking_barrier) { EnsureThreadLocalKeyInited(); - SbThreadSetLocalValue(s_thread_local_key, marking_barrier); + pthread_setspecific(s_thread_local_key, marking_barrier); } void WriteBarrier::ClearForThread(MarkingBarrier* marking_barrier) { - SbThreadSetLocalValue(s_thread_local_key, NULL); + EnsureThreadLocalKeyInited(); + pthread_setspecific(s_thread_local_key, NULL); } #endif