gecko/mobile/android/base/sync/DelayedWorkTracker.java
Nick Alexander e989e12867 Bug 844347 - Factor logging code that is not Sync-specific out of org.mozilla.gecko.sync. r=rnewman
--HG--
rename : mobile/android/base/sync/GlobalConstants.java.in => mobile/android/base/background/common/GlobalConstants.java.in
rename : mobile/android/base/sync/Logger.java => mobile/android/base/background/common/log/Logger.java
rename : mobile/android/base/sync/log/writers/AndroidLevelCachingLogWriter.java => mobile/android/base/background/common/log/writers/AndroidLevelCachingLogWriter.java
rename : mobile/android/base/sync/log/writers/AndroidLogWriter.java => mobile/android/base/background/common/log/writers/AndroidLogWriter.java
rename : mobile/android/base/sync/log/writers/LevelFilteringLogWriter.java => mobile/android/base/background/common/log/writers/LevelFilteringLogWriter.java
rename : mobile/android/base/sync/log/writers/LogWriter.java => mobile/android/base/background/common/log/writers/LogWriter.java
rename : mobile/android/base/sync/log/writers/PrintLogWriter.java => mobile/android/base/background/common/log/writers/PrintLogWriter.java
rename : mobile/android/base/sync/log/writers/SimpleTagLogWriter.java => mobile/android/base/background/common/log/writers/SimpleTagLogWriter.java
rename : mobile/android/base/sync/log/writers/StringLogWriter.java => mobile/android/base/background/common/log/writers/StringLogWriter.java
rename : mobile/android/base/sync/log/writers/TagLogWriter.java => mobile/android/base/background/common/log/writers/TagLogWriter.java
rename : mobile/android/base/sync/log/writers/ThreadLocalTagLogWriter.java => mobile/android/base/background/common/log/writers/ThreadLocalTagLogWriter.java
2013-02-27 15:44:21 -08:00

69 lines
1.9 KiB
Java

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync;
import org.mozilla.gecko.background.common.log.Logger;
/**
* A little class to allow us to maintain a count of extant
* things (in our case, callbacks that need to fire), and
* some work that we want done when that count hits 0.
*
* @author rnewman
*
*/
public class DelayedWorkTracker {
private static final String LOG_TAG = "DelayedWorkTracker";
protected Runnable workItem = null;
protected int outstandingCount = 0;
public int incrementOutstanding() {
Logger.trace(LOG_TAG, "Incrementing outstanding.");
synchronized(this) {
return ++outstandingCount;
}
}
public int decrementOutstanding() {
Logger.trace(LOG_TAG, "Decrementing outstanding.");
Runnable job = null;
int count;
synchronized(this) {
if ((count = --outstandingCount) == 0 &&
workItem != null) {
job = workItem;
workItem = null;
} else {
return count;
}
}
job.run();
// In case it's changed.
return getOutstandingOperations();
}
public int getOutstandingOperations() {
synchronized(this) {
return outstandingCount;
}
}
public void delayWorkItem(Runnable item) {
Logger.trace(LOG_TAG, "delayWorkItem.");
boolean runnableNow = false;
synchronized(this) {
Logger.trace(LOG_TAG, "outstandingCount: " + outstandingCount);
if (outstandingCount == 0) {
runnableNow = true;
} else {
if (workItem != null) {
throw new IllegalStateException("Work item already set!");
}
workItem = item;
}
}
if (runnableNow) {
Logger.trace(LOG_TAG, "Running item now.");
item.run();
}
}
}