Skip to content

Commit aecb1f4

Browse files
committed
Add VIRTUAL_NETTY support: ManualIoEventLoopTask, VirtualMultithreadManualIoEventLoopGroup
1 parent 34ae521 commit aecb1f4

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2026 The Netty VirtualThread Scheduler Project
3+
*
4+
* The Netty VirtualThread Scheduler Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the
11+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
* either express or implied. See the License for the specific language governing permissions
13+
* and limitations under the License.
14+
*/
15+
package io.netty.loom;
16+
17+
import io.netty.channel.IoEventLoopGroup;
18+
import io.netty.channel.IoHandlerFactory;
19+
import io.netty.channel.ManualIoEventLoop;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
public class ManualIoEventLoopTask extends ManualIoEventLoop implements Runnable {
24+
25+
private static final long RUNNING_YIELD_US = TimeUnit.MICROSECONDS
26+
.toNanos(Integer.getInteger("io.netty.loom.running.yield.us", 1));
27+
28+
public ManualIoEventLoopTask(IoEventLoopGroup parent, Thread owningThread, IoHandlerFactory factory) {
29+
super(parent, owningThread, factory);
30+
}
31+
32+
@Override
33+
public void run() {
34+
while (!isShuttingDown()) {
35+
run(0, RUNNING_YIELD_US);
36+
Thread.yield();
37+
runNonBlockingTasks(RUNNING_YIELD_US);
38+
Thread.yield();
39+
}
40+
while (!isTerminated()) {
41+
runNow();
42+
Thread.yield();
43+
}
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2026 The Netty VirtualThread Scheduler Project
3+
*
4+
* The Netty VirtualThread Scheduler Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the
11+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
* either express or implied. See the License for the specific language governing permissions
13+
* and limitations under the License.
14+
*/
15+
package io.netty.loom;
16+
17+
import io.netty.channel.IoEventLoop;
18+
import io.netty.channel.IoHandlerFactory;
19+
import io.netty.channel.MultiThreadIoEventLoopGroup;
20+
21+
import java.util.concurrent.Executor;
22+
import java.util.concurrent.ThreadFactory;
23+
24+
public class VirtualMultithreadManualIoEventLoopGroup extends MultiThreadIoEventLoopGroup {
25+
26+
private ThreadFactory threadFactory;
27+
28+
public VirtualMultithreadManualIoEventLoopGroup(int nThreads, IoHandlerFactory factory) {
29+
super(nThreads, (Executor) null, factory);
30+
}
31+
32+
@Override
33+
protected IoEventLoop newChild(Executor executor, IoHandlerFactory ioHandlerFactory, Object... args) {
34+
if (threadFactory == null) {
35+
threadFactory = Thread.ofVirtual().factory();
36+
}
37+
var manualTask = new ManualIoEventLoopTask(this, null, ioHandlerFactory);
38+
var newThread = threadFactory.newThread(manualTask);
39+
manualTask.setOwningThread(newThread);
40+
newThread.start();
41+
return manualTask;
42+
}
43+
}

0 commit comments

Comments
 (0)