Merge "Camera2: Support early finished tasks" into nyc-mr1-dev

This commit is contained in:
Chien-Yu Chen
2016-09-26 21:35:19 +00:00
committed by Android (Google) Code Review

View File

@@ -29,8 +29,9 @@ import static com.android.internal.util.Preconditions.*;
* (and new ones won't begin). * (and new ones won't begin).
* *
* <p>The initial state is to allow all tasks to be started and finished. A task may only be started * <p>The initial state is to allow all tasks to be started and finished. A task may only be started
* once, after which it must be finished before starting again. Likewise, finishing a task * once, after which it must be finished before starting again. Likewise, a task may only be
* that hasn't been started is also not allowed.</p> * finished once, after which it must be started before finishing again. It is okay to finish a
* task before starting it due to different threads handling starting and finishing.</p>
* *
* <p>When draining begins, no more new tasks can be started. This guarantees that at some * <p>When draining begins, no more new tasks can be started. This guarantees that at some
* point when all the tasks are finished there will be no more collective new tasks, * point when all the tasks are finished there will be no more collective new tasks,
@@ -60,6 +61,11 @@ public class TaskDrainer<T> {
/** Set of tasks which have been started but not yet finished with #taskFinished */ /** Set of tasks which have been started but not yet finished with #taskFinished */
private final Set<T> mTaskSet = new HashSet<T>(); private final Set<T> mTaskSet = new HashSet<T>();
/**
* Set of tasks which have been finished but not yet started with #taskStarted. This may happen
* if taskStarted and taskFinished are called from two different threads.
*/
private final Set<T> mEarlyFinishedTaskSet = new HashSet<T>();
private final Object mLock = new Object(); private final Object mLock = new Object();
private boolean mDraining = false; private boolean mDraining = false;
@@ -118,8 +124,12 @@ public class TaskDrainer<T> {
throw new IllegalStateException("Can't start more tasks after draining has begun"); throw new IllegalStateException("Can't start more tasks after draining has begun");
} }
if (!mTaskSet.add(task)) { // Try to remove the task from the early finished set.
throw new IllegalStateException("Task " + task + " was already started"); if (!mEarlyFinishedTaskSet.remove(task)) {
// The task is not finished early. Add it to the started set.
if (!mTaskSet.add(task)) {
throw new IllegalStateException("Task " + task + " was already started");
}
} }
} }
} }
@@ -128,8 +138,7 @@ public class TaskDrainer<T> {
/** /**
* Mark an asynchronous task as having finished. * Mark an asynchronous task as having finished.
* *
* <p>A task cannot be finished if it hasn't started. Once finished, a task * <p>A task cannot be finished more than once without first having started.</p>
* cannot be finished again (unless it's started again).</p>
* *
* @param task a key to identify a task * @param task a key to identify a task
* *
@@ -137,7 +146,7 @@ public class TaskDrainer<T> {
* @see #beginDrain * @see #beginDrain
* *
* @throws IllegalStateException * @throws IllegalStateException
* If attempting to start a task which is already finished (and not re-started), * If attempting to finish a task which is already finished (and not started),
*/ */
public void taskFinished(T task) { public void taskFinished(T task) {
synchronized (mLock) { synchronized (mLock) {
@@ -145,8 +154,12 @@ public class TaskDrainer<T> {
Log.v(TAG + "[" + mName + "]", "taskFinished " + task); Log.v(TAG + "[" + mName + "]", "taskFinished " + task);
} }
// Try to remove the task from started set.
if (!mTaskSet.remove(task)) { if (!mTaskSet.remove(task)) {
throw new IllegalStateException("Task " + task + " was already finished"); // Task is not started yet. Add it to the early finished set.
if (!mEarlyFinishedTaskSet.add(task)) {
throw new IllegalStateException("Task " + task + " was already finished");
}
} }
// If this is the last finished task and draining has already begun, fire #onDrained // If this is the last finished task and draining has already begun, fire #onDrained