0N/A/*
1757N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP
1879N/A#define SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP
1879N/A
1879N/A#ifndef SERIALGC
1879N/A#include "utilities/workgroup.hpp"
1879N/A#endif
1879N/A
0N/A
0N/A// Forward declarations
0N/Aclass YieldingFlexibleWorkGang;
0N/A
0N/A// Status of tasks
0N/Aenum Status {
0N/A INACTIVE,
0N/A ACTIVE,
0N/A YIELDING,
0N/A YIELDED,
0N/A ABORTING,
0N/A ABORTED,
0N/A COMPLETING,
0N/A COMPLETED
0N/A};
0N/A
0N/A// Class YieldingFlexibleGangWorker:
0N/A// Several instances of this class run in parallel as workers for a gang.
0N/Aclass YieldingFlexibleGangWorker: public GangWorker {
0N/Apublic:
0N/A // Ctor
0N/A YieldingFlexibleGangWorker(AbstractWorkGang* gang, int id) :
0N/A GangWorker(gang, id) { }
0N/A
0N/Apublic:
0N/A YieldingFlexibleWorkGang* yf_gang() const
0N/A { return (YieldingFlexibleWorkGang*)gang(); }
0N/A
0N/Aprotected: // Override from parent class
0N/A virtual void loop();
0N/A};
0N/A
1753N/Aclass FlexibleGangTask: public AbstractGangTask {
1753N/A int _actual_size; // size of gang obtained
1753N/Aprotected:
1753N/A int _requested_size; // size of gang requested
1753N/Apublic:
1753N/A FlexibleGangTask(const char* name): AbstractGangTask(name),
1753N/A _requested_size(0) {}
1753N/A
1753N/A // The abstract work method.
1753N/A // The argument tells you which member of the gang you are.
3008N/A virtual void work(uint worker_id) = 0;
1753N/A
1753N/A int requested_size() const { return _requested_size; }
1753N/A int actual_size() const { return _actual_size; }
1753N/A
1753N/A void set_requested_size(int sz) { _requested_size = sz; }
1753N/A void set_actual_size(int sz) { _actual_size = sz; }
1753N/A};
1753N/A
0N/A// An abstract task to be worked on by a flexible work gang,
0N/A// and where the workers will periodically yield, usually
0N/A// in response to some condition that is signalled by means
0N/A// that are specific to the task at hand.
0N/A// You subclass this to supply your own work() method.
0N/A// A second feature of this kind of work gang is that
0N/A// it allows for the signalling of certain exceptional
0N/A// conditions that may be encountered during the performance
0N/A// of the task and that may require the task at hand to be
0N/A// `aborted' forthwith. Finally, these gangs are `flexible'
0N/A// in that they can operate at partial capacity with some
0N/A// gang workers waiting on the bench; in other words, the
0N/A// size of the active worker pool can flex (up to an apriori
0N/A// maximum) in response to task requests at certain points.
0N/A// The last part (the flexible part) has not yet been fully
0N/A// fleshed out and is a work in progress.
1753N/Aclass YieldingFlexibleGangTask: public FlexibleGangTask {
0N/A Status _status;
0N/A YieldingFlexibleWorkGang* _gang;
0N/A
0N/Aprotected:
0N/A // Constructor and desctructor: only construct subclasses.
1753N/A YieldingFlexibleGangTask(const char* name): FlexibleGangTask(name),
0N/A _status(INACTIVE),
1753N/A _gang(NULL) { }
0N/A
0N/A virtual ~YieldingFlexibleGangTask() { }
0N/A
0N/A friend class YieldingFlexibleWorkGang;
0N/A friend class YieldingFlexibleGangWorker;
0N/A NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
0N/A return true;
0N/A })
0N/A
0N/A void set_status(Status s) {
0N/A _status = s;
0N/A }
0N/A YieldingFlexibleWorkGang* gang() {
0N/A return _gang;
0N/A }
0N/A void set_gang(YieldingFlexibleWorkGang* gang) {
0N/A assert(_gang == NULL || gang == NULL, "Clobber without intermediate reset?");
0N/A _gang = gang;
0N/A }
0N/A
0N/Apublic:
0N/A // The abstract work method.
0N/A // The argument tells you which member of the gang you are.
3008N/A virtual void work(uint worker_id) = 0;
0N/A
0N/A // Subclasses should call the parent's yield() method
0N/A // after having done any work specific to the subclass.
0N/A virtual void yield();
0N/A
0N/A // An abstract method supplied by
0N/A // a concrete sub-class which is used by the coordinator
0N/A // to do any "central yielding" work.
0N/A virtual void coordinator_yield() = 0;
0N/A
0N/A // Subclasses should call the parent's abort() method
0N/A // after having done any work specific to the sunbclass.
0N/A virtual void abort();
0N/A
0N/A Status status() const { return _status; }
1757N/A bool yielding() const { return _status == YIELDING; }
0N/A bool yielded() const { return _status == YIELDED; }
0N/A bool completed() const { return _status == COMPLETED; }
0N/A bool aborted() const { return _status == ABORTED; }
0N/A bool active() const { return _status == ACTIVE; }
0N/A};
0N/A// Class YieldingWorkGang: A subclass of WorkGang.
0N/A// In particular, a YieldingWorkGang is made up of
0N/A// YieldingGangWorkers, and provides infrastructure
0N/A// supporting yielding to the "GangOverseer",
0N/A// being the thread that orchestrates the WorkGang via run_task().
1753N/Aclass YieldingFlexibleWorkGang: public FlexibleWorkGang {
0N/A // Here's the public interface to this class.
0N/Apublic:
0N/A // Constructor and destructor.
3008N/A YieldingFlexibleWorkGang(const char* name, uint workers,
342N/A bool are_GC_task_threads);
0N/A
0N/A YieldingFlexibleGangTask* yielding_task() const {
0N/A assert(task() == NULL || task()->is_YieldingFlexibleGang_task(),
0N/A "Incorrect cast");
0N/A return (YieldingFlexibleGangTask*)task();
0N/A }
1753N/A // Allocate a worker and return a pointer to it.
3008N/A GangWorker* allocate_worker(uint which);
1753N/A
0N/A // Run a task; returns when the task is done, or the workers yield,
0N/A // or the task is aborted, or the work gang is terminated via stop().
0N/A // A task that has been yielded can be continued via this same interface
0N/A // by using the same task repeatedly as the argument to the call.
0N/A // It is expected that the YieldingFlexibleGangTask carries the appropriate
0N/A // continuation information used by workers to continue the task
0N/A // from its last yield point. Thus, a completed task will return
0N/A // immediately with no actual work having been done by the workers.
0N/A void run_task(AbstractGangTask* task) {
0N/A guarantee(false, "Use start_task instead");
0N/A }
0N/A void start_task(YieldingFlexibleGangTask* new_task);
0N/A void continue_task(YieldingFlexibleGangTask* gang_task);
0N/A
0N/A // Abort a currently running task, if any; returns when all the workers
0N/A // have stopped working on the current task and have returned to their
0N/A // waiting stations.
0N/A void abort_task();
0N/A
0N/A // Yield: workers wait at their current working stations
0N/A // until signalled to proceed by the overseer.
0N/A void yield();
0N/A
0N/A // Abort: workers are expected to return to their waiting
0N/A // stations, whence they are ready for the next task dispatched
0N/A // by the overseer.
0N/A void abort();
0N/A
0N/Aprivate:
3008N/A uint _yielded_workers;
0N/A void wait_for_gang();
0N/A
0N/Apublic:
0N/A // Accessors for fields
3008N/A uint yielded_workers() const {
0N/A return _yielded_workers;
0N/A }
0N/A
0N/Aprivate:
0N/A friend class YieldingFlexibleGangWorker;
0N/A void reset(); // NYI
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP