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

The following examples show how to use org.agrona.concurrent.AtomicBuffer#putInt() . 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: OneToOneRingBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean write(final int msgTypeId, final DirectBuffer srcBuffer, final int offset, final int length)
{
    checkTypeId(msgTypeId);
    checkMsgLength(length);

    final AtomicBuffer buffer = this.buffer;
    final int recordLength = length + HEADER_LENGTH;
    final int recordIndex = claimCapacity(buffer, recordLength);

    if (INSUFFICIENT_CAPACITY == recordIndex)
    {
        return false;
    }

    buffer.putBytes(encodedMsgOffset(recordIndex), srcBuffer, offset, length);
    buffer.putInt(typeOffset(recordIndex), msgTypeId);
    buffer.putIntOrdered(lengthOffset(recordIndex), recordLength);

    return true;
}
 
Example 2
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int tryClaim(final int msgTypeId, final int length)
{
    checkTypeId(msgTypeId);
    checkMsgLength(length);

    final AtomicBuffer buffer = this.buffer;
    final int recordLength = length + HEADER_LENGTH;
    final int recordIndex = claimCapacity(buffer, recordLength);

    if (INSUFFICIENT_CAPACITY == recordIndex)
    {
        return recordIndex;
    }

    buffer.putInt(typeOffset(recordIndex), msgTypeId);
    // Note: putInt is used to write negative length of the message since we are not yet publishing the message and
    // hence the order of writes of type field and negative length does not matter.
    // It is safe to do so, because the header was pre-zeroed during the capacity claim.
    buffer.putInt(lengthOffset(recordIndex), -recordLength);

    return encodedMsgOffset(recordIndex);
}
 
Example 3
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean write(final int msgTypeId, final DirectBuffer srcBuffer, final int offset, final int length)
{
    checkTypeId(msgTypeId);
    checkMsgLength(length);

    final AtomicBuffer buffer = this.buffer;
    final int recordLength = length + HEADER_LENGTH;
    final int recordIndex = claimCapacity(buffer, recordLength);

    if (INSUFFICIENT_CAPACITY == recordIndex)
    {
        return false;
    }

    buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
    UnsafeAccess.UNSAFE.storeFence();

    buffer.putInt(typeOffset(recordIndex), msgTypeId);
    buffer.putBytes(encodedMsgOffset(recordIndex), srcBuffer, offset, length);
    buffer.putIntOrdered(lengthOffset(recordIndex), recordLength);

    return true;
}
 
Example 4
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int tryClaim(final int msgTypeId, final int length)
{
    checkTypeId(msgTypeId);
    checkMsgLength(length);

    final AtomicBuffer buffer = this.buffer;
    final int recordLength = length + HEADER_LENGTH;
    final int recordIndex = claimCapacity(buffer, recordLength);

    if (INSUFFICIENT_CAPACITY == recordIndex)
    {
        return recordIndex;
    }

    buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
    UnsafeAccess.UNSAFE.storeFence();
    buffer.putInt(typeOffset(recordIndex), msgTypeId);

    return encodedMsgOffset(recordIndex);
}
 
Example 5
Source File: OneToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
public void run()
{
    final int length = SIZE_OF_INT + SIZE_OF_LONG;
    for (int i = 0; i < REPETITIONS; i++)
    {

        int index = -1;
        try
        {
            while (INSUFFICIENT_CAPACITY == (index = ringBuffer.tryClaim(MSG_TYPE_ID, length)))
            {
                Thread.yield();
            }

            final AtomicBuffer buffer = ringBuffer.buffer();
            buffer.putInt(index, i);
            buffer.putLong(index + SIZE_OF_INT, i * 20L);
        }
        finally
        {
            ringBuffer.commit(index);
        }
    }
}
 
Example 6
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 7
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void abort(final int index)
{
    final int recordIndex = computeRecordIndex(index);
    final AtomicBuffer buffer = this.buffer;
    final int recordLength = verifyClaimedSpaceNotReleased(buffer, recordIndex);

    buffer.putInt(typeOffset(recordIndex), PADDING_MSG_TYPE_ID);
    buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
}
 
Example 8
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void abort(final int index)
{
    final int recordIndex = computeRecordIndex(index);
    final AtomicBuffer buffer = this.buffer;
    final int recordLength = verifyClaimedSpaceNotReleased(buffer, recordIndex);

    buffer.putInt(typeOffset(recordIndex), PADDING_MSG_TYPE_ID);
    buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
}
 
Example 9
Source File: ManyToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
public void run()
{
    try
    {
        barrier.await();
    }
    catch (final Exception ignore)
    {
    }

    final int length = SIZE_OF_INT * 2;
    for (int i = 0; i < reps; i++)
    {
        int index = -1;
        try
        {
            while (INSUFFICIENT_CAPACITY == (index = ringBuffer.tryClaim(MSG_TYPE_ID, length)))
            {
                Thread.yield();
            }

            final AtomicBuffer buffer = ringBuffer.buffer();
            buffer.putInt(index, producerId);
            buffer.putInt(index + SIZE_OF_INT, i);
        }
        finally
        {
            ringBuffer.commit(index);
        }
    }
}
 
Example 10
Source File: BroadcastTransmitter.java    From agrona with Apache License 2.0 4 votes vote down vote up
private static void insertPaddingRecord(final AtomicBuffer buffer, final int recordOffset, final int length)
{
    buffer.putInt(lengthOffset(recordOffset), length);
    buffer.putInt(typeOffset(recordOffset), PADDING_MSG_TYPE_ID);
}
 
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;
}