mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-07 19:31:37 +01:00
acquire/release
This commit is contained in:
parent
75b54e6302
commit
204089d53d
@ -43,12 +43,12 @@ public final class CachedPacket implements SendablePacket {
|
||||
}
|
||||
|
||||
public void invalidate() {
|
||||
PACKET.setOpaque(this, null);
|
||||
PACKET.setRelease(this, null);
|
||||
}
|
||||
|
||||
public @NotNull ServerPacket packet() {
|
||||
@SuppressWarnings("unchecked")
|
||||
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getOpaque(this);
|
||||
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getAcquire(this);
|
||||
FramedPacket cache;
|
||||
if (ref != null && (cache = ref.get()) != null)
|
||||
return cache.packet(); // Avoid potential packet allocation
|
||||
@ -64,11 +64,11 @@ public final class CachedPacket implements SendablePacket {
|
||||
if (!PacketUtils.CACHED_PACKET)
|
||||
return null;
|
||||
@SuppressWarnings("unchecked")
|
||||
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getOpaque(this);
|
||||
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getAcquire(this);
|
||||
FramedPacket cache;
|
||||
if (ref == null || (cache = ref.get()) == null) {
|
||||
cache = PacketUtils.allocateTrimmedPacket(packetSupplier.get());
|
||||
PACKET.setOpaque(this, new SoftReference<>(cache));
|
||||
PACKET.setRelease(this, new SoftReference<>(cache));
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
@ -33,13 +33,13 @@ public final class LazyPacket implements SendablePacket {
|
||||
}
|
||||
|
||||
public @NotNull ServerPacket packet() {
|
||||
ServerPacket packet = (ServerPacket) PACKET.getOpaque(this);
|
||||
ServerPacket packet = (ServerPacket) PACKET.getAcquire(this);
|
||||
if (packet == null) {
|
||||
synchronized (this) {
|
||||
packet = this.packet;
|
||||
packet = (ServerPacket) PACKET.getAcquire(this);
|
||||
if (packet == null) {
|
||||
packet = packetSupplier.get();
|
||||
PACKET.setOpaque(this, packet);
|
||||
PACKET.setRelease(this, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user