aa
com.cliffc.aa.util.ConcurrentAutoTable.CAT Class Reference
Inheritance diagram for com.cliffc.aa.util.ConcurrentAutoTable.CAT:
[legend]
Collaboration diagram for com.cliffc.aa.util.ConcurrentAutoTable.CAT:
[legend]

Public Member Functions

long add_if (long x, int hash, ConcurrentAutoTable master)
 
long estimate_sum ()
 
void print ()
 
long sum ()
 
String toString ()
 

Package Functions

 CAT (CAT next, int sz, long init)
 

Static Private Member Functions

static boolean CAS (long[] A, int idx, long old, long nnn)
 
static long rawIndex (long[] ary, int i)
 

Private Attributes

volatile long _fuzzy_sum_cache
 
volatile long _fuzzy_time
 
final CAT _next
 
final long[] _t
 

Static Private Attributes

static final int _Lbase = _unsafe.arrayBaseOffset(long[].class)
 
static final int _Lscale = _unsafe.arrayIndexScale(long[].class)
 
static final Unsafe _unsafe = UtilUnsafe.getUnsafe()
 
static final int MAX_SPIN =1
 

Detailed Description

Definition at line 98 of file ConcurrentAutoTable.java.

Constructor & Destructor Documentation

◆ CAT()

com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAT ( CAT  next,
int  sz,
long  init 
)
package

Definition at line 122 of file ConcurrentAutoTable.java.

