|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 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 | +package jdk.internal.misc; |
| 26 | + |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tracks threads to help detect reentrancy without using ThreadLocal variables. |
| 32 | + * A thread invokes the {@code begin} or {@code tryBegin} methods at the start |
| 33 | + * of a block, and the {@code end} method at the end of a block. |
| 34 | + */ |
| 35 | +public class ThreadTracker { |
| 36 | + |
| 37 | + /** |
| 38 | + * A reference to a Thread that is suitable for use as a key in a collection. |
| 39 | + * The hashCode/equals methods do not invoke the Thread hashCode/equals method |
| 40 | + * as they may run arbitrary code and/or leak references to Thread objects. |
| 41 | + */ |
| 42 | + private final class ThreadRef { |
| 43 | + private final Thread thread; |
| 44 | + |
| 45 | + ThreadRef(Thread thread) { |
| 46 | + this.thread = thread; |
| 47 | + } |
| 48 | + |
| 49 | + Thread thread() { |
| 50 | + return this.thread; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public int hashCode() { |
| 55 | + return Long.hashCode(thread.getId()); |
| 56 | + } |
| 57 | + @Override |
| 58 | + public boolean equals(Object obj) { |
| 59 | + return (obj instanceof ThreadRef) |
| 60 | + && this.thread == ((ThreadRef)obj).thread; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private final Set<ThreadRef> threads = ConcurrentHashMap.newKeySet(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Adds the current thread to thread set if not already in the set. |
| 68 | + * Returns a key to remove the thread or {@code null} if already in the set. |
| 69 | + */ |
| 70 | + public Object tryBegin() { |
| 71 | + var threadRef = new ThreadRef(Thread.currentThread()); |
| 72 | + return threads.add(threadRef) ? threadRef : null; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds the current thread to thread set if not already in the set. |
| 77 | + * Returns a key to remove the thread. |
| 78 | + */ |
| 79 | + public Object begin() { |
| 80 | + var threadRef = new ThreadRef(Thread.currentThread()); |
| 81 | + boolean added = threads.add(threadRef); |
| 82 | + assert added; |
| 83 | + return threadRef; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Removes the thread identified by the key from the thread set. |
| 88 | + */ |
| 89 | + public void end(Object key) { |
| 90 | + var threadRef = (ThreadRef) key; |
| 91 | + assert threadRef.thread() == Thread.currentThread(); |
| 92 | + boolean removed = threads.remove(threadRef); |
| 93 | + assert removed; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns true if the given thread is tracked. |
| 98 | + */ |
| 99 | + public boolean contains(Thread thread) { |
| 100 | + var threadRef = new ThreadRef(thread); |
| 101 | + return threads.contains(threadRef); |
| 102 | + } |
| 103 | +} |
0 commit comments