src/share/vm/utilities/workgroup.hpp

Tue, 05 Apr 2011 14:12:31 -0700

author
trims
date
Tue, 05 Apr 2011 14:12:31 -0700
changeset 2708
1d1603768966
parent 2651
92da084fefc9
child 3156
f08d439fab8c
permissions
-rw-r--r--

7010070: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - second pass
Summary: Update the copyright to be 2010 on all changed files in OpenJDK
Reviewed-by: ohair

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

mercurial