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

The following examples show how to use org.agrona.concurrent.AtomicBuffer#putIntOrdered() . 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: 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 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: OneToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit(final int index)
{
    final int recordIndex = computeRecordIndex(index);
    final AtomicBuffer buffer = this.buffer;
    final int recordLength = verifyClaimedSpaceNotReleased(buffer, recordIndex);

    buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
}
 
Example 6
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 7
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit(final int index)
{
    final int recordIndex = computeRecordIndex(index);
    final AtomicBuffer buffer = this.buffer;
    final int recordLength = verifyClaimedSpaceNotReleased(buffer, recordIndex);

    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: 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 10
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 11
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;
}