forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backed out changeset 96dab44be3ba (bug 1351673) for Android build bus…
…tage CLOSED TREE MozReview-Commit-ID: 4XV7Be9AXmB
- Loading branch information
Showing
2 changed files
with
96 additions
and
31 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
mobile/android/services/src/main/java/org/mozilla/gecko/sync/DelayedWorkTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters