Java Code Examples for org.agrona.concurrent.AtomicBuffer#getLongVolatile()

The following examples show how to use org.agrona.concurrent.AtomicBuffer#getLongVolatile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
private void testAlignedAtomicMethods(final AtomicBuffer buffer, final int offset)
{
    buffer.getLongVolatile(offset + SIZE_OF_LONG);
    buffer.putLongVolatile(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.compareAndSetLong(offset + SIZE_OF_LONG, Long.MAX_VALUE, Long.MAX_VALUE);
    buffer.getAndAddLong(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.getAndSetLong(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.putLongOrdered(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.addLongOrdered(offset + SIZE_OF_LONG, Long.MAX_VALUE);

    buffer.getIntVolatile(offset + SIZE_OF_INT);
    buffer.putIntVolatile(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.compareAndSetInt(offset + SIZE_OF_INT, Integer.MAX_VALUE, Integer.MAX_VALUE);
    buffer.getAndAddInt(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.getAndSetInt(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.putIntOrdered(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.addIntOrdered(offset + SIZE_OF_INT, Integer.MAX_VALUE);

    buffer.getShortVolatile(offset + SIZE_OF_SHORT);
    buffer.putShortVolatile(offset + SIZE_OF_SHORT, Short.MAX_VALUE);
    buffer.getCharVolatile(offset + SIZE_OF_CHAR);
    buffer.putCharVolatile(offset + SIZE_OF_CHAR, Character.MAX_VALUE);
    buffer.getByteVolatile(offset + SIZE_OF_BYTE);
    buffer.putByteVolatile(offset + SIZE_OF_BYTE, Byte.MAX_VALUE);
}
 
Example 2
Source File: BroadcastReceiver.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a new broadcast receiver based on an underlying {@link AtomicBuffer}.
 * The underlying buffer must a power of 2 in size plus sufficient space
 * for the {@link BroadcastBufferDescriptor#TRAILER_LENGTH}.
 *
 * @param buffer via which messages will be exchanged.
 * @throws IllegalStateException if the buffer capacity is not a power of 2
 * plus {@link BroadcastBufferDescriptor#TRAILER_LENGTH} in capacity.
 */
public BroadcastReceiver(final AtomicBuffer buffer)
{
    this.buffer = buffer;
    this.capacity = buffer.capacity() - TRAILER_LENGTH;

    checkCapacity(capacity);
    buffer.verifyAlignment();

    tailIntentCounterIndex = capacity + TAIL_INTENT_COUNTER_OFFSET;
    tailCounterIndex = capacity + TAIL_COUNTER_OFFSET;
    latestCounterIndex = capacity + LATEST_COUNTER_OFFSET;

    cursor = nextRecord = buffer.getLongVolatile(latestCounterIndex);
    recordOffset = (int)cursor & (capacity - 1);
}
 
Example 3
Source File: IndexedPositionReader.java    From artio with Apache License 2.0 5 votes vote down vote up
long indexedPosition(final long recordingId)
{
    final IndexedPositionDecoder decoder = this.decoder;
    final int actingBlockLength = this.actingBlockLength;
    final int actingVersion = this.actingVersion;
    final AtomicBuffer buffer = this.buffer;

    int offset = HEADER_LENGTH;
    while (true)
    {
        offset = sectorFramer.claim(offset, RECORD_LENGTH);
        if (offset == OUT_OF_SPACE)
        {
            return UNKNOWN_POSITION;
        }

        decoder.wrap(buffer, offset, actingBlockLength, actingVersion);
        final long position = buffer.getLongVolatile(offset + POSITION_OFFSET);
        if (position == 0)
        {
            return UNKNOWN_POSITION;
        }
        if (decoder.recordingId() == recordingId)
        {
            return position;
        }

        offset += RECORD_LENGTH;
    }
}
 
Example 4
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
private void testUnAlignedAtomicMethods(final AtomicBuffer buffer, final int offset)
{
    buffer.getLongVolatile(offset); // assert that buffer[offset] is 8-bytes
    // aligned

    assertUnaligned(offset + SIZE_OF_INT, buffer::getLongVolatile);
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.putLongVolatile(i, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.compareAndSetLong(i, Long.MAX_VALUE, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.getAndAddLong(i, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.getAndSetLong(i, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.putLongOrdered(i, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.addLongOrdered(i, Long.MAX_VALUE));

    assertUnaligned(offset + SIZE_OF_SHORT, buffer::getIntVolatile);
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putIntVolatile(i, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT,
        (i) -> buffer.compareAndSetInt(i, Integer.MAX_VALUE, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.getAndAddInt(i, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.getAndSetInt(i, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putIntOrdered(i, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.addIntOrdered(i, Integer.MAX_VALUE));

    assertUnaligned(offset + SIZE_OF_BYTE, buffer::getShortVolatile);
    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putShortVolatile(i, Short.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_BYTE, buffer::getCharVolatile);
    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putCharVolatile(i, Character.MAX_VALUE));
}
 
Example 5
Source File: BroadcastReceiver.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Non-blocking receive of next message from the transmission stream.
 * <p>
 * If loss has occurred then {@link #lappedCount()} will be incremented.
 *
 * @return true if transmission is available with {@link #offset()}, {@link #length()} and {@link #typeId()}
 * set for the next message to be consumed. If no transmission is available then false.
 */
public boolean receiveNext()
{
    boolean isAvailable = false;
    final AtomicBuffer buffer = this.buffer;
    final long tail = buffer.getLongVolatile(tailCounterIndex);
    long cursor = nextRecord;

    if (tail > cursor)
    {
        final int capacity = this.capacity;
        int recordOffset = (int)cursor & (capacity - 1);

        if (!validate(cursor, buffer, capacity))
        {
            lappedCount.lazySet(lappedCount.get() + 1);

            cursor = buffer.getLong(latestCounterIndex);
            recordOffset = (int)cursor & (capacity - 1);
        }

        this.cursor = cursor;
        nextRecord = cursor + align(buffer.getInt(lengthOffset(recordOffset)), RECORD_ALIGNMENT);

        if (PADDING_MSG_TYPE_ID == buffer.getInt(typeOffset(recordOffset)))
        {
            recordOffset = 0;
            this.cursor = nextRecord;
            nextRecord += align(buffer.getInt(lengthOffset(recordOffset)), RECORD_ALIGNMENT);
        }

        this.recordOffset = recordOffset;
        isAvailable = true;
    }

    return isAvailable;
}
 
Example 6
Source File: ErrorLogReader.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Read all the errors in a log since a given timestamp.
 *
 * @param buffer         containing the {@link DistinctErrorLog}.
 * @param consumer       to be called for each exception encountered.
 * @param sinceTimestamp for filtering errors that have been recorded since this time.
 * @return the number of entries that has been read.
 */
public static int read(final AtomicBuffer buffer, final ErrorConsumer consumer, final long sinceTimestamp)
{
    int entries = 0;
    int offset = 0;
    final int capacity = buffer.capacity();

    while (offset < capacity)
    {
        final int length = buffer.getIntVolatile(offset + LENGTH_OFFSET);
        if (0 == length)
        {
            break;
        }

        final long lastObservationTimestamp = buffer.getLongVolatile(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET);
        if (lastObservationTimestamp >= sinceTimestamp)
        {
            ++entries;

            consumer.accept(
                buffer.getInt(offset + OBSERVATION_COUNT_OFFSET),
                buffer.getLong(offset + FIRST_OBSERVATION_TIMESTAMP_OFFSET),
                lastObservationTimestamp,
                buffer.getStringWithoutLengthUtf8(offset + ENCODED_ERROR_OFFSET, length - ENCODED_ERROR_OFFSET));
        }

        offset += align(length, RECORD_ALIGNMENT);
    }

    return entries;
}
 
Example 7
Source File: ReplayIndexDescriptor.java    From artio with Apache License 2.0 4 votes vote down vote up
static long endChangeVolatile(final AtomicBuffer buffer)
{
    return buffer.getLongVolatile(END_CHANGE_OFFSET);
}
 
Example 8
Source File: ReplayIndexDescriptor.java    From artio with Apache License 2.0 4 votes vote down vote up
static long beginChangeVolatile(final AtomicBuffer buffer)
{
    return buffer.getLongVolatile(BEGIN_CHANGE_OFFSET);
}
 
Example 9
Source File: LossReportReader.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Read a {@link LossReport} contained in the buffer. This can be done concurrently.
 *
 * @param buffer        containing the loss report.
 * @param entryConsumer to be called to accept each entry in the report.
 * @return the number of entries read.
 */
public static int read(final AtomicBuffer buffer, final EntryConsumer entryConsumer)
{
    final int capacity = buffer.capacity();

    int recordsRead = 0;
    int offset = 0;

    while (offset < capacity)
    {
        final long observationCount = buffer.getLongVolatile(offset + OBSERVATION_COUNT_OFFSET);
        if (observationCount <= 0)
        {
            break;
        }

        ++recordsRead;

        final String channel = buffer.getStringAscii(offset + CHANNEL_OFFSET);
        final String source = buffer.getStringAscii(
            offset + CHANNEL_OFFSET + BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT));

        entryConsumer.accept(
            observationCount,
            buffer.getLong(offset + TOTAL_BYTES_LOST_OFFSET),
            buffer.getLong(offset + FIRST_OBSERVATION_OFFSET),
            buffer.getLong(offset + LAST_OBSERVATION_OFFSET),
            buffer.getInt(offset + SESSION_ID_OFFSET),
            buffer.getInt(offset + STREAM_ID_OFFSET),
            channel,
            source);

        final int recordLength =
            CHANNEL_OFFSET +
            BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT) +
            SIZE_OF_INT + source.length();
        offset += BitUtil.align(recordLength, ENTRY_ALIGNMENT);
    }

    return recordsRead;
}
 
Example 10
Source File: BroadcastReceiver.java    From agrona with Apache License 2.0 4 votes vote down vote up
private boolean validate(final long cursor, final AtomicBuffer buffer, final int capacity)
{
    return (cursor + capacity) > buffer.getLongVolatile(tailIntentCounterIndex);
}
 
Example 11
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
private int claimCapacity(final AtomicBuffer buffer, final int recordLength)
{
    final int alignedRecordLength = align(recordLength, ALIGNMENT);
    final int requiredCapacity = alignedRecordLength + HEADER_LENGTH;
    final int capacity = this.capacity;
    final int tailPositionIndex = this.tailPositionIndex;
    final int headCachePositionIndex = this.headCachePositionIndex;
    final int mask = capacity - 1;

    long head = buffer.getLong(headCachePositionIndex);
    final long tail = buffer.getLong(tailPositionIndex);
    final int availableCapacity = capacity - (int)(tail - head);

    if (requiredCapacity > availableCapacity)
    {
        head = buffer.getLongVolatile(headPositionIndex);

        if (requiredCapacity > (capacity - (int)(tail - head)))
        {
            return INSUFFICIENT_CAPACITY;
        }

        buffer.putLong(headCachePositionIndex, head);
    }

    int padding = 0;
    int recordIndex = (int)tail & mask;
    final int toBufferEndLength = capacity - recordIndex;

    if (requiredCapacity > toBufferEndLength)
    {
        int headIndex = (int)head & mask;

        if (requiredCapacity > headIndex)
        {
            head = buffer.getLongVolatile(headPositionIndex);
            headIndex = (int)head & mask;
            if (requiredCapacity > headIndex)
            {
                return INSUFFICIENT_CAPACITY;
            }

            buffer.putLong(headCachePositionIndex, head);
        }

        padding = toBufferEndLength;
    }

    if (0 != padding)
    {
        buffer.putLong(0, 0L);
        buffer.putInt(typeOffset(recordIndex), PADDING_MSG_TYPE_ID);
        buffer.putIntOrdered(lengthOffset(recordIndex), padding);
        recordIndex = 0;
    }

    buffer.putLong(recordIndex + alignedRecordLength, 0L); // pre-zero next message header
    buffer.putLongOrdered(tailPositionIndex, tail + alignedRecordLength + padding);

    return recordIndex;
}
 
Example 12
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean unblock()
{
    final AtomicBuffer buffer = this.buffer;
    final long headPosition = buffer.getLongVolatile(headPositionIndex);
    final long tailPosition = buffer.getLongVolatile(tailPositionIndex);

    if (headPosition == tailPosition)
    {
        return false;
    }

    final int mask = capacity - 1;
    final int consumerIndex = (int)(headPosition & mask);
    final int producerIndex = (int)(tailPosition & mask);

    boolean unblocked = false;
    int length = buffer.getIntVolatile(consumerIndex);
    if (length < 0)
    {
        buffer.putInt(typeOffset(consumerIndex), PADDING_MSG_TYPE_ID);
        buffer.putIntOrdered(lengthOffset(consumerIndex), -length);
        unblocked = true;
    }
    else if (0 == length)
    {
        // go from (consumerIndex to producerIndex) or (consumerIndex to capacity)
        final int limit = producerIndex > consumerIndex ? producerIndex : capacity;
        int i = consumerIndex + ALIGNMENT;

        do
        {
            // read the top int of every long (looking for length aligned to 8=ALIGNMENT)
            length = buffer.getIntVolatile(i);
            if (0 != length)
            {
                if (scanBackToConfirmStillZeroed(buffer, i, consumerIndex))
                {
                    buffer.putInt(typeOffset(consumerIndex), PADDING_MSG_TYPE_ID);
                    buffer.putIntOrdered(lengthOffset(consumerIndex), i - consumerIndex);
                    unblocked = true;
                }

                break;
            }

            i += ALIGNMENT;
        }
        while (i < limit);
    }

    return unblocked;
}
 
Example 13
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
private int claimCapacity(final AtomicBuffer buffer, final int recordLength)
{
    final int requiredCapacity = align(recordLength, ALIGNMENT);
    final int capacity = this.capacity;
    final int tailPositionIndex = this.tailPositionIndex;
    final int headCachePositionIndex = this.headCachePositionIndex;
    final int mask = capacity - 1;

    long head = buffer.getLongVolatile(headCachePositionIndex);

    long tail;
    int tailIndex;
    int padding;
    do
    {
        tail = buffer.getLongVolatile(tailPositionIndex);
        final int availableCapacity = capacity - (int)(tail - head);

        if (requiredCapacity > availableCapacity)
        {
            head = buffer.getLongVolatile(headPositionIndex);

            if (requiredCapacity > (capacity - (int)(tail - head)))
            {
                return INSUFFICIENT_CAPACITY;
            }

            buffer.putLongOrdered(headCachePositionIndex, head);
        }

        padding = 0;
        tailIndex = (int)tail & mask;
        final int toBufferEndLength = capacity - tailIndex;

        if (requiredCapacity > toBufferEndLength)
        {
            int headIndex = (int)head & mask;

            if (requiredCapacity > headIndex)
            {
                head = buffer.getLongVolatile(headPositionIndex);
                headIndex = (int)head & mask;
                if (requiredCapacity > headIndex)
                {
                    return INSUFFICIENT_CAPACITY;
                }

                buffer.putLongOrdered(headCachePositionIndex, head);
            }

            padding = toBufferEndLength;
        }
    }
    while (!buffer.compareAndSetLong(tailPositionIndex, tail, tail + requiredCapacity + padding));

    if (0 != padding)
    {
        buffer.putIntOrdered(lengthOffset(tailIndex), -padding);
        UnsafeAccess.UNSAFE.storeFence();

        buffer.putInt(typeOffset(tailIndex), PADDING_MSG_TYPE_ID);
        buffer.putIntOrdered(lengthOffset(tailIndex), padding);
        tailIndex = 0;
    }

    return tailIndex;
}