Propagate background scheduling class across processes.

This is a very simply implementation: upon receiving an IPC, if the handling
thread is at a background priority (the driver will have taken care of
propagating this from the calling thread), then stick it in to the background
scheduling group.  Plus an API to turn this off for the process, which is
used by the system process.

This also pulls some of the code for managing scheduling classes out of
the Process JNI wrappers and in to some convenience methods in thread.h.
This commit is contained in:
Dianne Hackborn 2009-12-07 17:59:37 -08:00 committed by Alex Ray
parent 9a2d83e698
commit 235af97deb
2 changed files with 67 additions and 0 deletions

View file

@ -124,6 +124,24 @@ typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
extern void androidSetCreateThreadFunc(android_create_thread_fn func);
// ------------------------------------------------------------------
// Extra functions working with raw pids.
// Get pid for the current thread.
extern pid_t androidGetTid();
// Change the scheduling group of a particular thread. The group
// should be one of the ANDROID_TGROUP constants. Returns BAD_VALUE if
// grp is out of range, else another non-zero value with errno set if
// the operation failed.
extern int androidSetThreadSchedulingGroup(pid_t tid, int grp);
// Change the priority AND scheduling group of a particular thread. The priority
// should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION
// if the priority set failed, else another value if just the group set failed;
// in either case errno is set.
extern int androidSetThreadPriority(pid_t tid, int prio);
#ifdef __cplusplus
}
#endif

View file

@ -20,6 +20,8 @@
#include <utils/threads.h>
#include <utils/Log.h>
#include <cutils/sched_policy.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
@ -269,6 +271,53 @@ void androidSetCreateThreadFunc(android_create_thread_fn func)
gCreateThreadFn = func;
}
pid_t androidGetTid()
{
#ifdef HAVE_GETTID
return gettid();
#else
return getpid();
#endif
}
int androidSetThreadSchedulingGroup(pid_t tid, int grp)
{
if (grp > ANDROID_TGROUP_MAX || grp < 0) {
return BAD_VALUE;
}
if (set_sched_policy(tid, (grp == ANDROID_TGROUP_BG_NONINTERACT) ?
SP_BACKGROUND : SP_FOREGROUND)) {
return PERMISSION_DENIED;
}
return NO_ERROR;
}
int androidSetThreadPriority(pid_t tid, int pri)
{
int rc = 0;
int lasterr = 0;
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);
}
if (rc) {
lasterr = errno;
}
if (setpriority(PRIO_PROCESS, tid, pri) < 0) {
rc = INVALID_OPERATION;
} else {
errno = lasterr;
}
return rc;
}
namespace android {
/*