src/share/vm/utilities/workgroup.hpp

Sun, 25 Sep 2011 16:03:29 -0700

author
never
date
Sun, 25 Sep 2011 16:03:29 -0700
changeset 3156
f08d439fab8c
parent 2651
92da084fefc9
child 3294
bca17e38de00
permissions
-rw-r--r--

7089790: integrate bsd-port changes
Reviewed-by: kvn, twisti, jrose
Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>

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

mercurial