Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Beyad <abeyad@google.com>
  • Loading branch information
abeyad committed Sep 11, 2024
1 parent cdb2b1d commit 892143d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 6 additions & 7 deletions source/common/common/posix/thread_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ void setThreadPriority(const int64_t tid, const int priority) {
// the thread priority on Apple platforms, and directly invoking `setpriority()` on iOS fails with
// permissions issues, as discovered through manual testing.
Class nsthread = objc_getClass("NSThread");
if (nsthread != nullptr) {
id (*getCurrentNSThread)(Class, SEL) = reinterpret_cast<id (*)(Class, SEL)>(objc_msgSend);
id current_thread = getCurrentNSThread(nsthread, sel_registerName("currentThread"));
void (*setNSThreadPriority)(id, SEL, double) =
reinterpret_cast<void (*)(id, SEL, double)>(objc_msgSend);
setNSThreadPriority(current_thread, sel_registerName("setThreadPriority:"), priority);
}
id (*getCurrentNSThread)(Class, SEL) = reinterpret_cast<id (*)(Class, SEL)>(objc_msgSend);
id current_thread = getCurrentNSThread(nsthread, sel_registerName("currentThread"));
void (*setNSThreadPriority)(id, SEL, double) =
reinterpret_cast<void (*)(id, SEL, double)>(objc_msgSend);
double ns_priority = static_cast<double>(priority) / 100.0;
setNSThreadPriority(current_thread, sel_registerName("setThreadPriority:"), ns_priority);
#else
#error "Enable and test pthread id retrieval code for you arch in pthread/thread_impl.cc"
#endif
Expand Down
12 changes: 11 additions & 1 deletion test/common/common/thread_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ TEST(PosixThreadTest, Joinable) {
TEST(PosixThreadTest, ThreadPriority) {
auto thread_factory = PosixThreadFactory::create();
Options options;
options.priority_ = 10;
options.priority_ = 15;
double thread_priority;
auto thread = thread_factory->createThread(
[&]() { thread_priority = thread_factory->currentThreadPriority(); }, options,
Expand All @@ -288,6 +288,16 @@ TEST(PosixThreadTest, ThreadPriority) {
EXPECT_EQ(thread_priority, options.priority_);
}

TEST(PosixThreadTest, OptionsNoPriority) {
auto thread_factory = PosixThreadFactory::create();
Options options;
auto thread = thread_factory->createThread([&]() {}, options, /* crash_on_failure= */ true);

EXPECT_TRUE(thread->joinable());
thread->join();
EXPECT_FALSE(thread->joinable());
}

class PosixThreadFactoryFailCreate : public PosixThreadFactory {
protected:
int createPthread(ThreadHandle*) override { return 1; }
Expand Down

0 comments on commit 892143d

Please sign in to comment.