Skip to content

Commit

Permalink
AtomicCounter plain methods. (#317)
Browse files Browse the repository at this point in the history
* AtomicCounter plain methods.

There was already a getWeak. A more standard naming is 'normal' or
'plain'. So a new method getPlain has been added. Also setPlain and
'incrementPlain' have been added.

* Added missing @SInCE to setPlain

* Added decrementPlain and added missing @SInCE
  • Loading branch information
pveentjer authored Feb 1, 2025
1 parent b3bff73 commit 5cc328c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,26 @@ public long incrementRelease()
final long offset = addressOffset;
final long currentValue = UnsafeApi.getLong(array, offset);
UnsafeApi.putLongRelease(array, offset, currentValue + 1);
return currentValue;
}

/**
* Increment the counter.
* <p>
* This method is not atomic and this can lead to lost-updates due to race conditions. This load and store
* have plain memory semantics.
* <p>
* The typical use-case for this method is when writer and reader are the same thread.
*
* @return the previous value of the counter
* @since 2.1.0
*/
public long incrementPlain()
{
final byte[] array = byteArray;
final long offset = addressOffset;
final long currentValue = UnsafeApi.getLong(array, offset);
UnsafeApi.putLong(array, offset, currentValue + 1);
return currentValue;
}

Expand Down Expand Up @@ -289,6 +308,27 @@ public long decrementRelease()
return currentValue;
}

/**
* Decrements the counter.
* <p>
* This method is not atomic and this can lead to lost-updates due to race conditions. This load and store
* have plain memory semantics.
* <p>
* The typical use-case for this method is when writer and reader are the same thread.
*
* @return the previous value of the counter
* @since 2.1.0
*/
public long decrementPlain()
{
final byte[] array = byteArray;
final long offset = addressOffset;
final long currentValue = UnsafeApi.getLong(array, offset);
UnsafeApi.putLong(array, offset, currentValue - 1);

return currentValue;
}

/**
* Set the counter with volatile semantics.
*
Expand Down Expand Up @@ -326,10 +366,23 @@ public void setRelease(final long value)

/**
* Set the counter with normal semantics.
* <p>
* This method is identical to {@link #setPlain(long)} and that method should be used instead.
*
* @param value to be set with normal semantics.
*/
public void setWeak(final long value)
{
setPlain(value);
}

/**
* Set the counter value with plain memory semantics.
*
* @param value to be set with normal semantics.
* @since 2.1.0
*/
public void setPlain(final long value)
{
UnsafeApi.putLong(byteArray, addressOffset, value);
}
Expand Down Expand Up @@ -421,6 +474,17 @@ public long get()
* @return the value for the counter.
*/
public long getWeak()
{
return getPlain();
}

/**
* Get the value of the counter using plain memory semantics. This is the same a standard read of a field.
*
* @return the value for the counter.
* @since 2.1.0
*/
public long getPlain()
{
return UnsafeApi.getLong(byteArray, addressOffset);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014-2025 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.status;

import org.agrona.concurrent.AtomicBuffer;
import org.agrona.concurrent.UnsafeBuffer;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AtomicCountersTest
{

@Test
public void testPlain()
{
final AtomicBuffer buffer = new UnsafeBuffer(new byte[8]);
final AtomicCounter counter = new AtomicCounter(buffer, 0);

assertEquals(0, counter.getPlain());

counter.incrementPlain();
assertEquals(1, counter.getPlain());

counter.setPlain(42);
assertEquals(42, counter.getPlain());

counter.decrementPlain();
assertEquals(41, counter.getPlain());
}
}

0 comments on commit 5cc328c

Please sign in to comment.