src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.hpp

changeset 0
f90c822e73f8
child 1
2d8a650513c2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,786 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP
    1.29 +#define SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP
    1.30 +
    1.31 +#include "runtime/mutex.hpp"
    1.32 +#include "utilities/growableArray.hpp"
    1.33 +
    1.34 +//
    1.35 +// The GCTaskManager is a queue of GCTasks, and accessors
    1.36 +// to allow the queue to be accessed from many threads.
    1.37 +//
    1.38 +
    1.39 +// Forward declarations of types defined in this file.
    1.40 +class GCTask;
    1.41 +class GCTaskQueue;
    1.42 +class SynchronizedGCTaskQueue;
    1.43 +class GCTaskManager;
    1.44 +class NotifyDoneClosure;
    1.45 +// Some useful subclasses of GCTask.  You can also make up your own.
    1.46 +class NoopGCTask;
    1.47 +class BarrierGCTask;
    1.48 +class ReleasingBarrierGCTask;
    1.49 +class NotifyingBarrierGCTask;
    1.50 +class WaitForBarrierGCTask;
    1.51 +class IdleGCTask;
    1.52 +// A free list of Monitor*'s.
    1.53 +class MonitorSupply;
    1.54 +
    1.55 +// Forward declarations of classes referenced in this file via pointer.
    1.56 +class GCTaskThread;
    1.57 +class Mutex;
    1.58 +class Monitor;
    1.59 +class ThreadClosure;
    1.60 +
    1.61 +// The abstract base GCTask.
    1.62 +class GCTask : public ResourceObj {
    1.63 +public:
    1.64 +  // Known kinds of GCTasks, for predicates.
    1.65 +  class Kind : AllStatic {
    1.66 +  public:
    1.67 +    enum kind {
    1.68 +      unknown_task,
    1.69 +      ordinary_task,
    1.70 +      barrier_task,
    1.71 +      noop_task,
    1.72 +      idle_task
    1.73 +    };
    1.74 +    static const char* to_string(kind value);
    1.75 +  };
    1.76 +private:
    1.77 +  // Instance state.
    1.78 +  const Kind::kind _kind;               // For runtime type checking.
    1.79 +  const uint       _affinity;           // Which worker should run task.
    1.80 +  GCTask*          _newer;              // Tasks are on doubly-linked ...
    1.81 +  GCTask*          _older;              // ... lists.
    1.82 +public:
    1.83 +  virtual char* name() { return (char *)"task"; }
    1.84 +
    1.85 +  // Abstract do_it method
    1.86 +  virtual void do_it(GCTaskManager* manager, uint which) = 0;
    1.87 +  // Accessors
    1.88 +  Kind::kind kind() const {
    1.89 +    return _kind;
    1.90 +  }
    1.91 +  uint affinity() const {
    1.92 +    return _affinity;
    1.93 +  }
    1.94 +  GCTask* newer() const {
    1.95 +    return _newer;
    1.96 +  }
    1.97 +  void set_newer(GCTask* n) {
    1.98 +    _newer = n;
    1.99 +  }
   1.100 +  GCTask* older() const {
   1.101 +    return _older;
   1.102 +  }
   1.103 +  void set_older(GCTask* p) {
   1.104 +    _older = p;
   1.105 +  }
   1.106 +  // Predicates.
   1.107 +  bool is_ordinary_task() const {
   1.108 +    return kind()==Kind::ordinary_task;
   1.109 +  }
   1.110 +  bool is_barrier_task() const {
   1.111 +    return kind()==Kind::barrier_task;
   1.112 +  }
   1.113 +  bool is_noop_task() const {
   1.114 +    return kind()==Kind::noop_task;
   1.115 +  }
   1.116 +  bool is_idle_task() const {
   1.117 +    return kind()==Kind::idle_task;
   1.118 +  }
   1.119 +  void print(const char* message) const PRODUCT_RETURN;
   1.120 +protected:
   1.121 +  // Constructors: Only create subclasses.
   1.122 +  //     An ordinary GCTask.
   1.123 +  GCTask();
   1.124 +  //     A GCTask of a particular kind, usually barrier or noop.
   1.125 +  GCTask(Kind::kind kind);
   1.126 +  //     An ordinary GCTask with an affinity.
   1.127 +  GCTask(uint affinity);
   1.128 +  //     A GCTask of a particular kind, with and affinity.
   1.129 +  GCTask(Kind::kind kind, uint affinity);
   1.130 +  // We want a virtual destructor because virtual methods,
   1.131 +  // but since ResourceObj's don't have their destructors
   1.132 +  // called, we don't have one at all.  Instead we have
   1.133 +  // this method, which gets called by subclasses to clean up.
   1.134 +  virtual void destruct();
   1.135 +  // Methods.
   1.136 +  void initialize();
   1.137 +};
   1.138 +
   1.139 +// A doubly-linked list of GCTasks.
   1.140 +// The list is not synchronized, because sometimes we want to
   1.141 +// build up a list and then make it available to other threads.
   1.142 +// See also: SynchronizedGCTaskQueue.
   1.143 +class GCTaskQueue : public ResourceObj {
   1.144 +private:
   1.145 +  // Instance state.
   1.146 +  GCTask*    _insert_end;               // Tasks are enqueued at this end.
   1.147 +  GCTask*    _remove_end;               // Tasks are dequeued from this end.
   1.148 +  uint       _length;                   // The current length of the queue.
   1.149 +  const bool _is_c_heap_obj;            // Is this a CHeapObj?
   1.150 +public:
   1.151 +  // Factory create and destroy methods.
   1.152 +  //     Create as ResourceObj.
   1.153 +  static GCTaskQueue* create();
   1.154 +  //     Create as CHeapObj.
   1.155 +  static GCTaskQueue* create_on_c_heap();
   1.156 +  //     Destroyer.
   1.157 +  static void destroy(GCTaskQueue* that);
   1.158 +  // Accessors.
   1.159 +  //     These just examine the state of the queue.
   1.160 +  bool is_empty() const {
   1.161 +    assert(((insert_end() == NULL && remove_end() == NULL) ||
   1.162 +            (insert_end() != NULL && remove_end() != NULL)),
   1.163 +           "insert_end and remove_end don't match");
   1.164 +    assert((insert_end() != NULL) || (_length == 0), "Not empty");
   1.165 +    return insert_end() == NULL;
   1.166 +  }
   1.167 +  uint length() const {
   1.168 +    return _length;
   1.169 +  }
   1.170 +  // Methods.
   1.171 +  //     Enqueue one task.
   1.172 +  void enqueue(GCTask* task);
   1.173 +  //     Enqueue a list of tasks.  Empties the argument list.
   1.174 +  void enqueue(GCTaskQueue* list);
   1.175 +  //     Dequeue one task.
   1.176 +  GCTask* dequeue();
   1.177 +  //     Dequeue one task, preferring one with affinity.
   1.178 +  GCTask* dequeue(uint affinity);
   1.179 +protected:
   1.180 +  // Constructor. Clients use factory, but there might be subclasses.
   1.181 +  GCTaskQueue(bool on_c_heap);
   1.182 +  // Destructor-like method.
   1.183 +  // Because ResourceMark doesn't call destructors.
   1.184 +  // This method cleans up like one.
   1.185 +  virtual void destruct();
   1.186 +  // Accessors.
   1.187 +  GCTask* insert_end() const {
   1.188 +    return _insert_end;
   1.189 +  }
   1.190 +  void set_insert_end(GCTask* value) {
   1.191 +    _insert_end = value;
   1.192 +  }
   1.193 +  GCTask* remove_end() const {
   1.194 +    return _remove_end;
   1.195 +  }
   1.196 +  void set_remove_end(GCTask* value) {
   1.197 +    _remove_end = value;
   1.198 +  }
   1.199 +  void increment_length() {
   1.200 +    _length += 1;
   1.201 +  }
   1.202 +  void decrement_length() {
   1.203 +    _length -= 1;
   1.204 +  }
   1.205 +  void set_length(uint value) {
   1.206 +    _length = value;
   1.207 +  }
   1.208 +  bool is_c_heap_obj() const {
   1.209 +    return _is_c_heap_obj;
   1.210 +  }
   1.211 +  // Methods.
   1.212 +  void initialize();
   1.213 +  GCTask* remove();                     // Remove from remove end.
   1.214 +  GCTask* remove(GCTask* task);         // Remove from the middle.
   1.215 +  void print(const char* message) const PRODUCT_RETURN;
   1.216 +  // Debug support
   1.217 +  void verify_length() const PRODUCT_RETURN;
   1.218 +};
   1.219 +
   1.220 +// A GCTaskQueue that can be synchronized.
   1.221 +// This "has-a" GCTaskQueue and a mutex to do the exclusion.
   1.222 +class SynchronizedGCTaskQueue : public CHeapObj<mtGC> {
   1.223 +private:
   1.224 +  // Instance state.
   1.225 +  GCTaskQueue* _unsynchronized_queue;   // Has-a unsynchronized queue.
   1.226 +  Monitor *    _lock;                   // Lock to control access.
   1.227 +public:
   1.228 +  // Factory create and destroy methods.
   1.229 +  static SynchronizedGCTaskQueue* create(GCTaskQueue* queue, Monitor * lock) {
   1.230 +    return new SynchronizedGCTaskQueue(queue, lock);
   1.231 +  }
   1.232 +  static void destroy(SynchronizedGCTaskQueue* that) {
   1.233 +    if (that != NULL) {
   1.234 +      delete that;
   1.235 +    }
   1.236 +  }
   1.237 +  // Accessors
   1.238 +  GCTaskQueue* unsynchronized_queue() const {
   1.239 +    return _unsynchronized_queue;
   1.240 +  }
   1.241 +  Monitor * lock() const {
   1.242 +    return _lock;
   1.243 +  }
   1.244 +  // GCTaskQueue wrapper methods.
   1.245 +  // These check that you hold the lock
   1.246 +  // and then call the method on the queue.
   1.247 +  bool is_empty() const {
   1.248 +    guarantee(own_lock(), "don't own the lock");
   1.249 +    return unsynchronized_queue()->is_empty();
   1.250 +  }
   1.251 +  void enqueue(GCTask* task) {
   1.252 +    guarantee(own_lock(), "don't own the lock");
   1.253 +    unsynchronized_queue()->enqueue(task);
   1.254 +  }
   1.255 +  void enqueue(GCTaskQueue* list) {
   1.256 +    guarantee(own_lock(), "don't own the lock");
   1.257 +    unsynchronized_queue()->enqueue(list);
   1.258 +  }
   1.259 +  GCTask* dequeue() {
   1.260 +    guarantee(own_lock(), "don't own the lock");
   1.261 +    return unsynchronized_queue()->dequeue();
   1.262 +  }
   1.263 +  GCTask* dequeue(uint affinity) {
   1.264 +    guarantee(own_lock(), "don't own the lock");
   1.265 +    return unsynchronized_queue()->dequeue(affinity);
   1.266 +  }
   1.267 +  uint length() const {
   1.268 +    guarantee(own_lock(), "don't own the lock");
   1.269 +    return unsynchronized_queue()->length();
   1.270 +  }
   1.271 +  // For guarantees.
   1.272 +  bool own_lock() const {
   1.273 +    return lock()->owned_by_self();
   1.274 +  }
   1.275 +protected:
   1.276 +  // Constructor.  Clients use factory, but there might be subclasses.
   1.277 +  SynchronizedGCTaskQueue(GCTaskQueue* queue, Monitor * lock);
   1.278 +  // Destructor.  Not virtual because no virtuals.
   1.279 +  ~SynchronizedGCTaskQueue();
   1.280 +};
   1.281 +
   1.282 +// This is an abstract base class for getting notifications
   1.283 +// when a GCTaskManager is done.
   1.284 +class NotifyDoneClosure : public CHeapObj<mtGC> {
   1.285 +public:
   1.286 +  // The notification callback method.
   1.287 +  virtual void notify(GCTaskManager* manager) = 0;
   1.288 +protected:
   1.289 +  // Constructor.
   1.290 +  NotifyDoneClosure() {
   1.291 +    // Nothing to do.
   1.292 +  }
   1.293 +  // Virtual destructor because virtual methods.
   1.294 +  virtual ~NotifyDoneClosure() {
   1.295 +    // Nothing to do.
   1.296 +  }
   1.297 +};
   1.298 +
   1.299 +// Dynamic number of GC threads
   1.300 +//
   1.301 +//  GC threads wait in get_task() for work (i.e., a task) to perform.
   1.302 +// When the number of GC threads was static, the number of tasks
   1.303 +// created to do a job was equal to or greater than the maximum
   1.304 +// number of GC threads (ParallelGCThreads).  The job might be divided
   1.305 +// into a number of tasks greater than the number of GC threads for
   1.306 +// load balancing (i.e., over partitioning).  The last task to be
   1.307 +// executed by a GC thread in a job is a work stealing task.  A
   1.308 +// GC  thread that gets a work stealing task continues to execute
   1.309 +// that task until the job is done.  In the static number of GC theads
   1.310 +// case, tasks are added to a queue (FIFO).  The work stealing tasks are
   1.311 +// the last to be added.  Once the tasks are added, the GC threads grab
   1.312 +// a task and go.  A single thread can do all the non-work stealing tasks
   1.313 +// and then execute a work stealing and wait for all the other GC threads
   1.314 +// to execute their work stealing task.
   1.315 +//  In the dynamic number of GC threads implementation, idle-tasks are
   1.316 +// created to occupy the non-participating or "inactive" threads.  An
   1.317 +// idle-task makes the GC thread wait on a barrier that is part of the
   1.318 +// GCTaskManager.  The GC threads that have been "idled" in a IdleGCTask
   1.319 +// are released once all the active GC threads have finished their work
   1.320 +// stealing tasks.  The GCTaskManager does not wait for all the "idled"
   1.321 +// GC threads to resume execution. When those GC threads do resume
   1.322 +// execution in the course of the thread scheduling, they call get_tasks()
   1.323 +// as all the other GC threads do.  Because all the "idled" threads are
   1.324 +// not required to execute in order to finish a job, it is possible for
   1.325 +// a GC thread to still be "idled" when the next job is started.  Such
   1.326 +// a thread stays "idled" for the next job.  This can result in a new
   1.327 +// job not having all the expected active workers.  For example if on
   1.328 +// job requests 4 active workers out of a total of 10 workers so the
   1.329 +// remaining 6 are "idled", if the next job requests 6 active workers
   1.330 +// but all 6 of the "idled" workers are still idle, then the next job
   1.331 +// will only get 4 active workers.
   1.332 +//  The implementation for the parallel old compaction phase has an
   1.333 +// added complication.  In the static case parold partitions the chunks
   1.334 +// ready to be filled into stacks, one for each GC thread.  A GC thread
   1.335 +// executing a draining task (drains the stack of ready chunks)
   1.336 +// claims a stack according to it's id (the unique ordinal value assigned
   1.337 +// to each GC thread).  In the dynamic case not all GC threads will
   1.338 +// actively participate so stacks with ready to fill chunks can only be
   1.339 +// given to the active threads.  An initial implementation chose stacks
   1.340 +// number 1-n to get the ready chunks and required that GC threads
   1.341 +// 1-n be the active workers.  This was undesirable because it required
   1.342 +// certain threads to participate.  In the final implementation a
   1.343 +// list of stacks equal in number to the active workers are filled
   1.344 +// with ready chunks.  GC threads that participate get a stack from
   1.345 +// the task (DrainStacksCompactionTask), empty the stack, and then add it to a
   1.346 +// recycling list at the end of the task.  If the same GC thread gets
   1.347 +// a second task, it gets a second stack to drain and returns it.  The
   1.348 +// stacks are added to a recycling list so that later stealing tasks
   1.349 +// for this tasks can get a stack from the recycling list.  Stealing tasks
   1.350 +// use the stacks in its work in a way similar to the draining tasks.
   1.351 +// A thread is not guaranteed to get anything but a stealing task and
   1.352 +// a thread that only gets a stealing task has to get a stack. A failed
   1.353 +// implementation tried to have the GC threads keep the stack they used
   1.354 +// during a draining task for later use in the stealing task but that didn't
   1.355 +// work because as noted a thread is not guaranteed to get a draining task.
   1.356 +//
   1.357 +// For PSScavenge and ParCompactionManager the GC threads are
   1.358 +// held in the GCTaskThread** _thread array in GCTaskManager.
   1.359 +
   1.360 +
   1.361 +class GCTaskManager : public CHeapObj<mtGC> {
   1.362 + friend class ParCompactionManager;
   1.363 + friend class PSParallelCompact;
   1.364 + friend class PSScavenge;
   1.365 + friend class PSRefProcTaskExecutor;
   1.366 + friend class RefProcTaskExecutor;
   1.367 + friend class GCTaskThread;
   1.368 + friend class IdleGCTask;
   1.369 +private:
   1.370 +  // Instance state.
   1.371 +  NotifyDoneClosure*        _ndc;               // Notify on completion.
   1.372 +  const uint                _workers;           // Number of workers.
   1.373 +  Monitor*                  _monitor;           // Notification of changes.
   1.374 +  SynchronizedGCTaskQueue*  _queue;             // Queue of tasks.
   1.375 +  GCTaskThread**            _thread;            // Array of worker threads.
   1.376 +  uint                      _active_workers;    // Number of active workers.
   1.377 +  uint                      _busy_workers;      // Number of busy workers.
   1.378 +  uint                      _blocking_worker;   // The worker that's blocking.
   1.379 +  bool*                     _resource_flag;     // Array of flag per threads.
   1.380 +  uint                      _delivered_tasks;   // Count of delivered tasks.
   1.381 +  uint                      _completed_tasks;   // Count of completed tasks.
   1.382 +  uint                      _barriers;          // Count of barrier tasks.
   1.383 +  uint                      _emptied_queue;     // Times we emptied the queue.
   1.384 +  NoopGCTask*               _noop_task;         // The NoopGCTask instance.
   1.385 +  uint                      _noop_tasks;        // Count of noop tasks.
   1.386 +  WaitForBarrierGCTask*     _idle_inactive_task;// Task for inactive workers
   1.387 +  volatile uint             _idle_workers;      // Number of idled workers
   1.388 +public:
   1.389 +  // Factory create and destroy methods.
   1.390 +  static GCTaskManager* create(uint workers) {
   1.391 +    return new GCTaskManager(workers);
   1.392 +  }
   1.393 +  static GCTaskManager* create(uint workers, NotifyDoneClosure* ndc) {
   1.394 +    return new GCTaskManager(workers, ndc);
   1.395 +  }
   1.396 +  static void destroy(GCTaskManager* that) {
   1.397 +    if (that != NULL) {
   1.398 +      delete that;
   1.399 +    }
   1.400 +  }
   1.401 +  // Accessors.
   1.402 +  uint busy_workers() const {
   1.403 +    return _busy_workers;
   1.404 +  }
   1.405 +  volatile uint idle_workers() const {
   1.406 +    return _idle_workers;
   1.407 +  }
   1.408 +  //     Pun between Monitor* and Mutex*
   1.409 +  Monitor* monitor() const {
   1.410 +    return _monitor;
   1.411 +  }
   1.412 +  Monitor * lock() const {
   1.413 +    return _monitor;
   1.414 +  }
   1.415 +  WaitForBarrierGCTask* idle_inactive_task() {
   1.416 +    return _idle_inactive_task;
   1.417 +  }
   1.418 +  // Methods.
   1.419 +  //     Add the argument task to be run.
   1.420 +  void add_task(GCTask* task);
   1.421 +  //     Add a list of tasks.  Removes task from the argument list.
   1.422 +  void add_list(GCTaskQueue* list);
   1.423 +  //     Claim a task for argument worker.
   1.424 +  GCTask* get_task(uint which);
   1.425 +  //     Note the completion of a task by the argument worker.
   1.426 +  void note_completion(uint which);
   1.427 +  //     Is the queue blocked from handing out new tasks?
   1.428 +  bool is_blocked() const {
   1.429 +    return (blocking_worker() != sentinel_worker());
   1.430 +  }
   1.431 +  //     Request that all workers release their resources.
   1.432 +  void release_all_resources();
   1.433 +  //     Ask if a particular worker should release its resources.
   1.434 +  bool should_release_resources(uint which); // Predicate.
   1.435 +  //     Note the release of resources by the argument worker.
   1.436 +  void note_release(uint which);
   1.437 +  //     Create IdleGCTasks for inactive workers and start workers
   1.438 +  void task_idle_workers();
   1.439 +  //     Release the workers in IdleGCTasks
   1.440 +  void release_idle_workers();
   1.441 +  // Constants.
   1.442 +  //     A sentinel worker identifier.
   1.443 +  static uint sentinel_worker() {
   1.444 +    return (uint) -1;                   // Why isn't there a max_uint?
   1.445 +  }
   1.446 +
   1.447 +  //     Execute the task queue and wait for the completion.
   1.448 +  void execute_and_wait(GCTaskQueue* list);
   1.449 +
   1.450 +  void print_task_time_stamps();
   1.451 +  void print_threads_on(outputStream* st);
   1.452 +  void threads_do(ThreadClosure* tc);
   1.453 +
   1.454 +protected:
   1.455 +  // Constructors.  Clients use factory, but there might be subclasses.
   1.456 +  //     Create a GCTaskManager with the appropriate number of workers.
   1.457 +  GCTaskManager(uint workers);
   1.458 +  //     Create a GCTaskManager that calls back when there's no more work.
   1.459 +  GCTaskManager(uint workers, NotifyDoneClosure* ndc);
   1.460 +  //     Make virtual if necessary.
   1.461 +  ~GCTaskManager();
   1.462 +  // Accessors.
   1.463 +  uint workers() const {
   1.464 +    return _workers;
   1.465 +  }
   1.466 +  void set_active_workers(uint v) {
   1.467 +    assert(v <= _workers, "Trying to set more workers active than there are");
   1.468 +    _active_workers = MIN2(v, _workers);
   1.469 +    assert(v != 0, "Trying to set active workers to 0");
   1.470 +    _active_workers = MAX2(1U, _active_workers);
   1.471 +  }
   1.472 +  // Sets the number of threads that will be used in a collection
   1.473 +  void set_active_gang();
   1.474 +
   1.475 +  NotifyDoneClosure* notify_done_closure() const {
   1.476 +    return _ndc;
   1.477 +  }
   1.478 +  SynchronizedGCTaskQueue* queue() const {
   1.479 +    return _queue;
   1.480 +  }
   1.481 +  NoopGCTask* noop_task() const {
   1.482 +    return _noop_task;
   1.483 +  }
   1.484 +  //     Bounds-checking per-thread data accessors.
   1.485 +  GCTaskThread* thread(uint which);
   1.486 +  void set_thread(uint which, GCTaskThread* value);
   1.487 +  bool resource_flag(uint which);
   1.488 +  void set_resource_flag(uint which, bool value);
   1.489 +  // Modifier methods with some semantics.
   1.490 +  //     Is any worker blocking handing out new tasks?
   1.491 +  uint blocking_worker() const {
   1.492 +    return _blocking_worker;
   1.493 +  }
   1.494 +  void set_blocking_worker(uint value) {
   1.495 +    _blocking_worker = value;
   1.496 +  }
   1.497 +  void set_unblocked() {
   1.498 +    set_blocking_worker(sentinel_worker());
   1.499 +  }
   1.500 +  //     Count of busy workers.
   1.501 +  void reset_busy_workers() {
   1.502 +    _busy_workers = 0;
   1.503 +  }
   1.504 +  uint increment_busy_workers();
   1.505 +  uint decrement_busy_workers();
   1.506 +  //     Count of tasks delivered to workers.
   1.507 +  uint delivered_tasks() const {
   1.508 +    return _delivered_tasks;
   1.509 +  }
   1.510 +  void increment_delivered_tasks() {
   1.511 +    _delivered_tasks += 1;
   1.512 +  }
   1.513 +  void reset_delivered_tasks() {
   1.514 +    _delivered_tasks = 0;
   1.515 +  }
   1.516 +  //     Count of tasks completed by workers.
   1.517 +  uint completed_tasks() const {
   1.518 +    return _completed_tasks;
   1.519 +  }
   1.520 +  void increment_completed_tasks() {
   1.521 +    _completed_tasks += 1;
   1.522 +  }
   1.523 +  void reset_completed_tasks() {
   1.524 +    _completed_tasks = 0;
   1.525 +  }
   1.526 +  //     Count of barrier tasks completed.
   1.527 +  uint barriers() const {
   1.528 +    return _barriers;
   1.529 +  }
   1.530 +  void increment_barriers() {
   1.531 +    _barriers += 1;
   1.532 +  }
   1.533 +  void reset_barriers() {
   1.534 +    _barriers = 0;
   1.535 +  }
   1.536 +  //     Count of how many times the queue has emptied.
   1.537 +  uint emptied_queue() const {
   1.538 +    return _emptied_queue;
   1.539 +  }
   1.540 +  void increment_emptied_queue() {
   1.541 +    _emptied_queue += 1;
   1.542 +  }
   1.543 +  void reset_emptied_queue() {
   1.544 +    _emptied_queue = 0;
   1.545 +  }
   1.546 +  //     Count of the number of noop tasks we've handed out,
   1.547 +  //     e.g., to handle resource release requests.
   1.548 +  uint noop_tasks() const {
   1.549 +    return _noop_tasks;
   1.550 +  }
   1.551 +  void increment_noop_tasks() {
   1.552 +    _noop_tasks += 1;
   1.553 +  }
   1.554 +  void reset_noop_tasks() {
   1.555 +    _noop_tasks = 0;
   1.556 +  }
   1.557 +  void increment_idle_workers() {
   1.558 +    _idle_workers++;
   1.559 +  }
   1.560 +  void decrement_idle_workers() {
   1.561 +    _idle_workers--;
   1.562 +  }
   1.563 +  // Other methods.
   1.564 +  void initialize();
   1.565 +
   1.566 + public:
   1.567 +  // Return true if all workers are currently active.
   1.568 +  bool all_workers_active() { return workers() == active_workers(); }
   1.569 +  uint active_workers() const {
   1.570 +    return _active_workers;
   1.571 +  }
   1.572 +};
   1.573 +
   1.574 +//
   1.575 +// Some exemplary GCTasks.
   1.576 +//
   1.577 +
   1.578 +// A noop task that does nothing,
   1.579 +// except take us around the GCTaskThread loop.
   1.580 +class NoopGCTask : public GCTask {
   1.581 +private:
   1.582 +  const bool _is_c_heap_obj;            // Is this a CHeapObj?
   1.583 +public:
   1.584 +  // Factory create and destroy methods.
   1.585 +  static NoopGCTask* create();
   1.586 +  static NoopGCTask* create_on_c_heap();
   1.587 +  static void destroy(NoopGCTask* that);
   1.588 +
   1.589 +  virtual char* name() { return (char *)"noop task"; }
   1.590 +  // Methods from GCTask.
   1.591 +  void do_it(GCTaskManager* manager, uint which) {
   1.592 +    // Nothing to do.
   1.593 +  }
   1.594 +protected:
   1.595 +  // Constructor.
   1.596 +  NoopGCTask(bool on_c_heap) :
   1.597 +    GCTask(GCTask::Kind::noop_task),
   1.598 +    _is_c_heap_obj(on_c_heap) {
   1.599 +    // Nothing to do.
   1.600 +  }
   1.601 +  // Destructor-like method.
   1.602 +  void destruct();
   1.603 +  // Accessors.
   1.604 +  bool is_c_heap_obj() const {
   1.605 +    return _is_c_heap_obj;
   1.606 +  }
   1.607 +};
   1.608 +
   1.609 +// A BarrierGCTask blocks other tasks from starting,
   1.610 +// and waits until it is the only task running.
   1.611 +class BarrierGCTask : public GCTask {
   1.612 +public:
   1.613 +  // Factory create and destroy methods.
   1.614 +  static BarrierGCTask* create() {
   1.615 +    return new BarrierGCTask();
   1.616 +  }
   1.617 +  static void destroy(BarrierGCTask* that) {
   1.618 +    if (that != NULL) {
   1.619 +      that->destruct();
   1.620 +      delete that;
   1.621 +    }
   1.622 +  }
   1.623 +  // Methods from GCTask.
   1.624 +  void do_it(GCTaskManager* manager, uint which);
   1.625 +protected:
   1.626 +  // Constructor.  Clients use factory, but there might be subclasses.
   1.627 +  BarrierGCTask() :
   1.628 +    GCTask(GCTask::Kind::barrier_task) {
   1.629 +    // Nothing to do.
   1.630 +  }
   1.631 +  // Destructor-like method.
   1.632 +  void destruct();
   1.633 +
   1.634 +  virtual char* name() { return (char *)"barrier task"; }
   1.635 +  // Methods.
   1.636 +  //     Wait for this to be the only task running.
   1.637 +  void do_it_internal(GCTaskManager* manager, uint which);
   1.638 +};
   1.639 +
   1.640 +// A ReleasingBarrierGCTask is a BarrierGCTask
   1.641 +// that tells all the tasks to release their resource areas.
   1.642 +class ReleasingBarrierGCTask : public BarrierGCTask {
   1.643 +public:
   1.644 +  // Factory create and destroy methods.
   1.645 +  static ReleasingBarrierGCTask* create() {
   1.646 +    return new ReleasingBarrierGCTask();
   1.647 +  }
   1.648 +  static void destroy(ReleasingBarrierGCTask* that) {
   1.649 +    if (that != NULL) {
   1.650 +      that->destruct();
   1.651 +      delete that;
   1.652 +    }
   1.653 +  }
   1.654 +  // Methods from GCTask.
   1.655 +  void do_it(GCTaskManager* manager, uint which);
   1.656 +protected:
   1.657 +  // Constructor.  Clients use factory, but there might be subclasses.
   1.658 +  ReleasingBarrierGCTask() :
   1.659 +    BarrierGCTask() {
   1.660 +    // Nothing to do.
   1.661 +  }
   1.662 +  // Destructor-like method.
   1.663 +  void destruct();
   1.664 +};
   1.665 +
   1.666 +// A NotifyingBarrierGCTask is a BarrierGCTask
   1.667 +// that calls a notification method when it is the only task running.
   1.668 +class NotifyingBarrierGCTask : public BarrierGCTask {
   1.669 +private:
   1.670 +  // Instance state.
   1.671 +  NotifyDoneClosure* _ndc;              // The callback object.
   1.672 +public:
   1.673 +  // Factory create and destroy methods.
   1.674 +  static NotifyingBarrierGCTask* create(NotifyDoneClosure* ndc) {
   1.675 +    return new NotifyingBarrierGCTask(ndc);
   1.676 +  }
   1.677 +  static void destroy(NotifyingBarrierGCTask* that) {
   1.678 +    if (that != NULL) {
   1.679 +      that->destruct();
   1.680 +      delete that;
   1.681 +    }
   1.682 +  }
   1.683 +  // Methods from GCTask.
   1.684 +  void do_it(GCTaskManager* manager, uint which);
   1.685 +protected:
   1.686 +  // Constructor.  Clients use factory, but there might be subclasses.
   1.687 +  NotifyingBarrierGCTask(NotifyDoneClosure* ndc) :
   1.688 +    BarrierGCTask(),
   1.689 +    _ndc(ndc) {
   1.690 +    assert(notify_done_closure() != NULL, "can't notify on NULL");
   1.691 +  }
   1.692 +  // Destructor-like method.
   1.693 +  void destruct();
   1.694 +  // Accessor.
   1.695 +  NotifyDoneClosure* notify_done_closure() const { return _ndc; }
   1.696 +};
   1.697 +
   1.698 +// A WaitForBarrierGCTask is a BarrierGCTask
   1.699 +// with a method you can call to wait until
   1.700 +// the BarrierGCTask is done.
   1.701 +// This may cover many of the uses of NotifyingBarrierGCTasks.
   1.702 +class WaitForBarrierGCTask : public BarrierGCTask {
   1.703 +  friend class GCTaskManager;
   1.704 +  friend class IdleGCTask;
   1.705 +private:
   1.706 +  // Instance state.
   1.707 +  Monitor*      _monitor;                  // Guard and notify changes.
   1.708 +  volatile bool _should_wait;              // true=>wait, false=>proceed.
   1.709 +  const bool    _is_c_heap_obj;            // Was allocated on the heap.
   1.710 +public:
   1.711 +  virtual char* name() { return (char *) "waitfor-barrier-task"; }
   1.712 +
   1.713 +  // Factory create and destroy methods.
   1.714 +  static WaitForBarrierGCTask* create();
   1.715 +  static WaitForBarrierGCTask* create_on_c_heap();
   1.716 +  static void destroy(WaitForBarrierGCTask* that);
   1.717 +  // Methods.
   1.718 +  void     do_it(GCTaskManager* manager, uint which);
   1.719 +  void     wait_for(bool reset);
   1.720 +  void set_should_wait(bool value) {
   1.721 +    _should_wait = value;
   1.722 +  }
   1.723 +protected:
   1.724 +  // Constructor.  Clients use factory, but there might be subclasses.
   1.725 +  WaitForBarrierGCTask(bool on_c_heap);
   1.726 +  // Destructor-like method.
   1.727 +  void destruct();
   1.728 +  // Accessors.
   1.729 +  Monitor* monitor() const {
   1.730 +    return _monitor;
   1.731 +  }
   1.732 +  bool should_wait() const {
   1.733 +    return _should_wait;
   1.734 +  }
   1.735 +  bool is_c_heap_obj() {
   1.736 +    return _is_c_heap_obj;
   1.737 +  }
   1.738 +};
   1.739 +
   1.740 +// Task that is used to idle a GC task when fewer than
   1.741 +// the maximum workers are wanted.
   1.742 +class IdleGCTask : public GCTask {
   1.743 +  const bool    _is_c_heap_obj;            // Was allocated on the heap.
   1.744 + public:
   1.745 +  bool is_c_heap_obj() {
   1.746 +    return _is_c_heap_obj;
   1.747 +  }
   1.748 +  // Factory create and destroy methods.
   1.749 +  static IdleGCTask* create();
   1.750 +  static IdleGCTask* create_on_c_heap();
   1.751 +  static void destroy(IdleGCTask* that);
   1.752 +
   1.753 +  virtual char* name() { return (char *)"idle task"; }
   1.754 +  // Methods from GCTask.
   1.755 +  virtual void do_it(GCTaskManager* manager, uint which);
   1.756 +protected:
   1.757 +  // Constructor.
   1.758 +  IdleGCTask(bool on_c_heap) :
   1.759 +    GCTask(GCTask::Kind::idle_task),
   1.760 +    _is_c_heap_obj(on_c_heap) {
   1.761 +    // Nothing to do.
   1.762 +  }
   1.763 +  // Destructor-like method.
   1.764 +  void destruct();
   1.765 +};
   1.766 +
   1.767 +class MonitorSupply : public AllStatic {
   1.768 +private:
   1.769 +  // State.
   1.770 +  //     Control multi-threaded access.
   1.771 +  static Mutex*                   _lock;
   1.772 +  //     The list of available Monitor*'s.
   1.773 +  static GrowableArray<Monitor*>* _freelist;
   1.774 +public:
   1.775 +  // Reserve a Monitor*.
   1.776 +  static Monitor* reserve();
   1.777 +  // Release a Monitor*.
   1.778 +  static void release(Monitor* instance);
   1.779 +private:
   1.780 +  // Accessors.
   1.781 +  static Mutex* lock() {
   1.782 +    return _lock;
   1.783 +  }
   1.784 +  static GrowableArray<Monitor*>* freelist() {
   1.785 +    return _freelist;
   1.786 +  }
   1.787 +};
   1.788 +
   1.789 +#endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP

mercurial