ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/FutureTask.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/FutureTask.java (file contents):
Revision 1.103 by jsr166, Wed May 15 02:39:59 2013 UTC vs.
Revision 1.104 by jsr166, Mon Jul 21 05:22:06 2014 UTC

# Line 134 | Line 134 | public class FutureTask<V> implements Ru
134  
135      public boolean cancel(boolean mayInterruptIfRunning) {
136          if (!(state == NEW &&
137 <              UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
137 >              U.compareAndSwapInt(this, STATE, NEW,
138                    mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
139              return false;
140          try {    // in case call to interrupt throws exception
# Line 144 | Line 144 | public class FutureTask<V> implements Ru
144                      if (t != null)
145                          t.interrupt();
146                  } finally { // final state
147 <                    UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
147 >                    U.putOrderedInt(this, STATE, INTERRUPTED);
148                  }
149              }
150          } finally {
# Line 198 | Line 198 | public class FutureTask<V> implements Ru
198       * @param v the value
199       */
200      protected void set(V v) {
201 <        if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
201 >        if (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
202              outcome = v;
203 <            UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
203 >            U.putOrderedInt(this, STATE, NORMAL); // final state
204              finishCompletion();
205          }
206      }
# Line 216 | Line 216 | public class FutureTask<V> implements Ru
216       * @param t the cause of failure
217       */
218      protected void setException(Throwable t) {
219 <        if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
219 >        if (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
220              outcome = t;
221 <            UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
221 >            U.putOrderedInt(this, STATE, EXCEPTIONAL); // final state
222              finishCompletion();
223          }
224      }
225  
226      public void run() {
227          if (state != NEW ||
228 <            !UNSAFE.compareAndSwapObject(this, runnerOffset,
229 <                                         null, Thread.currentThread()))
228 >            !U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
229              return;
230          try {
231              Callable<V> c = callable;
# Line 267 | Line 266 | public class FutureTask<V> implements Ru
266       */
267      protected boolean runAndReset() {
268          if (state != NEW ||
269 <            !UNSAFE.compareAndSwapObject(this, runnerOffset,
271 <                                         null, Thread.currentThread()))
269 >            !U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
270              return false;
271          boolean ran = false;
272          int s = state;
# Line 335 | Line 333 | public class FutureTask<V> implements Ru
333      private void finishCompletion() {
334          // assert state > COMPLETING;
335          for (WaitNode q; (q = waiters) != null;) {
336 <            if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
336 >            if (U.compareAndSwapObject(this, WAITERS, q, null)) {
337                  for (;;) {
338                      Thread t = q.thread;
339                      if (t != null) {
# Line 386 | Line 384 | public class FutureTask<V> implements Ru
384              else if (q == null)
385                  q = new WaitNode();
386              else if (!queued)
387 <                queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
388 <                                                     q.next = waiters, q);
387 >                queued = U.compareAndSwapObject(this, WAITERS,
388 >                                                q.next = waiters, q);
389              else if (timed) {
390                  nanos = deadline - System.nanoTime();
391                  if (nanos <= 0L) {
# Line 425 | Line 423 | public class FutureTask<V> implements Ru
423                          if (pred.thread == null) // check for race
424                              continue retry;
425                      }
426 <                    else if (!UNSAFE.compareAndSwapObject(this, waitersOffset,
429 <                                                          q, s))
426 >                    else if (!U.compareAndSwapObject(this, WAITERS, q, s))
427                          continue retry;
428                  }
429                  break;
# Line 435 | Line 432 | public class FutureTask<V> implements Ru
432      }
433  
434      // Unsafe mechanics
435 <    private static final sun.misc.Unsafe UNSAFE;
436 <    private static final long stateOffset;
437 <    private static final long runnerOffset;
438 <    private static final long waitersOffset;
435 >    private static final sun.misc.Unsafe U;
436 >    private static final long STATE;
437 >    private static final long RUNNER;
438 >    private static final long WAITERS;
439      static {
440          try {
441 <            UNSAFE = sun.misc.Unsafe.getUnsafe();
441 >            U = sun.misc.Unsafe.getUnsafe();
442              Class<?> k = FutureTask.class;
443 <            stateOffset = UNSAFE.objectFieldOffset
444 <                (k.getDeclaredField("state"));
445 <            runnerOffset = UNSAFE.objectFieldOffset
449 <                (k.getDeclaredField("runner"));
450 <            waitersOffset = UNSAFE.objectFieldOffset
451 <                (k.getDeclaredField("waiters"));
443 >            STATE   = U.objectFieldOffset(k.getDeclaredField("state"));
444 >            RUNNER  = U.objectFieldOffset(k.getDeclaredField("runner"));
445 >            WAITERS = U.objectFieldOffset(k.getDeclaredField("waiters"));
446          } catch (Exception e) {
447              throw new Error(e);
448          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines