ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/CustomConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/extra166y/CustomConcurrentHashMap.java (file contents):
Revision 1.6 by dl, Sun Nov 1 22:14:39 2009 UTC vs.
Revision 1.13 by jsr166, Sat Oct 16 16:40:44 2010 UTC

# Line 53 | Line 53 | import sun.misc.Unsafe;
53   *     (STRONG,
54   *      new Equivalence<Person>() {
55   *          public boolean equal(Person k, Object x) {
56 < *            return x instanceOf Person && k.name.equals(((Person)x).name);
56 > *            return x instanceof Person && k.name.equals(((Person)x).name);
57   *          }
58   *          public int hash(Object x) {
59 < *             return (x instanceOf Person)? ((Person)x).name.hashCode() : 0;
59 > *             return (x instanceof Person)? ((Person)x).name.hashCode() : 0;
60   *          }
61   *        },
62   *      STRONG, EQUALS, 0);
# Line 533 | Line 533 | public class CustomConcurrentHashMap<K,
533              int sc = (int)((1L + (4L * es) / 3) >>> SEGMENT_BITS);
534              if (sc < MIN_SEGMENT_CAPACITY)
535                  sc = MIN_SEGMENT_CAPACITY;
536 <            else if (sc > MAX_SEGMENT_CAPACITY)
537 <                sc = MAX_SEGMENT_CAPACITY;
538 <            this.initialSegmentCapacity = sc;
536 >            int capacity = MIN_SEGMENT_CAPACITY; // ensure power of two
537 >            while (capacity < sc)
538 >                capacity <<= 1;
539 >            if (capacity > MAX_SEGMENT_CAPACITY)
540 >                capacity = MAX_SEGMENT_CAPACITY;
541 >            this.initialSegmentCapacity = capacity;
542          }
543          this.segments = (Segment[])new Segment[NSEGMENTS];
544      }
# Line 639 | Line 642 | public class CustomConcurrentHashMap<K,
642          int index = (hash >>> SEGMENT_SHIFT) & SEGMENT_MASK;
643          Segment seg = segs[index];
644          if (seg == null) {
645 <            synchronized(segs) {
645 >            synchronized (segs) {
646                  seg = segs[index];
647                  if (seg == null) {
648                      seg = new Segment();
# Line 1555 | Line 1558 | public class CustomConcurrentHashMap<K,
1558       * for each key-value mapping, followed by a null pair.
1559       * The key-value mappings are emitted in no particular order.
1560       */
1561 <    private void writeObject(java.io.ObjectOutputStream s) throws IOException  {
1561 >    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
1562          s.defaultWriteObject();
1563          for (Map.Entry<K,V> e : entrySet()) {
1564              s.writeObject(e.getKey());
# Line 1570 | Line 1573 | public class CustomConcurrentHashMap<K,
1573       * @param s the stream
1574       */
1575      private void readObject(java.io.ObjectInputStream s)
1576 <        throws IOException, ClassNotFoundException  {
1576 >        throws IOException, ClassNotFoundException {
1577          s.defaultReadObject();
1578          this.segments = (Segment[])(new Segment[NSEGMENTS]);
1579          for (;;) {
# Line 2098 | Line 2101 | public class CustomConcurrentHashMap<K,
2101          final int locator;
2102          final CustomConcurrentHashMap cchm;
2103          WeakKeyNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2104 <            super(key);
2104 >            super(key, getReclamationQueue());
2105              this.locator = locator;
2106              this.cchm = cchm;
2107          }
# Line 2393 | Line 2396 | public class CustomConcurrentHashMap<K,
2396          final int locator;
2397          final CustomConcurrentHashMap cchm;
2398          SoftKeyNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2399 <            super(key);
2399 >            super(key, getReclamationQueue());
2400              this.locator = locator;
2401              this.cchm = cchm;
2402          }
# Line 2981 | Line 2984 | public class CustomConcurrentHashMap<K,
2984  
2985      // Temporary Unsafe mechanics for preliminary release
2986  
2987 <    static final Unsafe _unsafe;
2987 >    static final Unsafe UNSAFE;
2988      static final long tableBase;
2989      static final int tableShift;
2990      static final long segmentsBase;
# Line 3012 | Line 3015 | public class CustomConcurrentHashMap<K,
3015  
3016      static {
3017          try {
3018 <            _unsafe = getUnsafe();
3019 <            tableBase = _unsafe.arrayBaseOffset(Node[].class);
3020 <            int s = _unsafe.arrayIndexScale(Node[].class);
3018 >            UNSAFE = getUnsafe();
3019 >            tableBase = UNSAFE.arrayBaseOffset(Node[].class);
3020 >            int s = UNSAFE.arrayIndexScale(Node[].class);
3021              if ((s & (s-1)) != 0)
3022                  throw new Error("data type scale not a power of two");
3023              tableShift = 31 - Integer.numberOfLeadingZeros(s);
3024 <            segmentsBase = _unsafe.arrayBaseOffset(Segment[].class);
3025 <            s = _unsafe.arrayIndexScale(Segment[].class);
3024 >            segmentsBase = UNSAFE.arrayBaseOffset(Segment[].class);
3025 >            s = UNSAFE.arrayIndexScale(Segment[].class);
3026              if ((s & (s-1)) != 0)
3027                  throw new Error("data type scale not a power of two");
3028              segmentsShift = 31 - Integer.numberOfLeadingZeros(s);
# Line 3032 | Line 3035 | public class CustomConcurrentHashMap<K,
3035      static final  void storeNode(Node[] table,
3036                                   int i, Node r) {
3037          long nodeOffset = ((long) i << tableShift) + tableBase;
3038 <        _unsafe.putOrderedObject(table, nodeOffset, r);
3038 >        UNSAFE.putOrderedObject(table, nodeOffset, r);
3039      }
3040  
3041      static final  void storeSegment(Segment[] segs,
3042                                      int i, Segment s) {
3043          long segmentOffset = ((long) i << segmentsShift) + segmentsBase;
3044 <        _unsafe.putOrderedObject(segs, segmentOffset, s);
3044 >        UNSAFE.putOrderedObject(segs, segmentOffset, s);
3045      }
3046  
3047  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines