1 /*
   2  * Copyright (c) 2002, 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 // Forward declarations of classes defined here
  26 
  27 class WorkGang;
  28 class GangWorker;
  29 class YieldingFlexibleGangWorker;
  30 class YieldingFlexibleGangTask;
  31 class WorkData;
  32 class AbstractWorkGang;
  33 
  34 // An abstract task to be worked on by a gang.
  35 // You subclass this to supply your own work() method
  36 class AbstractGangTask VALUE_OBJ_CLASS_SPEC {
  37 public:
  38   // The abstract work method.
  39   // The argument tells you which member of the gang you are.
  40   virtual void work(int i) = 0;
  41 
  42   // This method configures the task for proper termination.
  43   // Some tasks do not have any requirements on termination
  44   // and may inherit this method that does nothing.  Some
  45   // tasks do some coordination on termination and override
  46   // this method to implement that coordination.
  47   virtual void set_for_termination(int active_workers) {};
  48 
  49   // Debugging accessor for the name.
  50   const char* name() const PRODUCT_RETURN_(return NULL;);
  51   int counter() { return _counter; }
  52   void set_counter(int value) { _counter = value; }
  53   int *address_of_counter() { return &_counter; }
  54 
  55   // RTTI
  56   NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
  57     return false;
  58   })
  59 
  60 private:
  61   NOT_PRODUCT(const char* _name;)
  62   // ??? Should a task have a priority associated with it?
  63   // ??? Or can the run method adjust priority as needed?
  64   int _counter;
  65 
  66 protected:
  67   // Constructor and desctructor: only construct subclasses.
  68   AbstractGangTask(const char* name) {
  69     NOT_PRODUCT(_name = name);
  70     _counter = 0;
  71   }
  72   virtual ~AbstractGangTask() { }
  73 };
  74 
  75 class AbstractGangTaskWOopQueues : public AbstractGangTask {
  76   OopTaskQueueSet*       _queues;
  77   ParallelTaskTerminator _terminator;
  78  public:
  79   AbstractGangTaskWOopQueues(const char* name, OopTaskQueueSet* queues) :
  80     AbstractGangTask(name), _queues(queues), _terminator(0, _queues) {}
  81   ParallelTaskTerminator* terminator() { return &_terminator; }
  82   virtual void set_for_termination(int active_workers) {
  83     terminator()->reset_for_reuse(active_workers);
  84   }
  85   OopTaskQueueSet* queues() { return _queues; }
  86 };
  87 
  88 // Class AbstractWorkGang:
  89 // An abstract class representing a gang of workers.
  90 // You subclass this to supply an implementation of run_task().
  91 class AbstractWorkGang: public CHeapObj {
  92   // Here's the public interface to this class.
  93 public:
  94   // Constructor and destructor.
  95   AbstractWorkGang(const char* name, bool are_GC_task_threads,
  96                    bool are_ConcurrentGC_threads);
  97   ~AbstractWorkGang();
  98   // Run a task, returns when the task is done (or terminated).
  99   virtual void run_task(AbstractGangTask* task) = 0;
 100   // Stop and terminate all workers.
 101   virtual void stop();
 102 public:
 103   // Debugging.
 104   const char* name() const;
 105 protected:
 106   // Initialize only instance data.
 107   const bool _are_GC_task_threads;
 108   const bool _are_ConcurrentGC_threads;
 109   // Printing support.
 110   const char* _name;
 111   // The monitor which protects these data,
 112   // and notifies of changes in it.
 113   Monitor*  _monitor;
 114   // The count of the number of workers in the gang.
 115   int _total_workers;
 116   // Whether the workers should terminate.
 117   bool _terminate;
 118   // The array of worker threads for this gang.
 119   // This is only needed for cleaning up.
 120   GangWorker** _gang_workers;
 121   // The task for this gang.
 122   AbstractGangTask* _task;
 123   // A sequence number for the current task.
 124   int _sequence_number;
 125   // The number of started workers.
 126   int _started_workers;
 127   // The number of finished workers.
 128   int _finished_workers;
 129 public:
 130   // Accessors for fields
 131   Monitor* monitor() const {
 132     return _monitor;
 133   }
 134   int total_workers() const {
 135     return _total_workers;
 136   }
 137   virtual int active_workers() const {
 138     return _total_workers;
 139   }
 140   bool terminate() const {
 141     return _terminate;
 142   }
 143   GangWorker** gang_workers() const {
 144     return _gang_workers;
 145   }
 146   AbstractGangTask* task() const {
 147     return _task;
 148   }
 149   int sequence_number() const {
 150     return _sequence_number;
 151   }
 152   int started_workers() const {
 153     return _started_workers;
 154   }
 155   int finished_workers() const {
 156     return _finished_workers;
 157   }
 158   bool are_GC_task_threads() const {
 159     return _are_GC_task_threads;
 160   }
 161   bool are_ConcurrentGC_threads() const {
 162     return _are_ConcurrentGC_threads;
 163   }
 164   // Predicates.
 165   bool is_idle() const {
 166     return (task() == NULL);
 167   }
 168   // Return the Ith gang worker.
 169   GangWorker* gang_worker(int i) const;
 170 
 171   void threads_do(ThreadClosure* tc) const;
 172 
 173   // Printing
 174   void print_worker_threads_on(outputStream *st) const;
 175   void print_worker_threads() const {
 176     print_worker_threads_on(tty);
 177   }
 178 
 179 protected:
 180   friend class GangWorker;
 181   friend class YieldingFlexibleGangWorker;
 182   // Note activation and deactivation of workers.
 183   // These methods should only be called with the mutex held.
 184   void internal_worker_poll(WorkData* data) const;
 185   void internal_note_start();
 186   void internal_note_finish();
 187 };
 188 
 189 class WorkData: public StackObj {
 190   // This would be a struct, but I want accessor methods.
 191 private:
 192   bool              _terminate;
 193   AbstractGangTask* _task;
 194   int               _sequence_number;
 195 public:
 196   // Constructor and destructor
 197   WorkData() {
 198     _terminate       = false;
 199     _task            = NULL;
 200     _sequence_number = 0;
 201   }
 202   ~WorkData() {
 203   }
 204   // Accessors and modifiers
 205   bool terminate()                       const { return _terminate;  }
 206   void set_terminate(bool value)               { _terminate = value; }
 207   AbstractGangTask* task()               const { return _task; }
 208   void set_task(AbstractGangTask* value)       { _task = value; }
 209   int sequence_number()                  const { return _sequence_number; }
 210   void set_sequence_number(int value)          { _sequence_number = value; }
 211 
 212   YieldingFlexibleGangTask* yf_task()    const {
 213     return (YieldingFlexibleGangTask*)_task;
 214   }
 215 };
 216 
 217 // Class WorkGang:
 218 class WorkGang: public AbstractWorkGang {
 219 public:
 220   // Constructor
 221   WorkGang(const char* name, int workers,
 222            bool are_GC_task_threads, bool are_ConcurrentGC_threads);
 223   // Run a task, returns when the task is done (or terminated).
 224   virtual void run_task(AbstractGangTask* task);
 225   void run_task(AbstractGangTask* task, uint no_of_parallel_workers);
 226   // Allocate a worker and return a pointer to it.
 227   virtual GangWorker* allocate_worker(int which);
 228   // Initialize workers in the gang.  Return true if initialization
 229   // succeeded. The type of the worker can be overridden in a derived
 230   // class with the appropriate implementation of allocate_worker().
 231   bool initialize_workers();
 232 };
 233 
 234 // Class GangWorker:
 235 //   Several instances of this class run in parallel as workers for a gang.
 236 class GangWorker: public WorkerThread {
 237 public:
 238   // Constructors and destructor.
 239   GangWorker(AbstractWorkGang* gang, uint id);
 240 
 241   // The only real method: run a task for the gang.
 242   virtual void run();
 243   // Predicate for Thread
 244   virtual bool is_GC_task_thread() const;
 245   virtual bool is_ConcurrentGC_thread() const;
 246   // Printing
 247   void print_on(outputStream* st) const;
 248   virtual void print() const { print_on(tty); }
 249 protected:
 250   AbstractWorkGang* _gang;
 251 
 252   virtual void initialize();
 253   virtual void loop();
 254 
 255 public:
 256   AbstractWorkGang* gang() const { return _gang; }
 257 };
 258 
 259 class FlexibleWorkGang: public WorkGang {
 260  protected:
 261   int _active_workers;
 262  public:
 263   // Constructor and destructor.
 264   FlexibleWorkGang(const char* name, int workers,
 265                    bool are_GC_task_threads,
 266                    bool  are_ConcurrentGC_threads) :
 267     WorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads) {
 268     _active_workers = ParallelGCThreads;
 269   };
 270   // Accessors for fields
 271   virtual int active_workers() const { return _active_workers; }
 272   void set_active_workers(int v) { _active_workers = v; }
 273 };
 274 
 275 // Work gangs in garbage collectors: 2009-06-10
 276 //
 277 // SharedHeap - work gang for stop-the-world parallel collection.
 278 //   Used by
 279 //     ParNewGeneration
 280 //     CMSParRemarkTask
 281 //     CMSRefProcTaskExecutor
 282 //     G1CollectedHeap
 283 //     G1ParFinalCountTask
 284 // ConcurrentMark
 285 // CMSCollector
 286 
 287 // A class that acts as a synchronisation barrier. Workers enter
 288 // the barrier and must wait until all other workers have entered
 289 // before any of them may leave.
 290 
 291 class WorkGangBarrierSync : public StackObj {
 292 protected:
 293   Monitor _monitor;
 294   int     _n_workers;
 295   int     _n_completed;
 296   bool    _should_reset;
 297 
 298   Monitor* monitor()        { return &_monitor; }
 299   int      n_workers()      { return _n_workers; }
 300   int      n_completed()    { return _n_completed; }
 301   bool     should_reset()   { return _should_reset; }
 302 
 303   void     zero_completed() { _n_completed = 0; }
 304   void     inc_completed()  { _n_completed++; }
 305 
 306   void     set_should_reset(bool v) { _should_reset = v; }
 307 
 308 public:
 309   WorkGangBarrierSync();
 310   WorkGangBarrierSync(int n_workers, const char* name);
 311 
 312   // Set the number of workers that will use the barrier.
 313   // Must be called before any of the workers start running.
 314   void set_n_workers(int n_workers);
 315 
 316   // Enter the barrier. A worker that enters the barrier will
 317   // not be allowed to leave until all other threads have
 318   // also entered the barrier.
 319   void enter();
 320 };
 321 
 322 // A class to manage claiming of subtasks within a group of tasks.  The
 323 // subtasks will be identified by integer indices, usually elements of an
 324 // enumeration type.
 325 
 326 class SubTasksDone: public CHeapObj {
 327   jint* _tasks;
 328   int _n_tasks;
 329   int _n_threads;
 330   jint _threads_completed;
 331 #ifdef ASSERT
 332   volatile jint _claimed;
 333 #endif
 334 
 335   // Set all tasks to unclaimed.
 336   void clear();
 337 
 338 public:
 339   // Initializes "this" to a state in which there are "n" tasks to be
 340   // processed, none of the which are originally claimed.  The number of
 341   // threads doing the tasks is initialized 1.
 342   SubTasksDone(int n);
 343 
 344   // True iff the object is in a valid state.
 345   bool valid();
 346 
 347   // Get/set the number of parallel threads doing the tasks to "t".  Can only
 348   // be called before tasks start or after they are complete.
 349   int n_threads() { return _n_threads; }
 350   void set_n_threads(int t);
 351 
 352   // Returns "false" if the task "t" is unclaimed, and ensures that task is
 353   // claimed.  The task "t" is required to be within the range of "this".
 354   bool is_task_claimed(int t);
 355 
 356   // The calling thread asserts that it has attempted to claim all the
 357   // tasks that it will try to claim.  Every thread in the parallel task
 358   // must execute this.  (When the last thread does so, the task array is
 359   // cleared.)
 360   void all_tasks_completed();
 361 
 362   // Destructor.
 363   ~SubTasksDone();
 364 };
 365 
 366 // As above, but for sequential tasks, i.e. instead of claiming
 367 // sub-tasks from a set (possibly an enumeration), claim sub-tasks
 368 // in sequential order. This is ideal for claiming dynamically
 369 // partitioned tasks (like striding in the parallel remembered
 370 // set scanning). Note that unlike the above class this is
 371 // a stack object - is there any reason for it not to be?
 372 
 373 class SequentialSubTasksDone : public StackObj {
 374 protected:
 375   jint _n_tasks;     // Total number of tasks available.
 376   jint _n_claimed;   // Number of tasks claimed.
 377   // _n_threads is used to determine when a sub task is done.
 378   // See comments on SubTasksDone::_n_threads
 379   jint _n_threads;   // Total number of parallel threads.
 380   jint _n_completed; // Number of completed threads.
 381 
 382   void clear();
 383 
 384 public:
 385   SequentialSubTasksDone() {
 386     clear();
 387   }
 388   ~SequentialSubTasksDone() {}
 389 
 390   // True iff the object is in a valid state.
 391   bool valid();
 392 
 393   // number of tasks
 394   jint n_tasks() const { return _n_tasks; }
 395 
 396   // Get/set the number of parallel threads doing the tasks to t.
 397   // Should be called before the task starts but it is safe
 398   // to call this once a task is running provided that all
 399   // threads agree on the number of threads.
 400   int n_threads() { return _n_threads; }
 401   void set_n_threads(int t) { _n_threads = t; }
 402 
 403   // Set the number of tasks to be claimed to t. As above,
 404   // should be called before the tasks start but it is safe
 405   // to call this once a task is running provided all threads
 406   // agree on the number of tasks.
 407   void set_n_tasks(int t) { _n_tasks = t; }
 408 
 409   // Returns false if the next task in the sequence is unclaimed,
 410   // and ensures that it is claimed. Will set t to be the index
 411   // of the claimed task in the sequence. Will return true if
 412   // the task cannot be claimed and there are none left to claim.
 413   bool is_task_claimed(int& t);
 414 
 415   // The calling thread asserts that it has attempted to claim
 416   // all the tasks it possibly can in the sequence. Every thread
 417   // claiming tasks must promise call this. Returns true if this
 418   // is the last thread to complete so that the thread can perform
 419   // cleanup if necessary.
 420   bool all_tasks_completed();
 421 };
 422 
 423 // Represents a set of free small integer ids.
 424 class FreeIdSet {
 425   enum {
 426     end_of_list = -1,
 427     claimed = -2
 428   };
 429 
 430   int _sz;
 431   Monitor* _mon;
 432 
 433   int* _ids;
 434   int _hd;
 435   int _waiters;
 436   int _claimed;
 437 
 438   static bool _safepoint;
 439   typedef FreeIdSet* FreeIdSetPtr;
 440   static const int NSets = 10;
 441   static FreeIdSetPtr _sets[NSets];
 442   static bool _stat_init;
 443   int _index;
 444 
 445 public:
 446   FreeIdSet(int sz, Monitor* mon);
 447   ~FreeIdSet();
 448 
 449   static void set_safepoint(bool b);
 450 
 451   // Attempt to claim the given id permanently.  Returns "true" iff
 452   // successful.
 453   bool claim_perm_id(int i);
 454 
 455   // Returns an unclaimed parallel id (waiting for one to be released if
 456   // necessary).  Returns "-1" if a GC wakes up a wait for an id.
 457   int claim_par_id();
 458 
 459   void release_par_id(int id);
 460 };