From 887f355f99ff83d568ef2885a4fdcaae475583df Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Mon, 7 Dec 2009 17:59:37 -0800 Subject: 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. --- core/jni/android_util_Binder.cpp | 7 +++++++ core/jni/android_util_Process.cpp | 39 ++++++++++++--------------------------- 2 files changed, 19 insertions(+), 27 deletions(-) (limited to 'core/jni') diff --git a/core/jni/android_util_Binder.cpp b/core/jni/android_util_Binder.cpp index f0885fd..627fcbf 100644 --- a/core/jni/android_util_Binder.cpp +++ b/core/jni/android_util_Binder.cpp @@ -670,6 +670,12 @@ static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz) android::IPCThreadState::self()->joinThreadPool(); } +static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env, + jobject clazz, jboolean disable) +{ + IPCThreadState::disableBackgroundScheduling(disable ? true : false); +} + static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz) { LOGV("Gc has executed, clearing binder ops"); @@ -682,6 +688,7 @@ static const JNINativeMethod gBinderInternalMethods[] = { /* name, signature, funcPtr */ { "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject }, { "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool }, + { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling }, { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc } }; diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index 094b02d..e84f2e5 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -120,11 +120,7 @@ jint android_os_Process_myUid(JNIEnv* env, jobject clazz) jint android_os_Process_myTid(JNIEnv* env, jobject clazz) { -#ifdef HAVE_GETTID - return gettid(); -#else - return getpid(); -#endif + return androidGetTid(); } jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name) @@ -191,15 +187,11 @@ jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp) { - if (grp > ANDROID_TGROUP_MAX || grp < 0) { - signalExceptionForGroupError(env, clazz, EINVAL); + int res = androidSetThreadSchedulingGroup(pid, grp); + if (res != NO_ERROR) { + signalExceptionForGroupError(env, clazz, res == BAD_VALUE ? EINVAL : errno); return; } - - if (set_sched_policy(pid, (grp == ANDROID_TGROUP_BG_NONINTERACT) ? - SP_BACKGROUND : SP_FOREGROUND)) { - signalExceptionForGroupError(env, clazz, errno); - } } void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) @@ -275,22 +267,15 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, jint pid, jint pri) { - int rc = 0; - - if (pri >= ANDROID_PRIORITY_BACKGROUND) { - rc = set_sched_policy(pid, SP_BACKGROUND); - } else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) { - rc = set_sched_policy(pid, SP_FOREGROUND); - } - - if (rc) { - signalExceptionForGroupError(env, clazz, errno); - return; - } - - if (setpriority(PRIO_PROCESS, pid, pri) < 0) { - signalExceptionForPriorityError(env, clazz, errno); + int rc = androidSetThreadPriority(pid, pri); + if (rc != 0) { + if (rc == INVALID_OPERATION) { + signalExceptionForPriorityError(env, clazz, errno); + } else { + signalExceptionForGroupError(env, clazz, errno); + } } + //LOGI("Setting priority of %d: %d, getpriority returns %d\n", // pid, pri, getpriority(PRIO_PROCESS, pid)); } -- cgit v1.1