Skip to content

Commit

Permalink
Added opaque methods to Position (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
pveentjer authored Feb 3, 2025
1 parent 265594d commit c1f9314
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,22 @@ public long getVolatile()
return value.get();
}

@Override
/**
* {@inheritDoc}
*/
public long getAcquire()
{
return value.getAcquire();
}

/**
* {@inheritDoc}
*/
public long getOpaque()
{
return value.getOpaque();
}

/**
* {@inheritDoc}
*/
Expand All @@ -102,6 +112,14 @@ public void set(final long value)
this.value.setPlain(value);
}

/**
* {@inheritDoc}
*/
public void setOpaque(final long value)
{
this.value.setOpaque(value);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -158,6 +176,22 @@ public boolean proposeMaxRelease(final long proposedValue)
return updated;
}

/**
* {@inheritDoc}
*/
public boolean proposeMaxOpaque(final long proposedValue)
{
boolean updated = false;

if (value.get() < proposedValue)
{
value.setOpaque(proposedValue);
updated = true;
}

return updated;
}

/**
* {@inheritDoc}
*/
Expand Down
37 changes: 27 additions & 10 deletions agrona/src/main/java/org/agrona/concurrent/status/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Reports on how far through a buffer some component has progressed.
* <p>
* Threadsafe to write to from a single writer.
* Threadsafe to write to from a single writer unless methods with plain memory semantics are used.
*/
public abstract class Position extends ReadablePosition
{
Expand All @@ -36,19 +36,19 @@ public Position()
*/
public abstract boolean isClosed();

/**
* Get the current position of a component with plain memory ordering semantics.
/**
* Get the current position of a component with plain memory semantics.
*
* @return the current position of a component
*/
public abstract long get();

/**
* Sets the current position of the component without memory ordering semantics.
* Sets the current position of the component with volatile memory semantics.
*
* @param value the current position of the component.
*/
public abstract void set(long value);
public abstract void setVolatile(long value);

/**
* Sets the current position of the component with ordered memory semantics.
Expand All @@ -68,22 +68,30 @@ public Position()
public abstract void setRelease(long value);

/**
* Sets the current position of the component with volatile memory semantics.
* Sets the current position of the component with opaque memory semantics.
*
* @param value the current position of the component.
* @since 2.1.0
*/
public abstract void setVolatile(long value);
public abstract void setOpaque(long value);

/**
* Sets the current position of the component plain memory semantics.
*
* @param value the current position of the component.
*/
public abstract void set(long value);

/**
* Set the position to a new proposedValue if greater than the current value without memory ordering semantics.
* Set the position to a new proposedValue if greater than the current value with plain memory semantics.
*
* @param proposedValue for the new max.
* @return true if a new max as been set otherwise false.
*/
public abstract boolean proposeMax(long proposedValue);

/**
* Set the position to the new proposedValue if greater than the current value with memory ordering semantics.
* Set the position to the new proposedValue if greater than the current value with release memory semantics.
* <p>
* This method is identical to {@link #proposeMaxRelease(long)} and that method should be preferred instead.
*
Expand All @@ -93,11 +101,20 @@ public Position()
public abstract boolean proposeMaxOrdered(long proposedValue);

/**
* Set the position to the new proposedValue if greater than the current value with release memory ordering
* Set the position to the new proposedValue if greater than the current value with release memory
* semantics.
*
* @param proposedValue for the new max.
* @return true if a new max as been set otherwise false.
*/
public abstract boolean proposeMaxRelease(long proposedValue);

/**
* Set the position to the new proposedValue if greater than the current value with opaque memory
* semantics.
*
* @param proposedValue for the new max.
* @return true if a new max as been set otherwise false.
*/
public abstract boolean proposeMaxOpaque(long proposedValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,28 @@ public ReadablePosition()
public abstract int id();

/**
* Get the current position of a component with volatile semantics.
* Get the current position of a component with volatile memory semantics.
*
* @return the current position of a component with volatile semantics.
* @return the current position of a component.
*/
public abstract long getVolatile();

/**
* Get the current position of a component with acquire semantics.
* Get the current position of a component with acquire memory semantics.
*
* @return the current position of a component.
* @since 2.1.0
*/
public abstract long getAcquire();

/**
* Get the current position of a component with opaque memory semantics.
*
* @return the current position of a component
* @since 2.1.0
*/
public abstract long getOpaque();

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,41 @@ public int id()
/**
* {@inheritDoc}
*/
public long get()
public long getVolatile()
{
return UnsafeApi.getLong(byteArray, addressOffset);
return UnsafeApi.getLongVolatile(byteArray, addressOffset);
}

/**
* {@inheritDoc}
*/
public long getVolatile()
public long getAcquire()
{
return UnsafeApi.getLongVolatile(byteArray, addressOffset);
return UnsafeApi.getLongAcquire(byteArray, addressOffset);
}

/**
* {@inheritDoc}
*/
public long getAcquire()
public long getOpaque()
{
return UnsafeApi.getLongAcquire(byteArray, addressOffset);
return UnsafeApi.getLongOpaque(byteArray, addressOffset);
}

/**
* {@inheritDoc}
*/
public void set(final long value)
public long get()
{
UnsafeApi.putLong(byteArray, addressOffset, value);
return UnsafeApi.getLong(byteArray, addressOffset);
}

/**
* {@inheritDoc}
*/
public void setVolatile(final long value)
{
UnsafeApi.putLongVolatile(byteArray, addressOffset, value);
}

/**
Expand All @@ -134,11 +142,20 @@ public void setRelease(final long value)
/**
* {@inheritDoc}
*/
public void setVolatile(final long value)
public void setOpaque(final long value)
{
UnsafeApi.putLongVolatile(byteArray, addressOffset, value);
UnsafeApi.putLongOpaque(byteArray, addressOffset, value);
}

/**
* {@inheritDoc}
*/
public void set(final long value)
{
UnsafeApi.putLong(byteArray, addressOffset, value);
}


/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -183,6 +200,24 @@ public boolean proposeMaxRelease(final long proposedValue)
return updated;
}

/**
* {@inheritDoc}
*/
public boolean proposeMaxOpaque(final long proposedValue)
{
boolean updated = false;

final byte[] array = byteArray;
final long offset = addressOffset;
if (UnsafeApi.getLong(array, offset) < proposedValue)
{
UnsafeApi.putLongOpaque(array, offset, proposedValue);
updated = true;
}

return updated;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit c1f9314

Please sign in to comment.