< prev index next >

src/java.base/share/classes/java/lang/ClassValue.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.util.WeakHashMap;





  29 import java.lang.ref.WeakReference;


  30 import java.util.concurrent.atomic.AtomicInteger;
  31 
  32 import static java.lang.ClassValue.ClassValueMap.probeHomeLocation;
  33 import static java.lang.ClassValue.ClassValueMap.probeBackupLocations;
  34 
  35 /**
  36  * Lazily associate a computed value with (potentially) every type.
  37  * For example, if a dynamic language needs to construct a message dispatch
  38  * table for each class encountered at a message send call site,
  39  * it can use a {@code ClassValue} to cache information needed to
  40  * perform the message send quickly, for each class encountered.
  41  * @author John Rose, JSR 292 EG
  42  * @since 1.7
  43  */
  44 public abstract class ClassValue<T> {
  45     /**
  46      * Sole constructor.  (For invocation by subclass constructors, typically
  47      * implicit.)
  48      */
  49     protected ClassValue() {
  50     }
  51 
  52     /**
  53      * Computes the given class's derived value for this {@code ClassValue}.


  78      * is performed atomically.
  79      * At that point, if several racing threads have
  80      * computed values, one is chosen, and returned to
  81      * all the racing threads.
  82      * <p>
  83      * The {@code type} parameter is typically a class, but it may be any type,
  84      * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
  85      * <p>
  86      * In the absence of {@code remove} calls, a class value has a simple
  87      * state diagram:  uninitialized and initialized.
  88      * When {@code remove} calls are made,
  89      * the rules for value observation are more complex.
  90      * See the documentation for {@link #remove remove} for more information.
  91      *
  92      * @param type the type whose class value must be computed or retrieved
  93      * @return the current value associated with this {@code ClassValue}, for the given class or interface
  94      * @throws NullPointerException if the argument is null
  95      * @see #remove
  96      * @see #computeValue
  97      */


  98     public T get(Class<?> type) {
  99         // non-racing this.hashCodeForCache : final int
 100         Entry<?>[] cache;
 101         Entry<T> e = probeHomeLocation(cache = getCacheCarefully(type), this);
 102         // racing e : current value <=> stale value from current cache or from stale cache
 103         // invariant:  e is null or an Entry with readable Entry.version and Entry.value
 104         if (match(e))
 105             // invariant:  No false positive matches.  False negatives are OK if rare.
 106             // The key fact that makes this work: if this.version == e.version,
 107             // then this thread has a right to observe (final) e.value.
 108             return e.value();
 109         // The fast path can fail for any of these reasons:
 110         // 1. no entry has been computed yet
 111         // 2. hash code collision (before or after reduction mod cache.length)
 112         // 3. an entry has been removed (either on this type or another)
 113         // 4. the GC has somehow managed to delete e.version and clear the reference
 114         return getFromBackup(cache, type);

































 115     }
 116 
 117     /**
 118      * Removes the associated value for the given class.
 119      * If this value is subsequently {@linkplain #get read} for the same class,
 120      * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
 121      * This may result in an additional invocation of the
 122      * {@code computeValue} method for the given class.
 123      * <p>
 124      * In order to explain the interaction between {@code get} and {@code remove} calls,
 125      * we must model the state transitions of a class value to take into account
 126      * the alternation between uninitialized and initialized states.
 127      * To do this, number these states sequentially from zero, and note that
 128      * uninitialized (or removed) states are numbered with even numbers,
 129      * while initialized (or re-initialized) states have odd numbers.
 130      * <p>
 131      * When a thread {@code T} removes a class value in state {@code 2N},
 132      * nothing happens, since the class value is already uninitialized.
 133      * Otherwise, the state is advanced atomically to {@code 2N+1}.
 134      * <p>


 151      * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
 152      * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
 153      * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
 154      * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
 155      * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
 156      * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
 157      * <li> the previous actions of {@code T2} are repeated several times
 158      * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
 159      * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
 160      * </ul>
 161      * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
 162      * observe the time-dependent states as it computes {@code V1}, etc.
 163      * This does not remove the threat of a stale value, since there is a window of time
 164      * between the return of {@code computeValue} in {@code T} and the installation
 165      * of the new value.  No user synchronization is possible during this time.
 166      *
 167      * @param type the type whose class value must be removed
 168      * @throws NullPointerException if the argument is null
 169      */
 170     public void remove(Class<?> type) {
 171         ClassValueMap map = getMap(type);
 172         map.removeEntry(this);















 173     }
 174 
 175     // Possible functionality for JSR 292 MR 1
 176     /*public*/ void put(Class<?> type, T value) {

 177         ClassValueMap map = getMap(type);
 178         map.changeEntry(this, value);
 179     }
 180 
 181     /// --------
 182     /// Implementation...
 183     /// --------
 184 
 185     /** Return the cache, if it exists, else a dummy empty cache. */
 186     private static Entry<?>[] getCacheCarefully(Class<?> type) {
 187         // racing type.classValueMap{.cacheArray} : null => new Entry[X] <=> new Entry[Y]
 188         ClassValueMap map = type.classValueMap;
 189         if (map == null)  return EMPTY_CACHE;
 190         Entry<?>[] cache = map.getCache();
 191         return cache;
 192         // invariant:  returned value is safe to dereference and check for an Entry
 193     }
 194 
 195     /** Initial, one-element, empty cache used by all Class instances.  Must never be filled. */
 196     private static final Entry<?>[] EMPTY_CACHE = { null };
 197 
 198     /**
 199      * Slow tail of ClassValue.get to retry at nearby locations in the cache,
 200      * or take a slow lock and check the hash table.
 201      * Called only if the first probe was empty or a collision.
 202      * This is a separate method, so compilers can process it independently.
 203      */
 204     private T getFromBackup(Entry<?>[] cache, Class<?> type) {
 205         Entry<T> e = probeBackupLocations(cache, this);
 206         if (e != null)
 207             return e.value();
 208         return getFromHashMap(type);
 209     }
 210 
 211     // Hack to suppress warnings on the (T) cast, which is a no-op.
 212     @SuppressWarnings("unchecked")
 213     Entry<T> castEntry(Entry<?> e) { return (Entry<T>) e; }
 214 
 215     /** Called when the fast path of get fails, and cache reprobe also fails.
 216      */
 217     private T getFromHashMap(Class<?> type) {
 218         // The fail-safe recovery is to fall back to the underlying classValueMap.
 219         ClassValueMap map = getMap(type);
 220         for (;;) {
 221             Entry<T> e = map.startEntry(this);
 222             if (!e.isPromise())
 223                 return e.value();
 224             try {
 225                 // Try to make a real entry for the promised version.
 226                 e = makeEntry(e.version(), computeValue(type));
 227             } finally {
 228                 // Whether computeValue throws or returns normally,
 229                 // be sure to remove the empty entry.
 230                 e = map.finishEntry(this, e);
 231             }
 232             if (e != null)
 233                 return e.value();
 234             // else try again, in case a racing thread called remove (so e == null)
 235         }
 236     }
 237 
 238     /** Check that e is non-null, matches this ClassValue, and is live. */
 239     boolean match(Entry<?> e) {
 240         // racing e.version : null (blank) => unique Version token => null (GC-ed version)
 241         // non-racing this.version : v1 => v2 => ... (updates are read faithfully from volatile)
 242         return (e != null && e.get() == this.version);
 243         // invariant:  No false positives on version match.  Null is OK for false negative.
 244         // invariant:  If version matches, then e.value is readable (final set in Entry.<init>)
 245     }
 246 
 247     /** Internal hash code for accessing Class.classValueMap.cacheArray. */
 248     final int hashCodeForCache = nextHashCode.getAndAdd(HASH_INCREMENT) & HASH_MASK;
 249 
 250     /** Value stream for hashCodeForCache.  See similar structure in ThreadLocal. */
 251     private static final AtomicInteger nextHashCode = new AtomicInteger();
 252 
 253     /** Good for power-of-two tables.  See similar structure in ThreadLocal. */
 254     private static final int HASH_INCREMENT = 0x61c88647;
 255 
 256     /** Mask a hash code to be positive but not too large, to prevent wraparound. */
 257     static final int HASH_MASK = (-1 >>> 2);
 258 
 259     /**
 260      * Private key for retrieval of this object from ClassValueMap.
 261      */
 262     static class Identity {
 263     }
 264     /**
 265      * This ClassValue's identity, expressed as an opaque object.
 266      * The main object {@code ClassValue.this} is incorrect since
 267      * subclasses may override {@code ClassValue.equals}, which
 268      * could confuse keys in the ClassValueMap.
 269      */
 270     final Identity identity = new Identity();
 271 
 272     /**
 273      * Current version for retrieving this class value from the cache.
 274      * Any number of computeValue calls can be cached in association with one version.
 275      * But the version changes when a remove (on any type) is executed.
 276      * A version change invalidates all cache entries for the affected ClassValue,
 277      * by marking them as stale.  Stale cache entries do not force another call
 278      * to computeValue, but they do require a synchronized visit to a backing map.
 279      * <p>
 280      * All user-visible state changes on the ClassValue take place under
 281      * a lock inside the synchronized methods of ClassValueMap.
 282      * Readers (of ClassValue.get) are notified of such state changes
 283      * when this.version is bumped to a new token.
 284      * This variable must be volatile so that an unsynchronized reader
 285      * will receive the notification without delay.
 286      * <p>
 287      * If version were not volatile, one thread T1 could persistently hold onto
 288      * a stale value this.value == V1, while another thread T2 advances
 289      * (under a lock) to this.value == V2.  This will typically be harmless,
 290      * but if T1 and T2 interact causally via some other channel, such that
 291      * T1's further actions are constrained (in the JMM) to happen after
 292      * the V2 event, then T1's observation of V1 will be an error.
 293      * <p>
 294      * The practical effect of making this.version be volatile is that it cannot
 295      * be hoisted out of a loop (by an optimizing JIT) or otherwise cached.
 296      * Some machines may also require a barrier instruction to execute
 297      * before this.version.
 298      */
 299     private volatile Version<T> version = new Version<>(this);
 300     Version<T> version() { return version; }
 301     void bumpVersion() { version = new Version<>(this); }
 302     static class Version<T> {
 303         private final ClassValue<T> classValue;
 304         private final Entry<T> promise = new Entry<>(this);
 305         Version(ClassValue<T> classValue) { this.classValue = classValue; }
 306         ClassValue<T> classValue() { return classValue; }
 307         Entry<T> promise() { return promise; }
 308         boolean isLive() { return classValue.version() == this; }
 309     }
 310 
 311     /** One binding of a value to a class via a ClassValue.
 312      *  States are:<ul>
 313      *  <li> promise if value == Entry.this
 314      *  <li> else dead if version == null
 315      *  <li> else stale if version != classValue.version
 316      *  <li> else live </ul>
 317      *  Promises are never put into the cache; they only live in the
 318      *  backing map while a computeValue call is in flight.
 319      *  Once an entry goes stale, it can be reset at any time
 320      *  into the dead state.
 321      */
 322     static class Entry<T> extends WeakReference<Version<T>> {
 323         final Object value;  // usually of type T, but sometimes (Entry)this
 324         Entry(Version<T> version, T value) {
 325             super(version);
 326             this.value = value;  // for a regular entry, value is of type T
 327         }
 328         private void assertNotPromise() { assert(!isPromise()); }
 329         /** For creating a promise. */
 330         Entry(Version<T> version) {
 331             super(version);
 332             this.value = this;  // for a promise, value is not of type T, but Entry!
 333         }
 334         /** Fetch the value.  This entry must not be a promise. */
 335         @SuppressWarnings("unchecked")  // if !isPromise, type is T
 336         T value() { assertNotPromise(); return (T) value; }
 337         boolean isPromise() { return value == this; }
 338         Version<T> version() { return get(); }
 339         ClassValue<T> classValueOrNull() {
 340             Version<T> v = version();
 341             return (v == null) ? null : v.classValue();
 342         }
 343         boolean isLive() {
 344             Version<T> v = version();
 345             if (v == null)  return false;
 346             if (v.isLive())  return true;
 347             clear();
 348             return false;
 349         }
 350         Entry<T> refreshVersion(Version<T> v2) {
 351             assertNotPromise();
 352             @SuppressWarnings("unchecked")  // if !isPromise, type is T
 353             Entry<T> e2 = new Entry<>(v2, (T) value);
 354             clear();
 355             // value = null -- caller must drop
 356             return e2;
 357         }
 358         static final Entry<?> DEAD_ENTRY = new Entry<>(null, null);
 359     }
 360 
 361     /** Return the backing map associated with this type. */
 362     private static ClassValueMap getMap(Class<?> type) {
 363         // racing type.classValueMap : null (blank) => unique ClassValueMap
 364         // if a null is observed, a map is created (lazily, synchronously, uniquely)
 365         // all further access to that map is synchronized
 366         ClassValueMap map = type.classValueMap;
 367         if (map != null)  return map;
 368         return initializeMap(type);
 369     }
 370 
 371     private static final Object CRITICAL_SECTION = new Object();
 372     private static ClassValueMap initializeMap(Class<?> type) {
 373         ClassValueMap map;
 374         synchronized (CRITICAL_SECTION) {  // private object to avoid deadlocks
 375             // happens about once per type
 376             if ((map = type.classValueMap) == null)
 377                 type.classValueMap = map = new ClassValueMap();
 378         }
 379         return map;
 380     }
 381 
 382     static <T> Entry<T> makeEntry(Version<T> explicitVersion, T value) {
 383         // Note that explicitVersion might be different from this.version.
 384         return new Entry<>(explicitVersion, value);
 385 
 386         // As soon as the Entry is put into the cache, the value will be
 387         // reachable via a data race (as defined by the Java Memory Model).
 388         // This race is benign, assuming the value object itself can be
 389         // read safely by multiple threads.  This is up to the user.
 390         //
 391         // The entry and version fields themselves can be safely read via
 392         // a race because they are either final or have controlled states.
 393         // If the pointer from the entry to the version is still null,
 394         // or if the version goes immediately dead and is nulled out,
 395         // the reader will take the slow path and retry under a lock.
 396     }
 397 
 398     // The following class could also be top level and non-public:
 399 
 400     /** A backing map for all ClassValues.
 401      *  Gives a fully serialized "true state" for each pair (ClassValue cv, Class type).
 402      *  Also manages an unserialized fast-path cache.
 403      */
 404     static class ClassValueMap extends WeakHashMap<ClassValue.Identity, Entry<?>> {
 405         private Entry<?>[] cacheArray;
 406         private int cacheLoad, cacheLoadLimit;
 407 
 408         /** Number of entries initially allocated to each type when first used with any ClassValue.
 409          *  It would be pointless to make this much smaller than the Class and ClassValueMap objects themselves.
 410          *  Must be a power of 2.
 411          */
 412         private static final int INITIAL_ENTRIES = 32;
 413 
 414         /** Build a backing map for ClassValues.
 415          *  Also, create an empty cache array and install it on the class.
 416          */
 417         ClassValueMap() {
 418             sizeCache(INITIAL_ENTRIES);
 419         }
 420 
 421         Entry<?>[] getCache() { return cacheArray; }
 422 
 423         /** Initiate a query.  Store a promise (placeholder) if there is no value yet. */
 424         synchronized
 425         <T> Entry<T> startEntry(ClassValue<T> classValue) {
 426             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
 427             Entry<T> e = (Entry<T>) get(classValue.identity);
 428             Version<T> v = classValue.version();
 429             if (e == null) {
 430                 e = v.promise();
 431                 // The presence of a promise means that a value is pending for v.
 432                 // Eventually, finishEntry will overwrite the promise.
 433                 put(classValue.identity, e);
 434                 // Note that the promise is never entered into the cache!
 435                 return e;
 436             } else if (e.isPromise()) {
 437                 // Somebody else has asked the same question.
 438                 // Let the races begin!
 439                 if (e.version() != v) {
 440                     e = v.promise();
 441                     put(classValue.identity, e);
 442                 }
 443                 return e;
 444             } else {
 445                 // there is already a completed entry here; report it
 446                 if (e.version() != v) {
 447                     // There is a stale but valid entry here; make it fresh again.
 448                     // Once an entry is in the hash table, we don't care what its version is.
 449                     e = e.refreshVersion(v);
 450                     put(classValue.identity, e);
 451                 }
 452                 // Add to the cache, to enable the fast path, next time.
 453                 checkCacheLoad();
 454                 addToCache(classValue, e);
 455                 return e;
 456             }
 457         }
 458 
 459         /** Finish a query.  Overwrite a matching placeholder.  Drop stale incoming values. */
 460         synchronized
 461         <T> Entry<T> finishEntry(ClassValue<T> classValue, Entry<T> e) {
 462             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
 463             Entry<T> e0 = (Entry<T>) get(classValue.identity);
 464             if (e == e0) {
 465                 // We can get here during exception processing, unwinding from computeValue.
 466                 assert(e.isPromise());
 467                 remove(classValue.identity);
 468                 return null;
 469             } else if (e0 != null && e0.isPromise() && e0.version() == e.version()) {
 470                 // If e0 matches the intended entry, there has not been a remove call
 471                 // between the previous startEntry and now.  So now overwrite e0.
 472                 Version<T> v = classValue.version();
 473                 if (e.version() != v)
 474                     e = e.refreshVersion(v);
 475                 put(classValue.identity, e);
 476                 // Add to the cache, to enable the fast path, next time.
 477                 checkCacheLoad();
 478                 addToCache(classValue, e);
 479                 return e;
 480             } else {
 481                 // Some sort of mismatch; caller must try again.
 482                 return null;
 483             }
 484         }
 485 
 486         /** Remove an entry. */
 487         synchronized
 488         void removeEntry(ClassValue<?> classValue) {
 489             Entry<?> e = remove(classValue.identity);
 490             if (e == null) {
 491                 // Uninitialized, and no pending calls to computeValue.  No change.
 492             } else if (e.isPromise()) {
 493                 // State is uninitialized, with a pending call to finishEntry.
 494                 // Since remove is a no-op in such a state, keep the promise
 495                 // by putting it back into the map.
 496                 put(classValue.identity, e);
 497             } else {
 498                 // In an initialized state.  Bump forward, and de-initialize.
 499                 classValue.bumpVersion();
 500                 // Make all cache elements for this guy go stale.
 501                 removeStaleEntries(classValue);
 502             }
 503         }
 504 
 505         /** Change the value for an entry. */
 506         synchronized
 507         <T> void changeEntry(ClassValue<T> classValue, T value) {
 508             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
 509             Entry<T> e0 = (Entry<T>) get(classValue.identity);
 510             Version<T> version = classValue.version();
 511             if (e0 != null) {
 512                 if (e0.version() == version && e0.value() == value)
 513                     // no value change => no version change needed
 514                     return;
 515                 classValue.bumpVersion();
 516                 removeStaleEntries(classValue);
 517             }
 518             Entry<T> e = makeEntry(version, value);
 519             put(classValue.identity, e);
 520             // Add to the cache, to enable the fast path, next time.
 521             checkCacheLoad();
 522             addToCache(classValue, e);
 523         }
 524 
 525         /// --------
 526         /// Cache management.
 527         /// --------
 528 
 529         // Statics do not need synchronization.
 530 
 531         /** Load the cache entry at the given (hashed) location. */
 532         static Entry<?> loadFromCache(Entry<?>[] cache, int i) {
 533             // non-racing cache.length : constant
 534             // racing cache[i & (mask)] : null <=> Entry
 535             return cache[i & (cache.length-1)];
 536             // invariant:  returned value is null or well-constructed (ready to match)
 537         }
 538 
 539         /** Look in the cache, at the home location for the given ClassValue. */
 540         static <T> Entry<T> probeHomeLocation(Entry<?>[] cache, ClassValue<T> classValue) {
 541             return classValue.castEntry(loadFromCache(cache, classValue.hashCodeForCache));
 542         }
 543 
 544         /** Given that first probe was a collision, retry at nearby locations. */
 545         static <T> Entry<T> probeBackupLocations(Entry<?>[] cache, ClassValue<T> classValue) {
 546             if (PROBE_LIMIT <= 0)  return null;
 547             // Probe the cache carefully, in a range of slots.
 548             int mask = (cache.length-1);
 549             int home = (classValue.hashCodeForCache & mask);
 550             Entry<?> e2 = cache[home];  // victim, if we find the real guy
 551             if (e2 == null) {
 552                 return null;   // if nobody is at home, no need to search nearby
 553             }
 554             // assume !classValue.match(e2), but do not assert, because of races
 555             int pos2 = -1;
 556             for (int i = home + 1; i < home + PROBE_LIMIT; i++) {
 557                 Entry<?> e = cache[i & mask];
 558                 if (e == null) {
 559                     break;   // only search within non-null runs
 560                 }
 561                 if (classValue.match(e)) {
 562                     // relocate colliding entry e2 (from cache[home]) to first empty slot
 563                     cache[home] = e;
 564                     if (pos2 >= 0) {
 565                         cache[i & mask] = Entry.DEAD_ENTRY;
 566                     } else {
 567                         pos2 = i;
 568                     }
 569                     cache[pos2 & mask] = ((entryDislocation(cache, pos2, e2) < PROBE_LIMIT)
 570                                           ? e2                  // put e2 here if it fits
 571                                           : Entry.DEAD_ENTRY);
 572                     return classValue.castEntry(e);
 573                 }
 574                 // Remember first empty slot, if any:
 575                 if (!e.isLive() && pos2 < 0)  pos2 = i;
 576             }
 577             return null;
 578         }
 579 
 580         /** How far out of place is e? */
 581         private static int entryDislocation(Entry<?>[] cache, int pos, Entry<?> e) {
 582             ClassValue<?> cv = e.classValueOrNull();
 583             if (cv == null)  return 0;  // entry is not live!
 584             int mask = (cache.length-1);
 585             return (pos - cv.hashCodeForCache) & mask;
 586         }
 587 
 588         /// --------
 589         /// Below this line all functions are private, and assume synchronized access.
 590         /// --------
 591 
 592         private void sizeCache(int length) {
 593             assert((length & (length-1)) == 0);  // must be power of 2
 594             cacheLoad = 0;
 595             cacheLoadLimit = (int) ((double) length * CACHE_LOAD_LIMIT / 100);
 596             cacheArray = new Entry<?>[length];
 597         }
 598 
 599         /** Make sure the cache load stays below its limit, if possible. */
 600         private void checkCacheLoad() {
 601             if (cacheLoad >= cacheLoadLimit) {
 602                 reduceCacheLoad();
 603             }
 604         }
 605         private void reduceCacheLoad() {
 606             removeStaleEntries();
 607             if (cacheLoad < cacheLoadLimit)
 608                 return;  // win
 609             Entry<?>[] oldCache = getCache();
 610             if (oldCache.length > HASH_MASK)
 611                 return;  // lose
 612             sizeCache(oldCache.length * 2);
 613             for (Entry<?> e : oldCache) {
 614                 if (e != null && e.isLive()) {
 615                     addToCache(e);
 616                 }
 617             }
 618         }
 619 
 620         /** Remove stale entries in the given range.
 621          *  Should be executed under a Map lock.
 622          */
 623         private void removeStaleEntries(Entry<?>[] cache, int begin, int count) {
 624             if (PROBE_LIMIT <= 0)  return;
 625             int mask = (cache.length-1);
 626             int removed = 0;
 627             for (int i = begin; i < begin + count; i++) {
 628                 Entry<?> e = cache[i & mask];
 629                 if (e == null || e.isLive())
 630                     continue;  // skip null and live entries
 631                 Entry<?> replacement = null;
 632                 if (PROBE_LIMIT > 1) {
 633                     // avoid breaking up a non-null run
 634                     replacement = findReplacement(cache, i);
 635                 }
 636                 cache[i & mask] = replacement;
 637                 if (replacement == null)  removed += 1;
 638             }
 639             cacheLoad = Math.max(0, cacheLoad - removed);
 640         }
 641 
 642         /** Clearing a cache slot risks disconnecting following entries
 643          *  from the head of a non-null run, which would allow them
 644          *  to be found via reprobes.  Find an entry after cache[begin]
 645          *  to plug into the hole, or return null if none is needed.
 646          */
 647         private Entry<?> findReplacement(Entry<?>[] cache, int home1) {
 648             Entry<?> replacement = null;
 649             int haveReplacement = -1, replacementPos = 0;
 650             int mask = (cache.length-1);
 651             for (int i2 = home1 + 1; i2 < home1 + PROBE_LIMIT; i2++) {
 652                 Entry<?> e2 = cache[i2 & mask];
 653                 if (e2 == null)  break;  // End of non-null run.
 654                 if (!e2.isLive())  continue;  // Doomed anyway.
 655                 int dis2 = entryDislocation(cache, i2, e2);
 656                 if (dis2 == 0)  continue;  // e2 already optimally placed
 657                 int home2 = i2 - dis2;
 658                 if (home2 <= home1) {
 659                     // e2 can replace entry at cache[home1]
 660                     if (home2 == home1) {
 661                         // Put e2 exactly where he belongs.
 662                         haveReplacement = 1;
 663                         replacementPos = i2;
 664                         replacement = e2;
 665                     } else if (haveReplacement <= 0) {
 666                         haveReplacement = 0;
 667                         replacementPos = i2;
 668                         replacement = e2;
 669                     }
 670                     // And keep going, so we can favor larger dislocations.
 671                 }
 672             }
 673             if (haveReplacement >= 0) {
 674                 if (cache[(replacementPos+1) & mask] != null) {
 675                     // Be conservative, to avoid breaking up a non-null run.
 676                     cache[replacementPos & mask] = (Entry<?>) Entry.DEAD_ENTRY;
 677                 } else {
 678                     cache[replacementPos & mask] = null;
 679                     cacheLoad -= 1;
 680                 }
 681             }
 682             return replacement;
 683         }
 684 
 685         /** Remove stale entries in the range near classValue. */
 686         private void removeStaleEntries(ClassValue<?> classValue) {
 687             removeStaleEntries(getCache(), classValue.hashCodeForCache, PROBE_LIMIT);







 688         }
 689 
 690         /** Remove all stale entries, everywhere. */
 691         private void removeStaleEntries() {
 692             Entry<?>[] cache = getCache();
 693             removeStaleEntries(cache, 0, cache.length + PROBE_LIMIT - 1);


 694         }
 695 
 696         /** Add the given entry to the cache, in its home location, unless it is out of date. */
 697         private <T> void addToCache(Entry<T> e) {
 698             ClassValue<T> classValue = e.classValueOrNull();
 699             if (classValue != null)
 700                 addToCache(classValue, e);
 701         }
 702 
 703         /** Add the given entry to the cache, in its home location. */
 704         private <T> void addToCache(ClassValue<T> classValue, Entry<T> e) {
 705             if (PROBE_LIMIT <= 0)  return;  // do not fill cache
 706             // Add e to the cache.
 707             Entry<?>[] cache = getCache();
 708             int mask = (cache.length-1);
 709             int home = classValue.hashCodeForCache & mask;
 710             Entry<?> e2 = placeInCache(cache, home, e, false);
 711             if (e2 == null)  return;  // done
 712             if (PROBE_LIMIT > 1) {
 713                 // try to move e2 somewhere else in his probe range
 714                 int dis2 = entryDislocation(cache, home, e2);
 715                 int home2 = home - dis2;
 716                 for (int i2 = home2; i2 < home2 + PROBE_LIMIT; i2++) {
 717                     if (placeInCache(cache, i2 & mask, e2, true) == null) {
 718                         return;
 719                     }






 720                 }
 721             }
 722             // Note:  At this point, e2 is just dropped from the cache.
 723         }
 724 
 725         /** Store the given entry.  Update cacheLoad, and return any live victim.
 726          *  'Gently' means return self rather than dislocating a live victim.
 727          */
 728         private Entry<?> placeInCache(Entry<?>[] cache, int pos, Entry<?> e, boolean gently) {
 729             Entry<?> e2 = overwrittenEntry(cache[pos]);
 730             if (gently && e2 != null) {
 731                 // do not overwrite a live entry
 732                 return e;
 733             } else {
 734                 cache[pos] = e;
 735                 return e2;
 736             }
 737         }
 738 
 739         /** Note an entry that is about to be overwritten.
 740          *  If it is not live, quietly replace it by null.
 741          *  If it is an actual null, increment cacheLoad,
 742          *  because the caller is going to store something
 743          *  in its place.
 744          */
 745         private <T> Entry<T> overwrittenEntry(Entry<T> e2) {
 746             if (e2 == null)  cacheLoad += 1;
 747             else if (e2.isLive())  return e2;
 748             return null;
 749         }
 750 
 751         /** Percent loading of cache before resize. */
 752         private static final int CACHE_LOAD_LIMIT = 67;  // 0..100
 753         /** Maximum number of probes to attempt. */
 754         private static final int PROBE_LIMIT      =  6;       // 1..
 755         // N.B.  Set PROBE_LIMIT=0 to disable all fast paths.
 756     }
 757 }
   1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import jdk.internal.misc.Unsafe;
  29 import jdk.internal.vm.annotation.ForceInline;
  30 import jdk.internal.vm.annotation.Stable;
  31 
  32 import java.lang.ref.PhantomReference;
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;
  35 import java.util.concurrent.ConcurrentHashMap;
  36 import java.util.concurrent.atomic.AtomicBoolean;
  37 import java.util.concurrent.atomic.AtomicInteger;
  38 import java.util.concurrent.atomic.AtomicReference;


  39 
  40 /**
  41  * Lazily associate a computed value with (potentially) every type.
  42  * For example, if a dynamic language needs to construct a message dispatch
  43  * table for each class encountered at a message send call site,
  44  * it can use a {@code ClassValue} to cache information needed to
  45  * perform the message send quickly, for each class encountered.
  46  * @author John Rose, JSR 292 EG
  47  * @since 1.7
  48  */
  49 public abstract class ClassValue<T> {
  50     /**
  51      * Sole constructor.  (For invocation by subclass constructors, typically
  52      * implicit.)
  53      */
  54     protected ClassValue() {
  55     }
  56 
  57     /**
  58      * Computes the given class's derived value for this {@code ClassValue}.


  83      * is performed atomically.
  84      * At that point, if several racing threads have
  85      * computed values, one is chosen, and returned to
  86      * all the racing threads.
  87      * <p>
  88      * The {@code type} parameter is typically a class, but it may be any type,
  89      * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
  90      * <p>
  91      * In the absence of {@code remove} calls, a class value has a simple
  92      * state diagram:  uninitialized and initialized.
  93      * When {@code remove} calls are made,
  94      * the rules for value observation are more complex.
  95      * See the documentation for {@link #remove remove} for more information.
  96      *
  97      * @param type the type whose class value must be computed or retrieved
  98      * @return the current value associated with this {@code ClassValue}, for the given class or interface
  99      * @throws NullPointerException if the argument is null
 100      * @see #remove
 101      * @see #computeValue
 102      */
 103     @SuppressWarnings("unchecked")
 104     @ForceInline
 105     public T get(Class<?> type) {
 106         ClassValueMap map = type.classValueMap;
 107         Object value;
 108         if (map != null) {
 109             value = map.get(key);
 110             if (value != null && !(value instanceof Removed)) {
 111                 return (value == NULL_MASK) ? null : (T) value;
 112             }
 113         } else {
 114             value = null;
 115         }
 116         return getSlow(type, value);
 117     }
 118     // ...the rest in separate method
 119     @SuppressWarnings("unchecked")
 120     private T getSlow(Class<?> type, Object oldValue) {
 121         ClassValueMap.expungeStaleEntries();
 122         ClassValueMap map = getMap(type);
 123         while (true) {
 124             // assert oldValue == null || oldValue instanceof Removed;
 125             T newValue = computeValue(type);
 126             if (oldValue == null) {
 127                 oldValue = map.putIfAbsent(key, (newValue == null) ? NULL_MASK : newValue);
 128                 if (oldValue == null) {
 129                     return newValue;
 130                 } else {
 131                     // another thread has beaten us
 132                     if (!(oldValue instanceof Removed)) {
 133                         return (oldValue == NULL_MASK) ? null : (T) oldValue;
 134                     }
 135                 }
 136             } else {
 137                 // assert oldValue instanceof Removed;
 138                 if (map.replace(key, oldValue, (newValue == null) ? NULL_MASK : newValue)) {
 139                     // this only succeeds if oldValue is still the same instance of
 140                     // Removed as observed before computeValue invocation
 141                     return newValue;
 142                 } else {
 143                     // another thread has beaten us
 144                     oldValue = map.get(key);
 145                     if (oldValue != null && !(oldValue instanceof Removed)) {
 146                         return (oldValue == NULL_MASK) ? null : (T) oldValue;
 147                     }
 148                 }
 149             }
 150             // retry compute since we observed another version of Removed
 151             // as was current before compute which means that there was
 152             // at least one value present in between time which means
 153             // that the result of our compute is stale.
 154         }
 155     }
 156 
 157     /**
 158      * Removes the associated value for the given class.
 159      * If this value is subsequently {@linkplain #get read} for the same class,
 160      * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
 161      * This may result in an additional invocation of the
 162      * {@code computeValue} method for the given class.
 163      * <p>
 164      * In order to explain the interaction between {@code get} and {@code remove} calls,
 165      * we must model the state transitions of a class value to take into account
 166      * the alternation between uninitialized and initialized states.
 167      * To do this, number these states sequentially from zero, and note that
 168      * uninitialized (or removed) states are numbered with even numbers,
 169      * while initialized (or re-initialized) states have odd numbers.
 170      * <p>
 171      * When a thread {@code T} removes a class value in state {@code 2N},
 172      * nothing happens, since the class value is already uninitialized.
 173      * Otherwise, the state is advanced atomically to {@code 2N+1}.
 174      * <p>


 191      * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
 192      * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
 193      * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
 194      * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
 195      * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
 196      * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
 197      * <li> the previous actions of {@code T2} are repeated several times
 198      * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
 199      * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
 200      * </ul>
 201      * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
 202      * observe the time-dependent states as it computes {@code V1}, etc.
 203      * This does not remove the threat of a stale value, since there is a window of time
 204      * between the return of {@code computeValue} in {@code T} and the installation
 205      * of the new value.  No user synchronization is possible during this time.
 206      *
 207      * @param type the type whose class value must be removed
 208      * @throws NullPointerException if the argument is null
 209      */
 210     public void remove(Class<?> type) {
 211         ClassValueMap.expungeStaleEntries();
 212         ClassValueMap map = type.classValueMap;
 213         if (map != null) {
 214             Object value, removed = null;
 215             do {
 216                 value = map.get(key);
 217                 if (value == null || value instanceof Removed) {
 218                     // non-existence is treated as Removed(V0)
 219                     // invoking remove() when state is Removed(VN) does not
 220                     // change it to Removed(VN+1)
 221                     break;
 222                 }
 223                 if (removed == null) {
 224                     removed = new Removed();
 225                 }
 226             } while (!map.replace(key, value, removed));
 227         }
 228     }
 229 
 230     // Possible functionality for JSR 292 MR 1
 231     /*public*/ void put(Class<?> type, T value) {
 232         ClassValueMap.expungeStaleEntries();
 233         ClassValueMap map = getMap(type);
 234         map.put(key, (value == null) ? NULL_MASK : value);
 235     }
 236 
 237     /// --------
 238     /// Implementation...
 239     /// --------
 240 













 241     /**
 242      * The key used in ClassValueMap for this ClassValue.
 243      * Since ClassValue(s) are usually assigned to static final fields, it makes
 244      * sense to promote the key to constant too.

 245      */
 246     @Stable
 247     private final Key key = new Key(this);




 248 
 249     /**
 250      * Key of a ClassValue and a tracker of the ClassValue at the same time.



 251      */
 252     private static final class Key extends PhantomReference<ClassValue<?>> {































 253 
 254         /** Value stream for hash. See similar structure in ThreadLocal. */
 255         private static final AtomicInteger nextHash = new AtomicInteger();
 256 
 257         /** Good for power-of-two tables. See similar structure in ThreadLocal. */
 258         private static final int HASH_INCREMENT = 0x61c88647;
 259 
 260         /** Single global queue for all stale keys to be enqueued */
 261         static final ReferenceQueue<ClassValue<?>> queue = new ReferenceQueue<>();
 262 
 263         @Stable
 264         private final int hash;










 265 
 266         Key(ClassValue<?> cv) {
 267             super(cv, queue);
 268             int h = nextHash.getAndAdd(HASH_INCREMENT);
 269             // undo the effect of CHM.spread()
 270             this.hash = h ^ (h >>> 16);


















































































 271         }
 272 
 273         @Override
 274         public int hashCode() {
 275             return hash;





 276         }
 277 
 278         // equals is identity








 279     }
 280 
 281     /** A masked null value. */
 282     private static final Object NULL_MASK = new Object();





























 283 
 284     /** Removed value(s) */
 285     private static final class Removed {}




 286 
 287     /** Return the backing map associated with this type, possibly constructing it. */
 288     private static ClassValueMap getMap(Class<?> type) {
 289         ClassValueMap map = type.classValueMap;
 290         return (map != null) ? map : initializeMap(type);
































 291     }
 292 
 293     private static ClassValueMap initializeMap(Class<?> type) {
 294         ClassValueMap map = ClassValueMap.create();
 295         return U.compareAndSwapObject(type, CLASS_VALUE_MAP_OFFSET, null, map)
 296                ? map
 297                : (ClassValueMap) U.getObjectVolatile(type, CLASS_VALUE_MAP_OFFSET);




















 298     }
 299 
 300     /** A backing map for all ClassValues in a particular Class.
 301      */
 302     static final class ClassValueMap extends ConcurrentHashMap<Key, Object> {
 303         // eh, needed to silent the gods
 304         private static final long serialVersionUID = 1L;













 305 
 306         // a node in a linked list of WeakReference(s) to ClassValueMap(s)
 307         private static final class WeakNode extends WeakReference<ClassValueMap> {
 308             WeakNode next;
 309             WeakNode(ClassValueMap map) { super(map); }














 310         }
 311 
 312         // the head of the linked list of WeakReference(s) to ClassValueMap(s)
 313         private static final AtomicReference<WeakNode> mapsList = new AtomicReference<>();



 314 
 315         // the lock used to guard expunging logic
 316         private static final AtomicBoolean expungeInProgress = new AtomicBoolean();














































 317 
 318         private ClassValueMap() {
 319             super(1);




 320         }
 321 
 322         static ClassValueMap create() {
 323             ClassValueMap map = new ClassValueMap();
 324             WeakNode node = new WeakNode(map);
 325             // register the weakly referenced map into the list
 326             do {
 327                 node.next = mapsList.get();
 328             } while (!mapsList.compareAndSet(node.next, node));
 329             return map;

 330         }
 331 
 332         static void expungeStaleEntries() {
 333             // make sure only one thread at a time is expunging stale entries;
 334             // this is important for correctness so that no stale enqueued key
 335             // is missed from being removed from any map that might possibly
 336             // hold it.
 337             if (!expungeInProgress.get() &&
 338                 expungeInProgress.compareAndSet(false, true)) try {
 339                 expungeStaleEntriesImpl();
 340             } finally {
 341                 expungeInProgress.set(false);








 342             }
 343         }
 344 
 345         private static void expungeStaleEntriesImpl() {
 346             // start with an empty private list
 347             WeakNode first = null, last = null;
 348             // iterate Key(s) of GCed ClassValue(s)
 349             Key key;
 350             while ((key = (Key) Key.queue.poll()) != null) {
 351                 // steal any (newly) registered map(s) from the shared 'mapsList'
 352                 // as they might already be holding the stale 'key' that has just
 353                 // been removed from the queue. The following guarantees that
 354                 // no (stale-key, live-map) combination is missed:
 355                 // - a map is 1st registered into the mapsList and then non-stale
 356                 //   keys are inserted into it
 357                 // - a non-stale key becomes stale when a ClassValue that it
 358                 //   phantomly references is GCed; the key is then enqueued
 359                 // - a stale key is 1st removed from the queue and then mapsList
 360                 //   is checked for any (newly) registered map(s)
 361                 if (mapsList.get() != null) {
 362                     WeakNode stolen = mapsList.getAndSet(null);
 363                     // add them to the end of current private list
 364                     if (last != null) {
 365                         last.next = stolen;




































 366                     } else {
 367                         // assert first == null;
 368                         first = stolen;

 369                     }

 370                 }
 371                 // iterate the accumulated private list of maps
 372                 last = null;
 373                 for (WeakNode node = first; node != null; ) {
 374                     ClassValueMap map = node.get();
 375                     if (map == null) {
 376                         // Class and its ClassValueMap have already been GCed
 377                         // -> remove the node from the list
 378                         if (last == null) {
 379                             first = node = node.next;
 380                         } else {
 381                             last.next = node = node.next;
 382                         }
 383                     } else {
 384                         // ClassValueMap is still alive
 385                         // -> attempt to remove the key from it
 386                         map.remove(key);
 387                         // navigate to next in list
 388                         last = node;
 389                         node = node.next;
 390                     }






 391                 }
 392                 // at this time, 'last' points to the last WeakNode in the private
 393                 // list (if any) and the following always holds:
 394                 // assert (first == null) == (last == null);














 395             }
 396             // re-insert the stolen and expunged list of map(s) back into shared
 397             // mapsList
 398             if (last != null) {
 399                 do {
 400                     last.next = mapsList.get();
 401                 } while (!mapsList.compareAndSet(last.next, first));
 402             }
 403         }

 404     }
 405 
 406     /** We need to cas */
 407     private static final Unsafe U = Unsafe.getUnsafe();
 408     private static final long CLASS_VALUE_MAP_OFFSET;










 409 
 410     static {
 411         try {
 412             CLASS_VALUE_MAP_OFFSET = U.objectFieldOffset(
 413                 Class.class.getDeclaredField("classValueMap"));
 414         } catch (Exception e) {
 415             throw new Error(e);




 416         }






 417     }
 418 }
< prev index next >