Changeset 12969
- Timestamp:
- 10/09/10 23:28:31 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abcl/src/org/armedbear/lisp/HashTable.java
r12968 r12969 34 34 package org.armedbear.lisp; 35 35 36 import java.util.concurrent.ConcurrentHashMap; 37 import java.util.concurrent.locks.ReentrantReadWriteLock; 36 import java.util.concurrent.locks.ReentrantLock; 38 37 import static org.armedbear.lisp.Lisp.*; 39 38 … … 47 46 protected int threshold; 48 47 // Array containing the actual key-value mappings. 49 50 48 @SuppressWarnings("VolatileArrayField") 51 49 protected volatile HashEntry[] buckets; 52 53 50 // The number of key-value pairs. 54 protected int count; 55 private int mask; 51 protected volatile int count; 56 52 final Comparator comparator; 57 final private Reentrant ReadWriteLock lock = new ReentrantReadWriteLock();53 final private ReentrantLock lock = new ReentrantLock(); 58 54 59 55 protected HashTable(Comparator c, int size, LispObject rehashSize, … … 64 60 threshold = (int) (size * loadFactor); 65 61 comparator = c; 66 mask = buckets.length - 1;67 62 } 68 63 … … 157 152 158 153 public void clear() { 159 lock. writeLock().lock();154 lock.lock(); 160 155 try { 161 156 buckets = new HashEntry[buckets.length]; 162 157 count = 0; 163 158 } finally { 164 lock. writeLock().unlock();159 lock.unlock(); 165 160 } 166 161 } … … 234 229 235 230 protected HashEntry getEntry(LispObject key) { 236 int index = comparator.hash(key) & mask;237 HashEntry e = b uckets[index];231 HashEntry[] b = buckets; 232 HashEntry e = b[comparator.hash(key) & (b.length - 1)]; 238 233 while (e != null) { 239 234 if (comparator.keysEqual(key, e.key)) { … … 246 241 247 242 public LispObject get(LispObject key) { 248 lock.readLock().lock(); 243 HashEntry e = getEntry(key); 244 LispObject v = (e == null) ? null : e.value; 245 246 if (e == null || v != null) { 247 return v; 248 } 249 250 lock.lock(); 251 try { 252 return e.value; 253 } finally { 254 lock.unlock(); 255 } 256 } 257 258 public void put(LispObject key, LispObject value) { 259 lock.lock(); 249 260 try { 250 261 HashEntry e = getEntry(key); 251 return (e == null) ? null : e.value; 252 } finally { 253 lock.readLock().unlock(); 254 } 255 } 256 257 public void put(LispObject key, LispObject value) { 258 lock.writeLock().lock(); 259 try { 260 HashEntry e = getEntry(key); 261 if (e == null) { 262 if (e != null) { 262 263 e.value = value; 263 264 } else { … … 267 268 } 268 269 269 int index = comparator.hash(key) & mask;270 int index = comparator.hash(key) & (buckets.length - 1); 270 271 buckets[index] = new HashEntry(key, value, buckets[index]); 271 272 } 272 273 } finally { 273 lock. writeLock().unlock();274 lock.unlock(); 274 275 } 275 276 } 276 277 277 278 public LispObject remove(LispObject key) { 278 lock. writeLock().lock();279 lock.lock(); 279 280 try { 280 int index = comparator.hash(key) & mask;281 int index = comparator.hash(key) & (buckets.length - 1); 281 282 282 283 HashEntry e = buckets[index]; … … 297 298 return null; 298 299 } finally { 299 lock. writeLock().unlock();300 lock.unlock(); 300 301 } 301 302 } 302 303 303 304 protected void rehash() { 304 lock. writeLock().lock();305 lock.lock(); 305 306 try { 306 307 final int newCapacity = buckets.length * 2; 307 308 threshold = (int) (newCapacity * loadFactor); 308 mask = newCapacity - 1;309 int mask = newCapacity - 1; 309 310 HashEntry[] newBuckets = new HashEntry[newCapacity]; 310 311 … … 319 320 buckets = newBuckets; 320 321 } finally { 321 lock. writeLock().unlock();322 lock.unlock(); 322 323 } 323 324 } … … 416 417 417 418 LispObject key; 418 LispObject value;419 volatile LispObject value; 419 420 HashEntry next; 420 421
Note: See TracChangeset
for help on using the changeset viewer.