1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP
  26 #define SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP
  27 
  28 #ifndef SERIALGC
  29 #include "utilities/workgroup.hpp"
  30 #endif
  31 
  32 
  33 // Forward declarations
  34 class YieldingFlexibleWorkGang;
  35 
  36 // Status of tasks
  37 enum Status {
  38     INACTIVE,
  39     ACTIVE,
  40     YIELDING,
  41     YIELDED,
  42     ABORTING,
  43     ABORTED,
  44     COMPLETING,
  45     COMPLETED
  46 };
  47 
  48 // Class YieldingFlexibleGangWorker:
  49 //   Several instances of this class run in parallel as workers for a gang.
  50 class YieldingFlexibleGangWorker: public GangWorker {
  51 public:
  52   // Ctor
  53   YieldingFlexibleGangWorker(AbstractWorkGang* gang, int id) :
  54     GangWorker(gang, id) { }
  55 
  56 public:
  57   YieldingFlexibleWorkGang* yf_gang() const
  58     { return (YieldingFlexibleWorkGang*)gang(); }
  59 
  60 protected: // Override from parent class
  61   virtual void loop();
  62 };
  63 
  64 // An abstract task to be worked on by a flexible work gang,
  65 // and where the workers will periodically yield, usually
  66 // in response to some condition that is signalled by means
  67 // that are specific to the task at hand.
  68 // You subclass this to supply your own work() method.
  69 // A second feature of this kind of work gang is that
  70 // it allows for the signalling of certain exceptional
  71 // conditions that may be encountered during the performance
  72 // of the task and that may require the task at hand to be
  73 // `aborted' forthwith. Finally, these gangs are `flexible'
  74 // in that they can operate at partial capacity with some
  75 // gang workers waiting on the bench; in other words, the
  76 // size of the active worker pool can flex (up to an apriori
  77 // maximum) in response to task requests at certain points.
  78 // The last part (the flexible part) has not yet been fully
  79 // fleshed out and is a work in progress.
  80 class YieldingFlexibleGangTask: public AbstractGangTask {
  81   Status _status;
  82   YieldingFlexibleWorkGang* _gang;
  83   int _actual_size;                      // size of gang obtained
  84 
  85 protected:
  86   int _requested_size;                   // size of gang requested
  87 
  88   // Constructor and desctructor: only construct subclasses.
  89   YieldingFlexibleGangTask(const char* name): AbstractGangTask(name),
  90     _status(INACTIVE),
  91     _gang(NULL),
  92     _requested_size(0) { }
  93 
  94   virtual ~YieldingFlexibleGangTask() { }
  95 
  96   friend class YieldingFlexibleWorkGang;
  97   friend class YieldingFlexibleGangWorker;
  98   NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
  99     return true;
 100   })
 101 
 102   void set_status(Status s) {
 103     _status = s;
 104   }
 105   YieldingFlexibleWorkGang* gang() {
 106     return _gang;
 107   }
 108   void set_gang(YieldingFlexibleWorkGang* gang) {
 109     assert(_gang == NULL || gang == NULL, "Clobber without intermediate reset?");
 110     _gang = gang;
 111   }
 112 
 113 public:
 114   // The abstract work method.
 115   // The argument tells you which member of the gang you are.
 116   virtual void work(int i) = 0;
 117 
 118   // Subclasses should call the parent's yield() method
 119   // after having done any work specific to the subclass.
 120   virtual void yield();
 121 
 122   // An abstract method supplied by
 123   // a concrete sub-class which is used by the coordinator
 124   // to do any "central yielding" work.
 125   virtual void coordinator_yield() = 0;
 126 
 127   // Subclasses should call the parent's abort() method
 128   // after having done any work specific to the sunbclass.
 129   virtual void abort();
 130 
 131   Status status()  const { return _status; }
 132   bool yielded()   const { return _status == YIELDED; }
 133   bool completed() const { return _status == COMPLETED; }
 134   bool aborted()   const { return _status == ABORTED; }
 135   bool active()    const { return _status == ACTIVE; }
 136 
 137   int requested_size() const { return _requested_size; }
 138   int actual_size()    const { return _actual_size; }
 139 
 140   void set_requested_size(int sz) { _requested_size = sz; }
 141   void set_actual_size(int sz)    { _actual_size    = sz; }
 142 };
 143 
 144 // Class YieldingWorkGang: A subclass of WorkGang.
 145 // In particular, a YieldingWorkGang is made up of
 146 // YieldingGangWorkers, and provides infrastructure
 147 // supporting yielding to the "GangOverseer",
 148 // being the thread that orchestrates the WorkGang via run_task().
 149 class YieldingFlexibleWorkGang: public AbstractWorkGang {
 150   // Here's the public interface to this class.
 151 public:
 152   // Constructor and destructor.
 153   YieldingFlexibleWorkGang(const char* name, int workers,
 154                            bool are_GC_task_threads);
 155 
 156   YieldingFlexibleGangTask* yielding_task() const {
 157     assert(task() == NULL || task()->is_YieldingFlexibleGang_task(),
 158            "Incorrect cast");
 159     return (YieldingFlexibleGangTask*)task();
 160   }
 161   // Run a task; returns when the task is done, or the workers yield,
 162   // or the task is aborted, or the work gang is terminated via stop().
 163   // A task that has been yielded can be continued via this same interface
 164   // by using the same task repeatedly as the argument to the call.
 165   // It is expected that the YieldingFlexibleGangTask carries the appropriate
 166   // continuation information used by workers to continue the task
 167   // from its last yield point. Thus, a completed task will return
 168   // immediately with no actual work having been done by the workers.
 169   void run_task(AbstractGangTask* task) {
 170     guarantee(false, "Use start_task instead");
 171   }
 172   void start_task(YieldingFlexibleGangTask* new_task);
 173   void continue_task(YieldingFlexibleGangTask* gang_task);
 174 
 175   // Abort a currently running task, if any; returns when all the workers
 176   // have stopped working on the current task and have returned to their
 177   // waiting stations.
 178   void abort_task();
 179 
 180   // Yield: workers wait at their current working stations
 181   // until signalled to proceed by the overseer.
 182   void yield();
 183 
 184   // Abort: workers are expected to return to their waiting
 185   // stations, whence they are ready for the next task dispatched
 186   // by the overseer.
 187   void abort();
 188 
 189 private:
 190   // The currently active workers in this gang.
 191   // This is a number that is dynamically adjusted by
 192   // the run_task() method at each subsequent invocation,
 193   // using data in the YieldingFlexibleGangTask.
 194   int _active_workers;
 195   int _yielded_workers;
 196   void wait_for_gang();
 197 
 198 public:
 199   // Accessors for fields
 200   int active_workers() const {
 201     return _active_workers;
 202   }
 203 
 204   int yielded_workers() const {
 205     return _yielded_workers;
 206   }
 207 
 208 private:
 209   friend class YieldingFlexibleGangWorker;
 210   void reset(); // NYI
 211 };
 212 
 213 #endif // SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP