ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/CountedCompleter.java
Revision: 1.7
Committed: Tue Oct 30 16:05:35 2012 UTC (11 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6    
7     package jsr166y;
8    
9     /**
10 dl 1.3 * A {@link ForkJoinTask} with a completion action
11 dl 1.1 * performed when triggered and there are no remaining pending
12     * actions. Uses of CountedCompleter are similar to those of other
13     * completion based components (such as {@link
14     * java.nio.channels.CompletionHandler}) except that multiple
15     * <em>pending</em> completions may be necessary to trigger the {@link
16     * #onCompletion} action, not just one. Unless initialized otherwise,
17     * the {@link #getPendingCount pending count} starts at zero, but may
18     * be (atomically) changed using methods {@link #setPendingCount},
19     * {@link #addToPendingCount}, and {@link
20     * #compareAndSetPendingCount}. Upon invocation of {@link
21     * #tryComplete}, if the pending action count is nonzero, it is
22     * decremented; otherwise, the completion action is performed, and if
23     * this completer itself has a completer, the process is continued
24 dl 1.2 * with its completer. As is the case with related synchronization
25 jsr166 1.4 * components such as {@link java.util.concurrent.Phaser Phaser} and
26     * {@link java.util.concurrent.Semaphore Semaphore}, these methods
27     * affect only internal counts; they do not establish any further
28     * internal bookkeeping. In particular, the identities of pending
29     * tasks are not maintained. As illustrated below, you can create
30 jsr166 1.5 * subclasses that do record some or all pending tasks or their
31     * results when needed.
32 dl 1.1 *
33     * <p>A concrete CountedCompleter class must define method {@link
34     * #compute}, that should, in almost all use cases, invoke {@code
35 dl 1.2 * tryComplete()} once before returning. The class may also optionally
36 dl 1.1 * override method {@link #onCompletion} to perform an action upon
37 dl 1.2 * normal completion, and method {@link #onExceptionalCompletion} to
38     * perform an action upon any exception.
39 dl 1.1 *
40 dl 1.3 * <p>CountedCompleters most often do not bear results, in which case
41     * they are normally declared as {@code CountedCompleter<Void>}, and
42     * will always return {@code null} as a result value. In other cases,
43     * you should override method {@link #getRawResult} to provide a
44     * result from {@code join(), invoke()}, and related methods. (Method
45     * {@link #setRawResult} by default plays no role in CountedCompleters
46     * but may be overridden for example to maintain fields holding result
47     * data.)
48     *
49 dl 1.1 * <p>A CountedCompleter that does not itself have a completer (i.e.,
50     * one for which {@link #getCompleter} returns {@code null}) can be
51     * used as a regular ForkJoinTask with this added functionality.
52     * However, any completer that in turn has another completer serves
53     * only as an internal helper for other computations, so its own task
54     * status (as reported in methods such as {@link ForkJoinTask#isDone})
55     * is arbitrary; this status changes only upon explicit invocations of
56     * {@link #complete}, {@link ForkJoinTask#cancel}, {@link
57     * ForkJoinTask#completeExceptionally} or upon exceptional completion
58     * of method {@code compute}. Upon any exceptional completion, the
59 dl 1.2 * exception may be relayed to a task's completer (and its completer,
60     * and so on), if one exists and it has not otherwise already
61     * completed.
62 dl 1.1 *
63     * <p><b>Sample Usages.</b>
64     *
65     * <p><b>Parallel recursive decomposition.</b> CountedCompleters may
66     * be arranged in trees similar to those often used with {@link
67     * RecursiveAction}s, although the constructions involved in setting
68 dl 1.3 * them up typically vary. Here, the completer of each task is its
69     * parent in the computation tree. Even though they entail a bit more
70 dl 1.1 * bookkeeping, CountedCompleters may be better choices when applying
71     * a possibly time-consuming operation (that cannot be further
72     * subdivided) to each element of an array or collection; especially
73     * when the operation takes a significantly different amount of time
74     * to complete for some elements than others, either because of
75     * intrinsic variation (for example IO) or auxiliary effects such as
76     * garbage collection. Because CountedCompleters provide their own
77     * continuations, other threads need not block waiting to perform
78     * them.
79     *
80     * <p> For example, here is an initial version of a class that uses
81     * divide-by-two recursive decomposition to divide work into single
82     * pieces (leaf tasks). Even when work is split into individual calls,
83     * tree-based techniques are usually preferable to directly forking
84     * leaf tasks, because they reduce inter-thread communication and
85     * improve load balancing. In the recursive case, the second of each
86     * pair of subtasks to finish triggers completion of its parent
87     * (because no result combination is performed, the default no-op
88     * implementation of method {@code onCompletion} is not overridden). A
89     * static utility method sets up the base task and invokes it:
90     *
91     * <pre> {@code
92     * class MyOperation<E> { void apply(E e) { ... } }
93     *
94 dl 1.3 * class ForEach<E> extends CountedCompleter<Void> {
95 dl 1.1 *
96     * public static <E> void forEach(ForkJoinPool pool, E[] array, MyOperation<E> op) {
97     * pool.invoke(new ForEach<E>(null, array, op, 0, array.length));
98     * }
99     *
100     * final E[] array; final MyOperation<E> op; final int lo, hi;
101 dl 1.3 * ForEach(CountedCompleter<?> p, E[] array, MyOperation<E> op, int lo, int hi) {
102 dl 1.1 * super(p);
103     * this.array = array; this.op = op; this.lo = lo; this.hi = hi;
104     * }
105     *
106     * public void compute() { // version 1
107     * if (hi - lo >= 2) {
108     * int mid = (lo + hi) >>> 1;
109     * setPendingCount(2); // must set pending count before fork
110     * new ForEach(this, array, op, mid, hi).fork(); // right child
111     * new ForEach(this, array, op, lo, mid).fork(); // left child
112     * }
113     * else if (hi > lo)
114     * op.apply(array[lo]);
115     * tryComplete();
116     * }
117     * } }</pre>
118     *
119     * This design can be improved by noticing that in the recursive case,
120     * the task has nothing to do after forking its right task, so can
121     * directly invoke its left task before returning. (This is an analog
122     * of tail recursion removal.) Also, because the task returns upon
123     * executing its left task (rather than falling through to invoke
124     * tryComplete) the pending count is set to one:
125     *
126     * <pre> {@code
127     * class ForEach<E> ...
128     * public void compute() { // version 2
129     * if (hi - lo >= 2) {
130     * int mid = (lo + hi) >>> 1;
131     * setPendingCount(1); // only one pending
132     * new ForEach(this, array, op, mid, hi).fork(); // right child
133     * new ForEach(this, array, op, lo, mid).compute(); // direct invoke
134     * }
135     * else {
136     * if (hi > lo)
137     * op.apply(array[lo]);
138     * tryComplete();
139     * }
140     * }
141     * }</pre>
142     *
143     * As a further improvement, notice that the left task need not even
144     * exist. Instead of creating a new one, we can iterate using the
145 dl 1.6 * original task, and add a pending count for each fork. Additionally,
146     * this version uses {@code helpComplete} to streamline assistance in
147     * the execution of forked tasks.
148 dl 1.1 *
149     * <pre> {@code
150     * class ForEach<E> ...
151     * public void compute() { // version 3
152     * int l = lo, h = hi;
153     * while (h - l >= 2) {
154     * int mid = (l + h) >>> 1;
155     * addToPendingCount(1);
156     * new ForEach(this, array, op, mid, h).fork(); // right child
157     * h = mid;
158     * }
159     * if (h > l)
160     * op.apply(array[l]);
161 dl 1.6 * helpComplete();
162 dl 1.1 * }
163     * }</pre>
164     *
165     * Additional improvements of such classes might entail precomputing
166     * pending counts so that they can be established in constructors,
167     * specializing classes for leaf steps, subdividing by say, four,
168     * instead of two per iteration, and using an adaptive threshold
169     * instead of always subdividing down to single elements.
170     *
171     * <p><b>Recording subtasks.</b> CountedCompleter tasks that combine
172     * results of multiple subtasks usually need to access these results
173     * in method {@link #onCompletion}. As illustrated in the following
174     * class (that performs a simplified form of map-reduce where mappings
175     * and reductions are all of type {@code E}), one way to do this in
176     * divide and conquer designs is to have each subtask record its
177     * sibling, so that it can be accessed in method {@code onCompletion}.
178 dl 1.3 * This technique applies to reductions in which the order of
179     * combining left and right results does not matter; ordered
180     * reductions require explicit left/right designations. Variants of
181     * other streamlinings seen in the above examples may also apply.
182 dl 1.1 *
183     * <pre> {@code
184     * class MyMapper<E> { E apply(E v) { ... } }
185     * class MyReducer<E> { E apply(E x, E y) { ... } }
186 dl 1.3 * class MapReducer<E> extends CountedCompleter<E> {
187 dl 1.1 * final E[] array; final MyMapper<E> mapper;
188     * final MyReducer<E> reducer; final int lo, hi;
189 dl 1.3 * MapReducer<E> sibling;
190 dl 1.1 * E result;
191     * MapReducer(CountedCompleter p, E[] array, MyMapper<E> mapper,
192     * MyReducer<E> reducer, int lo, int hi) {
193     * super(p);
194     * this.array = array; this.mapper = mapper;
195     * this.reducer = reducer; this.lo = lo; this.hi = hi;
196     * }
197     * public void compute() {
198     * if (hi - lo >= 2) {
199     * int mid = (lo + hi) >>> 1;
200     * MapReducer<E> left = new MapReducer(this, array, mapper, reducer, lo, mid);
201     * MapReducer<E> right = new MapReducer(this, array, mapper, reducer, mid, hi);
202     * left.sibling = right;
203     * right.sibling = left;
204     * setPendingCount(1); // only right is pending
205     * right.fork();
206     * left.compute(); // directly execute left
207     * }
208     * else {
209     * if (hi > lo)
210     * result = mapper.apply(array[lo]);
211     * tryComplete();
212     * }
213     * }
214     * public void onCompletion(CountedCompleter caller) {
215     * if (caller != this) {
216     * MapReducer<E> child = (MapReducer<E>)caller;
217     * MapReducer<E> sib = child.sibling;
218     * if (sib == null || sib.result == null)
219     * result = child.result;
220     * else
221     * result = reducer.apply(child.result, sib.result);
222     * }
223     * }
224 dl 1.3 * public E getRawResult() { return result; }
225 dl 1.1 *
226     * public static <E> E mapReduce(ForkJoinPool pool, E[] array,
227     * MyMapper<E> mapper, MyReducer<E> reducer) {
228 dl 1.3 * return pool.invoke(new MapReducer<E>(null, array, mapper,
229     * reducer, 0, array.length));
230 dl 1.1 * }
231     * } }</pre>
232     *
233     * <p><b>Triggers.</b> Some CountedCompleters are themselves never
234     * forked, but instead serve as bits of plumbing in other designs;
235     * including those in which the completion of one of more async tasks
236     * triggers another async task. For example:
237     *
238     * <pre> {@code
239 dl 1.3 * class HeaderBuilder extends CountedCompleter<...> { ... }
240     * class BodyBuilder extends CountedCompleter<...> { ... }
241     * class PacketSender extends CountedCompleter<...> {
242 dl 1.1 * PacketSender(...) { super(null, 1); ... } // trigger on second completion
243     * public void compute() { } // never called
244 dl 1.3 * public void onCompletion(CountedCompleter<?> caller) { sendPacket(); }
245 dl 1.1 * }
246     * // sample use:
247     * PacketSender p = new PacketSender();
248     * new HeaderBuilder(p, ...).fork();
249     * new BodyBuilder(p, ...).fork();
250     * }</pre>
251     *
252     * @since 1.8
253     * @author Doug Lea
254     */
255 dl 1.3 public abstract class CountedCompleter<T> extends ForkJoinTask<T> {
256 dl 1.2 private static final long serialVersionUID = 5232453752276485070L;
257    
258 dl 1.1 /** This task's completer, or null if none */
259 dl 1.3 final CountedCompleter<?> completer;
260 dl 1.1 /** The number of pending tasks until completion */
261     volatile int pending;
262    
263     /**
264     * Creates a new CountedCompleter with the given completer
265     * and initial pending count.
266     *
267     * @param completer this tasks completer, or {@code null} if none
268     * @param initialPendingCount the initial pending count
269     */
270 dl 1.3 protected CountedCompleter(CountedCompleter<?> completer,
271 dl 1.1 int initialPendingCount) {
272     this.completer = completer;
273     this.pending = initialPendingCount;
274     }
275    
276     /**
277     * Creates a new CountedCompleter with the given completer
278     * and an initial pending count of zero.
279     *
280     * @param completer this tasks completer, or {@code null} if none
281     */
282 dl 1.3 protected CountedCompleter(CountedCompleter<?> completer) {
283 dl 1.1 this.completer = completer;
284     }
285    
286     /**
287     * Creates a new CountedCompleter with no completer
288     * and an initial pending count of zero.
289     */
290     protected CountedCompleter() {
291     this.completer = null;
292     }
293    
294     /**
295     * The main computation performed by this task.
296     */
297     public abstract void compute();
298    
299     /**
300 dl 1.2 * Performs an action when method {@link #tryComplete} is invoked
301     * and there are no pending counts, or when the unconditional
302     * method {@link #complete} is invoked. By default, this method
303     * does nothing.
304 dl 1.1 *
305     * @param caller the task invoking this method (which may
306     * be this task itself).
307     */
308 dl 1.3 public void onCompletion(CountedCompleter<?> caller) {
309 dl 1.1 }
310    
311     /**
312 dl 1.2 * Performs an action when method {@link #completeExceptionally}
313     * is invoked or method {@link #compute} throws an exception, and
314     * this task has not otherwise already completed normally. On
315     * entry to this method, this task {@link
316     * ForkJoinTask#isCompletedAbnormally}. The return value of this
317     * method controls further propagation: If {@code true} and this
318     * task has a completer, then this completer is also completed
319     * exceptionally. The default implementation of this method does
320     * nothing except return {@code true}.
321     *
322     * @param ex the exception
323     * @param caller the task invoking this method (which may
324     * be this task itself).
325     * @return true if this exception should be propagated to this
326     * tasks completer, if one exists.
327     */
328 dl 1.3 public boolean onExceptionalCompletion(Throwable ex, CountedCompleter<?> caller) {
329 dl 1.2 return true;
330     }
331    
332     /**
333 dl 1.1 * Returns the completer established in this task's constructor,
334     * or {@code null} if none.
335     *
336     * @return the completer
337     */
338 dl 1.3 public final CountedCompleter<?> getCompleter() {
339 dl 1.1 return completer;
340     }
341    
342     /**
343     * Returns the current pending count.
344     *
345     * @return the current pending count
346     */
347     public final int getPendingCount() {
348     return pending;
349     }
350    
351     /**
352     * Sets the pending count to the given value.
353     *
354     * @param count the count
355     */
356     public final void setPendingCount(int count) {
357     pending = count;
358     }
359    
360     /**
361     * Adds (atomically) the given value to the pending count.
362     *
363     * @param delta the value to add
364     */
365     public final void addToPendingCount(int delta) {
366     int c; // note: can replace with intrinsic in jdk8
367     do {} while (!U.compareAndSwapInt(this, PENDING, c = pending, c+delta));
368     }
369    
370     /**
371     * Sets (atomically) the pending count to the given count only if
372     * it currently holds the given expected value.
373     *
374     * @param expected the expected value
375     * @param count the new value
376     * @return true is successful
377     */
378     public final boolean compareAndSetPendingCount(int expected, int count) {
379     return U.compareAndSwapInt(this, PENDING, expected, count);
380     }
381    
382     /**
383 dl 1.6 * Returns the root of the current computation; i.e., this
384     * task if it has no completer, else its completer's root.
385     *
386     * @return the root of the current computation
387     */
388     public final CountedCompleter<?> getRoot() {
389     CountedCompleter<?> a = this, p;
390     while ((p = a.completer) != null)
391     a = p;
392     return a;
393     }
394    
395     /**
396 dl 1.1 * If the pending count is nonzero, decrements the count;
397     * otherwise invokes {@link #onCompletion} and then similarly
398     * tries to complete this task's completer, if one exists,
399     * else marks this task as complete.
400     */
401     public final void tryComplete() {
402 dl 1.3 CountedCompleter<?> a = this, s = a;
403 dl 1.2 for (int c;;) {
404 dl 1.1 if ((c = a.pending) == 0) {
405     a.onCompletion(s);
406     if ((a = (s = a).completer) == null) {
407     s.quietlyComplete();
408     return;
409     }
410     }
411     else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
412     return;
413     }
414     }
415    
416     /**
417 dl 1.6 * Identical to {@link #tryComplete}, but may additionally execute
418     * other tasks within the current computation (i.e., those
419     * with the same {@link #getRoot}.
420     */
421     public final void helpComplete() {
422     CountedCompleter<?> a = this, s = a;
423     for (int c;;) {
424     if ((c = a.pending) == 0) {
425     a.onCompletion(s);
426     if ((a = (s = a).completer) == null) {
427     s.quietlyComplete();
428     return;
429     }
430     }
431     else if (U.compareAndSwapInt(a, PENDING, c, c - 1)) {
432     CountedCompleter<?> root = a.getRoot();
433     Thread thread = Thread.currentThread();
434     ForkJoinPool.WorkQueue wq =
435 jsr166 1.7 (thread instanceof ForkJoinWorkerThread) ?
436 dl 1.6 ((ForkJoinWorkerThread)thread).workQueue : null;
437     ForkJoinTask<?> t;
438     while ((t = (wq != null) ? wq.popCC(root) :
439     ForkJoinPool.popCCFromCommonPool(root)) != null) {
440     t.doExec();
441     if (root.isDone())
442     break;
443     }
444     return;
445     }
446     }
447     }
448    
449     /**
450 dl 1.1 * Regardless of pending count, invokes {@link #onCompletion},
451 dl 1.3 * marks this task as complete and further triggers {@link
452     * #tryComplete} on this task's completer, if one exists. This
453     * method may be useful when forcing completion as soon as any one
454     * (versus all) of several subtask results are obtained. The
455     * given rawResult is used as an argument to {@link #setRawResult}
456     * before marking this task as complete; its value is meaningful
457     * only for classes overriding {@code setRawResult}.
458 dl 1.1 *
459 dl 1.3 * @param rawResult the raw result
460 dl 1.1 */
461 dl 1.3 public void complete(T rawResult) {
462     CountedCompleter<?> p;
463 dl 1.1 onCompletion(this);
464 dl 1.3 setRawResult(rawResult);
465 dl 1.1 quietlyComplete();
466     if ((p = completer) != null)
467     p.tryComplete();
468     }
469    
470     /**
471 dl 1.2 * Support for FJT exception propagation
472     */
473     void internalPropagateException(Throwable ex) {
474 dl 1.3 CountedCompleter<?> a = this, s = a;
475 dl 1.2 while (a.onExceptionalCompletion(ex, s) &&
476     (a = (s = a).completer) != null && a.status >= 0)
477     a.recordExceptionalCompletion(ex);
478     }
479    
480     /**
481 dl 1.1 * Implements execution conventions for CountedCompleters
482     */
483     protected final boolean exec() {
484     compute();
485     return false;
486     }
487    
488     /**
489 dl 1.3 * Returns the result of the computation. By default
490     * returns {@code null}, which is appropriate for {@code Void}
491     * actions, but in other cases should be overridden.
492 dl 1.1 *
493 dl 1.3 * @return the result of the computation
494 dl 1.1 */
495 dl 1.3 public T getRawResult() { return null; }
496 dl 1.1
497     /**
498 dl 1.3 * A method that result-bearing CountedCompleters may optionally
499     * use to help maintain result data. By default, does nothing.
500 dl 1.1 */
501 dl 1.3 protected void setRawResult(T t) { }
502 dl 1.1
503     // Unsafe mechanics
504     private static final sun.misc.Unsafe U;
505     private static final long PENDING;
506     static {
507     try {
508     U = getUnsafe();
509     PENDING = U.objectFieldOffset
510     (CountedCompleter.class.getDeclaredField("pending"));
511     } catch (Exception e) {
512     throw new Error(e);
513     }
514     }
515    
516     /**
517     * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
518     * Replace with a simple call to Unsafe.getUnsafe when integrating
519     * into a jdk.
520     *
521     * @return a sun.misc.Unsafe
522     */
523     private static sun.misc.Unsafe getUnsafe() {
524     try {
525     return sun.misc.Unsafe.getUnsafe();
526     } catch (SecurityException se) {
527     try {
528     return java.security.AccessController.doPrivileged
529     (new java.security
530     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
531     public sun.misc.Unsafe run() throws Exception {
532     java.lang.reflect.Field f = sun.misc
533     .Unsafe.class.getDeclaredField("theUnsafe");
534     f.setAccessible(true);
535     return (sun.misc.Unsafe) f.get(null);
536     }});
537     } catch (java.security.PrivilegedActionException e) {
538     throw new RuntimeException("Could not initialize intrinsics",
539     e.getCause());
540     }
541     }
542     }
543    
544     }