Changeset 502 for Tests


Ignore:
Timestamp:
03/27/12 13:59:05 (10 years ago)
Author:
BegemoT
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Tests/JAVA/test/src/main/java/test/threads/queue/unstressed/impl/CABSESDQueueUnrolled.java

    r501 r502  
    1313 */ 
    1414public class CABSESDQueueUnrolled<T> implements IQueue<T> { 
     15    private static final boolean USE_BITMASK = true; 
    1516    /* Elements range: [headCursor, tailCursor) 
    1617    * 
     
    2829    private final int length; 
    2930    private volatile long headCursor = 0; 
    30 //    private volatile long p11, p12, p13, p14, p15, p16, p17, p18 = 7; 
     31    //    private volatile long p11, p12, p13, p14, p15, p16, p17, p18 = 7; 
    3132    private volatile long tailCursor = 0; 
    3233//    private volatile long p21, p22, p23, p24, p25, p26, p27, p28 = 8; 
     
    4344    public CABSESDQueueUnrolled( final int length ) { 
    4445        checkArgument( length > 0, "length(%s) must be >0", length ); 
     46        if ( USE_BITMASK ) { 
     47            checkArgument( Integer.bitCount( length ) == 1, "length(%s) must be a power of 2", length ); 
     48        } 
    4549        this.length = length; 
    4650        elements = ( T[] ) new Object[length]; 
     
    9397        final long newTail = nextTail(); 
    9498 
    95         final int index = ( int ) ( ( newTail - 1 ) % length ); 
     99        final int index = index( newTail ); 
    96100        elements[index] = item; 
    97101 
     
    102106        final long newHead = nextHead(); 
    103107 
    104         final int index = ( int ) ( ( newHead - 1 ) % length ); 
     108        final int index = index( newHead ); 
    105109        final T item = elements[index]; 
    106110        elements[index] = null;//for GC 
     
    108112        publishHead( newHead ); 
    109113        return item; 
     114    } 
     115 
     116    private int index( final long sequence ) { 
     117        if ( USE_BITMASK ) { 
     118            return ( int ) ( ( sequence - 1 ) & ( length - 1 ) ); 
     119        } else { 
     120            return ( int ) ( ( sequence - 1 ) % length ); 
     121        } 
    110122    } 
    111123 
     
    127139        @Override 
    128140        public String toString() { 
    129             return "SESDQueueUnrolledFactory"; 
     141            return "SESDQueueUnrolledFactory[" + ( USE_BITMASK ? "&" : "%" ) + "]"; 
    130142        } 
    131143    }; 
Note: See TracChangeset for help on using the changeset viewer.