annotate src/share/vm/utilities/yieldingWorkgroup.hpp @ 342:37f87013dfd8

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