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

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.9 by jsr166, Mon Jul 20 22:26:03 2009 UTC vs.
Revision 1.12 by jsr166, Tue Jul 21 18:11:44 2009 UTC

# Line 81 | Line 81 | public class ForkJoinPool extends Abstra
81           * Returns a new worker thread operating in the given pool.
82           *
83           * @param pool the pool this thread works in
84 <         * @throws NullPointerException if pool is null;
84 >         * @throws NullPointerException if pool is null
85           */
86          public ForkJoinWorkerThread newThread(ForkJoinPool pool);
87      }
# Line 226 | Line 226 | public class ForkJoinPool extends Abstra
226      private static int workerCountsFor(int t, int r) { return (t << 16) + r; }
227  
228      /**
229 <     * Add delta (which may be negative) to running count.  This must
229 >     * Adds delta (which may be negative) to running count.  This must
230       * be called before (with negative arg) and after (with positive)
231 <     * any managed synchronization (i.e., mainly, joins)
231 >     * any managed synchronization (i.e., mainly, joins).
232       * @param delta the number to add
233       */
234      final void updateRunningCount(int delta) {
# Line 237 | Line 237 | public class ForkJoinPool extends Abstra
237      }
238  
239      /**
240 <     * Add delta (which may be negative) to both total and running
240 >     * Adds delta (which may be negative) to both total and running
241       * count.  This must be called upon creation and termination of
242       * worker threads.
243       * @param delta the number to add
# Line 273 | Line 273 | public class ForkJoinPool extends Abstra
273      /**
274       * Try incrementing active count; fail on contention. Called by
275       * workers before/during executing tasks.
276 <     * @return true on success;
276 >     * @return true on success
277       */
278      final boolean tryIncrementActiveCount() {
279          int c = runControl;
# Line 281 | Line 281 | public class ForkJoinPool extends Abstra
281      }
282  
283      /**
284 <     * Try decrementing active count; fail on contention.
285 <     * Possibly trigger termination on success
284 >     * Tries decrementing active count; fails on contention.
285 >     * Possibly triggers termination on success.
286       * Called by workers when they can't find tasks.
287       * @return true on success
288       */
# Line 297 | Line 297 | public class ForkJoinPool extends Abstra
297      }
298  
299      /**
300 <     * Return true if argument represents zero active count and
300 >     * Returns true if argument represents zero active count and
301       * nonzero runstate, which is the triggering condition for
302       * terminating on shutdown.
303       */
# Line 376 | Line 376 | public class ForkJoinPool extends Abstra
376       * @param parallelism the targeted number of worker threads
377       * @param factory the factory for creating new threads
378       * @throws IllegalArgumentException if parallelism less than or
379 <     * equal to zero, or greater than implementation limit.
379 >     * equal to zero, or greater than implementation limit
380       * @throws NullPointerException if factory is null
381       * @throws SecurityException if a security manager exists and
382       *         the caller is not permitted to modify threads
# Line 421 | Line 421 | public class ForkJoinPool extends Abstra
421      }
422  
423      /**
424 <     * Return a good size for worker array given pool size.
424 >     * Returns a good size for worker array given pool size.
425       * Currently requires size to be a power of two.
426       */
427      private static int arraySizeFor(int ps) {
# Line 429 | Line 429 | public class ForkJoinPool extends Abstra
429      }
430  
431      /**
432 <     * Create or resize array if necessary to hold newLength.
433 <     * Call only under exclusion or lock
432 >     * Creates or resizes array if necessary to hold newLength.
433 >     * Call only under exclusion or lock.
434       * @return the array
435       */
436      private ForkJoinWorkerThread[] ensureWorkerArrayCapacity(int newLength) {
# Line 719 | Line 719 | public class ForkJoinPool extends Abstra
719       * Sets the target parallelism level of this pool.
720       * @param parallelism the target parallelism
721       * @throws IllegalArgumentException if parallelism less than or
722 <     * equal to zero or greater than maximum size bounds.
722 >     * equal to zero or greater than maximum size bounds
723       * @throws SecurityException if a security manager exists and
724       *         the caller is not permitted to modify threads
725       *         because it does not hold {@link
# Line 782 | Line 782 | public class ForkJoinPool extends Abstra
782       * Setting this value has no effect on current pool size. It
783       * controls construction of new threads.
784       * @throws IllegalArgumentException if negative or greater then
785 <     * internal implementation limit.
785 >     * internal implementation limit
786       */
787      public void setMaximumPoolSize(int newMax) {
788          if (newMax < 0 || newMax > MAX_THREADS)
# Line 822 | Line 822 | public class ForkJoinPool extends Abstra
822       * invocations at other times may be unpredictable.
823       *
824       * @param async if true, use locally FIFO scheduling
825 <     * @return the previous mode.
825 >     * @return the previous mode
826       */
827      public boolean setAsyncMode(boolean async) {
828          boolean oldMode = locallyFifo;
# Line 842 | Line 842 | public class ForkJoinPool extends Abstra
842       * Returns true if this pool uses local first-in-first-out
843       * scheduling mode for forked tasks that are never joined.
844       *
845 <     * @return true if this pool uses async mode.
845 >     * @return true if this pool uses async mode
846       */
847      public boolean getAsyncMode() {
848          return locallyFifo;
# Line 863 | Line 863 | public class ForkJoinPool extends Abstra
863       * Returns an estimate of the number of threads that are currently
864       * stealing or executing tasks. This method may overestimate the
865       * number of active threads.
866 <     * @return the number of active threads.
866 >     * @return the number of active threads
867       */
868      public int getActiveThreadCount() {
869          return activeCountOf(runControl);
# Line 873 | Line 873 | public class ForkJoinPool extends Abstra
873       * Returns an estimate of the number of threads that are currently
874       * idle waiting for tasks. This method may underestimate the
875       * number of idle threads.
876 <     * @return the number of idle threads.
876 >     * @return the number of idle threads
877       */
878      final int getIdleThreadCount() {
879          int c = runningCountOf(workerCounts) - activeCountOf(runControl);
# Line 902 | Line 902 | public class ForkJoinPool extends Abstra
902       * tuning fork/join programs: In general, steal counts should be
903       * high enough to keep threads busy, but low enough to avoid
904       * overhead and contention across threads.
905 <     * @return the number of steals.
905 >     * @return the number of steals
906       */
907      public long getStealCount() {
908          return stealCount.get();
# Line 925 | Line 925 | public class ForkJoinPool extends Abstra
925       * an approximation, obtained by iterating across all threads in
926       * the pool. This method may be useful for tuning task
927       * granularities.
928 <     * @return the number of queued tasks.
928 >     * @return the number of queued tasks
929       */
930      public long getQueuedTaskCount() {
931          long count = 0;
# Line 944 | Line 944 | public class ForkJoinPool extends Abstra
944       * Returns an estimate of the number tasks submitted to this pool
945       * that have not yet begun executing. This method takes time
946       * proportional to the number of submissions.
947 <     * @return the number of queued submissions.
947 >     * @return the number of queued submissions
948       */
949      public int getQueuedSubmissionCount() {
950          return submissionQueue.size();
# Line 953 | Line 953 | public class ForkJoinPool extends Abstra
953      /**
954       * Returns true if there are any tasks submitted to this pool
955       * that have not yet begun executing.
956 <     * @return {@code true} if there are any queued submissions.
956 >     * @return {@code true} if there are any queued submissions
957       */
958      public boolean hasQueuedSubmissions() {
959          return !submissionQueue.isEmpty();
# Line 1183 | Line 1183 | public class ForkJoinPool extends Abstra
1183      }
1184  
1185      /**
1186 <     * Possibly terminate when on shutdown state
1186 >     * Possibly terminates when on shutdown state.
1187       */
1188      private void terminateOnShutdown() {
1189          if (!hasQueuedSubmissions() && canTerminateOnShutdown(runControl))
# Line 1191 | Line 1191 | public class ForkJoinPool extends Abstra
1191      }
1192  
1193      /**
1194 <     * Clear out and cancel submissions
1194 >     * Clears out and cancels submissions.
1195       */
1196      private void cancelQueuedSubmissions() {
1197          ForkJoinTask<?> task;
# Line 1200 | Line 1200 | public class ForkJoinPool extends Abstra
1200      }
1201  
1202      /**
1203 <     * Clean out worker queues.
1203 >     * Cleans out worker queues.
1204       */
1205      private void cancelQueuedWorkerTasks() {
1206          final ReentrantLock lock = this.workerLock;
# Line 1220 | Line 1220 | public class ForkJoinPool extends Abstra
1220      }
1221  
1222      /**
1223 <     * Set each worker's status to terminating. Requires lock to avoid
1224 <     * conflicts with add/remove
1223 >     * Sets each worker's status to terminating. Requires lock to avoid
1224 >     * conflicts with add/remove.
1225       */
1226      private void stopAllWorkers() {
1227          final ReentrantLock lock = this.workerLock;
# Line 1241 | Line 1241 | public class ForkJoinPool extends Abstra
1241      }
1242  
1243      /**
1244 <     * Interrupt all unterminated workers.  This is not required for
1244 >     * Interrupts all unterminated workers.  This is not required for
1245       * sake of internal control, but may help unstick user code during
1246       * shutdown.
1247       */
# Line 1311 | Line 1311 | public class ForkJoinPool extends Abstra
1311          }
1312  
1313          /**
1314 <         * Wake up waiter, returning false if known to already
1314 >         * Wakes up waiter, returning false if known to already
1315           */
1316          boolean signal() {
1317              ForkJoinWorkerThread t = thread;
# Line 1323 | Line 1323 | public class ForkJoinPool extends Abstra
1323          }
1324  
1325          /**
1326 <         * Await release on sync
1326 >         * Awaits release on sync.
1327           */
1328          void awaitSyncRelease(ForkJoinPool p) {
1329              while (thread != null && !p.syncIsReleasable(this))
# Line 1331 | Line 1331 | public class ForkJoinPool extends Abstra
1331          }
1332  
1333          /**
1334 <         * Await resumption as spare
1334 >         * Awaits resumption as spare.
1335           */
1336          void awaitSpareRelease() {
1337              while (thread != null) {
# Line 1371 | Line 1371 | public class ForkJoinPool extends Abstra
1371      }
1372  
1373      /**
1374 <     * Signal threads waiting to poll a task. Because method sync
1374 >     * Signals threads waiting to poll a task. Because method sync
1375       * rechecks availability, it is OK to only proceed if queue
1376       * appears to be non-empty, and OK to skip under contention to
1377       * increment count (since some other thread succeeded).
# Line 1458 | Line 1458 | public class ForkJoinPool extends Abstra
1458      //  Parallelism maintenance
1459  
1460      /**
1461 <     * Decrement running count; if too low, add spare.
1461 >     * Decrements running count; if too low, adds spare.
1462       *
1463       * Conceptually, all we need to do here is add or resume a
1464       * spare thread when one is about to block (and remove or
# Line 1548 | Line 1548 | public class ForkJoinPool extends Abstra
1548      }
1549  
1550      /**
1551 <     * Add a spare worker if lock available and no more than the
1552 <     * expected numbers of threads exist
1551 >     * Adds a spare worker if lock available and no more than the
1552 >     * expected numbers of threads exist.
1553       * @return true if successful
1554       */
1555      private boolean tryAddSpare(int expectedCounts) {
# Line 1582 | Line 1582 | public class ForkJoinPool extends Abstra
1582      }
1583  
1584      /**
1585 <     * Add the kth spare worker. On entry, pool counts are already
1585 >     * Adds the kth spare worker. On entry, pool counts are already
1586       * adjusted to reflect addition.
1587       */
1588      private void createAndStartSpare(int k) {
# Line 1604 | Line 1604 | public class ForkJoinPool extends Abstra
1604      }
1605  
1606      /**
1607 <     * Suspend calling thread w if there are excess threads.  Called
1608 <     * only from sync.  Spares are enqueued in a Treiber stack
1609 <     * using the same WaitQueueNodes as barriers.  They are resumed
1610 <     * mainly in preJoin, but are also woken on pool events that
1611 <     * require all threads to check run state.
1607 >     * Suspends calling thread w if there are excess threads.  Called
1608 >     * only from sync.  Spares are enqueued in a Treiber stack using
1609 >     * the same WaitQueueNodes as barriers.  They are resumed mainly
1610 >     * in preJoin, but are also woken on pool events that require all
1611 >     * threads to check run state.
1612       * @param w the caller
1613       */
1614      private boolean suspendIfSpare(ForkJoinWorkerThread w) {
# Line 1629 | Line 1629 | public class ForkJoinPool extends Abstra
1629      }
1630  
1631      /**
1632 <     * Try to pop and resume a spare thread.
1632 >     * Tries to pop and resume a spare thread.
1633       * @param updateCount if true, increment running count on success
1634       * @return true if successful
1635       */
# Line 1647 | Line 1647 | public class ForkJoinPool extends Abstra
1647      }
1648  
1649      /**
1650 <     * Pop and resume all spare threads. Same idea as ensureSync.
1650 >     * Pops and resumes all spare threads. Same idea as ensureSync.
1651       * @return true if any spares released
1652       */
1653      private boolean resumeAllSpares() {
# Line 1665 | Line 1665 | public class ForkJoinPool extends Abstra
1665      }
1666  
1667      /**
1668 <     * Pop and shutdown excessive spare threads. Call only while
1668 >     * Pops and shuts down excessive spare threads. Call only while
1669       * holding lock. This is not guaranteed to eliminate all excess
1670       * threads, only those suspended as spares, which are the ones
1671       * unlikely to be needed in the future.
# Line 1717 | Line 1717 | public class ForkJoinPool extends Abstra
1717           * Possibly blocks the current thread, for example waiting for
1718           * a lock or condition.
1719           * @return true if no additional blocking is necessary (i.e.,
1720 <         * if isReleasable would return true).
1720 >         * if isReleasable would return true)
1721           * @throws InterruptedException if interrupted while waiting
1722           * (the method is not required to do so, but is allowed to).
1723           */
# Line 1756 | Line 1756 | public class ForkJoinPool extends Abstra
1756       * attempt to maintain the pool's nominal parallelism; otherwise
1757       * activate a thread only if necessary to avoid complete
1758       * starvation.
1759 <     * @throws InterruptedException if blocker.block did so.
1759 >     * @throws InterruptedException if blocker.block did so
1760       */
1761      public static void managedBlock(ManagedBlocker blocker,
1762                                      boolean maintainParallelism)
# Line 1818 | Line 1818 | public class ForkJoinPool extends Abstra
1818  
1819      private static long fieldOffset(String fieldName)
1820              throws NoSuchFieldException {
1821 <        return _unsafe.objectFieldOffset
1821 >        return UNSAFE.objectFieldOffset
1822              (ForkJoinPool.class.getDeclaredField(fieldName));
1823      }
1824  
1825 <    static final Unsafe _unsafe;
1825 >    static final Unsafe UNSAFE;
1826      static final long eventCountOffset;
1827      static final long workerCountsOffset;
1828      static final long runControlOffset;
# Line 1831 | Line 1831 | public class ForkJoinPool extends Abstra
1831  
1832      static {
1833          try {
1834 <            _unsafe = getUnsafe();
1834 >            UNSAFE = getUnsafe();
1835              eventCountOffset = fieldOffset("eventCount");
1836              workerCountsOffset = fieldOffset("workerCounts");
1837              runControlOffset = fieldOffset("runControl");
# Line 1843 | Line 1843 | public class ForkJoinPool extends Abstra
1843      }
1844  
1845      private boolean casEventCount(long cmp, long val) {
1846 <        return _unsafe.compareAndSwapLong(this, eventCountOffset, cmp, val);
1846 >        return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val);
1847      }
1848      private boolean casWorkerCounts(int cmp, int val) {
1849 <        return _unsafe.compareAndSwapInt(this, workerCountsOffset, cmp, val);
1849 >        return UNSAFE.compareAndSwapInt(this, workerCountsOffset, cmp, val);
1850      }
1851      private boolean casRunControl(int cmp, int val) {
1852 <        return _unsafe.compareAndSwapInt(this, runControlOffset, cmp, val);
1852 >        return UNSAFE.compareAndSwapInt(this, runControlOffset, cmp, val);
1853      }
1854      private boolean casSpareStack(WaitQueueNode cmp, WaitQueueNode val) {
1855 <        return _unsafe.compareAndSwapObject(this, spareStackOffset, cmp, val);
1855 >        return UNSAFE.compareAndSwapObject(this, spareStackOffset, cmp, val);
1856      }
1857      private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) {
1858 <        return _unsafe.compareAndSwapObject(this, syncStackOffset, cmp, val);
1858 >        return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val);
1859      }
1860   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines