From ad560506175a89bde42801e51d4a92d3ee7b639e Mon Sep 17 00:00:00 2001 From: cobalt-github-releaser-bot <95661244+cobalt-github-releaser-bot@users.noreply.github.com> Date: Fri, 3 May 2024 12:51:15 -0700 Subject: [PATCH] Cherry pick PR #3153: Deprecate SbThreadGetName/SbThreadSetName (#3156) Refer to the original PR: https://github.com/youtube/cobalt/pull/3153 b/302335657 Change-Id: I483f9c77addde8bed1e45862430abd22acc28b6f Co-authored-by: Yavor Goulishev --- base/threading/platform_thread_starboard.cc | 3 ++- cobalt/base/init_cobalt.cc | 4 +++- cobalt/test/run_all_unittests.cc | 3 ++- starboard/CHANGELOG.md | 4 ++++ starboard/android/shared/jni_env_ext.cc | 2 +- starboard/android/shared/thread_create.cc | 2 +- starboard/android/shared/thread_get_name.cc | 4 ++++ starboard/common/experimental/concurrency_debug.h | 3 ++- starboard/common/log.cc | 4 +++- starboard/elf_loader/exported_symbols.cc | 4 ++-- starboard/nplb/thread_get_name_test.cc | 4 ++++ starboard/nplb/thread_set_name_test.cc | 4 ++++ starboard/shared/linux/thread_get_name.cc | 4 ++++ starboard/shared/linux/thread_set_name.cc | 4 ++++ starboard/shared/pthread/thread_create.cc | 2 +- starboard/shared/stub/thread_get_name.cc | 4 ++++ starboard/shared/stub/thread_set_name.cc | 4 ++++ starboard/shared/win32/posix_emu/pthread.cc | 2 +- starboard/shared/win32/thread_create.cc | 4 +++- starboard/shared/win32/thread_get_name.cc | 4 ++++ starboard/shared/win32/thread_set_name.cc | 4 ++++ starboard/thread.h | 2 +- v8/src/base/platform/platform-starboard.cc | 2 +- 23 files changed, 63 insertions(+), 14 deletions(-) diff --git a/base/threading/platform_thread_starboard.cc b/base/threading/platform_thread_starboard.cc index 54a4a96e7a59..95a30d59d349 100644 --- a/base/threading/platform_thread_starboard.cc +++ b/base/threading/platform_thread_starboard.cc @@ -14,6 +14,7 @@ #include "base/threading/platform_thread.h" +#include #include #include @@ -127,7 +128,7 @@ void PlatformThread::Sleep(TimeDelta duration) { // static void PlatformThread::SetName(const std::string& name) { ThreadIdNameManager::GetInstance()->SetName(name); - SbThreadSetName(name.c_str()); + pthread_setname_np(pthread_self(), name.c_str()); } // static diff --git a/cobalt/base/init_cobalt.cc b/cobalt/base/init_cobalt.cc index 3ab6a912f397..382f9426cd80 100644 --- a/cobalt/base/init_cobalt.cc +++ b/cobalt/base/init_cobalt.cc @@ -14,6 +14,8 @@ #include "cobalt/base/init_cobalt.h" +#include + #include #include "base/at_exit.h" @@ -55,7 +57,7 @@ void InitCobalt(int argc, char* argv[], const char* link) { // Copy the Starboard thread name to the PlatformThread name. char thread_name[128] = {'\0'}; - SbThreadGetName(thread_name, 127); + pthread_getname_np(pthread_self(), thread_name, 127); base::PlatformThread::SetName(thread_name); } diff --git a/cobalt/test/run_all_unittests.cc b/cobalt/test/run_all_unittests.cc index 18d34d40c356..b8324b021bb2 100644 --- a/cobalt/test/run_all_unittests.cc +++ b/cobalt/test/run_all_unittests.cc @@ -33,7 +33,8 @@ int InitAndRunAllTests(int argc, char** argv) { // Copy the Starboard thread name to the PlatformThread name. char thread_name[128] = {'\0'}; - SbThreadGetName(thread_name, 127); + pthread_getname_np(pthread_self(), thread_name, 127); + base::PlatformThread::SetName(thread_name); return test_suite.Run(); } diff --git a/starboard/CHANGELOG.md b/starboard/CHANGELOG.md index 3e4dbddcab14..3cc260f67f09 100644 --- a/starboard/CHANGELOG.md +++ b/starboard/CHANGELOG.md @@ -9,6 +9,10 @@ since the version previous to it. ## Version 16 +### Deprecated the `SbThreadSetName` and `SbThreadGetName` APIs. +Replaced the `SbThreadSetName`/`SbThreadGetName` with the POSIX +`pthread_setname_np`/`pthread_getname_np`. + ### Deprecated the `SbThreadLocalKey` APIs. Replaced the `SbThreadLocalKey` with the POSIX `pthread_key_t`. diff --git a/starboard/android/shared/jni_env_ext.cc b/starboard/android/shared/jni_env_ext.cc index 358cddb7cba5..9d6361d5d356 100644 --- a/starboard/android/shared/jni_env_ext.cc +++ b/starboard/android/shared/jni_env_ext.cc @@ -76,7 +76,7 @@ JniEnvExt* JniEnvExt::Get() { if (JNI_OK != g_vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6)) { // Tell the JVM our thread name so it doesn't change it. char thread_name[16]; - SbThreadGetName(thread_name, sizeof(thread_name)); + pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)); 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. diff --git a/starboard/android/shared/thread_create.cc b/starboard/android/shared/thread_create.cc index 5f69a16c09b4..d760a611d367 100644 --- a/starboard/android/shared/thread_create.cc +++ b/starboard/android/shared/thread_create.cc @@ -63,7 +63,7 @@ void* ThreadFunc(void* context) { void* real_context = thread_params->context; SbThreadAffinity affinity = thread_params->affinity; if (thread_params->name[0] != '\0') { - SbThreadSetName(thread_params->name); + pthread_setname_np(pthread_self(), thread_params->name); } starboard::shared::pthread::ThreadSetPriority(thread_params->priority); diff --git a/starboard/android/shared/thread_get_name.cc b/starboard/android/shared/thread_get_name.cc index 5a6033c48395..9385ad7f6f83 100644 --- a/starboard/android/shared/thread_get_name.cc +++ b/starboard/android/shared/thread_get_name.cc @@ -12,9 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include "starboard/thread.h" void SbThreadGetName(char* buffer, int buffer_size) { prctl(PR_GET_NAME, buffer, 0L, 0L, 0L); } + +#endif diff --git a/starboard/common/experimental/concurrency_debug.h b/starboard/common/experimental/concurrency_debug.h index 78414f620ce8..f07e2b2708fd 100644 --- a/starboard/common/experimental/concurrency_debug.h +++ b/starboard/common/experimental/concurrency_debug.h @@ -65,7 +65,8 @@ class ThreadTracker { } char name[kSbMaxThreadNameLength + 1]; - SbThreadGetName(name, kSbMaxThreadNameLength); + + pthread_getname_np(pthread_self(), name, kSbMaxThreadNameLength); int64_t thread_now = starboard::CurrentMonotonicThreadTime(); SB_LOG(INFO) << "Thread " << name << " uses " diff --git a/starboard/common/log.cc b/starboard/common/log.cc index 5ff420998609..68093d3146fa 100644 --- a/starboard/common/log.cc +++ b/starboard/common/log.cc @@ -14,6 +14,8 @@ #include "starboard/common/log.h" +#include + #include #include #include @@ -166,7 +168,7 @@ void LogMessage::Init(const char* file, int line) { filename.erase(0, last_slash_pos + 1); } char name[128] = {0}; - SbThreadGetName(name, SB_ARRAY_SIZE_INT(name)); + pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name)); stream_ << '['; stream_ << name << '/' << SbThreadGetId() << ':'; EzTimeValue time_value; diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index 4deab4f0e011..e065f069a2bb 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -392,9 +392,9 @@ ExportedSymbols::ExportedSymbols() { #if SB_API_VERSION < 16 REGISTER_SYMBOL(SbThreadGetLocalValue); + REGISTER_SYMBOL(SbThreadGetName); #endif // SB_API_VERSION < 16 - REGISTER_SYMBOL(SbThreadGetName); REGISTER_SYMBOL(SbThreadIsEqual); REGISTER_SYMBOL(SbThreadJoin); REGISTER_SYMBOL(SbThreadSamplerCreate); @@ -407,8 +407,8 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbThreadSetLocalValue); #endif // SB_API_VERSION < 16 - REGISTER_SYMBOL(SbThreadSetName); #if SB_API_VERSION < 16 + REGISTER_SYMBOL(SbThreadSetName); REGISTER_SYMBOL(SbThreadSleep); REGISTER_SYMBOL(SbThreadYield); REGISTER_SYMBOL(SbTimeGetMonotonicNow); diff --git a/starboard/nplb/thread_get_name_test.cc b/starboard/nplb/thread_get_name_test.cc index 3b8d940f5045..555bc4ff7395 100644 --- a/starboard/nplb/thread_get_name_test.cc +++ b/starboard/nplb/thread_get_name_test.cc @@ -14,6 +14,8 @@ // Thread joining is mostly tested in the other tests. +#if SB_API_VERSION < 16 + #include "starboard/nplb/thread_helpers.h" #include "starboard/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -44,3 +46,5 @@ TEST(SbThreadGetNameTest, SunnyDay) { } // namespace } // namespace nplb } // namespace starboard + +#endif diff --git a/starboard/nplb/thread_set_name_test.cc b/starboard/nplb/thread_set_name_test.cc index 3d79a129c153..8419dc51e248 100644 --- a/starboard/nplb/thread_set_name_test.cc +++ b/starboard/nplb/thread_set_name_test.cc @@ -14,6 +14,8 @@ // Thread joining is mostly tested in the other tests. +#if SB_API_VERSION < 16 + #include "starboard/nplb/thread_helpers.h" #include "starboard/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -59,3 +61,5 @@ TEST(SbThreadSetNameTest, SunnyDay) { } // namespace } // namespace nplb } // namespace starboard + +#endif diff --git a/starboard/shared/linux/thread_get_name.cc b/starboard/shared/linux/thread_get_name.cc index 32c0693a86ae..e2dfe80c4510 100644 --- a/starboard/shared/linux/thread_get_name.cc +++ b/starboard/shared/linux/thread_get_name.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 @@ -19,3 +21,5 @@ void SbThreadGetName(char* buffer, int buffer_size) { pthread_getname_np(pthread_self(), buffer, static_cast(buffer_size)); } + +#endif diff --git a/starboard/shared/linux/thread_set_name.cc b/starboard/shared/linux/thread_set_name.cc index ab975fd439c8..fbbe5a689743 100644 --- a/starboard/shared/linux/thread_set_name.cc +++ b/starboard/shared/linux/thread_set_name.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,5 @@ void SbThreadSetName(const char* name) { SB_DLOG(ERROR) << "Failed to set thread name to " << name; } } + +#endif diff --git a/starboard/shared/pthread/thread_create.cc b/starboard/shared/pthread/thread_create.cc index e913d8d96df7..1b9f7f14f696 100644 --- a/starboard/shared/pthread/thread_create.cc +++ b/starboard/shared/pthread/thread_create.cc @@ -47,7 +47,7 @@ void* ThreadFunc(void* context) { void* real_context = thread_params->context; SbThreadAffinity affinity = thread_params->affinity; if (thread_params->name[0] != '\0') { - SbThreadSetName(thread_params->name); + pthread_setname_np(pthread_self(), thread_params->name); } starboard::shared::pthread::ThreadSetPriority(thread_params->priority); diff --git a/starboard/shared/stub/thread_get_name.cc b/starboard/shared/stub/thread_get_name.cc index 3ea652cf9e3b..6f9719219726 100644 --- a/starboard/shared/stub/thread_get_name.cc +++ b/starboard/shared/stub/thread_get_name.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 SbThreadGetName(char* buffer, int buffer_size) {} + +#endif diff --git a/starboard/shared/stub/thread_set_name.cc b/starboard/shared/stub/thread_set_name.cc index b5b4e274a03d..dd5592db568b 100644 --- a/starboard/shared/stub/thread_set_name.cc +++ b/starboard/shared/stub/thread_set_name.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 SbThreadSetName(const char* name) {} + +#endif diff --git a/starboard/shared/win32/posix_emu/pthread.cc b/starboard/shared/win32/posix_emu/pthread.cc index 97e1e460134a..84cf21f1cb35 100644 --- a/starboard/shared/win32/posix_emu/pthread.cc +++ b/starboard/shared/win32/posix_emu/pthread.cc @@ -195,7 +195,7 @@ static unsigned ThreadTrampoline(void* thread_create_info_context) { ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); ThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); - SbThreadSetName(info->name_.c_str()); + pthread_setname_np(pthread_self(), info->name_.c_str()); void* result = info->entry_point_(info->user_context_); diff --git a/starboard/shared/win32/thread_create.cc b/starboard/shared/win32/thread_create.cc index b7ac621c5d9d..31a102c38f77 100644 --- a/starboard/shared/win32/thread_create.cc +++ b/starboard/shared/win32/thread_create.cc @@ -15,6 +15,8 @@ #include "starboard/thread.h" #include +#include + #include #include "starboard/common/log.h" @@ -113,7 +115,7 @@ unsigned ThreadTrampoline(void* thread_create_info_context) { ThreadSubsystemSingleton* singleton = GetThreadSubsystemSingleton(); ThreadSetLocalValue(singleton->thread_private_key_, &info->thread_private_); - SbThreadSetName(info->name_.c_str()); + pthread_setname_np(pthread_self(), info->name_.c_str()); void* result = info->entry_point_(info->user_context_); diff --git a/starboard/shared/win32/thread_get_name.cc b/starboard/shared/win32/thread_get_name.cc index e9b299d53786..80d784a14c72 100644 --- a/starboard/shared/win32/thread_get_name.cc +++ b/starboard/shared/win32/thread_get_name.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 SbThreadGetName(char* buffer, int buffer_size) { SbThreadPrivate* thread_private = GetCurrentSbThreadPrivate(); starboard::strlcpy(buffer, thread_private->name_.c_str(), buffer_size); } + +#endif diff --git a/starboard/shared/win32/thread_set_name.cc b/starboard/shared/win32/thread_set_name.cc index c112a8807ad2..6eec1a147fa6 100644 --- a/starboard/shared/win32/thread_set_name.cc +++ b/starboard/shared/win32/thread_set_name.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 @@ -64,3 +66,5 @@ void SbThreadSetName(const char* name) { thread_private->name_ = name; SetThreadName(static_cast(-1), name); } + +#endif diff --git a/starboard/thread.h b/starboard/thread.h index 4b0349cc2394..92d2650ac76c 100644 --- a/starboard/thread.h +++ b/starboard/thread.h @@ -220,6 +220,7 @@ SB_EXPORT SbThreadId SbThreadGetId(); // |thread2|: The second thread to compare. SB_EXPORT bool SbThreadIsEqual(SbThread thread1, SbThread thread2); +#if SB_API_VERSION < 16 // Returns the debug name of the currently executing thread. SB_EXPORT void SbThreadGetName(char* buffer, int buffer_size); @@ -229,7 +230,6 @@ 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|. // diff --git a/v8/src/base/platform/platform-starboard.cc b/v8/src/base/platform/platform-starboard.cc index c9eebae2068b..0d009baf3aa4 100644 --- a/v8/src/base/platform/platform-starboard.cc +++ b/v8/src/base/platform/platform-starboard.cc @@ -378,7 +378,7 @@ Thread::Thread(const Options& options) Thread::~Thread() { delete data_; } -static void SetThreadName(const char* name) { SbThreadSetName(name); } +static void SetThreadName(const char* name) { pthread_setname_np(pthread_self(), name); } static void* ThreadEntry(void* arg) { Thread* thread = reinterpret_cast(arg);