122  {
123  _next = next;
124  _t = new long[sz];
125  _t[0] = init;
126  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._next, and com.cliffc.aa.util.ConcurrentAutoTable.CAT._t.

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.CAT.add_if().

Here is the caller graph for this function:

Member Function Documentation

◆ add_if()

long com.cliffc.aa.util.ConcurrentAutoTable.CAT.add_if ( long  x,
int  hash,
ConcurrentAutoTable  master 
)

Definition at line 131 of file ConcurrentAutoTable.java.

131  {
132  final long[] t = _t;
133  final int idx = hash & (t.length-1);
134  // Peel loop; try once fast
135  long old = t[idx];
136  final boolean ok = CAS( t, idx, old, old+x );
137  if( ok ) return old; // Got it
138  // Try harder
139  int cnt=0;
140  while( true ) {
141  old = t[idx];
142  if( CAS( t, idx, old, old+x ) ) break; // Got it!
143  cnt++;
144  }
145  if( cnt < MAX_SPIN ) return old; // Allowable spin loop count
146  if( t.length >= 1024*1024 ) return old; // too big already
147 
148  // Too much contention; double array size in an effort to reduce contention
149  //long r = _resizers;
150  //final int newbytes = (t.length<<1)<<3/*word to bytes*/;
151  //while( !_resizerUpdater.compareAndSet(this,r,r+newbytes) )
152  // r = _resizers;
153  //r += newbytes;
154  if( master._cat != this ) return old; // Already doubled, don't bother
155  //if( (r>>17) != 0 ) { // Already too much allocation attempts?
156  // // We could use a wait with timeout, so we'll wakeup as soon as the new
157  // // table is ready, or after the timeout in any case. Annoyingly, this
158  // // breaks the non-blocking property - so for now we just briefly sleep.
159  // //synchronized( this ) { wait(8*megs); } // Timeout - we always wakeup
160  // try { Thread.sleep(r>>17); } catch( InterruptedException e ) { }
161  // if( master._cat != this ) return old;
162  //}
163 
164  CAT newcat = new CAT(this,t.length*2,0);
165  // Take 1 stab at updating the CAT with the new larger size. If this
166  // fails, we assume some other thread already expanded the CAT - so we
167  // do not need to retry until it succeeds.
168  while( master._cat == this && !master.CAS_cat(this,newcat) ) {/*empty*/}
169  return old;
170  }

References com.cliffc.aa.util.ConcurrentAutoTable._cat, com.cliffc.aa.util.ConcurrentAutoTable.CAT._t, com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAS(), com.cliffc.aa.util.ConcurrentAutoTable.CAS_cat(), com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAT(), com.cliffc.aa.util.ConcurrentAutoTable.hash(), and com.cliffc.aa.util.ConcurrentAutoTable.CAT.MAX_SPIN.

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.add_if().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CAS()

static boolean com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAS ( long[]  A,
int  idx,
long  old,
long  nnn 
)
staticprivate

Definition at line 108 of file ConcurrentAutoTable.java.

108  {
109  return _unsafe.compareAndSwapLong( A, rawIndex(A,idx), old, nnn );
110  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._unsafe, and com.cliffc.aa.util.ConcurrentAutoTable.CAT.rawIndex().

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.CAT.add_if().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ estimate_sum()

long com.cliffc.aa.util.ConcurrentAutoTable.CAT.estimate_sum ( )

Definition at line 184 of file ConcurrentAutoTable.java.

184  {
185  // For short tables, just do the work
186  if( _t.length <= 64 ) return sum();
187  // For bigger tables, periodically freshen a cached value
188  long millis = System.currentTimeMillis();
189  if( _fuzzy_time != millis ) { // Time marches on?
190  _fuzzy_sum_cache = sum(); // Get sum the hard way
191  _fuzzy_time = millis; // Indicate freshness of cached value
192  }
193  return _fuzzy_sum_cache; // Return cached sum
194  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_sum_cache, com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_time, com.cliffc.aa.util.ConcurrentAutoTable.CAT._t, and com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum().

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.estimate_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print()

void com.cliffc.aa.util.ConcurrentAutoTable.CAT.print ( )

Definition at line 198 of file ConcurrentAutoTable.java.

198  {
199  long[] t = _t;
200  System.out.print("["+t[0]);
201  for( int i=1; i<t.length; i++ )
202  System.out.print(","+t[i]);
203  System.out.print("]");
204  if( _next != null ) _next.print();
205  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._next, com.cliffc.aa.util.ConcurrentAutoTable.CAT._t, and com.cliffc.aa.util.ConcurrentAutoTable.CAT.print().

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.print(), and com.cliffc.aa.util.ConcurrentAutoTable.CAT.print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rawIndex()

static long com.cliffc.aa.util.ConcurrentAutoTable.CAT.rawIndex ( long[]  ary,
int  i 
)
staticprivate

Definition at line 104 of file ConcurrentAutoTable.java.

104  {
105  assert i >= 0 && i < ary.length;
106  return _Lbase + i * _Lscale;
107  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lbase, and com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lscale.

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAS().

Here is the caller graph for this function:

◆ sum()

long com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum ( )

Definition at line 175 of file ConcurrentAutoTable.java.

175  {
176  long sum = _next == null ? 0 : _next.sum(); // Recursively get cached sum
177  final long[] t = _t;
178  for( long cnt : t ) sum += cnt;
179  return sum;
180  }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT._next, com.cliffc.aa.util.ConcurrentAutoTable.CAT._t, and com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum().

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.CAT.estimate_sum(), com.cliffc.aa.util.ConcurrentAutoTable.get(), com.cliffc.aa.util.ConcurrentAutoTable.intValue(), com.cliffc.aa.util.ConcurrentAutoTable.longValue(), com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum(), and com.cliffc.aa.util.ConcurrentAutoTable.CAT.toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

String com.cliffc.aa.util.ConcurrentAutoTable.CAT.toString ( )

Definition at line 196 of file ConcurrentAutoTable.java.

196 { return Long.toString(sum()); }

References com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum().

Referenced by com.cliffc.aa.util.ConcurrentAutoTable.toString().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _fuzzy_sum_cache

volatile long com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_sum_cache
private

◆ _fuzzy_time

volatile long com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_time
private

◆ _Lbase

final int com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lbase = _unsafe.arrayBaseOffset(long[].class)
staticprivate

◆ _Lscale

final int com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lscale = _unsafe.arrayIndexScale(long[].class)
staticprivate

◆ _next

final CAT com.cliffc.aa.util.ConcurrentAutoTable.CAT._next
private

◆ _t

◆ _unsafe

final Unsafe com.cliffc.aa.util.ConcurrentAutoTable.CAT._unsafe = UtilUnsafe.getUnsafe()
staticprivate

◆ MAX_SPIN

final int com.cliffc.aa.util.ConcurrentAutoTable.CAT.MAX_SPIN =1
staticprivate

The documentation for this class was generated from the following file:
com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_time
volatile long _fuzzy_time
Definition: ConcurrentAutoTable.java:118
com.cliffc.aa.util.ConcurrentAutoTable.CAT._next
final CAT _next
Definition: ConcurrentAutoTable.java:116
com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAS
static boolean CAS(long[] A, int idx, long old, long nnn)
Definition: ConcurrentAutoTable.java:108
com.cliffc.aa.util.ConcurrentAutoTable.CAT.CAT
CAT(CAT next, int sz, long init)
Definition: ConcurrentAutoTable.java:122
com.cliffc.aa.util.ConcurrentAutoTable.CAT.print
void print()
Definition: ConcurrentAutoTable.java:198
com.cliffc.aa.util.ConcurrentAutoTable.CAT._unsafe
static final Unsafe _unsafe
Definition: ConcurrentAutoTable.java:101
com.cliffc.aa.util.ConcurrentAutoTable.CAT._t
final long[] _t
Definition: ConcurrentAutoTable.java:120
com.cliffc.aa.util.ConcurrentAutoTable.CAT.rawIndex
static long rawIndex(long[] ary, int i)
Definition: ConcurrentAutoTable.java:104
com.cliffc.aa.util.ConcurrentAutoTable.CAT._fuzzy_sum_cache
volatile long _fuzzy_sum_cache
Definition: ConcurrentAutoTable.java:117
com.cliffc.aa.util.ConcurrentAutoTable.CAT.MAX_SPIN
static final int MAX_SPIN
Definition: ConcurrentAutoTable.java:119
com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lscale
static final int _Lscale
Definition: ConcurrentAutoTable.java:103
com.cliffc.aa.util.ConcurrentAutoTable.CAT.sum
long sum()
Definition: ConcurrentAutoTable.java:175
com.cliffc.aa.util.ConcurrentAutoTable.hash
static int hash()
Definition: ConcurrentAutoTable.java:91
com.cliffc.aa.util.ConcurrentAutoTable.CAT._Lbase
static final int _Lbase
Definition: ConcurrentAutoTable.java:102