Bug 1041632 - Part 7: make MemoryMonitor thread-safe. r=ckitching

This commit is contained in:
Richard Newman 2014-07-21 10:22:45 -07:00
parent 3ab60ab52d
commit f8034be801

View File

@ -40,7 +40,7 @@ class MemoryMonitor extends BroadcastReceiver {
private static final String ACTION_MEMORY_DUMP = "org.mozilla.gecko.MEMORY_DUMP"; private static final String ACTION_MEMORY_DUMP = "org.mozilla.gecko.MEMORY_DUMP";
private static final String ACTION_FORCE_PRESSURE = "org.mozilla.gecko.FORCE_MEMORY_PRESSURE"; private static final String ACTION_FORCE_PRESSURE = "org.mozilla.gecko.FORCE_MEMORY_PRESSURE";
// Memory pressue levels, keep in sync with those in AndroidJavaWrappers.h // Memory pressure levels. Keep these in sync with those in AndroidJavaWrappers.h
private static final int MEMORY_PRESSURE_NONE = 0; private static final int MEMORY_PRESSURE_NONE = 0;
private static final int MEMORY_PRESSURE_CLEANUP = 1; private static final int MEMORY_PRESSURE_CLEANUP = 1;
private static final int MEMORY_PRESSURE_LOW = 2; private static final int MEMORY_PRESSURE_LOW = 2;
@ -54,8 +54,8 @@ class MemoryMonitor extends BroadcastReceiver {
} }
private final PressureDecrementer mPressureDecrementer; private final PressureDecrementer mPressureDecrementer;
private int mMemoryPressure; private int mMemoryPressure; // Synchronized access only.
private boolean mStoragePressure; private volatile boolean mStoragePressure; // Accessed via UI thread intent, background runnables.
private boolean mInited; private boolean mInited;
private MemoryMonitor() { private MemoryMonitor() {
@ -166,6 +166,13 @@ class MemoryMonitor extends BroadcastReceiver {
return true; return true;
} }
/**
* Thread-safe due to mStoragePressure's volatility.
*/
boolean isUnderStoragePressure() {
return mStoragePressure;
}
private boolean decreaseMemoryPressure() { private boolean decreaseMemoryPressure() {
int newLevel; int newLevel;
synchronized (this) { synchronized (this) {
@ -207,7 +214,7 @@ class MemoryMonitor extends BroadcastReceiver {
} }
} }
class StorageReducer implements Runnable { private static class StorageReducer implements Runnable {
private final Context mContext; private final Context mContext;
public StorageReducer(final Context context) { public StorageReducer(final Context context) {
this.mContext = context; this.mContext = context;
@ -221,8 +228,8 @@ class MemoryMonitor extends BroadcastReceiver {
return; return;
} }
if (!mStoragePressure) { if (!MemoryMonitor.getInstance().isUnderStoragePressure()) {
// pressure is off, so we can abort // Pressure is off, so we can abort.
return; return;
} }