forked from dragonwell-project/dragonwell8_jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Wisp] Thread-based asynchronous IO implementation
Summary: Use ThreadPool as asynchronous IO delegate for asynchronous IO implementation. The feature can be used in Wisp flow and non-Wisp flow. Test Plan: test/com/alibaba/wisp/io/ThreadPoolAIOTest.java Reviewed-by: leiyu, zhengxiaolinX Issue: dragonwell-project/dragonwell8#213 thread local judge
- Loading branch information
joeyleeeeeee97
authored and
joeylee.lz
committed
Apr 25, 2021
1 parent
2ee14c2
commit 6515bdb
Showing
18 changed files
with
535 additions
and
25 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
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
82 changes: 82 additions & 0 deletions
82
src/share/classes/com/alibaba/wisp/engine/WispAIOSupporter.java
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,82 @@ | ||
package com.alibaba.wisp.engine; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
enum WispAIOSupporter { | ||
INSTANCE; | ||
|
||
private ExecutorService executor; | ||
|
||
private ThreadGroup threadgroup; | ||
|
||
WispAIOSupporter() { | ||
} | ||
|
||
void startDaemon(ThreadGroup g) { | ||
threadgroup = g; | ||
ThreadPoolExecutor workPool; | ||
workPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), | ||
Runtime.getRuntime().availableProcessors(), Long.MAX_VALUE, | ||
TimeUnit.SECONDS, new LinkedBlockingDeque<>(), new AIOThreadPoolFactory()); | ||
workPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
this.executor = workPool; | ||
WispAsyncIO.wispAIOLoaded = true; | ||
} | ||
|
||
public <T> T invokeIOTask(Callable<T> command) throws IOException { | ||
Future<T> future; | ||
try { | ||
future = submitIOTask(command); | ||
} catch (RejectedExecutionException e) { | ||
throw new IOException("busy", e); | ||
} | ||
T result; | ||
while (true) { | ||
try { | ||
result = future.get(); | ||
return result; | ||
} catch (ExecutionException e) { | ||
Throwable cause = e.getCause(); | ||
Class<?> causeClass = cause.getClass(); | ||
if (IOException.class.isAssignableFrom(causeClass)) { | ||
throw (IOException) cause; | ||
} else if (RuntimeException.class.isAssignableFrom(causeClass)) { | ||
throw (RuntimeException) cause; | ||
} else if (Error.class.isAssignableFrom(causeClass)) { | ||
throw (Error) cause; | ||
} else { | ||
throw new Error(e); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.interrupted(); | ||
} | ||
} | ||
} | ||
|
||
<T> Future<T> submitIOTask(Callable<T> command) { | ||
return executor.submit(command); | ||
} | ||
|
||
private class AIOThreadPoolFactory implements ThreadFactory { | ||
private final AtomicInteger threadNumber = new AtomicInteger(1); | ||
private final static String namePrefix = "AIO-worker-thread-"; | ||
|
||
AIOThreadPoolFactory() { | ||
} | ||
|
||
@Override | ||
public Thread newThread(Runnable r) { | ||
Thread t; | ||
if (threadgroup != null) { | ||
t = new Thread(threadgroup, r, namePrefix + threadNumber.getAndIncrement()); | ||
} else { | ||
t = new Thread(r, namePrefix + threadNumber.getAndIncrement()); | ||
} | ||
t.setDaemon(true); | ||
return t; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/share/classes/com/alibaba/wisp/engine/WispAsyncIO.java
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,63 @@ | ||
package com.alibaba.wisp.engine; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.Callable; | ||
import java.util.Properties; | ||
|
||
import sun.misc.SharedSecrets; | ||
import sun.misc.WispAsyncIOAccess; | ||
|
||
class WispAsyncIO { | ||
// AIO | ||
static boolean wispAIOLoaded = false; | ||
|
||
static { | ||
Properties p = java.security.AccessController.doPrivileged( | ||
new java.security.PrivilegedAction<Properties>() { | ||
public Properties run() { | ||
return System.getProperties(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
static void setWispAsyncIOAccess() { | ||
// initialize TenantAccess | ||
if (SharedSecrets.getWispAsyncIOAccess() == null) { | ||
SharedSecrets.setWispAsyncIOAccess(new WispAsyncIOAccess() { | ||
@Override | ||
public boolean usingAsyncIO() { | ||
return wispAIOLoaded; | ||
} | ||
|
||
@Override | ||
public <T> T executeAsyncIO(Callable<T> command) throws IOException { | ||
return WispAIOSupporter.INSTANCE.invokeIOTask(command); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static boolean useAsyncIO() { | ||
return SharedSecrets.getWispAsyncIOAccess() != null | ||
&& SharedSecrets.getWispAsyncIOAccess().usingAsyncIO() | ||
&& SharedSecrets.getWispEngineAccess() != null | ||
&& SharedSecrets.getWispEngineAccess().runningAsCoroutine(Thread.currentThread()); | ||
} | ||
|
||
/* | ||
* Initialize the WispAsyncIO class, called after System.initializeSystemClass by VM. | ||
**/ | ||
static void initializeAsyncIOClass() { | ||
try { | ||
Class.forName(WispAIOSupporter.class.getName()); | ||
} catch (Exception e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
static void startAsyncIODaemon() { | ||
WispAIOSupporter.INSTANCE.startDaemon(WispEngine.DAEMON_THREAD_GROUP); | ||
setWispAsyncIOAccess(); | ||
} | ||
} |
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
Oops, something went wrong.