Refine set_sched_policy behavior
If a thread priority is greater than or equal to ANDROID_PRIORITY_BACKGROUND, and the new priority is less than ANDROID_PRIORITY_BACKGROUND, set the sched policy to its parent process. Bug: 139521784 Test: functionality verified Change-Id: Ie6fe33e08e4fda0a119b9869e8dd1733c164d79e
This commit is contained in:
parent
cb1a8e7fdd
commit
f7f4442176
1 changed files with 21 additions and 7 deletions
|
@ -18,8 +18,8 @@
|
|||
#define LOG_TAG "libutils.threads"
|
||||
|
||||
#include <assert.h>
|
||||
#include <utils/Thread.h>
|
||||
#include <utils/AndroidThreads.h>
|
||||
#include <utils/Thread.h>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
# include <sys/resource.h>
|
||||
|
@ -36,7 +36,10 @@
|
|||
|
||||
#include <utils/Log.h>
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include <processgroup/processgroup.h>
|
||||
#include <processgroup/sched_policy.h>
|
||||
#endif
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
# define __android_unused
|
||||
|
@ -64,6 +67,7 @@ using namespace android;
|
|||
|
||||
typedef void* (*android_pthread_entry)(void*);
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
struct thread_data_t {
|
||||
thread_func_t entryFunction;
|
||||
void* userData;
|
||||
|
@ -79,10 +83,11 @@ struct thread_data_t {
|
|||
char * name = t->threadName;
|
||||
delete t;
|
||||
setpriority(PRIO_PROCESS, 0, prio);
|
||||
|
||||
// A new thread will be in its parent's sched group by default,
|
||||
// so we just need to handle the background case.
|
||||
if (prio >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
set_sched_policy(0, SP_BACKGROUND);
|
||||
} else {
|
||||
set_sched_policy(0, SP_FOREGROUND);
|
||||
SetTaskProfiles(0, {"SCHED_SP_BACKGROUND"}, true);
|
||||
}
|
||||
|
||||
if (name) {
|
||||
|
@ -92,6 +97,7 @@ struct thread_data_t {
|
|||
return f(u);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
void androidSetThreadName(const char* name) {
|
||||
#if defined(__linux__)
|
||||
|
@ -300,11 +306,19 @@ int androidSetThreadPriority(pid_t tid, int pri)
|
|||
{
|
||||
int rc = 0;
|
||||
int lasterr = 0;
|
||||
int curr_pri = getpriority(PRIO_PROCESS, tid);
|
||||
|
||||
if (curr_pri == pri) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (pri >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
rc = set_sched_policy(tid, SP_BACKGROUND);
|
||||
} else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
rc = set_sched_policy(tid, SP_FOREGROUND);
|
||||
rc = SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1;
|
||||
} else if (curr_pri >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
SchedPolicy policy = SP_FOREGROUND;
|
||||
// Change to the sched policy group of the process.
|
||||
get_sched_policy(getpid(), &policy);
|
||||
rc = SetTaskProfiles(tid, {get_sched_policy_profile_name(policy)}, true) ? 0 : -1;
|
||||
}
|
||||
|
||||
if (rc) {
|
||||
|
|
Loading…
Reference in a new issue