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

Comparing jsr166/src/jsr166e/StampedLock.java (file contents):
Revision 1.19 by dl, Mon Oct 15 12:12:42 2012 UTC vs.
Revision 1.27 by jsr166, Mon Jan 14 19:00:01 2013 UTC

# Line 63 | Line 63 | import java.util.concurrent.TimeUnit;
63   *
64   * <p>StampedLocks are designed for use as internal utilities in the
65   * development of thread-safe components. Their use relies on
66 < * knowledge of the internal properties of the the data, objects, and
66 > * knowledge of the internal properties of the data, objects, and
67   * methods they are protecting.  They are not reentrant, so locked
68   * bodies should not call other unknown methods that may try to
69   * re-acquire locks (although you may pass a stamp to other methods
# Line 125 | Line 125 | import java.util.concurrent.TimeUnit;
125   *   }
126   *
127   *   double distanceFromOriginV2() { // combines code paths
128 + *     double currentX = 0.0, currentY = 0.0;
129   *     for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) {
129 *       double currentX = 0.0, currentY = 0.0;
130   *       try {
131   *         currentX = x;
132   *         currentY = y;
133   *       } finally {
134   *         if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate
135 < *           return Math.sqrt(currentX * currentX + currentY * currentY);
135 > *           break;
136   *       }
137   *     }
138 + *     return Math.sqrt(currentX * currentX + currentY * currentY);
139   *   }
140   *
141   *   void moveIfAtOrigin(double newX, double newY) { // upgrade
# Line 155 | Line 156 | import java.util.concurrent.TimeUnit;
156   *         }
157   *       }
158   *     } finally {
159 < *        sl.unlock(stamp);
159 > *       sl.unlock(stamp);
160   *     }
161   *   }
162   * }}</pre>
# Line 225 | Line 226 | public class StampedLock implements java
226       * threads.  Both await methods use a similar spin strategy: If
227       * the associated queue appears to be empty, then the thread
228       * spin-waits up to SPINS times (where each iteration decreases
229 <     * spin count with 50% probablility) before enqueing, and then, if
229 >     * spin count with 50% probability) before enqueuing, and then, if
230       * it is the first thread to be enqueued, spins again up to SPINS
231       * times before blocking. If, upon wakening it fails to obtain
232       * lock, and is still (or becomes) the first waiting thread (which
# Line 251 | Line 252 | public class StampedLock implements java
252       * be subject to future improvements.
253       */
254  
255 +    private static final long serialVersionUID = -6001602636862214147L;
256 +
257      /** Number of processors, for spin control */
258      private static final int NCPU = Runtime.getRuntime().availableProcessors();
259  
# Line 994 | Line 997 | public class StampedLock implements java
997                          else if ((time = deadline - System.nanoTime()) <= 0L)
998                              return cancelWriter(node, false);
999                          if (node.prev == p && p.status == WAITING &&
1000 <                            (p != whead || (state & WBIT) != 0L)) // recheck
1000 >                            (p != whead || (state & ABITS) != 0L)) // recheck
1001                              U.park(false, time);
1002                          if (interruptible && Thread.interrupted())
1003                              return cancelWriter(node, true);
# Line 1007 | Line 1010 | public class StampedLock implements java
1010      /**
1011       * If node non-null, forces cancel status and unsplices from queue
1012       * if possible. This is a variant of cancellation methods in
1013 <     * AbstractQueuedSynchronizer (see its detailed explanation in
1013 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1014       * internal documentation) that more conservatively wakes up other
1015 <     * threads that may have had their links changed so as to preserve
1015 >     * threads that may have had their links changed, so as to preserve
1016       * liveness in the main signalling methods.
1017       */
1018      private long cancelWriter(WNode node, boolean interrupted) {
# Line 1203 | Line 1206 | public class StampedLock implements java
1206      private static sun.misc.Unsafe getUnsafe() {
1207          try {
1208              return sun.misc.Unsafe.getUnsafe();
1209 <        } catch (SecurityException se) {
1210 <            try {
1211 <                return java.security.AccessController.doPrivileged
1212 <                    (new java.security
1213 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1214 <                        public sun.misc.Unsafe run() throws Exception {
1215 <                            java.lang.reflect.Field f = sun.misc
1216 <                                .Unsafe.class.getDeclaredField("theUnsafe");
1217 <                            f.setAccessible(true);
1218 <                            return (sun.misc.Unsafe) f.get(null);
1219 <                        }});
1220 <            } catch (java.security.PrivilegedActionException e) {
1221 <                throw new RuntimeException("Could not initialize intrinsics",
1222 <                                           e.getCause());
1223 <            }
1209 >        } catch (SecurityException tryReflectionInstead) {}
1210 >        try {
1211 >            return java.security.AccessController.doPrivileged
1212 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1213 >                public sun.misc.Unsafe run() throws Exception {
1214 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
1215 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
1216 >                        f.setAccessible(true);
1217 >                        Object x = f.get(null);
1218 >                        if (k.isInstance(x))
1219 >                            return k.cast(x);
1220 >                    }
1221 >                    throw new NoSuchFieldError("the Unsafe");
1222 >                }});
1223 >        } catch (java.security.PrivilegedActionException e) {
1224 >            throw new RuntimeException("Could not initialize intrinsics",
1225 >                                       e.getCause());
1226          }
1227      }
1223
1228   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines