Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AtomicBuffer opaque methods #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions agrona/src/main/java/org/agrona/concurrent/AtomicBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ public interface AtomicBuffer extends MutableDirectBuffer
*/
long addLongOrdered(int index, long increment);

/**
* Atomically put a value to a given index with opaque semantics.
*
* @param index in bytes for where to put.
* @param value for at a given index.
*/
void putLongOpaque(int index, long value);

/**
* Atomically get a value to a given index with opaque semantics.
*
* @param index in bytes for where to put.
* @return the value for at a given index.
*/
long getLongOpaque(int index);

/**
* Adds a value to a given index with opaque semantics. The read and write
* will be atomic, but the combination is not atomic. So don't use this
* method concurrently because you can run into lost updates due to
* a race-condition.
*
* @param index in bytes for where to put.
* @param increment by which the value at the index will be adjusted.
* @return the previous value at the index.
*/
long addLongOpaque(int index, long increment);

/**
* Atomically adds a value to a given index with ordered store semantics. Use a negative increment to decrement.
* <p>
Expand Down Expand Up @@ -270,6 +298,34 @@ public interface AtomicBuffer extends MutableDirectBuffer
*/
int addIntRelease(int index, int increment);

/**
* Atomically put a value to a given index with opaque semantics.
*
* @param index in bytes for where to put.
* @param value for at a given index.
*/
void putIntOpaque(int index, int value);

/**
* Atomically get a value to a given index with opaque semantics.
*
* @param index in bytes for where to put.
* @return the value for at a given index.
*/
int getIntOpaque(int index);

/**
* Adds a value to a given index with opaque semantics. The read and write
* will be atomic, but the combination is not atomic. So don't use this
* method concurrently because you can run into lost updates due to
* a race-condition.
*
* @param index in bytes for where to put.
* @param increment by which the value at the index will be adjusted.
* @return the previous value at the index.
*/
int addIntOpaque(int index, int increment);

/**
* Atomic compare and set of an int given an expected value.
* <p>
Expand Down
85 changes: 85 additions & 0 deletions agrona/src/main/java/org/agrona/concurrent/UnsafeBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,48 @@ public long addLongRelease(final int index, final long increment)
return UnsafeApi.getAndAddLongRelease(byteArray, addressOffset + index, increment);
}

/**
* {@inheritDoc}
*/
public void putLongOpaque(final int index, final long value)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

UnsafeApi.putLongOpaque(byteArray, addressOffset + index, value);
}

/**
* {@inheritDoc}
*/
public long getLongOpaque(final int index)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

return UnsafeApi.getLongOpaque(byteArray, addressOffset + index);
}

/**
* {@inheritDoc}
*/
public long addLongOpaque(final int index, final long increment)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

final long oldValue = UnsafeApi.getLongOpaque(byteArray, addressOffset + index);
final long newValue = oldValue + increment;
UnsafeApi.putLongOpaque(byteArray, addressOffset + index, newValue);
return oldValue;
}

vyazelenko marked this conversation as resolved.
Show resolved Hide resolved
/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -573,6 +615,49 @@ public int addIntRelease(final int index, final int increment)
return UnsafeApi.getAndAddIntRelease(byteArray, addressOffset + index, increment);
}


/**
* {@inheritDoc}
*/
public void putIntOpaque(final int index, final int value)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

UnsafeApi.putIntOpaque(byteArray, addressOffset + index, value);
}

/**
* {@inheritDoc}
*/
public int getIntOpaque(final int index)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

return UnsafeApi.getIntOpaque(byteArray, addressOffset + index);
}

/**
* {@inheritDoc}
*/
public int addIntOpaque(final int index, final int increment)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
}

final int oldValue = UnsafeApi.getIntOpaque(byteArray, addressOffset + index);
final int newValue = oldValue + increment;
UnsafeApi.putIntOpaque(byteArray, addressOffset + index, newValue);
return oldValue;
}

vyazelenko marked this conversation as resolved.
Show resolved Hide resolved
/**
* {@inheritDoc}
*/
Expand Down