package io.vertx.core.impl;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.*;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.spi.metrics.Metrics;
import io.vertx.core.spi.metrics.MetricsProvider;
import io.vertx.core.spi.metrics.PoolMetrics;
class WorkerExecutorImpl implements MetricsProvider, WorkerExecutorInternal {
private final VertxInternal vertx;
private final CloseHooks closeHooks;
private final VertxImpl.SharedWorkerPool pool;
private boolean closed;
public WorkerExecutorImpl(VertxInternal vertx, CloseHooks closeHooks, VertxImpl.SharedWorkerPool pool) {
this.vertx = vertx;
this.closeHooks = closeHooks;
this.pool = pool;
}
@Override
public Metrics getMetrics() {
return pool.metrics();
}
@Override
public boolean isMetricsEnabled() {
PoolMetrics metrics = pool.metrics();
return metrics != null;
}
@Override
public Vertx vertx() {
return vertx;
}
@Override
public WorkerPool getPool() {
return pool;
}
@Override
public <T> Future<@Nullable T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered) {
if (closed) {
throw new IllegalStateException("Worker executor closed");
}
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
ContextImpl impl = context instanceof DuplicatedContext ? ((DuplicatedContext)context).delegate : (ContextImpl) context;
return ContextImpl.executeBlocking(context, blockingCodeHandler, pool, ordered ? impl.orderedTasks : null);
}
public synchronized <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> asyncResultHandler) {
Future<T> fut = executeBlocking(blockingCodeHandler, ordered);
if (asyncResultHandler != null) {
fut.onComplete(asyncResultHandler);
}
}
@Override
public Future<Void> close() {
PromiseInternal<Void> promise = vertx.promise();
close(promise);
return promise.future();
}
@Override
public void close(Handler<AsyncResult<Void>> handler) {
close().onComplete(handler);
}
@Override
public void close(Promise<Void> completion) {
synchronized (this) {
if (!closed) {
closed = true;
closeHooks.remove(this);
pool.release();
}
}
completion.complete();
}
}