Skip to content

Commit

Permalink
Deprecate SbThreadLocalKey
Browse files Browse the repository at this point in the history
b/302335657

Test-On-Device: true
Change-Id: If4ea60a099fcfcf30632d18d7b76ed8bc53e5724
  • Loading branch information
y4vor committed Apr 29, 2024
1 parent 00c3930 commit 218d444
Show file tree
Hide file tree
Showing 68 changed files with 543 additions and 383 deletions.
11 changes: 6 additions & 5 deletions base/observer_list_threadsafe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions base/observer_list_threadsafe.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "third_party/abseil-cpp/absl/base/attributes.h"

#if defined(STARBOARD)
#include "starboard/thread.h"
#include <pthread.h>
#endif

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<NotificationDataBase*>(current_notification_void);
Expand Down Expand Up @@ -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<NotificationData*>(&notification));
void* scoped_reset_value = pthread_getspecific(GetThreadLocalKey());
pthread_setspecific(GetThreadLocalKey(), const_cast<NotificationData*>(&notification));
#else
const AutoReset<const NotificationDataBase*> resetter_(
&GetCurrentNotification(), &notification);
Expand All @@ -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
}

Expand Down
28 changes: 15 additions & 13 deletions base/run_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<RunLoop::Delegate*>(
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<const RunLoop::RunLoopTimeout*>(
SbThreadGetLocalValue(s_thread_local_timeout_key));
pthread_getspecific(s_thread_local_timeout_key));
}
#else
ABSL_CONST_INIT thread_local RunLoop::Delegate* delegate = nullptr;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<RunLoopTimeout*>(timeout));
pthread_setspecific(s_thread_local_timeout_key, const_cast<RunLoopTimeout*>(timeout));
#else
run_loop_timeout = timeout;
#endif
Expand Down
16 changes: 8 additions & 8 deletions base/sampling_heap_profiler/sampling_heap_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(SbThreadGetLocalValue(s_thread_local_key));
const char* thread_name = static_cast<const char*>(pthread_getspecific(s_thread_local_key));
if (name)
SbThreadSetLocalValue(s_thread_local_key, const_cast<char*>(name));
pthread_setspecific(s_thread_local_key, const_cast<char*>(name));
else if (!thread_name)
SbThreadSetLocalValue(s_thread_local_key, const_cast<char*>(GetAndLeakThreadName()));
return static_cast<const char*>(SbThreadGetLocalValue(s_thread_local_key));
pthread_setspecific(s_thread_local_key, const_cast<char*>(GetAndLeakThreadName()));
return static_cast<const char*>(pthread_getspecific(s_thread_local_key));
#else
static thread_local const char* thread_name;
if (name)
Expand Down
64 changes: 33 additions & 31 deletions base/sequence_token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<intptr_t>(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<intptr_t>(set_for_thread) != 0
: false;
}
Expand Down Expand Up @@ -121,8 +118,9 @@ SequenceToken SequenceToken::Create() {
SequenceToken SequenceToken::GetForCurrentThread() {
#if defined(STARBOARD)
if (IsSequenceSetForThread()) {
EnsureThreadLocalSequenceKeyInited();
int token = static_cast<int>(reinterpret_cast<intptr_t>(
SbThreadGetLocalValue(s_thread_local_sequence_key)));
pthread_getspecific(s_thread_local_sequence_key)));
return SequenceToken(token);
} else {
return SequenceToken();
Expand Down Expand Up @@ -151,8 +149,9 @@ TaskToken TaskToken::Create() {
TaskToken TaskToken::GetForCurrentThread() {
#if defined(STARBOARD)
if (IsTaskSetForThread()) {
EnsureThreadLocalTaskKeyInited();
int token = static_cast<int>(reinterpret_cast<intptr_t>(
SbThreadGetLocalValue(s_thread_local_task_key)));
pthread_getspecific(s_thread_local_task_key)));
return TaskToken(token);
} else {
return TaskToken();
Expand All @@ -171,7 +170,7 @@ ScopedSetSequenceTokenForCurrentThread::ScopedSetSequenceTokenForCurrentThread(
DCHECK(!sequence_reset_token.IsValid());
scoped_sequence_reset_value_ = reinterpret_cast<void*>(
static_cast<intptr_t>(sequence_reset_token.GetToken()));
SbThreadSetLocalValue(s_thread_local_sequence_key,
pthread_setspecific(s_thread_local_sequence_key,
reinterpret_cast<void*>(
static_cast<intptr_t>(sequence_token.GetToken())));

Expand All @@ -180,15 +179,15 @@ ScopedSetSequenceTokenForCurrentThread::ScopedSetSequenceTokenForCurrentThread(
DCHECK(!task_reset_token.IsValid());
scoped_task_reset_value_ = reinterpret_cast<void*>(
static_cast<intptr_t>(task_reset_token.GetToken()));
SbThreadSetLocalValue(s_thread_local_task_key,
pthread_setspecific(s_thread_local_task_key,
reinterpret_cast<void*>(static_cast<intptr_t>(
TaskToken::Create().GetToken())));

EnsureThreadLocalSequenceBoolKeyInited();
SbThreadSetLocalValue(s_thread_local_sequence_set_for_thread,
pthread_setspecific(s_thread_local_sequence_set_for_thread,
reinterpret_cast<void*>(static_cast<intptr_t>(true)));
EnsureThreadLocalTaskBoolKeyInited();
SbThreadSetLocalValue(s_thread_local_task_set_for_thread,
pthread_setspecific(s_thread_local_task_set_for_thread,
reinterpret_cast<void*>(static_cast<intptr_t>(true)));
}
#else
Expand All @@ -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::
Expand Down
13 changes: 7 additions & 6 deletions base/task/common/scoped_defer_task_posting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScopedDeferTaskPosting*>(
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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 218d444

Please sign in to comment.