mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-15 03:41:30 +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() {
|
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;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user