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