-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cask.internal | ||
|
||
import java.util.concurrent.{Executor, ThreadFactory} | ||
|
||
private[cask] final class NewThreadPerTaskExecutor(val threadFactory: ThreadFactory) | ||
extends Executor { | ||
override def execute(command: Runnable): Unit = { | ||
val thread = threadFactory.newThread(command) | ||
thread.start() | ||
if (thread.getState eq Thread.State.TERMINATED) { | ||
throw new IllegalStateException("Thread has already been terminated") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cask.internal | ||
|
||
import io.undertow.server.{HttpHandler, HttpServerExchange} | ||
|
||
private[cask] final class VirtualThreadBlockingHandler(val handler: HttpHandler) | ||
extends HttpHandler { | ||
override def handleRequest(exchange: HttpServerExchange): Unit = { | ||
exchange.startBlocking() | ||
exchange.dispatch(VirtualThreadBlockingHandler.EXECUTOR, handler) | ||
} | ||
} | ||
|
||
private[cask] object VirtualThreadBlockingHandler { | ||
private lazy val EXECUTOR = new NewThreadPerTaskExecutor( | ||
VirtualThreadSupport.create("cask")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cask.internal | ||
|
||
import java.lang.invoke.{MethodHandles, MethodType} | ||
import java.util.concurrent.ThreadFactory | ||
|
||
private[cask] object VirtualThreadSupport { | ||
|
||
/** | ||
* Returns if the current Runtime supports virtual threads. | ||
*/ | ||
lazy val isVirtualThreadSupported: Boolean = create("testIfSupported") ne null | ||
|
||
/** | ||
* Create a virtual thread factory, returns null when failed. | ||
*/ | ||
def create(prefix: String): ThreadFactory = | ||
try { | ||
val builderClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder") | ||
val ofVirtualClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder$OfVirtual") | ||
val lookup = MethodHandles.lookup | ||
val ofVirtualMethod = lookup.findStatic(classOf[Thread], "ofVirtual", MethodType.methodType(ofVirtualClass)) | ||
var builder = ofVirtualMethod.invoke() | ||
val nameMethod = lookup.findVirtual(ofVirtualClass, "name", | ||
MethodType.methodType(ofVirtualClass, classOf[String], classOf[Long])) | ||
val factoryMethod = lookup.findVirtual(builderClass, "factory", MethodType.methodType(classOf[ThreadFactory])) | ||
builder = nameMethod.invoke(builder, prefix + "-virtual-thread-", 0L) | ||
factoryMethod.invoke(builder).asInstanceOf[ThreadFactory] | ||
} catch { | ||
case _: Throwable => null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters