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

The following examples show how to use org.agrona.concurrent.AtomicBuffer#putLongOrdered() . 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: 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 void endChangeOrdered(final AtomicBuffer buffer, final long changePosition)
{
    buffer.putLongOrdered(END_CHANGE_OFFSET, changePosition);
}
 
Example 4
Source File: ReplayIndexDescriptor.java    From artio with Apache License 2.0 4 votes vote down vote up
static void beginChangeOrdered(final AtomicBuffer buffer, final long changePosition)
{
    buffer.putLongOrdered(BEGIN_CHANGE_OFFSET, changePosition);
}
 
Example 5
Source File: BroadcastTransmitter.java    From agrona with Apache License 2.0 4 votes vote down vote up
private void signalTailIntent(final AtomicBuffer buffer, final long newTail)
{
    buffer.putLongOrdered(tailIntentCountIndex, newTail);
    UnsafeAccess.UNSAFE.storeFence();
}
 
Example 6
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 7
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 8
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;
}
 
Example 9
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;
}