mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-07 19:31:37 +01:00
Opaque ordering for LazyPacket
This commit is contained in:
parent
10d40dd19d
commit
b79054f8e8
@ -31,6 +31,7 @@ public final class CachedPacket implements SendablePacket {
|
||||
}
|
||||
|
||||
private final Supplier<ServerPacket> packetSupplier;
|
||||
@SuppressWarnings("unused")
|
||||
private SoftReference<FramedPacket> packet;
|
||||
|
||||
public CachedPacket(@NotNull Supplier<@NotNull ServerPacket> packetSupplier) {
|
||||
|
@ -3,6 +3,8 @@ package net.minestom.server.network.packet.server;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@ -12,20 +14,32 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public final class LazyPacket implements SendablePacket {
|
||||
private static final VarHandle PACKET;
|
||||
|
||||
static {
|
||||
try {
|
||||
PACKET = MethodHandles.lookup().findVarHandle(LazyPacket.class, "packet", ServerPacket.class);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private final Supplier<ServerPacket> packetSupplier;
|
||||
private volatile ServerPacket packet;
|
||||
@SuppressWarnings("unused")
|
||||
private ServerPacket packet;
|
||||
|
||||
public LazyPacket(@NotNull Supplier<@NotNull ServerPacket> packetSupplier) {
|
||||
this.packetSupplier = packetSupplier;
|
||||
}
|
||||
|
||||
public @NotNull ServerPacket packet() {
|
||||
ServerPacket packet = this.packet;
|
||||
ServerPacket packet = (ServerPacket) PACKET.getOpaque(this);
|
||||
if (packet == null) {
|
||||
synchronized (this) {
|
||||
packet = this.packet;
|
||||
if (packet == null) {
|
||||
packet = this.packet = packetSupplier.get();
|
||||
packet = packetSupplier.get();
|
||||
PACKET.setOpaque(this, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user