src/share/vm/utilities/taskqueue.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 777
37f87013dfd8
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_taskqueue.cpp.incl"
    28 bool TaskQueueSuper::peek() {
    29   return _bottom != _age.top();
    30 }
    32 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
    33   const int a =      16807;
    34   const int m = 2147483647;
    35   const int q =     127773;  /* m div a */
    36   const int r =       2836;  /* m mod a */
    37   assert(sizeof(int) == 4, "I think this relies on that");
    38   int seed = *seed0;
    39   int hi   = seed / q;
    40   int lo   = seed % q;
    41   int test = a * lo - r * hi;
    42   if (test > 0)
    43     seed = test;
    44   else
    45     seed = test + m;
    46   *seed0 = seed;
    47   return seed;
    48 }
    50 ParallelTaskTerminator::
    51 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
    52   _n_threads(n_threads),
    53   _queue_set(queue_set),
    54   _offered_termination(0) {}
    56 bool ParallelTaskTerminator::peek_in_queue_set() {
    57   return _queue_set->peek();
    58 }
    60 void ParallelTaskTerminator::yield() {
    61   os::yield();
    62 }
    64 void ParallelTaskTerminator::sleep(uint millis) {
    65   os::sleep(Thread::current(), millis, false);
    66 }
    68 bool ParallelTaskTerminator::offer_termination() {
    69   Atomic::inc(&_offered_termination);
    71   juint yield_count = 0;
    72   while (true) {
    73     if (_offered_termination == _n_threads) {
    74       //inner_termination_loop();
    75       return true;
    76     } else {
    77       if (yield_count <= WorkStealingYieldsBeforeSleep) {
    78         yield_count++;
    79         yield();
    80       } else {
    81         if (PrintGCDetails && Verbose) {
    82          gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
    83            "thread %d sleeps after %d yields",
    84            Thread::current(), yield_count);
    85         }
    86         yield_count = 0;
    87         // A sleep will cause this processor to seek work on another processor's
    88         // runqueue, if it has nothing else to run (as opposed to the yield
    89         // which may only move the thread to the end of the this processor's
    90         // runqueue).
    91         sleep(WorkStealingSleepMillis);
    92       }
    94       if (peek_in_queue_set()) {
    95         Atomic::dec(&_offered_termination);
    96         return false;
    97       }
    98     }
    99   }
   100 }
   102 void ParallelTaskTerminator::reset_for_reuse() {
   103   if (_offered_termination != 0) {
   104     assert(_offered_termination == _n_threads,
   105            "Terminator may still be in use");
   106     _offered_termination = 0;
   107   }
   108 }
   110 bool ChunkTaskQueueWithOverflow::is_empty() {
   111   return (_chunk_queue.size() == 0) &&
   112          (_overflow_stack->length() == 0);
   113 }
   115 bool ChunkTaskQueueWithOverflow::stealable_is_empty() {
   116   return _chunk_queue.size() == 0;
   117 }
   119 bool ChunkTaskQueueWithOverflow::overflow_is_empty() {
   120   return _overflow_stack->length() == 0;
   121 }
   123 void ChunkTaskQueueWithOverflow::initialize() {
   124   _chunk_queue.initialize();
   125   assert(_overflow_stack == 0, "Creating memory leak");
   126   _overflow_stack =
   127     new (ResourceObj::C_HEAP) GrowableArray<ChunkTask>(10, true);
   128 }
   130 void ChunkTaskQueueWithOverflow::save(ChunkTask t) {
   131   if (TraceChunkTasksQueuing && Verbose) {
   132     gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
   133   }
   134   if(!_chunk_queue.push(t)) {
   135     _overflow_stack->push(t);
   136   }
   137 }
   139 // Note that using this method will retrieve all chunks
   140 // that have been saved but that it will always check
   141 // the overflow stack.  It may be more efficient to
   142 // check the stealable queue and the overflow stack
   143 // separately.
   144 bool ChunkTaskQueueWithOverflow::retrieve(ChunkTask& chunk_task) {
   145   bool result = retrieve_from_overflow(chunk_task);
   146   if (!result) {
   147     result = retrieve_from_stealable_queue(chunk_task);
   148   }
   149   if (TraceChunkTasksQueuing && Verbose && result) {
   150     gclog_or_tty->print_cr("  CTQ: retrieve " PTR_FORMAT, result);
   151   }
   152   return result;
   153 }
   155 bool ChunkTaskQueueWithOverflow::retrieve_from_stealable_queue(
   156                                    ChunkTask& chunk_task) {
   157   bool result = _chunk_queue.pop_local(chunk_task);
   158   if (TraceChunkTasksQueuing && Verbose) {
   159     gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
   160   }
   161   return result;
   162 }
   164 bool ChunkTaskQueueWithOverflow::retrieve_from_overflow(
   165                                         ChunkTask& chunk_task) {
   166   bool result;
   167   if (!_overflow_stack->is_empty()) {
   168     chunk_task = _overflow_stack->pop();
   169     result = true;
   170   } else {
   171     chunk_task = (ChunkTask) NULL;
   172     result = false;
   173   }
   174   if (TraceChunkTasksQueuing && Verbose) {
   175     gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
   176   }
   177   return result;
   178 }

mercurial