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

The following examples show how to use org.agrona.concurrent.AtomicBuffer#getLong() . 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: 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 2
Source File: BroadcastTransmitter.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Transmit a message to {@link BroadcastReceiver}s via the broadcast buffer.
 *
 * @param msgTypeId type of the message to be transmitted.
 * @param srcBuffer containing the encoded message to be transmitted.
 * @param srcIndex  srcIndex in the source buffer at which the encoded message begins.
 * @param length    in bytes of the encoded message.
 * @throws IllegalArgumentException of the msgTypeId is not valid,
 * or if the message length is greater than {@link #maxMsgLength()}.
 */
public void transmit(final int msgTypeId, final DirectBuffer srcBuffer, final int srcIndex, final int length)
{
    checkTypeId(msgTypeId);
    checkMessageLength(length);

    final AtomicBuffer buffer = this.buffer;
    long currentTail = buffer.getLong(tailCounterIndex);
    int recordOffset = (int)currentTail & (capacity - 1);
    final int recordLength = HEADER_LENGTH + length;
    final int recordLengthAligned = BitUtil.align(recordLength, RECORD_ALIGNMENT);
    final long newTail = currentTail + recordLengthAligned;

    final int toEndOfBuffer = capacity - recordOffset;
    if (toEndOfBuffer < recordLengthAligned)
    {
        signalTailIntent(buffer, newTail + toEndOfBuffer);
        insertPaddingRecord(buffer, recordOffset, toEndOfBuffer);

        currentTail += toEndOfBuffer;
        recordOffset = 0;
    }
    else
    {
        signalTailIntent(buffer, newTail);
    }

    buffer.putInt(lengthOffset(recordOffset), recordLength);
    buffer.putInt(typeOffset(recordOffset), msgTypeId);

    buffer.putBytes(msgOffset(recordOffset), srcBuffer, srcIndex, length);

    buffer.putLong(latestCounterIndex, currentTail);
    buffer.putLongOrdered(tailCounterIndex, currentTail + recordLengthAligned);
}
 
Example 3
Source File: ReplayIndexDescriptor.java    From artio with Apache License 2.0 4 votes vote down vote up
static long beginChange(final AtomicBuffer buffer)
{
    return buffer.getLong(BEGIN_CHANGE_OFFSET);
}
 
Example 4
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    int bytesRead = 0;

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int contiguousBlockLength = capacity - headIndex;

    try
    {
        while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}
 
Example 5
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 6
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int maxBlockLength = capacity - headIndex;
    int bytesRead = 0;

    try
    {
        while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.setMemory(headIndex, bytesRead, (byte)0);
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}