|
| 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 | +/** Provides virtual thread capabilities if available. */ |
| 11 | +public final class ThreadSupport { |
| 12 | + static final MethodHandle MH_IS_VIRTUAL = findIsVirtualMethodHandle(); |
| 13 | + static final MethodHandle MH_NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR = |
| 14 | + findNewVirtualThreadPerTaskExecutorMethodHandle(); |
| 15 | + |
| 16 | + private ThreadSupport() {} |
| 17 | + |
| 18 | + /** |
| 19 | + * Checks whether the current thread is a virtual thread. |
| 20 | + * |
| 21 | + * @return {@code true} if the current thread is a virtual thread, {@code false} otherwise. |
| 22 | + */ |
| 23 | + public static boolean isVirtual() { |
| 24 | + if (MH_IS_VIRTUAL == null) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return isVirtual(Thread.currentThread()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Checks whether the given thread is a virtual thread. |
| 32 | + * |
| 33 | + * @param thread The thread to check. |
| 34 | + * @return {@code true} if the given thread is virtual, {@code false} otherwise. |
| 35 | + */ |
| 36 | + public static boolean isVirtual(Thread thread) { |
| 37 | + if (MH_IS_VIRTUAL == null) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + try { |
| 41 | + return (boolean) MH_IS_VIRTUAL.invoke(thread); |
| 42 | + } catch (Throwable e) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the virtual thread per task executor if available. |
| 49 | + * |
| 50 | + * @return The virtual thread per task executor if available wrapped into an {@link Optional}, or |
| 51 | + * {@link Optional#empty()} otherwise. |
| 52 | + */ |
| 53 | + public static Optional<ExecutorService> newVirtualThreadPerTaskExecutor() { |
| 54 | + if (MH_NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR == null) { |
| 55 | + return Optional.empty(); |
| 56 | + } else { |
| 57 | + try { |
| 58 | + ExecutorService executorService = |
| 59 | + (ExecutorService) MH_NEW_VIRTUAL_THREAD_PER_TASK_EXECUTOR.invoke(); |
| 60 | + return Optional.of(executorService); |
| 61 | + } catch (Throwable e) { |
| 62 | + return Optional.empty(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static MethodHandle findIsVirtualMethodHandle() { |
| 68 | + if (JavaVersion.getRuntimeVersion().isAtLeast(21, 0, 0)) { |
| 69 | + try { |
| 70 | + return MethodHandles.lookup() |
| 71 | + .findVirtual(Thread.class, "isVirtual", MethodType.methodType(boolean.class)); |
| 72 | + } catch (Throwable ignored) { |
| 73 | + } |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + private static MethodHandle findNewVirtualThreadPerTaskExecutorMethodHandle() { |
| 79 | + if (JavaVersion.getRuntimeVersion().isAtLeast(21, 0, 0)) { |
| 80 | + try { |
| 81 | + return MethodHandles.lookup() |
| 82 | + .findStatic( |
| 83 | + Executors.class, |
| 84 | + "newVirtualThreadPerTaskExecutor", |
| 85 | + MethodType.methodType(ExecutorService.class)); |
| 86 | + } catch (Throwable ignored) { |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | +} |
0 commit comments