Skip to content

Commit

Permalink
Add SbThreadSetPriority/SbThreadGetPriority APIs (#3299)
Browse files Browse the repository at this point in the history
b/302335657

Change-Id: I2d0331f52523f8100f3e456ea0e1d268ae1c3125
  • Loading branch information
y4vor authored May 22, 2024
1 parent cddc2d7 commit db384eb
Show file tree
Hide file tree
Showing 16 changed files with 373 additions and 16 deletions.
4 changes: 4 additions & 0 deletions base/threading/platform_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ static_assert(std::is_integral_v<PlatformThreadId>, "Always an integer value.");
class PlatformThreadHandle {
public:
#if defined(STARBOARD)
#if SB_API_VERSION < 16
typedef SbThread Handle;
#else
typedef pthread_t Handle;
#endif
#elif BUILDFLAG(IS_WIN)
typedef void* Handle;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand Down
5 changes: 5 additions & 0 deletions base/threading/platform_thread_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "build/build_config.h"

#if defined(STARBOARD)
#include <pthread.h>
#include "starboard/thread.h"
#elif BUILDFLAG(IS_WIN)
#include "base/win/windows_types.h"
Expand All @@ -36,7 +37,11 @@ namespace base {
class PlatformThreadRef {
public:
#if defined(STARBOARD)
#if SB_API_VERSION < 16
typedef SbThread RefType;
#else
using RefType = pthread_t;
#endif
#elif BUILDFLAG(IS_WIN)
using RefType = DWORD;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Expand Down
76 changes: 72 additions & 4 deletions base/threading/platform_thread_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ namespace {
struct ThreadParams {
PlatformThread::Delegate* delegate;
bool joinable;
SbThreadPriority thread_priority;
std::string thread_name;
};

void* ThreadFunc(void* params) {
ThreadParams* thread_params = static_cast<ThreadParams*>(params);
PlatformThread::Delegate* delegate = thread_params->delegate;

#if SB_API_VERSION >= 16
if (kSbHasThreadPrioritySupport) {
SbThreadSetPriority(thread_params->thread_priority);
}
#endif // SB_API_VERSION >= 16
pthread_setname_np(pthread_self(), thread_params->thread_name.c_str());

absl::optional<ScopedDisallowSingleton> disallow_singleton;
if (!thread_params->joinable) {
disallow_singleton.emplace();
Expand All @@ -56,9 +66,49 @@ void* ThreadFunc(void* params) {
return NULL;
}

#if SB_API_VERSION >= 16
bool CreateThread(size_t stack_size,
SbThreadPriority priority,
bool joinable,
const char* name,
PlatformThread::Delegate* delegate,
PlatformThreadHandle* thread_handle) {
ThreadParams* params = new ThreadParams;
params->delegate = delegate;
params->joinable = joinable;
params->thread_priority = priority;
if (name != nullptr) {
params->thread_name = name;
}

pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
return false;
}

pthread_attr_setstacksize(&attr, stack_size);

if (!joinable) {
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
}

pthread_t thread = 0;
pthread_create(&thread, &attr, ThreadFunc, params);
pthread_attr_destroy(&attr);

if (thread != 0) {
if (thread_handle) {
*thread_handle = PlatformThreadHandle(thread);
}

return true;
}

return false;
}
#else
bool CreateThread(size_t stack_size,
SbThreadPriority priority,
SbThreadAffinity affinity,
bool joinable,
const char* name,
PlatformThread::Delegate* delegate,
Expand All @@ -67,7 +117,7 @@ bool CreateThread(size_t stack_size,
params->delegate = delegate;
params->joinable = joinable;

SbThread thread = SbThreadCreate(stack_size, priority, affinity, joinable,
SbThread thread = SbThreadCreate(stack_size, priority, kSbThreadNoAffinity, joinable,
name, ThreadFunc, params);
if (SbThreadIsValid(thread)) {
if (thread_handle) {
Expand All @@ -79,6 +129,7 @@ bool CreateThread(size_t stack_size,

return false;
}
#endif // SB_API_VERSION >= 16

inline SbThreadPriority toSbPriority(ThreadType priority) {
switch (priority) {
Expand Down Expand Up @@ -108,12 +159,21 @@ PlatformThreadId PlatformThread::CurrentId() {

// static
PlatformThreadRef PlatformThread::CurrentRef() {
#if SB_API_VERSION < 16
return PlatformThreadRef(SbThreadGetCurrent());
#else
return PlatformThreadRef(pthread_self());
#endif // SB_API_VERSION < 16

}

// static
PlatformThreadHandle PlatformThread::CurrentHandle() {
#if SB_API_VERSION < 16
return PlatformThreadHandle(SbThreadGetCurrent());
#else
return PlatformThreadHandle(pthread_self());
#endif // SB_API_VERSION < 16
}

// static
Expand Down Expand Up @@ -145,7 +205,7 @@ bool PlatformThread::CreateWithType(size_t stack_size,
PlatformThreadHandle* thread_handle,
ThreadType priority,
MessagePumpType /* pump_type_hint */) {
return CreateThread(stack_size, toSbPriority(priority), kSbThreadNoAffinity,
return CreateThread(stack_size, toSbPriority(priority),
true /* joinable thread */, NULL, delegate,
thread_handle);
}
Expand All @@ -155,7 +215,7 @@ bool PlatformThread::CreateNonJoinableWithType(size_t stack_size,
Delegate* delegate,
ThreadType priority,
MessagePumpType /* pump_type_hint */) {
return CreateThread(stack_size, toSbPriority(priority), kSbThreadNoAffinity,
return CreateThread(stack_size, toSbPriority(priority),
false /* joinable thread */, NULL, delegate, NULL);
}

Expand All @@ -165,11 +225,19 @@ void PlatformThread::Join(PlatformThreadHandle thread_handle) {
// the thread referred to by |thread_handle| may still be running long-lived /
// blocking tasks.
internal::AssertBlockingAllowed();
#if SB_API_VERSION < 16
SbThreadJoin(thread_handle.platform_handle(), NULL);
#else
pthread_join(thread_handle.platform_handle(), NULL);
#endif // SB_API_VERSION < 16
}

void PlatformThread::Detach(PlatformThreadHandle thread_handle) {
#if SB_API_VERSION < 16
SbThreadDetach(thread_handle.platform_handle());
#else
pthread_detach(thread_handle.platform_handle());
#endif // SB_API_VERSION < 16
}

void internal::SetCurrentThreadTypeImpl(ThreadType /* thread_type */, MessagePumpType /*pump_type_hint*/) {
Expand Down
41 changes: 41 additions & 0 deletions starboard/android/shared/thread_create_priority.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,32 @@
#include <sys/resource.h>

#include "starboard/common/log.h"
#include "starboard/thread.h"

namespace {

SbThreadPriority NiceToSbPriority(int nice) {
if (nice == 19) {
return kSbThreadPriorityLowest;
}
if (nice > 0 && nice < 19) {
return kSbThreadPriorityLow;
}
if (nice == 0) {
return kSbThreadPriorityNormal;
}
if (nice >= -8 && nice < 0) {
return kSbThreadPriorityHigh;
}
if (nice > -19 && nice < -8) {
return kSbThreadPriorityHighest;
}
if (nice == -19) {
return kSbThreadPriorityRealTime;
}
}

} // namespace
namespace starboard {
namespace shared {
namespace pthread {
Expand Down Expand Up @@ -65,3 +90,19 @@ void ThreadSetPriority(SbThreadPriority priority) {
} // namespace pthread
} // namespace shared
} // namespace starboard

bool SbThreadSetPriority(SbThreadPriority priority) {
starboard::shared::pthread::ThreadSetPriority(priority);
return true;
}

bool SbThreadGetPriority(SbThreadPriority* priority) {
errno = 0;
int ret = getpriority(PRIO_PROCESS, 0);
if (ret == -1 && errno != 0) {
return false;
}
*priority = NiceToSbPriority(ret);

return true;
}
11 changes: 11 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(SbThreadGetCurrent);
REGISTER_SYMBOL(SbThreadGetId);

#if SB_API_VERSION >= 16
REGISTER_SYMBOL(SbThreadGetPriority);
#endif // SB_API_VERSION >= 16

#if SB_API_VERSION < 16
REGISTER_SYMBOL(SbThreadGetLocalValue);
REGISTER_SYMBOL(SbThreadGetName);
Expand All @@ -412,6 +416,13 @@ ExportedSymbols::ExportedSymbols() {

#if SB_API_VERSION < 16
REGISTER_SYMBOL(SbThreadSetName);
#endif // SB_API_VERSION < 16

#if SB_API_VERSION >= 16
REGISTER_SYMBOL(SbThreadSetPriority);
#endif // SB_API_VERSION >= 16

#if SB_API_VERSION < 16
REGISTER_SYMBOL(SbThreadSleep);
REGISTER_SYMBOL(SbThreadYield);
REGISTER_SYMBOL(SbTimeGetMonotonicNow);
Expand Down
1 change: 1 addition & 0 deletions starboard/linux/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ static_library("starboard_platform_sources") {
"//starboard/shared/pthread/thread_get_local_value.cc",
"//starboard/shared/pthread/thread_is_equal.cc",
"//starboard/shared/pthread/thread_join.cc",
"//starboard/shared/pthread/thread_priority.cc",
"//starboard/shared/pthread/thread_sampler_create.cc",
"//starboard/shared/pthread/thread_sampler_destroy.cc",
"//starboard/shared/pthread/thread_sampler_freeze.cc",
Expand Down
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ target(gtest_target_type, "nplb") {
"thread_is_equal_test.cc",
"thread_join_test.cc",
"thread_local_value_test.cc",
"thread_priority_test.cc",
"thread_sampler_test.cc",
"thread_set_name_test.cc",
"thread_sleep_test.cc",
Expand Down
47 changes: 47 additions & 0 deletions starboard/nplb/thread_priority_test.cc
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.

#if SB_API_VERSION >= 16
#include "starboard/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

TEST(SbThreadPriorityTest, SunnyDay) {
SbThreadPriority priority = kSbThreadPriorityNormal;

if (kSbHasThreadPrioritySupport) {
// The test only lower the priority as raising the priority
// requires permissions.
EXPECT_TRUE(SbThreadSetPriority(kSbThreadPriorityNormal));
EXPECT_TRUE(SbThreadGetPriority(&priority));
EXPECT_EQ(priority, kSbThreadPriorityNormal);

EXPECT_TRUE(SbThreadSetPriority(kSbThreadPriorityLow));
EXPECT_TRUE(SbThreadGetPriority(&priority));
EXPECT_EQ(priority, kSbThreadPriorityLow);

EXPECT_TRUE(SbThreadSetPriority(kSbThreadPriorityLowest));
EXPECT_TRUE(SbThreadGetPriority(&priority));
EXPECT_EQ(priority, kSbThreadPriorityLowest);
}
}

} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
41 changes: 41 additions & 0 deletions starboard/raspi/shared/thread_create_priority.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,44 @@ void ThreadSetPriority(SbThreadPriority priority) {
} // namespace pthread
} // namespace shared
} // namespace starboard

bool SbThreadSetPriority(SbThreadPriority priority) {
::starboard::shared::pthread::ThreadSetPriority(priority);
return true;
}

bool SbThreadGetPriority(SbThreadPriority* priority) {
int scheduler = sched_getscheduler(0);
if (scheduler == -1) {
return false;
}
switch (scheduler) {
case SCHED_IDLE:
*priority = kSbThreadPriorityLowest;
return true;
case SCHED_OTHER:
*priority = kSbThreadPriorityNormal;
return true;
}
if (scheduler == SCHED_RR) {
struct sched_param param;
int res = sched_getparam(0, &param);
if (res == -1) {
return false;
}
switch (param.sched_priority) {
case 0:
*priority = kSbThreadPriorityHigh;
return true;
case 1:
*priority = kSbThreadPriorityHighest;
return true;
case 2:
*priority = kSbThreadPriorityRealTime;
return true;
default:
return false;
}
}
return false;
}
Loading

0 comments on commit db384eb

Please sign in to comment.