Skip to content

Commit 2895215

Browse files
committed
Fix JMH deadlock: use tryPost with backpressure, add missing zRuntime.start(), fix HandlerRegistration leaks
1 parent 61d22e4 commit 2895215

4 files changed

Lines changed: 39 additions & 15 deletions

File tree

‎zthread-benchmark/src/main/java/io/github/namanoncode/zthread/benchmark/inotify/FileWatchBenchmark.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void benchJavaNioWatchService(Blackhole bh) throws InterruptedException,
102102
@OperationsPerInvocation(BATCH_SIZE)
103103
public void benchZThreadInotify(Blackhole bh) throws InterruptedException, IOException {
104104
latch = new CountDownLatch(BATCH_SIZE);
105-
zRuntime.on(FileEvent.class, evt -> {
105+
io.github.namanoncode.zthread.handler.HandlerRegistration reg = zRuntime.on(FileEvent.class, evt -> {
106106
bh.consume(evt);
107107
latch.countDown();
108108
});
@@ -112,6 +112,7 @@ public void benchZThreadInotify(Blackhole bh) throws InterruptedException, IOExc
112112
}
113113

114114
latch.await();
115+
reg.cancel();
115116

116117
// Cleanup for next invocation
117118
for (int i = 0; i < BATCH_SIZE; i++) {

‎zthread-benchmark/src/main/java/io/github/namanoncode/zthread/benchmark/throughput/MpmcEventBenchmark.java‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void setup() {
6161
zRuntimes = new ZRuntime[CONSUMERS];
6262
for (int i = 0; i < CONSUMERS; i++) {
6363
zRuntimes[i] = ZRuntime.builder().build();
64+
zRuntimes[i].start();
6465
}
6566

6667
nettyEventLoopGroup = new DefaultEventLoopGroup(CONSUMERS);
@@ -206,23 +207,29 @@ public void benchConcurrentLinkedQueue(Blackhole bh) throws InterruptedException
206207
@OperationsPerInvocation(BATCH_SIZE)
207208
public void benchZThread(Blackhole bh) throws InterruptedException {
208209
CountDownLatch latch = new CountDownLatch(BATCH_SIZE);
209-
EventHandler<io.github.namanoncode.zthread.event.CustomEvent> handler = evt -> {
210+
io.github.namanoncode.zthread.handler.EventHandler<io.github.namanoncode.zthread.event.CustomEvent> handler = evt -> {
210211
bh.consume(evt);
211212
latch.countDown();
212213
};
214+
java.util.List<io.github.namanoncode.zthread.handler.HandlerRegistration> regs = new java.util.ArrayList<>();
213215
for (ZRuntime zr : zRuntimes) {
214-
zr.on(io.github.namanoncode.zthread.event.CustomEvent.class, handler);
216+
regs.add(zr.on(io.github.namanoncode.zthread.event.CustomEvent.class, handler));
215217
}
216218

217219
AtomicInteger index = new AtomicInteger();
218220
runProducers(() -> {
219221
int pIndex = index.getAndIncrement();
220222
ZRuntime target = zRuntimes[pIndex % CONSUMERS];
221223
for (int i = 0; i < EVENTS_PER_PRODUCER; i++) {
222-
target.post(EVENT);
224+
while (!target.tryPost(EVENT)) {
225+
java.util.concurrent.locks.LockSupport.parkNanos(10);
226+
}
223227
}
224228
});
225229
latch.await();
230+
for (io.github.namanoncode.zthread.handler.HandlerRegistration reg : regs) {
231+
reg.cancel();
232+
}
226233
}
227234

228235
@Benchmark
@@ -270,9 +277,13 @@ public void benchReactor(Blackhole bh) throws InterruptedException {
270277

271278
runProducers(() -> {
272279
for (int i = 0; i < EVENTS_PER_PRODUCER; i++) {
273-
while (reactorSink.tryEmitNext(EVENT) == reactor.core.publisher.Sinks.EmitResult.FAIL_NON_SERIALIZED) {
274-
java.util.concurrent.locks.LockSupport.parkNanos(10);
275-
}
280+
reactor.core.publisher.Sinks.EmitResult res;
281+
do {
282+
res = reactorSink.tryEmitNext(EVENT);
283+
if (res != reactor.core.publisher.Sinks.EmitResult.OK) {
284+
java.util.concurrent.locks.LockSupport.parkNanos(10);
285+
}
286+
} while (res != reactor.core.publisher.Sinks.EmitResult.OK);
276287
}
277288
});
278289
latch.await();

‎zthread-benchmark/src/main/java/io/github/namanoncode/zthread/benchmark/throughput/MpscEventBenchmark.java‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void setup() {
5959
synchronousQueue = new SynchronousQueue<>();
6060

6161
zRuntime = ZRuntime.builder().build();
62+
zRuntime.start();
6263
nettyEventLoop = new DefaultEventLoop();
6364
vertx = io.vertx.core.Vertx.vertx();
6465
virtualThreadExecutor = Executors.newVirtualThreadPerTaskExecutor();
@@ -212,17 +213,20 @@ public void benchConcurrentLinkedQueue(Blackhole bh) throws InterruptedException
212213
@OperationsPerInvocation(BATCH_SIZE)
213214
public void benchZThread(Blackhole bh) throws InterruptedException {
214215
CountDownLatch latch = new CountDownLatch(BATCH_SIZE);
215-
zRuntime.on(io.github.namanoncode.zthread.event.CustomEvent.class, evt -> {
216+
io.github.namanoncode.zthread.handler.HandlerRegistration reg = zRuntime.on(io.github.namanoncode.zthread.event.CustomEvent.class, evt -> {
216217
bh.consume(evt);
217218
latch.countDown();
218219
});
219220

220221
runProducers(() -> {
221222
for (int i = 0; i < EVENTS_PER_PRODUCER; i++) {
222-
zRuntime.post(EVENT);
223+
while (!zRuntime.tryPost(EVENT)) {
224+
java.util.concurrent.locks.LockSupport.parkNanos(10);
225+
}
223226
}
224227
});
225228
latch.await();
229+
reg.cancel();
226230
}
227231

228232
@Benchmark
@@ -270,9 +274,13 @@ public void benchReactor(Blackhole bh) throws InterruptedException {
270274

271275
runProducers(() -> {
272276
for (int i = 0; i < EVENTS_PER_PRODUCER; i++) {
273-
while (reactorSink.tryEmitNext(EVENT) == reactor.core.publisher.Sinks.EmitResult.FAIL_NON_SERIALIZED) {
274-
java.util.concurrent.locks.LockSupport.parkNanos(10);
275-
}
277+
reactor.core.publisher.Sinks.EmitResult res;
278+
do {
279+
res = reactorSink.tryEmitNext(EVENT);
280+
if (res != reactor.core.publisher.Sinks.EmitResult.OK) {
281+
java.util.concurrent.locks.LockSupport.parkNanos(10);
282+
}
283+
} while (res != reactor.core.publisher.Sinks.EmitResult.OK);
276284
}
277285
});
278286
latch.await();

‎zthread-benchmark/src/main/java/io/github/namanoncode/zthread/benchmark/throughput/SpscEventBenchmark.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ public void benchReactor(Blackhole bh) throws InterruptedException {
194194
});
195195

196196
for (int i = 0; i < BATCH_SIZE; i++) {
197-
while (reactorSink.tryEmitNext(EVENT) == reactor.core.publisher.Sinks.EmitResult.FAIL_NON_SERIALIZED) {
198-
java.util.concurrent.locks.LockSupport.parkNanos(10);
199-
}
197+
reactor.core.publisher.Sinks.EmitResult res;
198+
do {
199+
res = reactorSink.tryEmitNext(EVENT);
200+
if (res != reactor.core.publisher.Sinks.EmitResult.OK) {
201+
java.util.concurrent.locks.LockSupport.parkNanos(10);
202+
}
203+
} while (res != reactor.core.publisher.Sinks.EmitResult.OK);
200204
}
201205
latch.await();
202206
}

0 commit comments

Comments
 (0)