Skip to content

Commit

Permalink
Fixes cycles in android.os.AsyncTask.
Browse files Browse the repository at this point in the history
	Change on 2017/02/01 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146278073
  • Loading branch information
kstanger committed Feb 3, 2017
1 parent be36eda commit 46939f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package android.os;

import com.google.j2objc.annotations.RetainedWith;
import com.google.j2objc.annotations.WeakOuter;
import java.util.ArrayDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -209,6 +211,7 @@ public Thread newThread(Runnable r) {
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;

private final WorkerRunnable<Params, Result> mWorker;
@RetainedWith
private final FutureTask<Result> mFuture;

private volatile Status mStatus = Status.PENDING;
Expand Down Expand Up @@ -270,15 +273,16 @@ public static void setDefaultExecutor(Executor exec) {
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
@WeakOuter class WorkerRunnableImpl extends WorkerRunnable<Params, Result> {
public Result call() throws Exception {
mTaskInvoked.set(true);

Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
//noinspection unchecked
return postResult(doInBackground(mParams));
}
};
}
mWorker = new WorkerRunnableImpl();

mFuture = new FutureTask<Result>(mWorker) {
@Override
Expand All @@ -305,9 +309,8 @@ private void postResultIfNotInvoked(Result result) {
}

private native Result postResult(Result result) /*-[
AndroidOsAsyncTask __block *blockSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[blockSelf finishWithId:result];
[self finishWithId:result];
});
return result;
]-*/;
Expand Down Expand Up @@ -611,9 +614,8 @@ public static void execute(Runnable runnable) {
*/
protected native final void publishProgress(Progress... values) /*-[
if (!AndroidOsAsyncTask_isCancelled(self)) {
AndroidOsAsyncTask __block *blockSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[blockSelf onProgressUpdateWithNSObjectArray:values];
[self onProgressUpdateWithNSObjectArray:values];
});
}
]-*/;
Expand Down
4 changes: 1 addition & 3 deletions jre_emul/cycle_whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ FIELD java.util.stream.StreamSpliterators.UnorderedSliceSpliterator.s
FIELD java.util.stream.Streams.ConcatSpliterator.aSpliterator
FIELD java.util.stream.Streams.ConcatSpliterator.bSpliterator

FIELD android.os.AsyncTask.mFuture
FIELD android.os.AsyncTask.mWorker
# mActive always refers to the active task and is cleared/reassigned upon completion.
FIELD android.os.AsyncTask.SerialExecutor.mActive
FIELD android.os.AsyncTask.SerialExecutor.mTasks

# These thread fields are cleared when the thread finishes, regardless of any exceptions.
FIELD java.lang.Thread.interruptActions
Expand Down

0 comments on commit 46939f3

Please sign in to comment.