acquire/release

This commit is contained in:
themode 2022-01-02 05:43:21 +01:00 committed by TheMode
parent 75b54e6302
commit 204089d53d
2 changed files with 7 additions and 7 deletions

View File

@ -43,12 +43,12 @@ public final class CachedPacket implements SendablePacket {
} }
public void invalidate() { public void invalidate() {
PACKET.setOpaque(this, null); PACKET.setRelease(this, null);
} }
public @NotNull ServerPacket packet() { public @NotNull ServerPacket packet() {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getOpaque(this); SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getAcquire(this);
FramedPacket cache; FramedPacket cache;
if (ref != null && (cache = ref.get()) != null) if (ref != null && (cache = ref.get()) != null)
return cache.packet(); // Avoid potential packet allocation return cache.packet(); // Avoid potential packet allocation
@ -64,11 +64,11 @@ public final class CachedPacket implements SendablePacket {
if (!PacketUtils.CACHED_PACKET) if (!PacketUtils.CACHED_PACKET)
return null; return null;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getOpaque(this); SoftReference<FramedPacket> ref = (SoftReference<FramedPacket>) PACKET.getAcquire(this);
FramedPacket cache; FramedPacket cache;
if (ref == null || (cache = ref.get()) == null) { if (ref == null || (cache = ref.get()) == null) {
cache = PacketUtils.allocateTrimmedPacket(packetSupplier.get()); cache = PacketUtils.allocateTrimmedPacket(packetSupplier.get());
PACKET.setOpaque(this, new SoftReference<>(cache)); PACKET.setRelease(this, new SoftReference<>(cache));
} }
return cache; return cache;
} }

View File

@ -33,13 +33,13 @@ public final class LazyPacket implements SendablePacket {
} }
public @NotNull ServerPacket packet() { public @NotNull ServerPacket packet() {
ServerPacket packet = (ServerPacket) PACKET.getOpaque(this); ServerPacket packet = (ServerPacket) PACKET.getAcquire(this);
if (packet == null) { if (packet == null) {
synchronized (this) { synchronized (this) {
packet = this.packet; packet = (ServerPacket) PACKET.getAcquire(this);
if (packet == null) { if (packet == null) {
packet = packetSupplier.get(); packet = packetSupplier.get();
PACKET.setOpaque(this, packet); PACKET.setRelease(this, packet);
} }
} }
} }