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