| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.common; | |
| 6 | |
| 7 import android.os.Handler; | |
| 8 import android.os.Looper; | |
| 9 import android.os.Message; | |
| 10 | |
| 11 import org.chromium.base.Log; | |
| 12 import org.chromium.base.ThreadUtils; | |
| 13 import org.chromium.base.TraceEvent; | |
| 14 | |
| 15 import java.lang.ref.ReferenceQueue; | |
| 16 import java.lang.ref.WeakReference; | |
| 17 import java.util.HashSet; | |
| 18 import java.util.Set; | |
| 19 | |
| 20 /** | |
| 21 * Handles running cleanup tasks when an object becomes eligible for GC. Cleanup
tasks | |
| 22 * are always executed on the main thread. In general, classes should not have | |
| 23 * finalizers and likewise should not use this class for the same reasons. The | |
| 24 * exception is where public APIs exist that require native side resources to be | |
| 25 * cleaned up in response to java side GC of API objects. (Private/internal | |
| 26 * interfaces should always favor explicit resource releases / destroy() | |
| 27 * protocol for this rather than depend on GC to trigger native cleanup). | |
| 28 * NOTE this uses WeakReference rather than PhantomReference, to avoid delaying
the | |
| 29 * cleanup processing until after finalizers (if any) have run. In general usage
of | |
| 30 * this class indicates the client does NOT use finalizers anyway (Good), so thi
s should | |
| 31 * not be a visible difference in practice. | |
| 32 */ | |
| 33 public class CleanupReference extends WeakReference<Object> { | |
| 34 private static final String TAG = "cr.CleanupReference"; | |
| 35 | |
| 36 private static final boolean DEBUG = false; // Always check in as false! | |
| 37 | |
| 38 // The VM will enqueue CleanupReference instance onto sGcQueue when it becom
es eligible for | |
| 39 // garbage collection (i.e. when all references to the underlying object are
nullified). | |
| 40 // |sReaperThread| processes this queue by forwarding the references on to t
he UI thread | |
| 41 // (via REMOVE_REF message) to perform cleanup. | |
| 42 private static ReferenceQueue<Object> sGcQueue = new ReferenceQueue<Object>(
); | |
| 43 private static Object sCleanupMonitor = new Object(); | |
| 44 | |
| 45 private static final Thread sReaperThread = new Thread(TAG) { | |
| 46 @Override | |
| 47 @SuppressWarnings("WaitNotInLoop") | |
| 48 public void run() { | |
| 49 while (true) { | |
| 50 try { | |
| 51 CleanupReference ref = (CleanupReference) sGcQueue.remove(); | |
| 52 if (DEBUG) Log.d(TAG, "removed one ref from GC queue"); | |
| 53 synchronized (sCleanupMonitor) { | |
| 54 Message.obtain(LazyHolder.sHandler, REMOVE_REF, ref).sen
dToTarget(); | |
| 55 // Give the UI thread chance to run cleanup before loopi
ng around and | |
| 56 // taking the next item from the queue, to avoid Message
bombing it. | |
| 57 sCleanupMonitor.wait(500); | |
| 58 } | |
| 59 } catch (Exception e) { | |
| 60 Log.e(TAG, "Queue remove exception:", e); | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 static { | |
| 67 sReaperThread.setDaemon(true); | |
| 68 sReaperThread.start(); | |
| 69 } | |
| 70 | |
| 71 // Message's sent in the |what| field to |sHandler|. | |
| 72 | |
| 73 // Add a new reference to sRefs. |msg.obj| is the CleanupReference to add. | |
| 74 private static final int ADD_REF = 1; | |
| 75 // Remove reference from sRefs. |msg.obj| is the CleanupReference to remove. | |
| 76 private static final int REMOVE_REF = 2; | |
| 77 | |
| 78 /** | |
| 79 * This {@link Handler} polls {@link #sRefs}, looking for cleanup tasks that | |
| 80 * are ready to run. | |
| 81 * This is lazily initialized as ThreadUtils.getUiThreadLooper() may not be | |
| 82 * set yet early in startup. | |
| 83 */ | |
| 84 private static class LazyHolder { | |
| 85 static final Handler sHandler = new Handler(ThreadUtils.getUiThreadLoope
r()) { | |
| 86 @Override | |
| 87 public void handleMessage(Message msg) { | |
| 88 try { | |
| 89 TraceEvent.begin("CleanupReference.LazyHolder.handleMessage"
); | |
| 90 CleanupReference ref = (CleanupReference) msg.obj; | |
| 91 switch (msg.what) { | |
| 92 case ADD_REF: | |
| 93 sRefs.add(ref); | |
| 94 break; | |
| 95 case REMOVE_REF: | |
| 96 ref.runCleanupTaskInternal(); | |
| 97 break; | |
| 98 default: | |
| 99 Log.e(TAG, "Bad message=%d", msg.what); | |
| 100 break; | |
| 101 } | |
| 102 | |
| 103 if (DEBUG) Log.d(TAG, "will try and cleanup; max = %d", sRef
s.size()); | |
| 104 | |
| 105 synchronized (sCleanupMonitor) { | |
| 106 // Always run the cleanup loop here even when adding or
removing refs, to | |
| 107 // avoid falling behind on rapid garbage allocation inne
r loops. | |
| 108 while ((ref = (CleanupReference) sGcQueue.poll()) != nul
l) { | |
| 109 ref.runCleanupTaskInternal(); | |
| 110 } | |
| 111 sCleanupMonitor.notifyAll(); | |
| 112 } | |
| 113 } finally { | |
| 114 TraceEvent.end("CleanupReference.LazyHolder.handleMessage"); | |
| 115 } | |
| 116 } | |
| 117 }; | |
| 118 } | |
| 119 | |
| 120 /** | |
| 121 * Keep a strong reference to {@link CleanupReference} so that it will | |
| 122 * actually get enqueued. | |
| 123 * Only accessed on the UI thread. | |
| 124 */ | |
| 125 private static Set<CleanupReference> sRefs = new HashSet<CleanupReference>()
; | |
| 126 | |
| 127 private Runnable mCleanupTask; | |
| 128 | |
| 129 /** | |
| 130 * @param obj the object whose loss of reachability should trigger the | |
| 131 * cleanup task. | |
| 132 * @param cleanupTask the task to run once obj loses reachability. | |
| 133 */ | |
| 134 public CleanupReference(Object obj, Runnable cleanupTask) { | |
| 135 super(obj, sGcQueue); | |
| 136 if (DEBUG) Log.d(TAG, "+++ CREATED ONE REF"); | |
| 137 mCleanupTask = cleanupTask; | |
| 138 handleOnUiThread(ADD_REF); | |
| 139 } | |
| 140 | |
| 141 /** | |
| 142 * Clear the cleanup task {@link Runnable} so that nothing will be done | |
| 143 * after garbage collection. | |
| 144 */ | |
| 145 public void cleanupNow() { | |
| 146 handleOnUiThread(REMOVE_REF); | |
| 147 } | |
| 148 | |
| 149 private void handleOnUiThread(int what) { | |
| 150 Message msg = Message.obtain(LazyHolder.sHandler, what, this); | |
| 151 if (Looper.myLooper() == msg.getTarget().getLooper()) { | |
| 152 msg.getTarget().handleMessage(msg); | |
| 153 msg.recycle(); | |
| 154 } else { | |
| 155 msg.sendToTarget(); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 private void runCleanupTaskInternal() { | |
| 160 if (DEBUG) Log.d(TAG, "runCleanupTaskInternal"); | |
| 161 sRefs.remove(this); | |
| 162 if (mCleanupTask != null) { | |
| 163 if (DEBUG) Log.i(TAG, "--- CLEANING ONE REF"); | |
| 164 mCleanupTask.run(); | |
| 165 mCleanupTask = null; | |
| 166 } | |
| 167 clear(); | |
| 168 } | |
| 169 } | |
| OLD | NEW |