|
| 1 | +package org.jboss.threads.virtual; |
| 2 | + |
| 3 | +import java.lang.invoke.MethodHandle; |
| 4 | +import java.lang.invoke.MethodHandles; |
| 5 | +import java.lang.invoke.MethodType; |
| 6 | +import java.lang.reflect.UndeclaredThrowableException; |
| 7 | +import java.util.concurrent.Executor; |
| 8 | +import java.util.concurrent.ScheduledExecutorService; |
| 9 | + |
| 10 | +import jdk.internal.vm.ThreadContainer; |
| 11 | + |
| 12 | +/** |
| 13 | + * Access methods for virtual thread internals. |
| 14 | + */ |
| 15 | +final class Access { |
| 16 | + private static final MethodHandle currentCarrierThread; |
| 17 | + private static final MethodHandle virtualThreadFactory; |
| 18 | + private static final MethodHandle threadStartWithContainer; |
| 19 | + private static final MethodHandle schedulerGetter; |
| 20 | + private static final MethodHandle continuationGetter; |
| 21 | + |
| 22 | + static { |
| 23 | + MethodHandle ct; |
| 24 | + MethodHandle vtf; |
| 25 | + MethodHandle tswc; |
| 26 | + MethodHandle sg; |
| 27 | + MethodHandle cg; |
| 28 | + try { |
| 29 | + MethodHandles.Lookup thr = MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()); |
| 30 | + ct = thr.findStatic(Thread.class, "currentCarrierThread", MethodType.methodType(Thread.class)); |
| 31 | + Class<?> vtbClass = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder", false, null); |
| 32 | + try { |
| 33 | + vtf = thr.findConstructor(vtbClass, MethodType.methodType(void.class, Executor.class)); |
| 34 | + } catch (NoSuchMethodError ignored) { |
| 35 | + // patched JDK |
| 36 | + vtf = thr.findConstructor(vtbClass, MethodType.methodType(void.class, ScheduledExecutorService.class)); |
| 37 | + } |
| 38 | + // create efficient transformer |
| 39 | + vtf = vtf.asType(MethodType.methodType(Thread.Builder.OfVirtual.class, ThreadScheduler.class)); |
| 40 | + // todo: maybe instead, we can directly call `java.lang.ThreadBuilders.newVirtualThread` |
| 41 | + //void start(jdk.internal.vm.ThreadContainer container) |
| 42 | + tswc = thr.findVirtual(Thread.class, "start", MethodType.methodType(void.class, ThreadContainer.class)); |
| 43 | + Class<?> vtc = thr.findClass("java.lang.VirtualThread"); |
| 44 | + MethodHandles.Lookup vthr = MethodHandles.privateLookupIn(vtc, MethodHandles.lookup()); |
| 45 | + sg = vthr.findGetter(vtc, "scheduler", Executor.class).asType(MethodType.methodType(Executor.class, Thread.class)); |
| 46 | + cg = vthr.findGetter(vtc, "runContinuation", Runnable.class).asType(MethodType.methodType(Runnable.class, Thread.class)); |
| 47 | + } catch (Throwable e) { |
| 48 | + // no good |
| 49 | + throw new InternalError("Cannot initialize virtual threads", e); |
| 50 | + } |
| 51 | + currentCarrierThread = ct; |
| 52 | + virtualThreadFactory = vtf; |
| 53 | + threadStartWithContainer = tswc; |
| 54 | + schedulerGetter = sg; |
| 55 | + continuationGetter = cg; |
| 56 | + } |
| 57 | + |
| 58 | + static Thread currentCarrier() { |
| 59 | + try { |
| 60 | + return (Thread) currentCarrierThread.invokeExact(); |
| 61 | + } catch (RuntimeException | Error e) { |
| 62 | + throw e; |
| 63 | + } catch (Throwable e) { |
| 64 | + throw new UndeclaredThrowableException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + static Thread.Builder.OfVirtual threadBuilder(ThreadScheduler threadScheduler) { |
| 69 | + try { |
| 70 | + return (Thread.Builder.OfVirtual) virtualThreadFactory.invokeExact(threadScheduler); |
| 71 | + } catch (RuntimeException | Error e) { |
| 72 | + throw e; |
| 73 | + } catch (Throwable e) { |
| 74 | + throw new UndeclaredThrowableException(e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static void startThread(Thread thread, ThreadContainer threadContainer) { |
| 79 | + try { |
| 80 | + threadStartWithContainer.invokeExact(thread, threadContainer); |
| 81 | + } catch (RuntimeException | Error e) { |
| 82 | + throw e; |
| 83 | + } catch (Throwable e) { |
| 84 | + throw new UndeclaredThrowableException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static ThreadScheduler schedulerOf(Thread thread) { |
| 89 | + try { |
| 90 | + return (ThreadScheduler) (Executor) schedulerGetter.invokeExact(thread); |
| 91 | + } catch (RuntimeException | Error e) { |
| 92 | + throw e; |
| 93 | + } catch (Throwable e) { |
| 94 | + throw new UndeclaredThrowableException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static Runnable continuationOf(Thread thread) { |
| 99 | + try { |
| 100 | + return (Runnable) continuationGetter.invokeExact(thread); |
| 101 | + } catch (RuntimeException | Error e) { |
| 102 | + throw e; |
| 103 | + } catch (Throwable e) { |
| 104 | + throw new UndeclaredThrowableException(e); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments