12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.ref;
27
28 import sun.misc.Cleaner;
29 import jdk.internal.HotSpotIntrinsicCandidate;
30 import jdk.internal.misc.JavaLangRefAccess;
31 import jdk.internal.misc.SharedSecrets;
32 import sun.misc.ManagedLocalsThread;
33
34 /**
35 * Abstract base class for reference objects. This class defines the
36 * operations common to all reference objects. Because reference objects are
37 * implemented in close cooperation with the garbage collector, this class may
38 * not be subclassed directly.
39 *
40 * @author Mark Reinhold
41 * @since 1.2
42 */
43
44 public abstract class Reference<T> {
45
46 /* A Reference instance is in one of four possible internal states:
47 *
48 * Active: Subject to special treatment by the garbage collector. Some
49 * time after the collector detects that the reachability of the
50 * referent has changed to the appropriate state, it changes the
51 * instance's state to either Pending or Inactive, depending upon
52 * whether or not the instance was registered with a queue when it was
111
112
113 /* Object used to synchronize with the garbage collector. The collector
114 * must acquire this lock at the beginning of each collection cycle. It is
115 * therefore critical that any code holding this lock complete as quickly
116 * as possible, allocate no new objects, and avoid calling user code.
117 */
118 private static class Lock { }
119 private static Lock lock = new Lock();
120
121
122 /* List of References waiting to be enqueued. The collector adds
123 * References to this list, while the Reference-handler thread removes
124 * them. This list is protected by the above lock object. The
125 * list uses the discovered field to link its elements.
126 */
127 private static Reference<Object> pending = null;
128
129 /* High-priority thread to enqueue pending References
130 */
131 private static class ReferenceHandler extends ManagedLocalsThread {
132
133 private static void ensureClassInitialized(Class<?> clazz) {
134 try {
135 Class.forName(clazz.getName(), true, clazz.getClassLoader());
136 } catch (ClassNotFoundException e) {
137 throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
138 }
139 }
140
141 static {
142 // pre-load and initialize InterruptedException and Cleaner classes
143 // so that we don't get into trouble later in the run loop if there's
144 // memory shortage while loading/initializing them lazily.
145 ensureClassInitialized(InterruptedException.class);
146 ensureClassInitialized(Cleaner.class);
147 }
148
149 ReferenceHandler(ThreadGroup g, String name) {
150 super(g, name);
151 }
152
153 public void run() {
154 while (true) {
155 tryHandlePending(true);
156 }
157 }
158 }
159
160 /**
161 * Try handle pending {@link Reference} if there is one.<p>
162 * Return {@code true} as a hint that there might be another
163 * {@link Reference} pending or {@code false} when there are no more pending
164 * {@link Reference}s at the moment and the program can do some other
165 * useful work instead of looping.
166 *
167 * @param waitForNotify if {@code true} and there was no pending
168 * {@link Reference}, wait until notified from VM
169 * or interrupted; if {@code false}, return immediately
170 * when there is no pending {@link Reference}.
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.ref;
27
28 import sun.misc.Cleaner;
29 import jdk.internal.HotSpotIntrinsicCandidate;
30 import jdk.internal.misc.JavaLangRefAccess;
31 import jdk.internal.misc.SharedSecrets;
32
33 /**
34 * Abstract base class for reference objects. This class defines the
35 * operations common to all reference objects. Because reference objects are
36 * implemented in close cooperation with the garbage collector, this class may
37 * not be subclassed directly.
38 *
39 * @author Mark Reinhold
40 * @since 1.2
41 */
42
43 public abstract class Reference<T> {
44
45 /* A Reference instance is in one of four possible internal states:
46 *
47 * Active: Subject to special treatment by the garbage collector. Some
48 * time after the collector detects that the reachability of the
49 * referent has changed to the appropriate state, it changes the
50 * instance's state to either Pending or Inactive, depending upon
51 * whether or not the instance was registered with a queue when it was
110
111
112 /* Object used to synchronize with the garbage collector. The collector
113 * must acquire this lock at the beginning of each collection cycle. It is
114 * therefore critical that any code holding this lock complete as quickly
115 * as possible, allocate no new objects, and avoid calling user code.
116 */
117 private static class Lock { }
118 private static Lock lock = new Lock();
119
120
121 /* List of References waiting to be enqueued. The collector adds
122 * References to this list, while the Reference-handler thread removes
123 * them. This list is protected by the above lock object. The
124 * list uses the discovered field to link its elements.
125 */
126 private static Reference<Object> pending = null;
127
128 /* High-priority thread to enqueue pending References
129 */
130 private static class ReferenceHandler extends Thread {
131
132 private static void ensureClassInitialized(Class<?> clazz) {
133 try {
134 Class.forName(clazz.getName(), true, clazz.getClassLoader());
135 } catch (ClassNotFoundException e) {
136 throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
137 }
138 }
139
140 static {
141 // pre-load and initialize InterruptedException and Cleaner classes
142 // so that we don't get into trouble later in the run loop if there's
143 // memory shortage while loading/initializing them lazily.
144 ensureClassInitialized(InterruptedException.class);
145 ensureClassInitialized(Cleaner.class);
146 }
147
148 ReferenceHandler(ThreadGroup g, String name) {
149 super(g, null, name, 0, false);
150 }
151
152 public void run() {
153 while (true) {
154 tryHandlePending(true);
155 }
156 }
157 }
158
159 /**
160 * Try handle pending {@link Reference} if there is one.<p>
161 * Return {@code true} as a hint that there might be another
162 * {@link Reference} pending or {@code false} when there are no more pending
163 * {@link Reference}s at the moment and the program can do some other
164 * useful work instead of looping.
165 *
166 * @param waitForNotify if {@code true} and there was no pending
167 * {@link Reference}, wait until notified from VM
168 * or interrupted; if {@code false}, return immediately
169 * when there is no pending {@link Reference}.
|