src/share/classes/java/util/concurrent/ForkJoinWorkerThread.java

Print this page




 175      */
 176     private static final int  SMASK  = 0xffff;
 177 
 178     /**
 179      * Capacity of work-stealing queue array upon initialization.
 180      * Must be a power of two. Initial size must be at least 4, but is
 181      * padded to minimize cache effects.
 182      */
 183     private static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
 184 
 185     /**
 186      * Maximum size for queue array. Must be a power of two
 187      * less than or equal to 1 << (31 - width of array entry) to
 188      * ensure lack of index wraparound, but is capped at a lower
 189      * value to help users trap runaway computations.
 190      */
 191     private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 24; // 16M
 192 
 193     /**
 194      * The work-stealing queue array. Size must be a power of two.
 195      * Initialized when started (as oposed to when constructed), to
 196      * improve memory locality.
 197      */
 198     ForkJoinTask<?>[] queue;
 199 
 200     /**
 201      * The pool this thread works in. Accessed directly by ForkJoinTask.
 202      */
 203     final ForkJoinPool pool;
 204 
 205     /**
 206      * Index (mod queue.length) of next queue slot to push to or pop
 207      * from. It is written only by owner thread, and accessed by other
 208      * threads only after reading (volatile) queueBase.  Both queueTop
 209      * and queueBase are allowed to wrap around on overflow, but
 210      * (queueTop - queueBase) still estimates size.
 211      */
 212     int queueTop;
 213 
 214     /**
 215      * Index (mod queue.length) of least valid queue slot, which is


 343         int r = seed;
 344         r ^= r << 13;
 345         r ^= r >>> 17;
 346         r ^= r << 5;
 347         return seed = r;
 348     }
 349 
 350     // Run State management
 351 
 352     /**
 353      * Initializes internal state after construction but before
 354      * processing any tasks. If you override this method, you must
 355      * invoke {@code super.onStart()} at the beginning of the method.
 356      * Initialization requires care: Most fields must have legal
 357      * default values, to ensure that attempted accesses from other
 358      * threads work correctly even before this thread starts
 359      * processing tasks.
 360      */
 361     protected void onStart() {
 362         queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
 363         int r = pool.workerSeedGenerator.nextInt();
 364         seed = (r == 0) ? 1 : r; //  must be nonzero
 365     }
 366 
 367     /**
 368      * Performs cleanup associated with termination of this worker
 369      * thread.  If you override this method, you must invoke
 370      * {@code super.onTermination} at the end of the overridden method.
 371      *
 372      * @param exception the exception causing this thread to abort due
 373      * to an unrecoverable error, or {@code null} if completed normally
 374      */
 375     protected void onTermination(Throwable exception) {
 376         try {
 377             terminate = true;
 378             cancelTasks();
 379             pool.deregisterWorker(this, exception);
 380         } catch (Throwable ex) {        // Shouldn't ever happen
 381             if (exception == null)      // but if so, at least rethrown
 382                 exception = ex;
 383         } finally {


 967                     p.addActiveCount(-1);
 968                 }
 969                 if (p.isQuiescent()) {
 970                     p.addActiveCount(1);
 971                     p.addQuiescerCount(-1);
 972                     break;
 973                 }
 974             }
 975         }
 976     }
 977 
 978     // Unsafe mechanics
 979     private static final sun.misc.Unsafe UNSAFE;
 980     private static final long ABASE;
 981     private static final int ASHIFT;
 982 
 983     static {
 984         int s;
 985         try {
 986             UNSAFE = sun.misc.Unsafe.getUnsafe();
 987             Class a = ForkJoinTask[].class;
 988             ABASE = UNSAFE.arrayBaseOffset(a);
 989             s = UNSAFE.arrayIndexScale(a);
 990         } catch (Exception e) {
 991             throw new Error(e);
 992         }
 993         if ((s & (s-1)) != 0)
 994             throw new Error("data type scale not a power of two");
 995         ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
 996     }
 997 
 998 }


 175      */
 176     private static final int  SMASK  = 0xffff;
 177 
 178     /**
 179      * Capacity of work-stealing queue array upon initialization.
 180      * Must be a power of two. Initial size must be at least 4, but is
 181      * padded to minimize cache effects.
 182      */
 183     private static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
 184 
 185     /**
 186      * Maximum size for queue array. Must be a power of two
 187      * less than or equal to 1 << (31 - width of array entry) to
 188      * ensure lack of index wraparound, but is capped at a lower
 189      * value to help users trap runaway computations.
 190      */
 191     private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 24; // 16M
 192 
 193     /**
 194      * The work-stealing queue array. Size must be a power of two.
 195      * Initialized when started (as opposed to when constructed), to
 196      * improve memory locality.
 197      */
 198     ForkJoinTask<?>[] queue;
 199 
 200     /**
 201      * The pool this thread works in. Accessed directly by ForkJoinTask.
 202      */
 203     final ForkJoinPool pool;
 204 
 205     /**
 206      * Index (mod queue.length) of next queue slot to push to or pop
 207      * from. It is written only by owner thread, and accessed by other
 208      * threads only after reading (volatile) queueBase.  Both queueTop
 209      * and queueBase are allowed to wrap around on overflow, but
 210      * (queueTop - queueBase) still estimates size.
 211      */
 212     int queueTop;
 213 
 214     /**
 215      * Index (mod queue.length) of least valid queue slot, which is


 343         int r = seed;
 344         r ^= r << 13;
 345         r ^= r >>> 17;
 346         r ^= r << 5;
 347         return seed = r;
 348     }
 349 
 350     // Run State management
 351 
 352     /**
 353      * Initializes internal state after construction but before
 354      * processing any tasks. If you override this method, you must
 355      * invoke {@code super.onStart()} at the beginning of the method.
 356      * Initialization requires care: Most fields must have legal
 357      * default values, to ensure that attempted accesses from other
 358      * threads work correctly even before this thread starts
 359      * processing tasks.
 360      */
 361     protected void onStart() {
 362         queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
 363         int r = ForkJoinPool.workerSeedGenerator.nextInt();
 364         seed = (r == 0) ? 1 : r; //  must be nonzero
 365     }
 366 
 367     /**
 368      * Performs cleanup associated with termination of this worker
 369      * thread.  If you override this method, you must invoke
 370      * {@code super.onTermination} at the end of the overridden method.
 371      *
 372      * @param exception the exception causing this thread to abort due
 373      * to an unrecoverable error, or {@code null} if completed normally
 374      */
 375     protected void onTermination(Throwable exception) {
 376         try {
 377             terminate = true;
 378             cancelTasks();
 379             pool.deregisterWorker(this, exception);
 380         } catch (Throwable ex) {        // Shouldn't ever happen
 381             if (exception == null)      // but if so, at least rethrown
 382                 exception = ex;
 383         } finally {


 967                     p.addActiveCount(-1);
 968                 }
 969                 if (p.isQuiescent()) {
 970                     p.addActiveCount(1);
 971                     p.addQuiescerCount(-1);
 972                     break;
 973                 }
 974             }
 975         }
 976     }
 977 
 978     // Unsafe mechanics
 979     private static final sun.misc.Unsafe UNSAFE;
 980     private static final long ABASE;
 981     private static final int ASHIFT;
 982 
 983     static {
 984         int s;
 985         try {
 986             UNSAFE = sun.misc.Unsafe.getUnsafe();
 987             Class<?> a = ForkJoinTask[].class;
 988             ABASE = UNSAFE.arrayBaseOffset(a);
 989             s = UNSAFE.arrayIndexScale(a);
 990         } catch (Exception e) {
 991             throw new Error(e);
 992         }
 993         if ((s & (s-1)) != 0)
 994             throw new Error("data type scale not a power of two");
 995         ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
 996     }
 997 
 998 }