-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refer to the original PR: #2885 b/302335657 Test-On-Device: true Change-Id: I3b3bef9fbcbb671ff44bceaf23fd2a6dbb75afe8 Co-authored-by: Yavor Goulishev <[email protected]>
- Loading branch information
1 parent
f143e4c
commit f1879dd
Showing
15 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_PTHREAD_H_ | ||
#define STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_PTHREAD_H_ | ||
|
||
#include_next <pthread.h> | ||
|
||
#if __ANDROID_API__ < 26 | ||
int pthread_getname_np(pthread_t thread, char* name, size_t len); | ||
#endif // __ANDROID_API__ < 26 | ||
|
||
#endif // STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_PTHREAD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <pthread.h> | ||
|
||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <sys/prctl.h> | ||
#include <unistd.h> | ||
|
||
#if __ANDROID_API__ < 26 | ||
// The API doesn exist before API Level 26 and we currently target 24 | ||
// If this is the current thread we can obtain the name using `prctl`. | ||
int pthread_getname_np(pthread_t thread, char* name, size_t len) { | ||
// The PR_GET_NAME expects a buffer of size 16 bytes. | ||
if (pthread_equal(pthread_self(), thread) && len >= 16) { | ||
prctl(PR_GET_NAME, name, 0L, 0L, 0L); | ||
return 0; | ||
} | ||
return -1; | ||
} | ||
#endif // __ANDROID_API__ < 26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
starboard/nplb/posix_compliance/posix_thread_get_name_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/nplb/thread_helpers.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
void* GetThreadNameEntryPoint(void* context) { | ||
pthread_setname_np(pthread_self(), kThreadName); | ||
|
||
char name[4096] = {0}; | ||
pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name)); | ||
std::string* result = static_cast<std::string*>(context); | ||
*result = name; | ||
return NULL; | ||
} | ||
|
||
TEST(PosixThreadGetNameTest, SunnyDay) { | ||
std::string result; | ||
|
||
pthread_t thread; | ||
EXPECT_EQ(pthread_create(&thread, NULL, GetThreadNameEntryPoint, &result), 0); | ||
|
||
EXPECT_TRUE(thread != 0); | ||
EXPECT_EQ(pthread_join(thread, NULL), 0); | ||
EXPECT_EQ(kThreadName, result); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard |
60 changes: 60 additions & 0 deletions
60
starboard/nplb/posix_compliance/posix_thread_set_name_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/nplb/thread_helpers.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
struct Context { | ||
std::string got_name1; | ||
std::string name_to_set; | ||
std::string got_name2; | ||
}; | ||
|
||
// Gets the thread's name and sets it in the context. | ||
void* SetThreadNameEntryPoint(void* context) { | ||
char name[4096] = {0}; | ||
Context* real_context = static_cast<Context*>(context); | ||
|
||
pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name)); | ||
real_context->got_name1 = name; | ||
|
||
pthread_setname_np(pthread_self(), real_context->name_to_set.c_str()); | ||
|
||
pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name)); | ||
real_context->got_name2 = name; | ||
|
||
return NULL; | ||
} | ||
|
||
TEST(PosixThreadSetNameTest, SunnyDay) { | ||
Context context; | ||
context.name_to_set = kAltThreadName; | ||
pthread_t thread; | ||
EXPECT_EQ(pthread_create(&thread, NULL, SetThreadNameEntryPoint, &context), | ||
0); | ||
EXPECT_TRUE(thread != 0); | ||
EXPECT_EQ(pthread_join(thread, NULL), 0); | ||
EXPECT_NE(kAltThreadName, context.got_name1); | ||
EXPECT_EQ(kAltThreadName, context.got_name2); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters