hotspot/src/share/vm/utilities/workgroup.hpp
changeset 1 489c9b5090e2
child 1374 4c24294029a9
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2002-2007 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  */
       
    24 
       
    25 // Forward declarations of classes defined here
       
    26 
       
    27 class WorkGang;
       
    28 class GangWorker;
       
    29 class YieldingFlexibleGangWorker;
       
    30 class YieldingFlexibleGangTask;
       
    31 class WorkData;
       
    32 
       
    33 // An abstract task to be worked on by a gang.
       
    34 // You subclass this to supply your own work() method
       
    35 class AbstractGangTask: public CHeapObj {
       
    36 public:
       
    37   // The abstract work method.
       
    38   // The argument tells you which member of the gang you are.
       
    39   virtual void work(int i) = 0;
       
    40 
       
    41   // Debugging accessor for the name.
       
    42   const char* name() const PRODUCT_RETURN_(return NULL;);
       
    43   int counter() { return _counter; }
       
    44   void set_counter(int value) { _counter = value; }
       
    45   int *address_of_counter() { return &_counter; }
       
    46 
       
    47   // RTTI
       
    48   NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
       
    49     return false;
       
    50   })
       
    51 
       
    52 private:
       
    53   NOT_PRODUCT(const char* _name;)
       
    54   // ??? Should a task have a priority associated with it?
       
    55   // ??? Or can the run method adjust priority as needed?
       
    56   int _counter;
       
    57 
       
    58 protected:
       
    59   // Constructor and desctructor: only construct subclasses.
       
    60   AbstractGangTask(const char* name) {
       
    61     NOT_PRODUCT(_name = name);
       
    62     _counter = 0;
       
    63   }
       
    64   virtual ~AbstractGangTask() { }
       
    65 };
       
    66 
       
    67 
       
    68 // Class AbstractWorkGang:
       
    69 // An abstract class representing a gang of workers.
       
    70 // You subclass this to supply an implementation of run_task().
       
    71 class AbstractWorkGang: public CHeapObj {
       
    72   // Here's the public interface to this class.
       
    73 public:
       
    74   // Constructor and destructor.
       
    75   AbstractWorkGang(const char* name, bool are_GC_threads);
       
    76   ~AbstractWorkGang();
       
    77   // Run a task, returns when the task is done (or terminated).
       
    78   virtual void run_task(AbstractGangTask* task) = 0;
       
    79   // Stop and terminate all workers.
       
    80   virtual void stop();
       
    81 public:
       
    82   // Debugging.
       
    83   const char* name() const;
       
    84 protected:
       
    85   // Initialize only instance data.
       
    86   const bool _are_GC_threads;
       
    87   // Printing support.
       
    88   const char* _name;
       
    89   // The monitor which protects these data,
       
    90   // and notifies of changes in it.
       
    91   Monitor*  _monitor;
       
    92   // The count of the number of workers in the gang.
       
    93   int _total_workers;
       
    94   // Whether the workers should terminate.
       
    95   bool _terminate;
       
    96   // The array of worker threads for this gang.
       
    97   // This is only needed for cleaning up.
       
    98   GangWorker** _gang_workers;
       
    99   // The task for this gang.
       
   100   AbstractGangTask* _task;
       
   101   // A sequence number for the current task.
       
   102   int _sequence_number;
       
   103   // The number of started workers.
       
   104   int _started_workers;
       
   105   // The number of finished workers.
       
   106   int _finished_workers;
       
   107 public:
       
   108   // Accessors for fields
       
   109   Monitor* monitor() const {
       
   110     return _monitor;
       
   111   }
       
   112   int total_workers() const {
       
   113     return _total_workers;
       
   114   }
       
   115   bool terminate() const {
       
   116     return _terminate;
       
   117   }
       
   118   GangWorker** gang_workers() const {
       
   119     return _gang_workers;
       
   120   }
       
   121   AbstractGangTask* task() const {
       
   122     return _task;
       
   123   }
       
   124   int sequence_number() const {
       
   125     return _sequence_number;
       
   126   }
       
   127   int started_workers() const {
       
   128     return _started_workers;
       
   129   }
       
   130   int finished_workers() const {
       
   131     return _finished_workers;
       
   132   }
       
   133   bool are_GC_threads() const {
       
   134     return _are_GC_threads;
       
   135   }
       
   136   // Predicates.
       
   137   bool is_idle() const {
       
   138     return (task() == NULL);
       
   139   }
       
   140   // Return the Ith gang worker.
       
   141   GangWorker* gang_worker(int i) const;
       
   142 
       
   143   void threads_do(ThreadClosure* tc) const;
       
   144 
       
   145   // Printing
       
   146   void print_worker_threads_on(outputStream *st) const;
       
   147   void print_worker_threads() const {
       
   148     print_worker_threads_on(tty);
       
   149   }
       
   150 
       
   151 protected:
       
   152   friend class GangWorker;
       
   153   friend class YieldingFlexibleGangWorker;
       
   154   // Note activation and deactivation of workers.
       
   155   // These methods should only be called with the mutex held.
       
   156   void internal_worker_poll(WorkData* data) const;
       
   157   void internal_note_start();
       
   158   void internal_note_finish();
       
   159 };
       
   160 
       
   161 class WorkData: public StackObj {
       
   162   // This would be a struct, but I want accessor methods.
       
   163 private:
       
   164   bool              _terminate;
       
   165   AbstractGangTask* _task;
       
   166   int               _sequence_number;
       
   167 public:
       
   168   // Constructor and destructor
       
   169   WorkData() {
       
   170     _terminate       = false;
       
   171     _task            = NULL;
       
   172     _sequence_number = 0;
       
   173   }
       
   174   ~WorkData() {
       
   175   }
       
   176   // Accessors and modifiers
       
   177   bool terminate()                       const { return _terminate;  }
       
   178   void set_terminate(bool value)               { _terminate = value; }
       
   179   AbstractGangTask* task()               const { return _task; }
       
   180   void set_task(AbstractGangTask* value)       { _task = value; }
       
   181   int sequence_number()                  const { return _sequence_number; }
       
   182   void set_sequence_number(int value)          { _sequence_number = value; }
       
   183 
       
   184   YieldingFlexibleGangTask* yf_task()    const {
       
   185     return (YieldingFlexibleGangTask*)_task;
       
   186   }
       
   187 };
       
   188 
       
   189 // Class WorkGang:
       
   190 class WorkGang: public AbstractWorkGang {
       
   191 public:
       
   192   // Constructor
       
   193   WorkGang(const char* name, int workers, bool are_GC_threads);
       
   194   // Run a task, returns when the task is done (or terminated).
       
   195   virtual void run_task(AbstractGangTask* task);
       
   196 };
       
   197 
       
   198 // Class GangWorker:
       
   199 //   Several instances of this class run in parallel as workers for a gang.
       
   200 class GangWorker: public WorkerThread {
       
   201 public:
       
   202   // Constructors and destructor.
       
   203   GangWorker(AbstractWorkGang* gang, uint id);
       
   204 
       
   205   // The only real method: run a task for the gang.
       
   206   virtual void run();
       
   207   // Predicate for Thread
       
   208   virtual bool is_GC_task_thread() const;
       
   209   // Printing
       
   210   void print_on(outputStream* st) const;
       
   211   virtual void print() const { print_on(tty); }
       
   212 protected:
       
   213   AbstractWorkGang* _gang;
       
   214 
       
   215   virtual void initialize();
       
   216   virtual void loop();
       
   217 
       
   218 public:
       
   219   AbstractWorkGang* gang() const { return _gang; }
       
   220 };
       
   221 
       
   222 // A class that acts as a synchronisation barrier. Workers enter
       
   223 // the barrier and must wait until all other workers have entered
       
   224 // before any of them may leave.
       
   225 
       
   226 class WorkGangBarrierSync : public StackObj {
       
   227 protected:
       
   228   Monitor _monitor;
       
   229   int     _n_workers;
       
   230   int     _n_completed;
       
   231 
       
   232   Monitor* monitor()       { return &_monitor; }
       
   233   int      n_workers()     { return _n_workers; }
       
   234   int      n_completed()   { return _n_completed; }
       
   235 
       
   236   void     inc_completed() { _n_completed++; }
       
   237 
       
   238 public:
       
   239   WorkGangBarrierSync();
       
   240   WorkGangBarrierSync(int n_workers, const char* name);
       
   241 
       
   242   // Set the number of workers that will use the barrier.
       
   243   // Must be called before any of the workers start running.
       
   244   void set_n_workers(int n_workers);
       
   245 
       
   246   // Enter the barrier. A worker that enters the barrier will
       
   247   // not be allowed to leave until all other threads have
       
   248   // also entered the barrier.
       
   249   void enter();
       
   250 };
       
   251 
       
   252 // A class to manage claiming of subtasks within a group of tasks.  The
       
   253 // subtasks will be identified by integer indices, usually elements of an
       
   254 // enumeration type.
       
   255 
       
   256 class SubTasksDone: public CHeapObj {
       
   257   jint* _tasks;
       
   258   int _n_tasks;
       
   259   int _n_threads;
       
   260   jint _threads_completed;
       
   261 #ifdef ASSERT
       
   262   jint _claimed;
       
   263 #endif
       
   264 
       
   265   // Set all tasks to unclaimed.
       
   266   void clear();
       
   267 
       
   268 public:
       
   269   // Initializes "this" to a state in which there are "n" tasks to be
       
   270   // processed, none of the which are originally claimed.  The number of
       
   271   // threads doing the tasks is initialized 1.
       
   272   SubTasksDone(int n);
       
   273 
       
   274   // True iff the object is in a valid state.
       
   275   bool valid();
       
   276 
       
   277   // Set the number of parallel threads doing the tasks to "t".  Can only
       
   278   // be called before tasks start or after they are complete.
       
   279   void set_par_threads(int t);
       
   280 
       
   281   // Returns "false" if the task "t" is unclaimed, and ensures that task is
       
   282   // claimed.  The task "t" is required to be within the range of "this".
       
   283   bool is_task_claimed(int t);
       
   284 
       
   285   // The calling thread asserts that it has attempted to claim all the
       
   286   // tasks that it will try to claim.  Every thread in the parallel task
       
   287   // must execute this.  (When the last thread does so, the task array is
       
   288   // cleared.)
       
   289   void all_tasks_completed();
       
   290 
       
   291   // Destructor.
       
   292   ~SubTasksDone();
       
   293 };
       
   294 
       
   295 // As above, but for sequential tasks, i.e. instead of claiming
       
   296 // sub-tasks from a set (possibly an enumeration), claim sub-tasks
       
   297 // in sequential order. This is ideal for claiming dynamically
       
   298 // partitioned tasks (like striding in the parallel remembered
       
   299 // set scanning). Note that unlike the above class this is
       
   300 // a stack object - is there any reason for it not to be?
       
   301 
       
   302 class SequentialSubTasksDone : public StackObj {
       
   303 protected:
       
   304   jint _n_tasks;     // Total number of tasks available.
       
   305   jint _n_claimed;   // Number of tasks claimed.
       
   306   jint _n_threads;   // Total number of parallel threads.
       
   307   jint _n_completed; // Number of completed threads.
       
   308 
       
   309   void clear();
       
   310 
       
   311 public:
       
   312   SequentialSubTasksDone() { clear(); }
       
   313   ~SequentialSubTasksDone() {}
       
   314 
       
   315   // True iff the object is in a valid state.
       
   316   bool valid();
       
   317 
       
   318   // number of tasks
       
   319   jint n_tasks() const { return _n_tasks; }
       
   320 
       
   321   // Set the number of parallel threads doing the tasks to t.
       
   322   // Should be called before the task starts but it is safe
       
   323   // to call this once a task is running provided that all
       
   324   // threads agree on the number of threads.
       
   325   void set_par_threads(int t) { _n_threads = t; }
       
   326 
       
   327   // Set the number of tasks to be claimed to t. As above,
       
   328   // should be called before the tasks start but it is safe
       
   329   // to call this once a task is running provided all threads
       
   330   // agree on the number of tasks.
       
   331   void set_n_tasks(int t) { _n_tasks = t; }
       
   332 
       
   333   // Returns false if the next task in the sequence is unclaimed,
       
   334   // and ensures that it is claimed. Will set t to be the index
       
   335   // of the claimed task in the sequence. Will return true if
       
   336   // the task cannot be claimed and there are none left to claim.
       
   337   bool is_task_claimed(int& t);
       
   338 
       
   339   // The calling thread asserts that it has attempted to claim
       
   340   // all the tasks it possibly can in the sequence. Every thread
       
   341   // claiming tasks must promise call this. Returns true if this
       
   342   // is the last thread to complete so that the thread can perform
       
   343   // cleanup if necessary.
       
   344   bool all_tasks_completed();
       
   345 };