#include "base/threading/platform_thread.h"
#include <errno.h>
#include <stddef.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <unistd.h>
#include "base/android/jni_android.h"
#include "base/base_jni_headers/ThreadUtils_jni.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/threading/platform_thread_internal_posix.h"
#include "base/threading/thread_id_name_manager.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace base {
namespace internal {
const ThreadPriorityToNiceValuePairForTest
kThreadPriorityToNiceValueMapForTest[5] = {
{ThreadPriorityForTest::kRealtimeAudio, -16},
{ThreadPriorityForTest::kDisplay, -4},
{ThreadPriorityForTest::kNormal, 0},
{ThreadPriorityForTest::kUtility, 1},
{ThreadPriorityForTest::kBackground, 10},
};
const ThreadTypeToNiceValuePair kThreadTypeToNiceValueMap[7] = {
{ThreadType::kBackground, 10}, {ThreadType::kUtility, 1},
{ThreadType::kResourceEfficient, 0}, {ThreadType::kDefault, 0},
{ThreadType::kCompositing, -4}, {ThreadType::kDisplayCritical, -4},
{ThreadType::kRealtimeAudio, -16},
};
bool CanSetThreadTypeToRealtimeAudio() {
return true;
}
bool SetCurrentThreadTypeForPlatform(ThreadType thread_type,
MessagePumpType pump_type_hint) {
if (thread_type == ThreadType::kRealtimeAudio) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
return true;
}
if (thread_type == ThreadType::kCompositing &&
pump_type_hint == MessagePumpType::UI &&
GetCurrentThreadNiceValue() <=
ThreadTypeToNiceValue(ThreadType::kCompositing)) {
return true;
}
return false;
}
absl::optional<ThreadPriorityForTest>
GetCurrentThreadPriorityForPlatformForTest() {
JNIEnv* env = base::android::AttachCurrentThread();
if (Java_ThreadUtils_isThreadPriorityAudio(
env, PlatformThread::CurrentId())) {
return absl::make_optional(ThreadPriorityForTest::kRealtimeAudio);
}
return absl::nullopt;
}
}
void PlatformThread::SetName(const std::string& name) {
ThreadIdNameManager::GetInstance()->SetName(name);
if (PlatformThread::CurrentId() == getpid())
return;
int err = prctl(PR_SET_NAME, name.c_str());
if (err < 0 && errno != EPERM)
DPLOG(ERROR) << "prctl(PR_SET_NAME)";
}
void InitThreading() {
}
void TerminateOnThread() {
base::android::DetachFromVM();
}
size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
#if !defined(ADDRESS_SANITIZER)
return 0;
#else
return 2 * (1 << 20);
#endif
}
}