Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate SbThreadLocalKey #3096

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions base/observer_list_threadsafe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ 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));
int res = pthread_key_create(&s_thread_local_key , NULL);
DCHECK(res == 0);
}

}

void ObserverListThreadSafeBase::EnsureThreadLocalKeyInited() {
pthread_once(&s_once_flag, InitThreadLocalKey);
DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
}

const SbThreadLocalKey ObserverListThreadSafeBase::GetThreadLocalKey() {
const pthread_key_t ObserverListThreadSafeBase::GetThreadLocalKey() {
EnsureThreadLocalKeyInited();
y4vor marked this conversation as resolved.
Show resolved Hide resolved
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
26 changes: 13 additions & 13 deletions base/run_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,39 @@ 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));
int res = pthread_key_create(&s_thread_local_delegate_key , NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalDelegateKeyInited() {
pthread_once(&s_once_delegate_flag, InitThreadLocalDelegateKey);
DCHECK(SbThreadIsValidLocalKey(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));
int res = pthread_key_create(&s_thread_local_timeout_key, NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalTimeoutKeyInited() {
pthread_once(&s_once_timeout_flag, InitThreadLocalTimeoutKey);
DCHECK(SbThreadIsValidLocalKey(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 +103,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 +138,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 +392,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
15 changes: 7 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,21 @@ 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));
int res = pthread_key_create(&s_thread_local_key , NULL);
DCHECK(res == 0);
};

pthread_once(&s_once_flag, InitThreadLocalKey);
DCHECK(SbThreadIsValidLocalKey(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
60 changes: 29 additions & 31 deletions base/sequence_token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,69 +25,62 @@ 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));
int res = pthread_key_create(&s_thread_local_sequence_key, NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalSequenceKeyInited() {
pthread_once(&s_once_sequence_flag, InitThreadLocalSequenceKey);
DCHECK(SbThreadIsValidLocalKey(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));
int res = pthread_key_create(&s_thread_local_sequence_set_for_thread, NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalSequenceBoolKeyInited() {
pthread_once(&s_once_set_sequence_flag, InitThreadLocalSequenceBoolKey);
DCHECK(SbThreadIsValidLocalKey(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));
int res = pthread_key_create(&s_thread_local_task_key , NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalTaskKeyInited() {
pthread_once(&s_once_task_flag, InitThreadLocalTaskKey);
DCHECK(SbThreadIsValidLocalKey(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));
int res = pthread_key_create(&s_thread_local_task_set_for_thread, NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalTaskBoolKeyInited() {
pthread_once(&s_once_set_task_flag, InitThreadLocalTaskBoolKey);
DCHECK(SbThreadIsValidLocalKey(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 +114,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 +145,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 +166,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 +175,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 +204,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
12 changes: 6 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,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));
int res = pthread_key_create(&s_thread_local_key , NULL);
DCHECK(res == 0);
}

void EnsureThreadLocalKeyInited() {
pthread_once(&s_once_flag, InitThreadLocalKey);
DCHECK(SbThreadIsValidLocalKey(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 +83,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
Loading