summaryrefslogtreecommitdiff
path: root/support
diff options
context:
space:
mode:
authorMiao Wang <miaowang@google.com>2016-05-11 22:25:10 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2016-05-11 22:25:10 +0000
commitf854f15b8fa0f0d0ac11cc08712343d8d58e511a (patch)
treeef1ecac948e053b7f131006f69c9bc81dc2c8223 /support
parentcf8cd79a6e480ef1b92cab2a113c1192de5d623f (diff)
parent5bab643b33fc853808cbcd74d8dde97e269ae96a (diff)
downloadrs-f854f15b8fa0f0d0ac11cc08712343d8d58e511a.tar.gz
Merge "[RenderScript] Add finalizer to support lib context."
Diffstat (limited to 'support')
-rw-r--r--support/java/src/android/support/v8/renderscript/RenderScript.java76
1 files changed, 50 insertions, 26 deletions
diff --git a/support/java/src/android/support/v8/renderscript/RenderScript.java b/support/java/src/android/support/v8/renderscript/RenderScript.java
index 85b6763b..88accae2 100644
--- a/support/java/src/android/support/v8/renderscript/RenderScript.java
+++ b/support/java/src/android/support/v8/renderscript/RenderScript.java
@@ -1045,6 +1045,7 @@ public class RenderScript {
}
long mContext;
+ private boolean mDestroyed = false;
//Dummy device & context for Inc Support Lib
long mIncCon;
//indicator of whether inc support lib has been loaded or not.
@@ -1657,6 +1658,54 @@ public class RenderScript {
nContextFinish();
}
+ private void helpDestroy() {
+ boolean shouldDestroy = false;
+ synchronized(this) {
+ if (!mDestroyed) {
+ shouldDestroy = true;
+ mDestroyed = true;
+ }
+ }
+
+ if (shouldDestroy) {
+ nContextFinish();
+ if (mIncCon != 0) {
+ nIncContextFinish();
+ nIncContextDestroy();
+ mIncCon = 0;
+ }
+ nContextDeinitToClient(mContext);
+ mMessageThread.mRun = false;
+ // Interrupt mMessageThread so it gets to see immediately that mRun is false
+ // and exit rightaway.
+ mMessageThread.interrupt();
+
+ // Wait for mMessageThread to join. Try in a loop, in case this thread gets interrupted
+ // during the wait. If interrupted, set the "interrupted" status of the current thread.
+ boolean hasJoined = false, interrupted = false;
+ while (!hasJoined) {
+ try {
+ mMessageThread.join();
+ hasJoined = true;
+ } catch (InterruptedException e) {
+ interrupted = true;
+ }
+ }
+ if (interrupted) {
+ Log.v(LOG_TAG, "Interrupted during wait for MessageThread to join");
+ Thread.currentThread().interrupt();
+ }
+
+ nContextDestroy();
+ }
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ helpDestroy();
+ super.finalize();
+ }
+
/**
* Destroys this RenderScript context. Once this function is called,
* using this context or any objects belonging to this context is
@@ -1672,32 +1721,7 @@ public class RenderScript {
return;
}
validate();
- nContextFinish();
- if (mIncCon != 0) {
- nIncContextFinish();
- nIncContextDestroy();
- mIncCon = 0;
- }
- nContextDeinitToClient(mContext);
- mMessageThread.mRun = false;
-
- // Wait for mMessageThread to join. Try in a loop, in case this thread gets interrupted
- // during the wait. If interrupted, set the "interrupted" status of the current thread.
- boolean hasJoined = false, interrupted = false;
- while (!hasJoined) {
- try {
- mMessageThread.join();
- hasJoined = true;
- } catch (InterruptedException e) {
- interrupted = true;
- }
- }
- if (interrupted) {
- Log.v(LOG_TAG, "Interrupted during wait for MessageThread to join");
- Thread.currentThread().interrupt();
- }
-
- nContextDestroy();
+ helpDestroy();
}
boolean isAlive() {