Skip to content

Commit

Permalink
Update status value using atomic field updater.
Browse files Browse the repository at this point in the history
Make status field final.
  • Loading branch information
MishaDemianenko committed Jan 12, 2017
1 parent 0b54a4f commit 7cbd8f0
Showing 1 changed file with 17 additions and 16 deletions.
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Supplier;

import org.neo4j.collection.pool.Pool;
Expand Down Expand Up @@ -116,7 +116,7 @@ public class KernelTransactionImplementation implements KernelTransaction, TxSta
private SecurityContext securityContext;
private volatile StatementLocks statementLocks;
private boolean beforeHookInvoked;
private TransactionStatus transactionStatus = new TransactionStatus();
private final TransactionStatus transactionStatus = new TransactionStatus();
private boolean failure;
private boolean success;
private long startTimeMillis;
Expand Down Expand Up @@ -351,7 +351,6 @@ public boolean hasTxStateWithChanges()

private void markAsClosed( long txId )
{
assertTransactionOpen();
closeCurrentStatementIfAny();
for ( CloseListener closeListener : closeListeners )
{
Expand Down Expand Up @@ -749,7 +748,7 @@ public void dispose()
storageStatement.close();
}

void markAsShutdown()
private void markAsShutdown()
{
if ( transactionStatus.shutdown() )
{
Expand Down Expand Up @@ -814,12 +813,14 @@ static class TransactionStatus
private static final int NON_STATE_BITS_MASK = 0xFFFF_FFFC;
private static final int TERMINATED = 1 << 3;

private AtomicInteger status = new AtomicInteger( CLOSED );
private static final AtomicIntegerFieldUpdater<TransactionStatus> statusUpdater =
AtomicIntegerFieldUpdater.newUpdater( TransactionStatus.class, "status" );
private volatile int status = CLOSED;
private Status terminationReason;

public void init()
{
status.set( OPEN );
statusUpdater.set( this, OPEN );
reset();
}

Expand All @@ -845,22 +846,22 @@ public boolean isClosing()

public boolean isTerminated()
{
return (status.get() & TERMINATED) > 0;
return (statusUpdater.get( this ) & TERMINATED) != 0;
}

public boolean terminate( Status reason )
{
int currentStatus;
do
{
currentStatus = status.get();
currentStatus = statusUpdater.get( this );
if ( (currentStatus != OPEN) && (currentStatus != CLOSING) )
{
return false;
}
terminationReason = reason;
}
while ( !status.compareAndSet( currentStatus, currentStatus | TERMINATED ) );
while ( !statusUpdater.compareAndSet( this, currentStatus, currentStatus | TERMINATED ) );
return true;
}

Expand All @@ -869,13 +870,13 @@ public boolean closing()
int currentStatus;
do
{
currentStatus = status.get();
currentStatus = statusUpdater.get( this );
if ( (currentStatus & STATE_BITS_MASK) != OPEN )
{
return false;
}
}
while ( !status.compareAndSet( currentStatus, (currentStatus & NON_STATE_BITS_MASK) | CLOSING ) );
while ( !statusUpdater.compareAndSet( this, currentStatus, (currentStatus & NON_STATE_BITS_MASK) | CLOSING ) );
return true;
}

Expand All @@ -884,30 +885,30 @@ boolean shutdown()
int currentStatus;
do
{
currentStatus = status.get();
currentStatus = statusUpdater.get( this );
if ( (currentStatus & STATE_BITS_MASK) != OPEN )
{
return false;
}
}
while ( !status.compareAndSet( currentStatus, (currentStatus & NON_STATE_BITS_MASK) | SHUTDOWN ) );
while ( !statusUpdater.compareAndSet( this, currentStatus, (currentStatus & NON_STATE_BITS_MASK) | SHUTDOWN ) );
return true;
}

public void close()
{
status.set( CLOSED );
statusUpdater.set( this, CLOSED );
reset();
}

Optional<Status> getTerminationReason()
{
return isTerminated() ? Optional.ofNullable( terminationReason ) : Optional.empty();
return Optional.ofNullable( terminationReason );
}

private boolean is( int statusCode )
{
return is( status.get(), statusCode );
return is( statusUpdater.get( this ), statusCode );
}

private boolean is( int currentStatus, int statusCode )
Expand Down

0 comments on commit 7cbd8f0

Please sign in to comment.