src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java

Print this page




 261         }
 262     }
 263 
 264     private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
 265         private static final Unsafe unsafe = Unsafe.getUnsafe();
 266         private final long offset;
 267         private final Class<T> tclass;
 268         private final Class cclass;
 269 
 270         CASUpdater(Class<T> tclass, String fieldName) {
 271             Field field = null;
 272             Class caller = null;
 273             int modifiers = 0;
 274             try {
 275                 field = tclass.getDeclaredField(fieldName);
 276                 caller = sun.reflect.Reflection.getCallerClass(3);
 277                 modifiers = field.getModifiers();
 278                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 279                     caller, tclass, null, modifiers);
 280                 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
 281             } catch(Exception ex) {
 282                 throw new RuntimeException(ex);
 283             }
 284 
 285             Class fieldt = field.getType();
 286             if (fieldt != long.class)
 287                 throw new IllegalArgumentException("Must be long type");
 288 
 289             if (!Modifier.isVolatile(modifiers))
 290                 throw new IllegalArgumentException("Must be volatile type");
 291 
 292             this.cclass = (Modifier.isProtected(modifiers) &&
 293                            caller != tclass) ? caller : null;
 294             this.tclass = tclass;
 295             offset = unsafe.objectFieldOffset(field);
 296         }
 297 
 298         private void fullCheck(T obj) {
 299             if (!tclass.isInstance(obj))
 300                 throw new ClassCastException();
 301             if (cclass != null)


 314 
 315         public void set(T obj, long newValue) {
 316             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 317             unsafe.putLongVolatile(obj, offset, newValue);
 318         }
 319 
 320         public void lazySet(T obj, long newValue) {
 321             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 322             unsafe.putOrderedLong(obj, offset, newValue);
 323         }
 324 
 325         public long get(T obj) {
 326             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 327             return unsafe.getLongVolatile(obj, offset);
 328         }
 329 
 330         private void ensureProtectedAccess(T obj) {
 331             if (cclass.isInstance(obj)) {
 332                 return;
 333             }
 334             throw new RuntimeException (
 335                 new IllegalAccessException("Class " +
 336                     cclass.getName() +
 337                     " can not access a protected member of class " +
 338                     tclass.getName() +
 339                     " using an instance of " +
 340                     obj.getClass().getName()
 341                 )
 342             );
 343         }
 344     }
 345 
 346 
 347     private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
 348         private static final Unsafe unsafe = Unsafe.getUnsafe();
 349         private final long offset;
 350         private final Class<T> tclass;
 351         private final Class cclass;
 352 
 353         LockedUpdater(Class<T> tclass, String fieldName) {
 354             Field field = null;
 355             Class caller = null;
 356             int modifiers = 0;
 357             try {
 358                 field = tclass.getDeclaredField(fieldName);
 359                 caller = sun.reflect.Reflection.getCallerClass(3);
 360                 modifiers = field.getModifiers();
 361                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 362                     caller, tclass, null, modifiers);
 363                 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
 364             } catch(Exception ex) {
 365                 throw new RuntimeException(ex);
 366             }
 367 
 368             Class fieldt = field.getType();
 369             if (fieldt != long.class)
 370                 throw new IllegalArgumentException("Must be long type");
 371 
 372             if (!Modifier.isVolatile(modifiers))
 373                 throw new IllegalArgumentException("Must be volatile type");
 374 
 375             this.cclass = (Modifier.isProtected(modifiers) &&
 376                            caller != tclass) ? caller : null;
 377             this.tclass = tclass;
 378             offset = unsafe.objectFieldOffset(field);
 379         }
 380 
 381         private void fullCheck(T obj) {
 382             if (!tclass.isInstance(obj))
 383                 throw new ClassCastException();
 384             if (cclass != null)
 385                 ensureProtectedAccess(obj);
 386         }
 387 
 388         public boolean compareAndSet(T obj, long expect, long update) {
 389             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 390             synchronized(this) {
 391                 long v = unsafe.getLong(obj, offset);
 392                 if (v != expect)
 393                     return false;
 394                 unsafe.putLong(obj, offset, update);
 395                 return true;
 396             }
 397         }
 398 
 399         public boolean weakCompareAndSet(T obj, long expect, long update) {
 400             return compareAndSet(obj, expect, update);
 401         }
 402 
 403         public void set(T obj, long newValue) {
 404             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 405             synchronized(this) {
 406                 unsafe.putLong(obj, offset, newValue);
 407             }
 408         }
 409 
 410         public void lazySet(T obj, long newValue) {
 411             set(obj, newValue);
 412         }
 413 
 414         public long get(T obj) {
 415             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 416             synchronized(this) {
 417                 return unsafe.getLong(obj, offset);
 418             }
 419         }
 420 
 421         private void ensureProtectedAccess(T obj) {
 422             if (cclass.isInstance(obj)) {
 423                 return;
 424             }
 425             throw new RuntimeException (
 426                 new IllegalAccessException("Class " +
 427                     cclass.getName() +
 428                     " can not access a protected member of class " +
 429                     tclass.getName() +
 430                     " using an instance of " +
 431                     obj.getClass().getName()
 432                 )
 433             );
 434         }
 435     }
 436 }


 261         }
 262     }
 263 
 264     private static class CASUpdater<T> extends AtomicLongFieldUpdater<T> {
 265         private static final Unsafe unsafe = Unsafe.getUnsafe();
 266         private final long offset;
 267         private final Class<T> tclass;
 268         private final Class cclass;
 269 
 270         CASUpdater(Class<T> tclass, String fieldName) {
 271             Field field = null;
 272             Class caller = null;
 273             int modifiers = 0;
 274             try {
 275                 field = tclass.getDeclaredField(fieldName);
 276                 caller = sun.reflect.Reflection.getCallerClass(3);
 277                 modifiers = field.getModifiers();
 278                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 279                     caller, tclass, null, modifiers);
 280                 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
 281             } catch (Exception ex) {
 282                 throw new RuntimeException(ex);
 283             }
 284 
 285             Class fieldt = field.getType();
 286             if (fieldt != long.class)
 287                 throw new IllegalArgumentException("Must be long type");
 288 
 289             if (!Modifier.isVolatile(modifiers))
 290                 throw new IllegalArgumentException("Must be volatile type");
 291 
 292             this.cclass = (Modifier.isProtected(modifiers) &&
 293                            caller != tclass) ? caller : null;
 294             this.tclass = tclass;
 295             offset = unsafe.objectFieldOffset(field);
 296         }
 297 
 298         private void fullCheck(T obj) {
 299             if (!tclass.isInstance(obj))
 300                 throw new ClassCastException();
 301             if (cclass != null)


 314 
 315         public void set(T obj, long newValue) {
 316             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 317             unsafe.putLongVolatile(obj, offset, newValue);
 318         }
 319 
 320         public void lazySet(T obj, long newValue) {
 321             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 322             unsafe.putOrderedLong(obj, offset, newValue);
 323         }
 324 
 325         public long get(T obj) {
 326             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 327             return unsafe.getLongVolatile(obj, offset);
 328         }
 329 
 330         private void ensureProtectedAccess(T obj) {
 331             if (cclass.isInstance(obj)) {
 332                 return;
 333             }
 334             throw new RuntimeException(
 335                 new IllegalAccessException("Class " +
 336                     cclass.getName() +
 337                     " can not access a protected member of class " +
 338                     tclass.getName() +
 339                     " using an instance of " +
 340                     obj.getClass().getName()
 341                 )
 342             );
 343         }
 344     }
 345 
 346 
 347     private static class LockedUpdater<T> extends AtomicLongFieldUpdater<T> {
 348         private static final Unsafe unsafe = Unsafe.getUnsafe();
 349         private final long offset;
 350         private final Class<T> tclass;
 351         private final Class cclass;
 352 
 353         LockedUpdater(Class<T> tclass, String fieldName) {
 354             Field field = null;
 355             Class caller = null;
 356             int modifiers = 0;
 357             try {
 358                 field = tclass.getDeclaredField(fieldName);
 359                 caller = sun.reflect.Reflection.getCallerClass(3);
 360                 modifiers = field.getModifiers();
 361                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
 362                     caller, tclass, null, modifiers);
 363                 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
 364             } catch (Exception ex) {
 365                 throw new RuntimeException(ex);
 366             }
 367 
 368             Class fieldt = field.getType();
 369             if (fieldt != long.class)
 370                 throw new IllegalArgumentException("Must be long type");
 371 
 372             if (!Modifier.isVolatile(modifiers))
 373                 throw new IllegalArgumentException("Must be volatile type");
 374 
 375             this.cclass = (Modifier.isProtected(modifiers) &&
 376                            caller != tclass) ? caller : null;
 377             this.tclass = tclass;
 378             offset = unsafe.objectFieldOffset(field);
 379         }
 380 
 381         private void fullCheck(T obj) {
 382             if (!tclass.isInstance(obj))
 383                 throw new ClassCastException();
 384             if (cclass != null)
 385                 ensureProtectedAccess(obj);
 386         }
 387 
 388         public boolean compareAndSet(T obj, long expect, long update) {
 389             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 390             synchronized (this) {
 391                 long v = unsafe.getLong(obj, offset);
 392                 if (v != expect)
 393                     return false;
 394                 unsafe.putLong(obj, offset, update);
 395                 return true;
 396             }
 397         }
 398 
 399         public boolean weakCompareAndSet(T obj, long expect, long update) {
 400             return compareAndSet(obj, expect, update);
 401         }
 402 
 403         public void set(T obj, long newValue) {
 404             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 405             synchronized (this) {
 406                 unsafe.putLong(obj, offset, newValue);
 407             }
 408         }
 409 
 410         public void lazySet(T obj, long newValue) {
 411             set(obj, newValue);
 412         }
 413 
 414         public long get(T obj) {
 415             if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
 416             synchronized (this) {
 417                 return unsafe.getLong(obj, offset);
 418             }
 419         }
 420 
 421         private void ensureProtectedAccess(T obj) {
 422             if (cclass.isInstance(obj)) {
 423                 return;
 424             }
 425             throw new RuntimeException(
 426                 new IllegalAccessException("Class " +
 427                     cclass.getName() +
 428                     " can not access a protected member of class " +
 429                     tclass.getName() +
 430                     " using an instance of " +
 431                     obj.getClass().getName()
 432                 )
 433             );
 434         }
 435     }
 436 }