Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions vertx-core/src/main/java/io/vertx/core/impl/VertxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.ref.Cleaner;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
Expand All @@ -107,12 +108,26 @@ public class VertxImpl implements VertxInternal, MetricsProvider {
// https://github.com/eclipse-vertx/vert.x/issues/4611

private static final Logger log = LoggerFactory.getLogger(VertxImpl.class);
private static final VarHandle INTERNAL_TIMER_HANDLER_DISPOSED;

static {
try {
INTERNAL_TIMER_HANDLER_DISPOSED = MethodHandles.lookup()
.findVarHandle(InternalTimerHandler.class, "disposed", boolean.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}

static final Object[] EMPTY_CONTEXT_LOCALS = new Object[0];
private static final String CLUSTER_MAP_NAME = "__vertx.haInfo";
private static final String NETTY_IO_RATIO_PROPERTY_NAME = "vertx.nettyIORatio";
private static final int NETTY_IO_RATIO = Integer.getInteger(NETTY_IO_RATIO_PROPERTY_NAME, 50);

private static boolean disposedCAS(InternalTimerHandler handler) {
return INTERNAL_TIMER_HANDLER_DISPOSED.compareAndSet(handler, false, true);
}

// Not cached for graalvm
private static ThreadFactory virtualThreadFactory() {
try {
Expand Down Expand Up @@ -1063,7 +1078,7 @@ class InternalTimerHandler implements Handler<Void>, Closeable, Runnable {
private final boolean periodic;
private final long id;
private final ContextInternal context;
private final AtomicBoolean disposed = new AtomicBoolean();
private volatile boolean disposed;
private volatile java.util.concurrent.Future<?> future;

InternalTimerHandler(long id, Handler<Long> runnable, boolean periodic, ContextInternal context) {
Expand All @@ -1084,10 +1099,10 @@ public void run() {

public void handle(Void v) {
if (periodic) {
if (!disposed.get()) {
if (!disposed) {
handler.handle(id);
}
} else if (disposed.compareAndSet(false, true)) {
} else if (disposedCAS(this)) {
timeouts.remove(id);
try {
handler.handle(id);
Expand All @@ -1109,7 +1124,7 @@ private boolean cancel() {
}

private boolean tryCancel() {
if (disposed.compareAndSet(false, true)) {
if (disposedCAS(this)) {
timeouts.remove(id);
future.cancel(false);
return true;
Expand Down
Loading