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

Comparing jsr166/src/jsr166y/ForkJoinTask.java (file contents):
Revision 1.73 by jsr166, Sun Nov 28 21:21:03 2010 UTC vs.
Revision 1.74 by dl, Tue Feb 22 00:39:31 2011 UTC

# Line 12 | Line 12 | import java.util.Collections;
12   import java.util.List;
13   import java.util.RandomAccess;
14   import java.util.Map;
15 < import java.util.WeakHashMap;
15 > import java.lang.ref.WeakReference;
16 > import java.lang.ref.ReferenceQueue;
17   import java.util.concurrent.Callable;
18   import java.util.concurrent.CancellationException;
19   import java.util.concurrent.ExecutionException;
# Line 23 | Line 24 | import java.util.concurrent.RejectedExec
24   import java.util.concurrent.RunnableFuture;
25   import java.util.concurrent.TimeUnit;
26   import java.util.concurrent.TimeoutException;
27 + import java.util.concurrent.locks.ReentrantLock;
28 + import java.lang.reflect.Constructor;
29  
30   /**
31   * Abstract base class for tasks that run within a {@link ForkJoinPool}.
# Line 66 | Line 69 | import java.util.concurrent.TimeoutExcep
69   * rethrown to callers attempting to join them. These exceptions may
70   * additionally include {@link RejectedExecutionException} stemming
71   * from internal resource exhaustion, such as failure to allocate
72 < * internal task queues.
72 > * internal task queues. Rethrown exceptions behave in the same way as
73 > * regular exceptions, but, when possible, contain stack traces (as
74 > * displayed for example using {@code ex.printStackTrace()}) of both
75 > * the thread that initiated the computation as well as the thread
76 > * actually encountering the exception; minimally only the latter.
77   *
78   * <p>The primary method for awaiting completion and extracting
79   * results of a task is {@link #join}, but there are several variants:
# Line 163 | Line 170 | public abstract class ForkJoinTask<V> im
170       * status maintenance (2) execution and awaiting completion (3)
171       * user-level methods that additionally report results. This is
172       * sometimes hard to see because this file orders exported methods
173 <     * in a way that flows well in javadocs. In particular, most
167 <     * join mechanics are in method quietlyJoin, below.
173 >     * in a way that flows well in javadocs.
174       */
175  
176      /*
# Line 186 | Line 192 | public abstract class ForkJoinTask<V> im
192  
193      /** The run status of this task */
194      volatile int status; // accessed directly by pool and workers
189
195      private static final int NORMAL      = -1;
196      private static final int CANCELLED   = -2;
197      private static final int EXCEPTIONAL = -3;
198      private static final int SIGNAL      =  1;
199  
200      /**
196     * Table of exceptions thrown by tasks, to enable reporting by
197     * callers. Because exceptions are rare, we don't directly keep
198     * them with task objects, but instead use a weak ref table.  Note
199     * that cancellation exceptions don't appear in the table, but are
200     * instead recorded as status values.
201     * TODO: Use ConcurrentReferenceHashMap
202     */
203    static final Map<ForkJoinTask<?>, Throwable> exceptionMap =
204        Collections.synchronizedMap
205        (new WeakHashMap<ForkJoinTask<?>, Throwable>());
206
207    // Maintaining completion status
208
209    /**
201       * Marks completion and wakes up threads waiting to join this task,
202       * also clearing signal request bits.
203       *
204       * @param completion one of NORMAL, CANCELLED, EXCEPTIONAL
205 +     * @return completion status on exit
206       */
207 <    private void setCompletion(int completion) {
208 <        int s;
209 <        while ((s = status) >= 0) {
207 >    private int setCompletion(int completion) {
208 >        for (int s;;) {
209 >            if ((s = status) < 0)
210 >                return s;
211              if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) {
212                  if (s != 0)
213                      synchronized (this) { notifyAll(); }
214 <                break;
214 >                return completion;
215              }
216          }
217      }
218  
219      /**
220 <     * Records exception and sets exceptional completion.
220 >     * Tries to block a worker thread until completed or timed out.
221 >     * Uses Object.wait time argument conventions.
222 >     * May fail on contention or interrupt.
223       *
224 <     * @return status on exit
224 >     * @param millis if > 0, wait time.
225       */
226 <    private void setExceptionalCompletion(Throwable rex) {
227 <        exceptionMap.put(this, rex);
228 <        setCompletion(EXCEPTIONAL);
229 <    }
230 <
231 <    /**
232 <     * Blocks a worker thread until completed or timed out.  Called
238 <     * only by pool.
239 <     */
240 <    final void internalAwaitDone(long millis, int nanos) {
241 <        int s = status;
242 <        if ((s == 0 &&
243 <             UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL)) ||
244 <            s > 0)  {
245 <            try {     // the odd construction reduces lock bias effects
226 >    final void tryAwaitDone(long millis) {
227 >        int s;
228 >        try {
229 >            if (((s = status) > 0 ||
230 >                 (s == 0 &&
231 >                  UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL))) &&
232 >                status > 0) {
233                  synchronized (this) {
234                      if (status > 0)
235 <                        wait(millis, nanos);
249 <                    else
250 <                        notifyAll();
235 >                        wait(millis);
236                  }
252            } catch (InterruptedException ie) {
253                cancelIfTerminating();
237              }
238 +        } catch (InterruptedException ie) {
239 +            // caller must check termination
240          }
241      }
242  
243      /**
244       * Blocks a non-worker-thread until completion.
245 +     * @return status upon completion
246       */
247 <    private void externalAwaitDone() {
248 <        if (status >= 0) {
247 >    private int externalAwaitDone() {
248 >        int s;
249 >        if ((s = status) >= 0) {
250              boolean interrupted = false;
251              synchronized (this) {
252 <                for (;;) {
266 <                    int s = status;
252 >                while ((s = status) >= 0) {
253                      if (s == 0)
254                          UNSAFE.compareAndSwapInt(this, statusOffset,
255                                                   0, SIGNAL);
270                    else if (s < 0) {
271                        notifyAll();
272                        break;
273                    }
256                      else {
257                          try {
258                              wait();
# Line 283 | Line 265 | public abstract class ForkJoinTask<V> im
265              if (interrupted)
266                  Thread.currentThread().interrupt();
267          }
268 +        return s;
269      }
270  
271      /**
272       * Blocks a non-worker-thread until completion or interruption or timeout.
273       */
274 <    private void externalInterruptibleAwaitDone(boolean timed, long nanos)
274 >    private int externalInterruptibleAwaitDone(long millis)
275          throws InterruptedException {
276 +        int s;
277          if (Thread.interrupted())
278              throw new InterruptedException();
279 <        if (status >= 0) {
296 <            long startTime = timed ? System.nanoTime() : 0L;
279 >        if ((s = status) >= 0) {
280              synchronized (this) {
281 <                for (;;) {
299 <                    long nt;
300 <                    int s = status;
281 >                while ((s = status) >= 0) {
282                      if (s == 0)
283                          UNSAFE.compareAndSwapInt(this, statusOffset,
284                                                   0, SIGNAL);
304                    else if (s < 0) {
305                        notifyAll();
306                        break;
307                    }
308                    else if (!timed)
309                        wait();
310                    else if ((nt = nanos - (System.nanoTime()-startTime)) > 0L)
311                        wait(nt / 1000000, (int)(nt % 1000000));
285                      else
286 <                        break;
286 >                        wait(millis);
287                  }
288              }
289          }
290 +        return s;
291      }
292  
293      /**
294 <     * Unless done, calls exec and records status if completed, but
295 <     * doesn't wait for completion otherwise. Primary execution method
296 <     * for ForkJoinWorkerThread.
294 >     * Primary execution method for stolen tasks. Unless done, calls
295 >     * exec and records status if completed, but doesn't wait for
296 >     * completion otherwise.
297       */
298 <    final void quietlyExec() {
299 <        try {
300 <            if (status < 0 || !exec())
298 >    final void doExec() {
299 >        if (status >= 0) {
300 >            boolean completed;
301 >            try {
302 >                completed = exec();
303 >            } catch (Throwable rex) {
304 >                setExceptionalCompletion(rex);
305                  return;
306 +            }
307 +            if (completed)
308 +                setCompletion(NORMAL); // must be outside try block
309 +        }
310 +    }
311 +
312 +    /**
313 +     * Primary mechanics for join, get, quietlyJoin.
314 +     * @return status upon completion
315 +     */
316 +    private int doJoin() {
317 +        Thread t; ForkJoinWorkerThread w; int s; boolean completed;
318 +        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
319 +            if ((s = status) < 0)
320 +                return s;
321 +            if ((w = (ForkJoinWorkerThread)t).unpushTask(this)) {
322 +                try {
323 +                    completed = exec();
324 +                } catch (Throwable rex) {
325 +                    return setExceptionalCompletion(rex);
326 +                }
327 +                if (completed)
328 +                    return setCompletion(NORMAL);
329 +            }
330 +            return w.joinTask(this);
331 +        }
332 +        else
333 +            return externalAwaitDone();
334 +    }
335 +
336 +    /**
337 +     * Primary mechanics for invoke, quietlyInvoke.
338 +     * @return status upon completion
339 +     */
340 +    private int doInvoke() {
341 +        int s; boolean completed;
342 +        if ((s = status) < 0)
343 +            return s;
344 +        try {
345 +            completed = exec();
346          } catch (Throwable rex) {
347 <            setExceptionalCompletion(rex);
330 <            return;
347 >            return setExceptionalCompletion(rex);
348          }
349 <        setCompletion(NORMAL); // must be outside try block
349 >        if (completed)
350 >            return setCompletion(NORMAL);
351 >        else
352 >            return doJoin();
353 >    }
354 >
355 >    // Exception table support
356 >
357 >    /**
358 >     * Table of exceptions thrown by tasks, to enable reporting by
359 >     * callers. Because exceptions are rare, we don't directly keep
360 >     * them with task objects, but instead use a weak ref table.  Note
361 >     * that cancellation exceptions don't appear in the table, but are
362 >     * instead recorded as status values.
363 >     *
364 >     * Note: These statics are initialized below in static block.
365 >     */
366 >    private static final ExceptionNode[] exceptionTable;
367 >    private static final ReentrantLock exceptionTableLock;
368 >    private static final ReferenceQueue<Object> exceptionTableRefQueue;
369 >
370 >    /**
371 >     * Fixed capacity for exceptionTable.
372 >     */
373 >    private static final int EXCEPTION_MAP_CAPACITY = 32;
374 >
375 >    /**
376 >     * Key-value nodes for exception table.  The chained hash table
377 >     * uses identity comparisons, full locking, and weak references
378 >     * for keys. The table has a fixed capacity because it only
379 >     * maintains task exceptions long enough for joiners to access
380 >     * them, so should never become very large for sustained
381 >     * periods. However, since we do not know when the last joiner
382 >     * completes, we must use weak references and expunge them. We do
383 >     * so on each operation (hence full locking). Also, some thread in
384 >     * any ForkJoinPool will call helpExpunge when its pool becomes
385 >     * isQuiescent.
386 >     */
387 >    static final class ExceptionNode extends WeakReference<ForkJoinTask<?>>{
388 >        final Throwable ex;
389 >        ExceptionNode next;
390 >        final long thrower;
391 >        ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next) {
392 >            super(task, exceptionTableRefQueue);
393 >            this.ex = ex;
394 >            this.next = next;
395 >            this.thrower = Thread.currentThread().getId();
396 >        }
397 >    }
398 >
399 >    /**
400 >     * Records exception and sets exceptional completion.
401 >     *
402 >     * @return status on exit
403 >     */
404 >    private int setExceptionalCompletion(Throwable ex) {
405 >        int h = System.identityHashCode(this);
406 >        ReentrantLock lock = exceptionTableLock;
407 >        lock.lock();
408 >        try {
409 >            expungeStaleExceptions();
410 >            ExceptionNode[] t = exceptionTable;
411 >            int i = h & (t.length - 1);
412 >            for (ExceptionNode e = t[i]; ; e = e.next) {
413 >                if (e == null) {
414 >                    t[i] = new ExceptionNode(this, ex, t[i]);
415 >                    break;
416 >                }
417 >                if (e.get() == this) // already present
418 >                    break;
419 >            }
420 >        } finally {
421 >            lock.unlock();
422 >        }
423 >        return setCompletion(EXCEPTIONAL);
424 >    }
425 >
426 >    /**
427 >     * Removes exception node and clears status
428 >     */
429 >    private void clearExceptionalCompletion() {
430 >        int h = System.identityHashCode(this);
431 >        ReentrantLock lock = exceptionTableLock;
432 >        lock.lock();
433 >        try {
434 >            ExceptionNode[] t = exceptionTable;
435 >            int i = h & (t.length - 1);
436 >            ExceptionNode e = t[i];
437 >            ExceptionNode pred = null;
438 >            while (e != null) {
439 >                ExceptionNode next = e.next;
440 >                if (e.get() == this) {
441 >                    if (pred == null)
442 >                        t[i] = next;
443 >                    else
444 >                        pred.next = next;
445 >                    break;
446 >                }
447 >                pred = e;
448 >                e = next;
449 >            }
450 >            expungeStaleExceptions();
451 >            status = 0;
452 >        } finally {
453 >            lock.unlock();
454 >        }
455 >    }
456 >
457 >    /**
458 >     * Returns a rethrowable exception for the given task, if
459 >     * available. To provide accurate stack traces, if the exception
460 >     * was not thrown by the current thread, we try to create a new
461 >     * exception of the same type as the one thrown, but with the
462 >     * recorded exception as its cause. If there is no such
463 >     * constructor, we instead try to use a no-arg constructor,
464 >     * followed by initCause, to the same effect. If none of these
465 >     * apply, or any fail due to other exceptions, we return the
466 >     * recorded exception, which is still correct, although it may
467 >     * contain a misleading stack trace.
468 >     *
469 >     * @return the exception, or null if none
470 >     */
471 >    private Throwable getThrowableException() {
472 >        if (status != EXCEPTIONAL)
473 >            return null;
474 >        int h = System.identityHashCode(this);
475 >        ExceptionNode e;
476 >        ReentrantLock lock = exceptionTableLock;
477 >        lock.lock();
478 >        try {
479 >            expungeStaleExceptions();
480 >            ExceptionNode[] t = exceptionTable;
481 >            e = t[h & (t.length - 1)];
482 >            while (e != null && e.get() != this)
483 >                e = e.next;
484 >        } finally {
485 >            lock.unlock();
486 >        }
487 >        Throwable ex;
488 >        if (e == null || (ex = e.ex) == null)
489 >            return null;
490 >        if (e.thrower != Thread.currentThread().getId()) {
491 >            Class ec = ex.getClass();
492 >            try {
493 >                Constructor<?> noArgCtor = null;
494 >                Constructor<?>[] cs = ec.getConstructors();// public ctors only
495 >                for (int i = 0; i < cs.length; ++i) {
496 >                    Constructor<?> c = cs[i];
497 >                    Class<?>[] ps = c.getParameterTypes();
498 >                    if (ps.length == 0)
499 >                        noArgCtor = c;
500 >                    else if (ps.length == 1 && ps[0] == Throwable.class)
501 >                        return (Throwable)(c.newInstance(ex));
502 >                }
503 >                if (noArgCtor != null) {
504 >                    Throwable wx = (Throwable)(noArgCtor.newInstance());
505 >                    wx.initCause(ex);
506 >                    return wx;
507 >                }
508 >            } catch (Exception ignore) {
509 >            }
510 >        }
511 >        return ex;
512 >    }
513 >
514 >    /**
515 >     * Poll stale refs and remove them. Call only while holding lock.
516 >     */
517 >    private static void expungeStaleExceptions() {
518 >        for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
519 >            if (x instanceof ExceptionNode) {
520 >                ForkJoinTask<?> key = ((ExceptionNode)x).get();
521 >                ExceptionNode[] t = exceptionTable;
522 >                int i = System.identityHashCode(key) & (t.length - 1);
523 >                ExceptionNode e = t[i];
524 >                ExceptionNode pred = null;
525 >                while (e != null) {
526 >                    ExceptionNode next = e.next;
527 >                    if (e == x) {
528 >                        if (pred == null)
529 >                            t[i] = next;
530 >                        else
531 >                            pred.next = next;
532 >                        break;
533 >                    }
534 >                    pred = e;
535 >                    e = next;
536 >                }
537 >            }
538 >        }
539 >    }
540 >
541 >    /**
542 >     * If lock is available, poll any stale refs and remove them.
543 >     * Called from ForkJoinPool when pools become quiescent.
544 >     */
545 >    static final void helpExpungeStaleExceptions() {
546 >        ReentrantLock lock = exceptionTableLock;
547 >        if (lock.tryLock()) {
548 >            try {
549 >                expungeStaleExceptions();
550 >            } finally {
551 >                lock.unlock();
552 >            }
553 >        }
554 >    }
555 >
556 >    /**
557 >     * Report the result of invoke or join; called only upon
558 >     * non-normal return of internal versions.
559 >     */
560 >    private V reportResult() {
561 >        int s; Throwable ex;
562 >        if ((s = status) == CANCELLED)
563 >            throw new CancellationException();
564 >        if (s == EXCEPTIONAL && (ex = getThrowableException()) != null)
565 >            UNSAFE.throwException(ex);
566 >        return getRawResult();
567      }
568  
569      // public methods
# Line 370 | Line 604 | public abstract class ForkJoinTask<V> im
604       * @return the computed result
605       */
606      public final V join() {
607 <        quietlyJoin();
608 <        Throwable ex;
609 <        if (status < NORMAL && (ex = getException()) != null)
610 <            UNSAFE.throwException(ex);
377 <        return getRawResult();
607 >        if (doJoin() != NORMAL)
608 >            return reportResult();
609 >        else
610 >            return getRawResult();
611      }
612  
613      /**
# Line 386 | Line 619 | public abstract class ForkJoinTask<V> im
619       * @return the computed result
620       */
621      public final V invoke() {
622 <        quietlyInvoke();
623 <        Throwable ex;
624 <        if (status < NORMAL && (ex = getException()) != null)
625 <            UNSAFE.throwException(ex);
393 <        return getRawResult();
622 >        if (doInvoke() != NORMAL)
623 >            return reportResult();
624 >        else
625 >            return getRawResult();
626      }
627  
628      /**
# Line 454 | Line 686 | public abstract class ForkJoinTask<V> im
686              }
687              else if (i != 0)
688                  t.fork();
689 <            else {
690 <                t.quietlyInvoke();
459 <                if (ex == null && t.status < NORMAL)
460 <                    ex = t.getException();
461 <            }
689 >            else if (t.doInvoke() < NORMAL && ex == null)
690 >                ex = t.getException();
691          }
692          for (int i = 1; i <= last; ++i) {
693              ForkJoinTask<?> t = tasks[i];
694              if (t != null) {
695                  if (ex != null)
696                      t.cancel(false);
697 <                else {
698 <                    t.quietlyJoin();
470 <                    if (ex == null && t.status < NORMAL)
471 <                        ex = t.getException();
472 <                }
697 >                else if (t.doJoin() < NORMAL && ex == null)
698 >                    ex = t.getException();
699              }
700          }
701          if (ex != null)
# Line 517 | Line 743 | public abstract class ForkJoinTask<V> im
743              }
744              else if (i != 0)
745                  t.fork();
746 <            else {
747 <                t.quietlyInvoke();
522 <                if (ex == null && t.status < NORMAL)
523 <                    ex = t.getException();
524 <            }
746 >            else if (t.doInvoke() < NORMAL && ex == null)
747 >                ex = t.getException();
748          }
749          for (int i = 1; i <= last; ++i) {
750              ForkJoinTask<?> t = ts.get(i);
751              if (t != null) {
752                  if (ex != null)
753                      t.cancel(false);
754 <                else {
755 <                    t.quietlyJoin();
533 <                    if (ex == null && t.status < NORMAL)
534 <                        ex = t.getException();
535 <                }
754 >                else if (t.doJoin() < NORMAL && ex == null)
755 >                    ex = t.getException();
756              }
757          }
758          if (ex != null)
# Line 568 | Line 788 | public abstract class ForkJoinTask<V> im
788       * @return {@code true} if this task is now cancelled
789       */
790      public boolean cancel(boolean mayInterruptIfRunning) {
791 <        setCompletion(CANCELLED);
572 <        return status == CANCELLED;
791 >        return setCompletion(CANCELLED) == CANCELLED;
792      }
793  
794      /**
# Line 585 | Line 804 | public abstract class ForkJoinTask<V> im
804          }
805      }
806  
588    /**
589     * Cancels if current thread is a terminating worker thread,
590     * ignoring any exceptions thrown by cancel.
591     */
592    final void cancelIfTerminating() {
593        Thread t = Thread.currentThread();
594        if ((t instanceof ForkJoinWorkerThread) &&
595            ((ForkJoinWorkerThread) t).isTerminating()) {
596            try {
597                cancel(false);
598            } catch (Throwable ignore) {
599            }
600        }
601    }
602
807      public final boolean isDone() {
808          return status < 0;
809      }
# Line 639 | Line 843 | public abstract class ForkJoinTask<V> im
843          int s = status;
844          return ((s >= NORMAL)    ? null :
845                  (s == CANCELLED) ? new CancellationException() :
846 <                exceptionMap.get(this));
846 >                getThrowableException());
847      }
848  
849      /**
# Line 697 | Line 901 | public abstract class ForkJoinTask<V> im
901       * member of a ForkJoinPool and was interrupted while waiting
902       */
903      public final V get() throws InterruptedException, ExecutionException {
904 <        Thread t = Thread.currentThread();
905 <        if (t instanceof ForkJoinWorkerThread)
906 <            quietlyJoin();
907 <        else
908 <            externalInterruptibleAwaitDone(false, 0L);
909 <        int s = status;
910 <        if (s != NORMAL) {
707 <            Throwable ex;
708 <            if (s == CANCELLED)
709 <                throw new CancellationException();
710 <            if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null)
711 <                throw new ExecutionException(ex);
712 <        }
904 >        int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ?
905 >            doJoin() : externalInterruptibleAwaitDone(0L);
906 >        Throwable ex;
907 >        if (s == CANCELLED)
908 >            throw new CancellationException();
909 >        if (s == EXCEPTIONAL && (ex = getThrowableException()) != null)
910 >            throw new ExecutionException(ex);
911          return getRawResult();
912      }
913  
# Line 729 | Line 927 | public abstract class ForkJoinTask<V> im
927       */
928      public final V get(long timeout, TimeUnit unit)
929          throws InterruptedException, ExecutionException, TimeoutException {
732        long nanos = unit.toNanos(timeout);
930          Thread t = Thread.currentThread();
931 <        if (t instanceof ForkJoinWorkerThread)
932 <            ((ForkJoinWorkerThread)t).joinTask(this, true, nanos);
933 <        else
934 <            externalInterruptibleAwaitDone(true, nanos);
931 >        if (t instanceof ForkJoinWorkerThread) {
932 >            ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
933 >            long nanos = unit.toNanos(timeout);
934 >            if (status >= 0) {
935 >                boolean completed = false;
936 >                if (w.unpushTask(this)) {
937 >                    try {
938 >                        completed = exec();
939 >                    } catch (Throwable rex) {
940 >                        setExceptionalCompletion(rex);
941 >                    }
942 >                }
943 >                if (completed)
944 >                    setCompletion(NORMAL);
945 >                else if (status >= 0 && nanos > 0)
946 >                    w.pool.timedAwaitJoin(this, nanos);
947 >            }
948 >        }
949 >        else {
950 >            long millis = unit.toMillis(timeout);
951 >            if (millis > 0)
952 >                externalInterruptibleAwaitDone(millis);
953 >        }
954          int s = status;
955          if (s != NORMAL) {
956              Throwable ex;
957              if (s == CANCELLED)
958                  throw new CancellationException();
959 <            if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null)
959 >            if (s != EXCEPTIONAL)
960 >                throw new TimeoutException();
961 >            if ((ex = getThrowableException()) != null)
962                  throw new ExecutionException(ex);
745            throw new TimeoutException();
963          }
964          return getRawResult();
965      }
# Line 754 | Line 971 | public abstract class ForkJoinTask<V> im
971       * known to have aborted.
972       */
973      public final void quietlyJoin() {
974 <        Thread t;
758 <        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
759 <            ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
760 <            if (status >= 0) {
761 <                if (w.unpushTask(this)) {
762 <                    boolean completed;
763 <                    try {
764 <                        completed = exec();
765 <                    } catch (Throwable rex) {
766 <                        setExceptionalCompletion(rex);
767 <                        return;
768 <                    }
769 <                    if (completed) {
770 <                        setCompletion(NORMAL);
771 <                        return;
772 <                    }
773 <                }
774 <                w.joinTask(this, false, 0L);
775 <            }
776 <        }
777 <        else
778 <            externalAwaitDone();
974 >        doJoin();
975      }
976  
977      /**
# Line 784 | Line 980 | public abstract class ForkJoinTask<V> im
980       * exception.
981       */
982      public final void quietlyInvoke() {
983 <        if (status >= 0) {
788 <            boolean completed;
789 <            try {
790 <                completed = exec();
791 <            } catch (Throwable rex) {
792 <                setExceptionalCompletion(rex);
793 <                return;
794 <            }
795 <            if (completed)
796 <                setCompletion(NORMAL);
797 <            else
798 <                quietlyJoin();
799 <        }
983 >        doInvoke();
984      }
985  
986      /**
# Line 835 | Line 1019 | public abstract class ForkJoinTask<V> im
1019       */
1020      public void reinitialize() {
1021          if (status == EXCEPTIONAL)
1022 <            exceptionMap.remove(this);
1023 <        status = 0;
1022 >            clearExceptionalCompletion();
1023 >        else
1024 >            status = 0;
1025      }
1026  
1027      /**
# Line 1147 | Line 1332 | public abstract class ForkJoinTask<V> im
1332          s.defaultReadObject();
1333          Object ex = s.readObject();
1334          if (ex != null)
1335 <            setExceptionalCompletion((Throwable) ex);
1335 >            setExceptionalCompletion((Throwable)ex);
1336      }
1337  
1338      // Unsafe mechanics
1339 <
1340 <    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1341 <    private static final long statusOffset =
1342 <        objectFieldOffset("status", ForkJoinTask.class);
1343 <
1344 <    private static long objectFieldOffset(String field, Class<?> klazz) {
1339 >    private static final sun.misc.Unsafe UNSAFE;
1340 >    private static final long statusOffset;
1341 >    static {
1342 >        exceptionTableLock = new ReentrantLock();
1343 >        exceptionTableRefQueue = new ReferenceQueue<Object>();
1344 >        exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY];
1345          try {
1346 <            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1347 <        } catch (NoSuchFieldException e) {
1348 <            // Convert Exception to corresponding Error
1349 <            NoSuchFieldError error = new NoSuchFieldError(field);
1350 <            error.initCause(e);
1166 <            throw error;
1346 >            UNSAFE = getUnsafe();
1347 >            statusOffset = UNSAFE.objectFieldOffset
1348 >                (ForkJoinTask.class.getDeclaredField("status"));
1349 >        } catch (Exception e) {
1350 >            throw new Error(e);
1351          }
1352      }
1353  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines