This source file includes following definitions.
- ThreadNiceValue
- SetThreadPriority
- SetName
- InitThreading
- InitOnThread
- TerminateOnThread
- GetDefaultThreadStackSize
- RegisterThreadUtils
#include "base/threading/platform_thread.h"
#include <errno.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include "base/android/jni_android.h"
#include "base/android/thread_utils.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/tracked_objects.h"
#include "jni/ThreadUtils_jni.h"
namespace base {
namespace {
int ThreadNiceValue(ThreadPriority priority) {
static const int threadPriorityAudio = -16;
static const int threadPriorityBackground = 10;
static const int threadPriorityDefault = 0;
static const int threadPriorityDisplay = -6;
switch (priority) {
case kThreadPriority_RealtimeAudio:
return threadPriorityAudio;
case kThreadPriority_Background:
return threadPriorityBackground;
case kThreadPriority_Normal:
return threadPriorityDefault;
case kThreadPriority_Display:
return threadPriorityDisplay;
default:
NOTREACHED() << "Unknown priority.";
return 0;
}
}
}
void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
ThreadPriority priority) {
if (priority == kThreadPriority_RealtimeAudio) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
return;
}
DCHECK_NE(handle.id_, kInvalidThreadId);
int kNiceSetting = ThreadNiceValue(priority);
if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting))
LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting;
}
void PlatformThread::SetName(const char* name) {
ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
tracked_objects::ThreadData::InitializeThreadContext(name);
if (PlatformThread::CurrentId() == getpid())
return;
int err = prctl(PR_SET_NAME, name);
if (err < 0 && errno != EPERM)
DPLOG(ERROR) << "prctl(PR_SET_NAME)";
}
void InitThreading() {
}
void InitOnThread() {
PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
kThreadPriority_Normal);
}
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
}
bool RegisterThreadUtils(JNIEnv* env) {
return RegisterNativesImpl(env);
}
}