#include "base/threading/platform_thread_internal_posix.h"
#include <errno.h>
#include <sys/resource.h>
#include <ostream>
#include "base/containers/adapters.h"
#include "base/logging.h"
#include "base/notimplemented.h"
namespace base::internal {
ThreadType NiceValueToThreadTypeForTest(int nice_value) {
for (const auto& pair : kThreadTypeToNiceValueMapForTest) {
if (pair.nice_value >= nice_value) {
return pair.priority;
}
}
return ThreadType::kBackground;
}
int GetCurrentThreadNiceValue() {
return GetThreadNiceValue(PlatformThreadId{0});
}
int GetThreadNiceValue(PlatformThreadId id) {
errno = 0;
int nice_value = getpriority(PRIO_PROCESS, static_cast<id_t>(id.raw()));
if (errno != 0) {
DVPLOG(1) << "Failed to get nice value of thread ("
<< PlatformThread::CurrentId() << ")";
return 0;
}
return nice_value;
}
bool SetThreadNiceFromType(PlatformThreadId thread_id, ThreadType thread_type) {
pid_t syscall_tid =
thread_id == PlatformThread::CurrentId() ? 0 : thread_id.raw();
const int nice_setting = internal::ThreadTypeToNiceValue(thread_type);
if (setpriority(PRIO_PROCESS, static_cast<id_t>(syscall_tid), nice_setting)) {
DVPLOG(1) << "Failed to set nice value of thread " << thread_id << " to "
<< nice_setting;
return false;
}
return true;
}
}