|
| 1 | +package datadog.environment; |
| 2 | + |
| 3 | +import java.lang.invoke.MethodHandle; |
| 4 | +import java.lang.invoke.MethodHandles; |
| 5 | +import java.lang.invoke.MethodType; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper class for working with {@link Thread}s. |
| 12 | + * |
| 13 | + * <p>Uses feature detection and provides static helpers to work with different versions of Java. |
| 14 | + * |
| 15 | + * <p>This class is designed to use {@link MethodHandle}s that constant propagate to minimize the |
| 16 | + * overhead. |
| 17 | + */ |
| 18 | +public final class ThreadSupport { |
| 19 | + static final MethodHandle THREAD_ID_MH = findThreadIdMethodHandle(); |
| 20 | + static final MethodHandle IS_VIRTUAL_MH = findIsVirtualMethodHandle(); |
| 21 | + static final MethodHandle NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR_MH = |
| 22 | + findNewVirtualThreadPerTaskExecutorMethodHandle(); |
| 23 | + |
| 24 | + private ThreadSupport() {} |
| 25 | + |
| 26 | + /** |
| 27 | + * Provides the best identifier available for the current {@link Thread}. Uses {@link |
| 28 | + * Thread#threadId()} on 19+ or {@link Thread#getId()} on older JVMs. |
| 29 | + * |
| 30 | + * @return The best identifier available for the current {@link Thread}. |
| 31 | + */ |
| 32 | + public static long threadId() { |
| 33 | + return threadId(Thread.currentThread()); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Provides the best identifier available for the given {@link Thread}. Uses {@link |
| 38 | + * Thread#threadId()} on 19+ or {@link Thread#getId()} on older JVMs. |
| 39 | + * |
| 40 | + * @return The best identifier available for the given {@link Thread}. |
| 41 | + */ |
| 42 | + public static long threadId(Thread thread) { |
| 43 | + if (THREAD_ID_MH != null) { |
| 44 | + try { |
| 45 | + return (long) THREAD_ID_MH.invoke(thread); |
| 46 | + } catch (Throwable ignored) { |
| 47 | + } |
| 48 | + } |
| 49 | + return thread.getId(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Checks whether virtual threads are supported on this JVM. |
| 54 | + * |
| 55 | + * @return {@code true} if virtual threads are supported, {@code false} otherwise. |
| 56 | + */ |
| 57 | + public static boolean supportsVirtualThreads() { |
| 58 | + return (IS_VIRTUAL_MH != null); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks whether the current thread is a virtual thread. |
| 63 | + * |
| 64 | + * @return {@code true} if the current thread is a virtual thread, {@code false} otherwise. |
| 65 | + */ |
| 66 | + public static boolean isVirtual() { |
| 67 | + // IS_VIRTUAL_MH will constant propagate -- then dead code eliminate -- and inline as needed |
| 68 | + return IS_VIRTUAL_MH != null && isVirtual(Thread.currentThread()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Checks whether the given thread is a virtual thread. |
| 73 | + * |
| 74 | + * @param thread The thread to check. |
| 75 | + * @return {@code true} if the given thread is virtual, {@code false} otherwise. |
| 76 | + */ |
| 77 | + public static boolean isVirtual(Thread thread) { |
| 78 | + // IS_VIRTUAL_MH will constant propagate -- then dead code eliminate -- and inline as needed |
| 79 | + if (IS_VIRTUAL_MH != null) { |
| 80 | + try { |
| 81 | + return (boolean) IS_VIRTUAL_MH.invoke(thread); |
| 82 | + } catch (Throwable ignored) { |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the virtual thread per task executor if available. |
| 90 | + * |
| 91 | + * @return The virtual thread per task executor if available wrapped into an {@link Optional}, or |
| 92 | + * {@link Optional#empty()} otherwise. |
| 93 | + */ |
| 94 | + public static Optional<ExecutorService> newVirtualThreadPerTaskExecutor() { |
| 95 | + if (NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR_MH != null) { |
| 96 | + try { |
| 97 | + ExecutorService executorService = |
| 98 | + (ExecutorService) NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR_MH.invoke(); |
| 99 | + return Optional.of(executorService); |
| 100 | + } catch (Throwable ignored) { |
| 101 | + } |
| 102 | + } |
| 103 | + return Optional.empty(); |
| 104 | + } |
| 105 | + |
| 106 | + private static MethodHandle findThreadIdMethodHandle() { |
| 107 | + if (JavaVirtualMachine.isJavaVersionAtLeast(19)) { |
| 108 | + try { |
| 109 | + return MethodHandles.lookup() |
| 110 | + .findVirtual(Thread.class, "threadId", MethodType.methodType(long.class)); |
| 111 | + } catch (Throwable ignored) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + private static MethodHandle findIsVirtualMethodHandle() { |
| 119 | + if (JavaVirtualMachine.isJavaVersionAtLeast(21)) { |
| 120 | + try { |
| 121 | + return MethodHandles.lookup() |
| 122 | + .findVirtual(Thread.class, "isVirtual", MethodType.methodType(boolean.class)); |
| 123 | + } catch (Throwable ignored) { |
| 124 | + } |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + private static MethodHandle findNewVirtualThreadPerTaskExecutorMethodHandle() { |
| 130 | + if (JavaVirtualMachine.isJavaVersionAtLeast(21)) { |
| 131 | + try { |
| 132 | + return MethodHandles.lookup() |
| 133 | + .findStatic( |
| 134 | + Executors.class, |
| 135 | + "newVirtualThreadPerTaskExecutor", |
| 136 | + MethodType.methodType(ExecutorService.class)); |
| 137 | + } catch (Throwable ignored) { |
| 138 | + } |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | +} |
0 commit comments