2021-12-16 00:15:55 +01:00
|
|
|
package net.minestom.server.timer;
|
|
|
|
|
2022-03-10 16:57:36 +01:00
|
|
|
import it.unimi.dsi.fastutil.HashCommon;
|
2021-12-16 00:15:55 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2022-03-10 16:57:36 +01:00
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.VarHandle;
|
2021-12-16 00:15:55 +01:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2022-03-10 16:57:36 +01:00
|
|
|
final class TaskImpl implements Task {
|
|
|
|
private static final VarHandle PARKED;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
PARKED = MethodHandles.lookup().findVarHandle(TaskImpl.class, "parked", boolean.class);
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final int id;
|
|
|
|
private final @NotNull Supplier<TaskSchedule> task;
|
|
|
|
private final @NotNull ExecutionType executionType;
|
|
|
|
private final @NotNull SchedulerImpl owner;
|
|
|
|
|
|
|
|
volatile boolean alive;
|
|
|
|
volatile boolean parked;
|
|
|
|
|
|
|
|
TaskImpl(int id,
|
|
|
|
@NotNull Supplier<TaskSchedule> task,
|
|
|
|
@NotNull ExecutionType executionType,
|
|
|
|
@NotNull SchedulerImpl owner) {
|
|
|
|
this.id = id;
|
|
|
|
this.task = task;
|
|
|
|
this.executionType = executionType;
|
|
|
|
this.owner = owner;
|
|
|
|
this.alive = true;
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:15:55 +01:00
|
|
|
@Override
|
|
|
|
public void unpark() {
|
|
|
|
this.owner.unparkTask(this);
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:57:36 +01:00
|
|
|
boolean tryUnpark() {
|
|
|
|
return PARKED.compareAndSet(this, true, false);
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:15:55 +01:00
|
|
|
@Override
|
|
|
|
public boolean isParked() {
|
2022-03-10 16:57:36 +01:00
|
|
|
return parked;
|
2021-12-16 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void cancel() {
|
2022-03-10 16:57:36 +01:00
|
|
|
this.alive = false;
|
2021-12-16 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isAlive() {
|
2022-03-10 16:57:36 +01:00
|
|
|
return alive;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int id() {
|
|
|
|
return id;
|
2021-12-16 00:15:55 +01:00
|
|
|
}
|
2022-03-10 16:57:36 +01:00
|
|
|
|
|
|
|
public @NotNull Supplier<TaskSchedule> task() {
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @NotNull ExecutionType executionType() {
|
|
|
|
return executionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @NotNull SchedulerImpl owner() {
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj == this) return true;
|
|
|
|
if (obj == null || obj.getClass() != this.getClass()) return false;
|
|
|
|
var that = (TaskImpl) obj;
|
|
|
|
return this.id == that.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return HashCommon.murmurHash3(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "TaskImpl[" +
|
|
|
|
"id=" + id + ", " +
|
|
|
|
"task=" + task + ", " +
|
|
|
|
"executionType=" + executionType + ", " +
|
|
|
|
"owner=" + owner + ']';
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:15:55 +01:00
|
|
|
}
|