Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP |
aoqi@0 | 26 | #define SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "utilities/macros.hpp" |
aoqi@0 | 29 | #include "utilities/workgroup.hpp" |
aoqi@0 | 30 | |
aoqi@0 | 31 | // Forward declarations |
aoqi@0 | 32 | class YieldingFlexibleWorkGang; |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Status of tasks |
aoqi@0 | 35 | enum Status { |
aoqi@0 | 36 | INACTIVE, |
aoqi@0 | 37 | ACTIVE, |
aoqi@0 | 38 | YIELDING, |
aoqi@0 | 39 | YIELDED, |
aoqi@0 | 40 | ABORTING, |
aoqi@0 | 41 | ABORTED, |
aoqi@0 | 42 | COMPLETING, |
aoqi@0 | 43 | COMPLETED |
aoqi@0 | 44 | }; |
aoqi@0 | 45 | |
aoqi@0 | 46 | // Class YieldingFlexibleGangWorker: |
aoqi@0 | 47 | // Several instances of this class run in parallel as workers for a gang. |
aoqi@0 | 48 | class YieldingFlexibleGangWorker: public GangWorker { |
aoqi@0 | 49 | public: |
aoqi@0 | 50 | // Ctor |
aoqi@0 | 51 | YieldingFlexibleGangWorker(AbstractWorkGang* gang, int id) : |
aoqi@0 | 52 | GangWorker(gang, id) { } |
aoqi@0 | 53 | |
aoqi@0 | 54 | public: |
aoqi@0 | 55 | YieldingFlexibleWorkGang* yf_gang() const |
aoqi@0 | 56 | { return (YieldingFlexibleWorkGang*)gang(); } |
aoqi@0 | 57 | |
aoqi@0 | 58 | protected: // Override from parent class |
aoqi@0 | 59 | virtual void loop(); |
aoqi@0 | 60 | }; |
aoqi@0 | 61 | |
aoqi@0 | 62 | class FlexibleGangTask: public AbstractGangTask { |
aoqi@0 | 63 | int _actual_size; // size of gang obtained |
aoqi@0 | 64 | protected: |
aoqi@0 | 65 | int _requested_size; // size of gang requested |
aoqi@0 | 66 | public: |
aoqi@0 | 67 | FlexibleGangTask(const char* name): AbstractGangTask(name), |
aoqi@0 | 68 | _requested_size(0) {} |
aoqi@0 | 69 | |
aoqi@0 | 70 | // The abstract work method. |
aoqi@0 | 71 | // The argument tells you which member of the gang you are. |
aoqi@0 | 72 | virtual void work(uint worker_id) = 0; |
aoqi@0 | 73 | |
aoqi@0 | 74 | int requested_size() const { return _requested_size; } |
aoqi@0 | 75 | int actual_size() const { return _actual_size; } |
aoqi@0 | 76 | |
aoqi@0 | 77 | void set_requested_size(int sz) { _requested_size = sz; } |
aoqi@0 | 78 | void set_actual_size(int sz) { _actual_size = sz; } |
aoqi@0 | 79 | }; |
aoqi@0 | 80 | |
aoqi@0 | 81 | // An abstract task to be worked on by a flexible work gang, |
aoqi@0 | 82 | // and where the workers will periodically yield, usually |
aoqi@0 | 83 | // in response to some condition that is signalled by means |
aoqi@0 | 84 | // that are specific to the task at hand. |
aoqi@0 | 85 | // You subclass this to supply your own work() method. |
aoqi@0 | 86 | // A second feature of this kind of work gang is that |
aoqi@0 | 87 | // it allows for the signalling of certain exceptional |
aoqi@0 | 88 | // conditions that may be encountered during the performance |
aoqi@0 | 89 | // of the task and that may require the task at hand to be |
aoqi@0 | 90 | // `aborted' forthwith. Finally, these gangs are `flexible' |
aoqi@0 | 91 | // in that they can operate at partial capacity with some |
aoqi@0 | 92 | // gang workers waiting on the bench; in other words, the |
aoqi@0 | 93 | // size of the active worker pool can flex (up to an apriori |
aoqi@0 | 94 | // maximum) in response to task requests at certain points. |
aoqi@0 | 95 | // The last part (the flexible part) has not yet been fully |
aoqi@0 | 96 | // fleshed out and is a work in progress. |
aoqi@0 | 97 | class YieldingFlexibleGangTask: public FlexibleGangTask { |
aoqi@0 | 98 | Status _status; |
aoqi@0 | 99 | YieldingFlexibleWorkGang* _gang; |
aoqi@0 | 100 | |
aoqi@0 | 101 | protected: |
aoqi@0 | 102 | // Constructor and desctructor: only construct subclasses. |
aoqi@0 | 103 | YieldingFlexibleGangTask(const char* name): FlexibleGangTask(name), |
aoqi@0 | 104 | _status(INACTIVE), |
aoqi@0 | 105 | _gang(NULL) { } |
aoqi@0 | 106 | |
aoqi@0 | 107 | ~YieldingFlexibleGangTask() { } |
aoqi@0 | 108 | |
aoqi@0 | 109 | friend class YieldingFlexibleWorkGang; |
aoqi@0 | 110 | friend class YieldingFlexibleGangWorker; |
aoqi@0 | 111 | NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const { |
aoqi@0 | 112 | return true; |
aoqi@0 | 113 | }) |
aoqi@0 | 114 | |
aoqi@0 | 115 | void set_status(Status s) { |
aoqi@0 | 116 | _status = s; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | YieldingFlexibleWorkGang* gang() { |
aoqi@0 | 119 | return _gang; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | void set_gang(YieldingFlexibleWorkGang* gang) { |
aoqi@0 | 122 | assert(_gang == NULL || gang == NULL, "Clobber without intermediate reset?"); |
aoqi@0 | 123 | _gang = gang; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | public: |
aoqi@0 | 127 | // The abstract work method. |
aoqi@0 | 128 | // The argument tells you which member of the gang you are. |
aoqi@0 | 129 | virtual void work(uint worker_id) = 0; |
aoqi@0 | 130 | |
aoqi@0 | 131 | // Subclasses should call the parent's yield() method |
aoqi@0 | 132 | // after having done any work specific to the subclass. |
aoqi@0 | 133 | virtual void yield(); |
aoqi@0 | 134 | |
aoqi@0 | 135 | // An abstract method supplied by |
aoqi@0 | 136 | // a concrete sub-class which is used by the coordinator |
aoqi@0 | 137 | // to do any "central yielding" work. |
aoqi@0 | 138 | virtual void coordinator_yield() = 0; |
aoqi@0 | 139 | |
aoqi@0 | 140 | // Subclasses should call the parent's abort() method |
aoqi@0 | 141 | // after having done any work specific to the sunbclass. |
aoqi@0 | 142 | virtual void abort(); |
aoqi@0 | 143 | |
aoqi@0 | 144 | Status status() const { return _status; } |
aoqi@0 | 145 | bool yielding() const { return _status == YIELDING; } |
aoqi@0 | 146 | bool yielded() const { return _status == YIELDED; } |
aoqi@0 | 147 | bool completed() const { return _status == COMPLETED; } |
aoqi@0 | 148 | bool aborted() const { return _status == ABORTED; } |
aoqi@0 | 149 | bool active() const { return _status == ACTIVE; } |
aoqi@0 | 150 | }; |
aoqi@0 | 151 | // Class YieldingWorkGang: A subclass of WorkGang. |
aoqi@0 | 152 | // In particular, a YieldingWorkGang is made up of |
aoqi@0 | 153 | // YieldingGangWorkers, and provides infrastructure |
aoqi@0 | 154 | // supporting yielding to the "GangOverseer", |
aoqi@0 | 155 | // being the thread that orchestrates the WorkGang via run_task(). |
aoqi@0 | 156 | class YieldingFlexibleWorkGang: public FlexibleWorkGang { |
aoqi@0 | 157 | // Here's the public interface to this class. |
aoqi@0 | 158 | public: |
aoqi@0 | 159 | // Constructor and destructor. |
aoqi@0 | 160 | YieldingFlexibleWorkGang(const char* name, uint workers, |
aoqi@0 | 161 | bool are_GC_task_threads); |
aoqi@0 | 162 | |
aoqi@0 | 163 | YieldingFlexibleGangTask* yielding_task() const { |
aoqi@0 | 164 | assert(task() == NULL || task()->is_YieldingFlexibleGang_task(), |
aoqi@0 | 165 | "Incorrect cast"); |
aoqi@0 | 166 | return (YieldingFlexibleGangTask*)task(); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | // Allocate a worker and return a pointer to it. |
aoqi@0 | 169 | GangWorker* allocate_worker(uint which); |
aoqi@0 | 170 | |
aoqi@0 | 171 | // Run a task; returns when the task is done, or the workers yield, |
aoqi@0 | 172 | // or the task is aborted, or the work gang is terminated via stop(). |
aoqi@0 | 173 | // A task that has been yielded can be continued via this same interface |
aoqi@0 | 174 | // by using the same task repeatedly as the argument to the call. |
aoqi@0 | 175 | // It is expected that the YieldingFlexibleGangTask carries the appropriate |
aoqi@0 | 176 | // continuation information used by workers to continue the task |
aoqi@0 | 177 | // from its last yield point. Thus, a completed task will return |
aoqi@0 | 178 | // immediately with no actual work having been done by the workers. |
aoqi@0 | 179 | void run_task(AbstractGangTask* task) { |
aoqi@0 | 180 | guarantee(false, "Use start_task instead"); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | void start_task(YieldingFlexibleGangTask* new_task); |
aoqi@0 | 183 | void continue_task(YieldingFlexibleGangTask* gang_task); |
aoqi@0 | 184 | |
aoqi@0 | 185 | // Abort a currently running task, if any; returns when all the workers |
aoqi@0 | 186 | // have stopped working on the current task and have returned to their |
aoqi@0 | 187 | // waiting stations. |
aoqi@0 | 188 | void abort_task(); |
aoqi@0 | 189 | |
aoqi@0 | 190 | // Yield: workers wait at their current working stations |
aoqi@0 | 191 | // until signalled to proceed by the overseer. |
aoqi@0 | 192 | void yield(); |
aoqi@0 | 193 | |
aoqi@0 | 194 | // Abort: workers are expected to return to their waiting |
aoqi@0 | 195 | // stations, whence they are ready for the next task dispatched |
aoqi@0 | 196 | // by the overseer. |
aoqi@0 | 197 | void abort(); |
aoqi@0 | 198 | |
aoqi@0 | 199 | private: |
aoqi@0 | 200 | uint _yielded_workers; |
aoqi@0 | 201 | void wait_for_gang(); |
aoqi@0 | 202 | |
aoqi@0 | 203 | public: |
aoqi@0 | 204 | // Accessors for fields |
aoqi@0 | 205 | uint yielded_workers() const { |
aoqi@0 | 206 | return _yielded_workers; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | private: |
aoqi@0 | 210 | friend class YieldingFlexibleGangWorker; |
aoqi@0 | 211 | void reset(); // NYI |
aoqi@0 | 212 | }; |
aoqi@0 | 213 | |
aoqi@0 | 214 | #endif // SHARE_VM_UTILITIES_YIELDINGWORKGROUP_HPP |