From 894ed3d0c0269fdbbbd518938e3aa3cf7eea89ad Mon Sep 17 00:00:00 2001 From: Dmytro Vyazelenko <696855+vyazelenko@users.noreply.github.com> Date: Mon, 11 Nov 2024 14:59:56 +0100 Subject: [PATCH] [Java] Remove MemoryAccess. --- .../org/agrona/concurrent/MemoryAccess.java | 52 ------------------- .../broadcast/BroadcastReceiver.java | 4 +- .../broadcast/BroadcastTransmitter.java | 5 +- .../ringbuffer/ManyToOneRingBuffer.java | 9 ++-- .../ringbuffer/OneToOneRingBuffer.java | 9 ++-- .../agrona/concurrent/MemoryAccessTest.java | 41 --------------- 6 files changed, 15 insertions(+), 105 deletions(-) delete mode 100644 agrona/src/main/java/org/agrona/concurrent/MemoryAccess.java delete mode 100644 agrona/src/test/java/org/agrona/concurrent/MemoryAccessTest.java diff --git a/agrona/src/main/java/org/agrona/concurrent/MemoryAccess.java b/agrona/src/main/java/org/agrona/concurrent/MemoryAccess.java deleted file mode 100644 index 2f22f8964..000000000 --- a/agrona/src/main/java/org/agrona/concurrent/MemoryAccess.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2014-2024 Real Logic Limited. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.agrona.concurrent; - -import org.agrona.UnsafeApi; - -/** - * Memory access operations which encapsulate the use of Unsafe. - */ -public final class MemoryAccess -{ - private MemoryAccess() - { - } - - /** - * Ensures that loads before the fence will not be reordered with loads and stores after the fence. - */ - public static void acquireFence() - { - UnsafeApi.loadFence(); - } - - /** - * Ensures that loads and stores before the fence will not be reordered with stores after the fence. - */ - public static void releaseFence() - { - UnsafeApi.storeFence(); - } - - /** - * Ensures that loads and stores before the fence will not be reordered with loads and stores after the fence. - */ - public static void fullFence() - { - UnsafeApi.fullFence(); - } -} diff --git a/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastReceiver.java b/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastReceiver.java index cc6e3628a..c7fa9d268 100644 --- a/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastReceiver.java +++ b/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastReceiver.java @@ -17,8 +17,8 @@ import org.agrona.MutableDirectBuffer; import org.agrona.concurrent.AtomicBuffer; -import org.agrona.concurrent.MemoryAccess; +import java.lang.invoke.VarHandle; import java.util.concurrent.atomic.AtomicLong; import static org.agrona.BitUtil.align; @@ -191,7 +191,7 @@ public boolean receiveNext() */ public boolean validate() { - MemoryAccess.acquireFence(); + VarHandle.acquireFence(); return validate(cursor, buffer, capacity); } diff --git a/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastTransmitter.java b/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastTransmitter.java index 8d5d24e40..f427453a8 100644 --- a/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastTransmitter.java +++ b/agrona/src/main/java/org/agrona/concurrent/broadcast/BroadcastTransmitter.java @@ -18,7 +18,8 @@ import org.agrona.BitUtil; import org.agrona.DirectBuffer; import org.agrona.concurrent.AtomicBuffer; -import org.agrona.concurrent.MemoryAccess; + +import java.lang.invoke.VarHandle; import static org.agrona.concurrent.broadcast.BroadcastBufferDescriptor.*; import static org.agrona.concurrent.broadcast.RecordDescriptor.*; @@ -128,7 +129,7 @@ public void transmit(final int msgTypeId, final DirectBuffer srcBuffer, final in private void signalTailIntent(final AtomicBuffer buffer, final long newTail) { buffer.putLongOrdered(tailIntentCountIndex, newTail); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); } private static void insertPaddingRecord(final AtomicBuffer buffer, final int recordOffset, final int length) diff --git a/agrona/src/main/java/org/agrona/concurrent/ringbuffer/ManyToOneRingBuffer.java b/agrona/src/main/java/org/agrona/concurrent/ringbuffer/ManyToOneRingBuffer.java index c665c4015..0e9a71a35 100644 --- a/agrona/src/main/java/org/agrona/concurrent/ringbuffer/ManyToOneRingBuffer.java +++ b/agrona/src/main/java/org/agrona/concurrent/ringbuffer/ManyToOneRingBuffer.java @@ -18,9 +18,10 @@ import org.agrona.DirectBuffer; import org.agrona.concurrent.AtomicBuffer; import org.agrona.concurrent.ControlledMessageHandler; -import org.agrona.concurrent.MemoryAccess; import org.agrona.concurrent.MessageHandler; +import java.lang.invoke.VarHandle; + import static java.lang.Math.max; import static org.agrona.BitUtil.align; import static org.agrona.concurrent.ControlledMessageHandler.Action.*; @@ -97,7 +98,7 @@ public boolean write(final int msgTypeId, final DirectBuffer srcBuffer, final in } buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putBytes(encodedMsgOffset(recordIndex), srcBuffer, offset, length); buffer.putInt(typeOffset(recordIndex), msgTypeId); @@ -124,7 +125,7 @@ public int tryClaim(final int msgTypeId, final int length) } buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putInt(typeOffset(recordIndex), msgTypeId); return encodedMsgOffset(recordIndex); @@ -536,7 +537,7 @@ private int claimCapacity(final AtomicBuffer buffer, final int recordLength) if (0 != padding) { buffer.putIntOrdered(lengthOffset(tailIndex), -padding); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putInt(typeOffset(tailIndex), PADDING_MSG_TYPE_ID); buffer.putIntOrdered(lengthOffset(tailIndex), padding); diff --git a/agrona/src/main/java/org/agrona/concurrent/ringbuffer/OneToOneRingBuffer.java b/agrona/src/main/java/org/agrona/concurrent/ringbuffer/OneToOneRingBuffer.java index 4a110d973..a83bac4ed 100644 --- a/agrona/src/main/java/org/agrona/concurrent/ringbuffer/OneToOneRingBuffer.java +++ b/agrona/src/main/java/org/agrona/concurrent/ringbuffer/OneToOneRingBuffer.java @@ -18,9 +18,10 @@ import org.agrona.DirectBuffer; import org.agrona.concurrent.AtomicBuffer; import org.agrona.concurrent.ControlledMessageHandler; -import org.agrona.concurrent.MemoryAccess; import org.agrona.concurrent.MessageHandler; +import java.lang.invoke.VarHandle; + import static java.lang.Math.max; import static org.agrona.BitUtil.align; import static org.agrona.concurrent.ControlledMessageHandler.Action.*; @@ -100,7 +101,7 @@ public boolean write(final int msgTypeId, final DirectBuffer srcBuffer, final in } buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putBytes(encodedMsgOffset(recordIndex), srcBuffer, offset, length); buffer.putInt(typeOffset(recordIndex), msgTypeId); @@ -127,7 +128,7 @@ public int tryClaim(final int msgTypeId, final int length) } buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putInt(typeOffset(recordIndex), msgTypeId); return encodedMsgOffset(recordIndex); @@ -472,7 +473,7 @@ else if (requiredCapacity > toBufferEndLength) { buffer.putLong(0, 0L); buffer.putIntOrdered(lengthOffset(recordIndex), -padding); - MemoryAccess.releaseFence(); + VarHandle.releaseFence(); buffer.putInt(typeOffset(recordIndex), PADDING_MSG_TYPE_ID); buffer.putIntOrdered(lengthOffset(recordIndex), padding); diff --git a/agrona/src/test/java/org/agrona/concurrent/MemoryAccessTest.java b/agrona/src/test/java/org/agrona/concurrent/MemoryAccessTest.java deleted file mode 100644 index 7e75093ad..000000000 --- a/agrona/src/test/java/org/agrona/concurrent/MemoryAccessTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2014-2024 Real Logic Limited. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.agrona.concurrent; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class MemoryAccessTest -{ - @Test - void acquireFence() - { - MemoryAccess.acquireFence(); - } - - @Test - void releaseFence() - { - MemoryAccess.releaseFence(); - } - - @Test - void fullFence() - { - MemoryAccess.fullFence(); - } -} \ No newline at end of file