From 1dacdd482894bee9fbc1993cbb525fb800750222 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Sun, 8 Nov 2009 14:29:02 -0800 Subject: [PATCH 1/3] Reset binder service threads' cgroup/priority after command completion To prevent buggy command implementations from poisoning binder threads' scheduling class & priority for future command execution, we now reset the cgroup and thread priority to foreground/normal when a binder service thread finishes executing the designated command. Change-Id: Ibc0ab2485751453f6dc96fdb4eb877fd02796e3f --- libs/binder/IPCThreadState.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libs/binder/IPCThreadState.cpp b/libs/binder/IPCThreadState.cpp index e04b2bfab8584..b2a7db8301b36 100644 --- a/libs/binder/IPCThreadState.cpp +++ b/libs/binder/IPCThreadState.cpp @@ -426,6 +426,21 @@ void IPCThreadState::joinThreadPool(bool isMain) result = executeCommand(cmd); } + // After executing the command, ensure that the thread is returned to the + // default cgroup and priority before rejoining the pool. This is a failsafe + // in case the command implementation failed to properly restore the thread's + // scheduling parameters upon completion. + int my_id; +#ifdef HAVE_GETTID + my_id = gettid(); +#else + my_id = getpid(); +#endif + if (!set_sched_policy(my_id, SP_FOREGROUND)) { + // success; reset the priority as well + setpriority(PRIO_PROCESS, my_id, ANDROID_PRIORITY_NORMAL); + } + // Let this thread exit the thread pool if it is no longer // needed and it is not the main process thread. if(result == TIMED_OUT && !isMain) { From 497087e33d422d576d007f72fb970613d008f1cf Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Sun, 8 Nov 2009 18:33:03 -0500 Subject: [PATCH 2/3] Ignore the light sensor if the screen is turning off. Fixes bug b/2202165. The light sensor was interfering with the screen off animation. Change-Id: I1bc566a164af689b60b066e6cb3dcbf7959bc50a Signed-off-by: Mike Lockwood --- .../com/android/server/PowerManagerService.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java index af93d3639de7a..596325c2e2325 100644 --- a/services/java/com/android/server/PowerManagerService.java +++ b/services/java/com/android/server/PowerManagerService.java @@ -1440,6 +1440,8 @@ class PowerManagerService extends IPowerManager.Stub sendNotificationLocked(true, -1); } } else { + // cancel light sensor task + mHandler.removeCallbacks(mAutoBrightnessTask); mScreenOffTime = SystemClock.elapsedRealtime(); long identity = Binder.clearCallingIdentity(); try { @@ -1803,6 +1805,10 @@ class PowerManagerService extends IPowerManager.Stub } } + private boolean isScreenTurningOffLocked() { + return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0); + } + private void forceUserActivityLocked() { // cancel animation so userActivity will succeed mScreenBrightness.animating = false; @@ -1863,7 +1869,7 @@ class PowerManagerService extends IPowerManager.Stub + " force=" + force); } // ignore user activity if we are in the process of turning off the screen - if (mScreenBrightness.animating && mScreenBrightness.targetValue == 0) { + if (isScreenTurningOffLocked()) { Log.d(TAG, "ignoring user activity while turning off screen"); return; } @@ -2441,6 +2447,11 @@ class PowerManagerService extends IPowerManager.Stub SensorEventListener mLightListener = new SensorEventListener() { public void onSensorChanged(SensorEvent event) { synchronized (mLocks) { + // ignore light sensor while screen is turning off + if (isScreenTurningOffLocked()) { + return; + } + int value = (int)event.values[0]; long milliseconds = event.timestamp / 1000000; if (mDebugLightSensor) { From ba8eb1efefdc98c016b64e10c26f9729697f254f Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Sun, 8 Nov 2009 19:31:18 -0500 Subject: [PATCH 3/3] Use SystemClock.elapsedRealtime() instead of sensor timestamp when processing light and proximity events. This should fix loss of 2 second hysterisis for light sensor (b/2243521) Change-Id: Ia3ce331e67d803eb5e51810cb7161b7c528312d7 Signed-off-by: Mike Lockwood --- services/java/com/android/server/PowerManagerService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java index 596325c2e2325..fd518c3c87f6d 100644 --- a/services/java/com/android/server/PowerManagerService.java +++ b/services/java/com/android/server/PowerManagerService.java @@ -2416,7 +2416,7 @@ class PowerManagerService extends IPowerManager.Stub SensorEventListener mProximityListener = new SensorEventListener() { public void onSensorChanged(SensorEvent event) { - long milliseconds = event.timestamp / 1000000; + long milliseconds = SystemClock.elapsedRealtime(); synchronized (mLocks) { float distance = event.values[0]; long timeSinceLastEvent = milliseconds - mLastProximityEventTime; @@ -2453,7 +2453,7 @@ class PowerManagerService extends IPowerManager.Stub } int value = (int)event.values[0]; - long milliseconds = event.timestamp / 1000000; + long milliseconds = SystemClock.elapsedRealtime(); if (mDebugLightSensor) { Log.d(TAG, "onSensorChanged: light value: " + value); }