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_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_CLEANUP = 1;
private static final int MEMORY_PRESSURE_LOW = 2;
@ -54,8 +54,8 @@ class MemoryMonitor extends BroadcastReceiver {
}
private final PressureDecrementer mPressureDecrementer;
private int mMemoryPressure;
private boolean mStoragePressure;
private int mMemoryPressure; // Synchronized access only.
private volatile boolean mStoragePressure; // Accessed via UI thread intent, background runnables.
private boolean mInited;
private MemoryMonitor() {
@ -166,6 +166,13 @@ class MemoryMonitor extends BroadcastReceiver {
return true;
}
/**
* Thread-safe due to mStoragePressure's volatility.
*/
boolean isUnderStoragePressure() {
return mStoragePressure;
}
private boolean decreaseMemoryPressure() {
int newLevel;
synchronized (this) {
@ -207,7 +214,7 @@ class MemoryMonitor extends BroadcastReceiver {
}
}
class StorageReducer implements Runnable {
private static class StorageReducer implements Runnable {
private final Context mContext;
public StorageReducer(final Context context) {
this.mContext = context;
@ -221,8 +228,8 @@ class MemoryMonitor extends BroadcastReceiver {
return;
}
if (!mStoragePressure) {
// pressure is off, so we can abort
if (!MemoryMonitor.getInstance().isUnderStoragePressure()) {
// Pressure is off, so we can abort.
return;
}