diff --git a/source/common/common/posix/thread_impl.cc b/source/common/common/posix/thread_impl.cc index a070ad278c38..66ba6f2d5c78 100644 --- a/source/common/common/posix/thread_impl.cc +++ b/source/common/common/posix/thread_impl.cc @@ -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(objc_msgSend); - id current_thread = getCurrentNSThread(nsthread, sel_registerName("currentThread")); - void (*setNSThreadPriority)(id, SEL, double) = - reinterpret_cast(objc_msgSend); - setNSThreadPriority(current_thread, sel_registerName("setThreadPriority:"), priority); - } + id (*getCurrentNSThread)(Class, SEL) = reinterpret_cast(objc_msgSend); + id current_thread = getCurrentNSThread(nsthread, sel_registerName("currentThread")); + void (*setNSThreadPriority)(id, SEL, double) = + reinterpret_cast(objc_msgSend); + double ns_priority = static_cast(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 diff --git a/test/common/common/thread_test.cc b/test/common/common/thread_test.cc index 11cd78fe68f3..acfbe267c034 100644 --- a/test/common/common/thread_test.cc +++ b/test/common/common/thread_test.cc @@ -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, @@ -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; }