annotate src/share/vm/utilities/yieldingWorkgroup.cpp @ 20543:e7d0505c8a30

8059758: Footprint regressions with JDK-8038423 Summary: Changes in JDK-8038423 always initialize (zero out) virtual memory used for auxiliary data structures. This causes a footprint regression for G1 in startup benchmarks. This is because they do not touch that memory at all, so the operating system does not actually commit these pages. The fix is to, if the initialization value of the data structures matches the default value of just committed memory (=0), do not do anything. Reviewed-by: jwilhelm, brutisso
author tschatzl
date Fri, 10 Oct 2014 15:51:58 +0200
parents 55fb97c4c58d
children 4ca6dc0799b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17467
55fb97c4c58d 8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
mikael
parents: 8085
diff changeset
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
0
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 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 342
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 342
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 342
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
25 #include "precompiled.hpp"
8001
db9981fd3124 8005915: Unify SERIALGC and INCLUDE_ALTERNATE_GCS
jprovino
parents: 4728
diff changeset
26 #include "utilities/macros.hpp"
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
27 #include "utilities/yieldingWorkgroup.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 // Forward declaration of classes declared here.
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 class GangWorker;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 class WorkData;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 YieldingFlexibleWorkGang::YieldingFlexibleWorkGang(
4728
441e946dc1af 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 4095
diff changeset
35 const char* name, uint workers, bool are_GC_task_threads) :
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
36 FlexibleWorkGang(name, workers, are_GC_task_threads, false),
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
37 _yielded_workers(0) {}
0
a61af66fc99e Initial load
duke
parents:
diff changeset
38
4728
441e946dc1af 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 4095
diff changeset
39 GangWorker* YieldingFlexibleWorkGang::allocate_worker(uint which) {
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
40 YieldingFlexibleGangWorker* new_member =
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
41 new YieldingFlexibleGangWorker(this, which);
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
42 return (YieldingFlexibleGangWorker*) new_member;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
43 }
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // Run a task; returns when the task is done, or the workers yield,
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // or the task is aborted, or the work gang is terminated via stop().
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // A task that has been yielded can be continued via this interface
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // by using the same task repeatedly as the argument to the call.
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // It is expected that the YieldingFlexibleGangTask carries the appropriate
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // continuation information used by workers to continue the task
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // from its last yield point. Thus, a completed task will return
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // immediately with no actual work having been done by the workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
53 /////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // Implementatiuon notes: remove before checking XXX
a61af66fc99e Initial load
duke
parents:
diff changeset
55 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
56 Each gang is working on a task at a certain time.
a61af66fc99e Initial load
duke
parents:
diff changeset
57 Some subset of workers may have yielded and some may
a61af66fc99e Initial load
duke
parents:
diff changeset
58 have finished their quota of work. Until this task has
a61af66fc99e Initial load
duke
parents:
diff changeset
59 been completed, the workers are bound to that task.
a61af66fc99e Initial load
duke
parents:
diff changeset
60 Once the task has been completed, the gang unbounds
a61af66fc99e Initial load
duke
parents:
diff changeset
61 itself from the task.
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 The yielding work gang thus exports two invokation
a61af66fc99e Initial load
duke
parents:
diff changeset
64 interfaces: run_task() and continue_task(). The
a61af66fc99e Initial load
duke
parents:
diff changeset
65 first is used to initiate a new task and bind it
a61af66fc99e Initial load
duke
parents:
diff changeset
66 to the workers; the second is used to continue an
a61af66fc99e Initial load
duke
parents:
diff changeset
67 already bound task that has yielded. Upon completion
a61af66fc99e Initial load
duke
parents:
diff changeset
68 the binding is released and a new binding may be
a61af66fc99e Initial load
duke
parents:
diff changeset
69 created.
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 The shape of a yielding work gang is as follows:
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 Overseer invokes run_task(*task).
a61af66fc99e Initial load
duke
parents:
diff changeset
74 Lock gang monitor
a61af66fc99e Initial load
duke
parents:
diff changeset
75 Check that there is no existing binding for the gang
a61af66fc99e Initial load
duke
parents:
diff changeset
76 If so, abort with an error
a61af66fc99e Initial load
duke
parents:
diff changeset
77 Else, create a new binding of this gang to the given task
a61af66fc99e Initial load
duke
parents:
diff changeset
78 Set number of active workers (as asked)
a61af66fc99e Initial load
duke
parents:
diff changeset
79 Notify workers that work is ready to be done
a61af66fc99e Initial load
duke
parents:
diff changeset
80 [the requisite # workers would then start up
a61af66fc99e Initial load
duke
parents:
diff changeset
81 and do the task]
a61af66fc99e Initial load
duke
parents:
diff changeset
82 Wait on the monitor until either
a61af66fc99e Initial load
duke
parents:
diff changeset
83 all work is completed or the task has yielded
a61af66fc99e Initial load
duke
parents:
diff changeset
84 -- this is normally done through
a61af66fc99e Initial load
duke
parents:
diff changeset
85 yielded + completed == active
a61af66fc99e Initial load
duke
parents:
diff changeset
86 [completed workers are rest to idle state by overseer?]
a61af66fc99e Initial load
duke
parents:
diff changeset
87 return appropriate status to caller
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 Overseer invokes continue_task(*task),
a61af66fc99e Initial load
duke
parents:
diff changeset
90 Lock gang monitor
a61af66fc99e Initial load
duke
parents:
diff changeset
91 Check that task is the same as current binding
a61af66fc99e Initial load
duke
parents:
diff changeset
92 If not, abort with an error
a61af66fc99e Initial load
duke
parents:
diff changeset
93 Else, set the number of active workers as requested?
a61af66fc99e Initial load
duke
parents:
diff changeset
94 Notify workers that they can continue from yield points
a61af66fc99e Initial load
duke
parents:
diff changeset
95 New workers can also start up as required
a61af66fc99e Initial load
duke
parents:
diff changeset
96 while satisfying the constraint that
a61af66fc99e Initial load
duke
parents:
diff changeset
97 active + yielded does not exceed required number
a61af66fc99e Initial load
duke
parents:
diff changeset
98 Wait (as above).
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 NOTE: In the above, for simplicity in a first iteration
a61af66fc99e Initial load
duke
parents:
diff changeset
101 our gangs will be of fixed population and will not
a61af66fc99e Initial load
duke
parents:
diff changeset
102 therefore be flexible work gangs, just yielding work
a61af66fc99e Initial load
duke
parents:
diff changeset
103 gangs. Once this works well, we will in a second
a61af66fc99e Initial load
duke
parents:
diff changeset
104 iteration.refinement introduce flexibility into
a61af66fc99e Initial load
duke
parents:
diff changeset
105 the work gang.
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 NOTE: we can always create a new gang per each iteration
a61af66fc99e Initial load
duke
parents:
diff changeset
108 in order to get the flexibility, but we will for now
a61af66fc99e Initial load
duke
parents:
diff changeset
109 desist that simplified route.
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 */
a61af66fc99e Initial load
duke
parents:
diff changeset
112 /////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
113 void YieldingFlexibleWorkGang::start_task(YieldingFlexibleGangTask* new_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
115 assert(task() == NULL, "Gang currently tied to a task");
a61af66fc99e Initial load
duke
parents:
diff changeset
116 assert(new_task != NULL, "Null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // Bind task to gang
a61af66fc99e Initial load
duke
parents:
diff changeset
118 _task = new_task;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 new_task->set_gang(this); // Establish 2-way binding to support yielding
a61af66fc99e Initial load
duke
parents:
diff changeset
120 _sequence_number++;
a61af66fc99e Initial load
duke
parents:
diff changeset
121
4728
441e946dc1af 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 4095
diff changeset
122 uint requested_size = new_task->requested_size();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
123 assert(requested_size >= 0, "Should be non-negative");
a61af66fc99e Initial load
duke
parents:
diff changeset
124 if (requested_size != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 _active_workers = MIN2(requested_size, total_workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
126 } else {
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
127 _active_workers = active_workers();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129 new_task->set_actual_size(_active_workers);
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
130 new_task->set_for_termination(_active_workers);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 assert(_started_workers == 0, "Tabula rasa non");
a61af66fc99e Initial load
duke
parents:
diff changeset
133 assert(_finished_workers == 0, "Tabula rasa non");
a61af66fc99e Initial load
duke
parents:
diff changeset
134 assert(_yielded_workers == 0, "Tabula rasa non");
a61af66fc99e Initial load
duke
parents:
diff changeset
135 yielding_task()->set_status(ACTIVE);
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // Wake up all the workers, the first few will get to work,
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // and the rest will go back to sleep
a61af66fc99e Initial load
duke
parents:
diff changeset
139 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 wait_for_gang();
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 void YieldingFlexibleWorkGang::wait_for_gang() {
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 assert(monitor()->owned_by_self(), "Data race");
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // Wait for task to complete or yield
a61af66fc99e Initial load
duke
parents:
diff changeset
147 for (Status status = yielding_task()->status();
a61af66fc99e Initial load
duke
parents:
diff changeset
148 status != COMPLETED && status != YIELDED && status != ABORTED;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 status = yielding_task()->status()) {
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
150 assert(started_workers() <= active_workers(), "invariant");
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
151 assert(finished_workers() <= active_workers(), "invariant");
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
152 assert(yielded_workers() <= active_workers(), "invariant");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
153 monitor()->wait(Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 switch (yielding_task()->status()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 case COMPLETED:
a61af66fc99e Initial load
duke
parents:
diff changeset
157 case ABORTED: {
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
158 assert(finished_workers() == active_workers(), "Inconsistent status");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
159 assert(yielded_workers() == 0, "Invariant");
a61af66fc99e Initial load
duke
parents:
diff changeset
160 reset(); // for next task; gang<->task binding released
a61af66fc99e Initial load
duke
parents:
diff changeset
161 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163 case YIELDED: {
a61af66fc99e Initial load
duke
parents:
diff changeset
164 assert(yielded_workers() > 0, "Invariant");
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
165 assert(yielded_workers() + finished_workers() == active_workers(),
0
a61af66fc99e Initial load
duke
parents:
diff changeset
166 "Inconsistent counts");
a61af66fc99e Initial load
duke
parents:
diff changeset
167 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
168 }
a61af66fc99e Initial load
duke
parents:
diff changeset
169 case ACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
170 case INACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
171 case COMPLETING:
a61af66fc99e Initial load
duke
parents:
diff changeset
172 case YIELDING:
a61af66fc99e Initial load
duke
parents:
diff changeset
173 case ABORTING:
a61af66fc99e Initial load
duke
parents:
diff changeset
174 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
175 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 void YieldingFlexibleWorkGang::continue_task(
a61af66fc99e Initial load
duke
parents:
diff changeset
180 YieldingFlexibleGangTask* gang_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
183 assert(task() != NULL && task() == gang_task, "Incorrect usage");
a61af66fc99e Initial load
duke
parents:
diff changeset
184 assert(_started_workers == _active_workers, "Precondition");
a61af66fc99e Initial load
duke
parents:
diff changeset
185 assert(_yielded_workers > 0 && yielding_task()->status() == YIELDED,
a61af66fc99e Initial load
duke
parents:
diff changeset
186 "Else why are we calling continue_task()");
a61af66fc99e Initial load
duke
parents:
diff changeset
187 // Restart the yielded gang workers
a61af66fc99e Initial load
duke
parents:
diff changeset
188 yielding_task()->set_status(ACTIVE);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
190 wait_for_gang();
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 void YieldingFlexibleWorkGang::reset() {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 _started_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 _finished_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 yielding_task()->set_gang(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
197 _task = NULL; // unbind gang from task
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199
a61af66fc99e Initial load
duke
parents:
diff changeset
200 void YieldingFlexibleWorkGang::yield() {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 assert(task() != NULL, "Inconsistency; should have task binding");
a61af66fc99e Initial load
duke
parents:
diff changeset
202 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
203 assert(yielded_workers() < active_workers(), "Consistency check");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (yielding_task()->status() == ABORTING) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // Do not yield; we need to abort as soon as possible
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // XXX NOTE: This can cause a performance pathology in the
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // current implementation in Mustang, as of today, and
a61af66fc99e Initial load
duke
parents:
diff changeset
208 // pre-Mustang in that as soon as an overflow occurs,
a61af66fc99e Initial load
duke
parents:
diff changeset
209 // yields will not be honoured. The right way to proceed
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // of course is to fix bug # TBF, so that abort's cause
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // us to return at each potential yield point.
a61af66fc99e Initial load
duke
parents:
diff changeset
212 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
4095
bca17e38de00 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 3860
diff changeset
214 if (++_yielded_workers + finished_workers() == active_workers()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
215 yielding_task()->set_status(YIELDED);
a61af66fc99e Initial load
duke
parents:
diff changeset
216 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
217 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 yielding_task()->set_status(YIELDING);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
222 switch (yielding_task()->status()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 case YIELDING:
a61af66fc99e Initial load
duke
parents:
diff changeset
224 case YIELDED: {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 monitor()->wait(Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
226 break; // from switch
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228 case ACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
229 case ABORTING:
a61af66fc99e Initial load
duke
parents:
diff changeset
230 case COMPLETING: {
a61af66fc99e Initial load
duke
parents:
diff changeset
231 assert(_yielded_workers > 0, "Else why am i here?");
a61af66fc99e Initial load
duke
parents:
diff changeset
232 _yielded_workers--;
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 case INACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
236 case ABORTED:
a61af66fc99e Initial load
duke
parents:
diff changeset
237 case COMPLETED:
a61af66fc99e Initial load
duke
parents:
diff changeset
238 default: {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
240 }
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243 // Only return is from inside switch statement above
a61af66fc99e Initial load
duke
parents:
diff changeset
244 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247 void YieldingFlexibleWorkGang::abort() {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 assert(task() != NULL, "Inconsistency; should have task binding");
a61af66fc99e Initial load
duke
parents:
diff changeset
249 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
250 assert(yielded_workers() < active_workers(), "Consistency check");
a61af66fc99e Initial load
duke
parents:
diff changeset
251 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
252 switch (yielding_task()->status()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
253 // allowed states
a61af66fc99e Initial load
duke
parents:
diff changeset
254 case ACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
255 case ABORTING:
a61af66fc99e Initial load
duke
parents:
diff changeset
256 case COMPLETING:
a61af66fc99e Initial load
duke
parents:
diff changeset
257 case YIELDING:
a61af66fc99e Initial load
duke
parents:
diff changeset
258 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // not allowed states
a61af66fc99e Initial load
duke
parents:
diff changeset
260 case INACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
261 case ABORTED:
a61af66fc99e Initial load
duke
parents:
diff changeset
262 case COMPLETED:
a61af66fc99e Initial load
duke
parents:
diff changeset
263 case YIELDED:
a61af66fc99e Initial load
duke
parents:
diff changeset
264 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
265 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267 #endif // !PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
268 Status prev_status = yielding_task()->status();
a61af66fc99e Initial load
duke
parents:
diff changeset
269 yielding_task()->set_status(ABORTING);
a61af66fc99e Initial load
duke
parents:
diff changeset
270 if (prev_status == YIELDING) {
a61af66fc99e Initial load
duke
parents:
diff changeset
271 assert(yielded_workers() > 0, "Inconsistency");
a61af66fc99e Initial load
duke
parents:
diff changeset
272 // At least one thread has yielded, wake it up
a61af66fc99e Initial load
duke
parents:
diff changeset
273 // so it can go back to waiting stations ASAP.
a61af66fc99e Initial load
duke
parents:
diff changeset
274 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277
a61af66fc99e Initial load
duke
parents:
diff changeset
278 ///////////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
279 // YieldingFlexibleGangTask
a61af66fc99e Initial load
duke
parents:
diff changeset
280 ///////////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
281 void YieldingFlexibleGangTask::yield() {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 assert(gang() != NULL, "No gang to signal");
a61af66fc99e Initial load
duke
parents:
diff changeset
283 gang()->yield();
a61af66fc99e Initial load
duke
parents:
diff changeset
284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
285
a61af66fc99e Initial load
duke
parents:
diff changeset
286 void YieldingFlexibleGangTask::abort() {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 assert(gang() != NULL, "No gang to signal");
a61af66fc99e Initial load
duke
parents:
diff changeset
288 gang()->abort();
a61af66fc99e Initial load
duke
parents:
diff changeset
289 }
a61af66fc99e Initial load
duke
parents:
diff changeset
290
a61af66fc99e Initial load
duke
parents:
diff changeset
291 ///////////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
292 // YieldingFlexibleGangWorker
a61af66fc99e Initial load
duke
parents:
diff changeset
293 ///////////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
294 void YieldingFlexibleGangWorker::loop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 int previous_sequence_number = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 Monitor* gang_monitor = gang()->monitor();
a61af66fc99e Initial load
duke
parents:
diff changeset
297 MutexLockerEx ml(gang_monitor, Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
298 WorkData data;
a61af66fc99e Initial load
duke
parents:
diff changeset
299 int id;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
301 // Check if there is work to do or if we have been asked
a61af66fc99e Initial load
duke
parents:
diff changeset
302 // to terminate
a61af66fc99e Initial load
duke
parents:
diff changeset
303 gang()->internal_worker_poll(&data);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 if (data.terminate()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
305 // We have been asked to terminate.
a61af66fc99e Initial load
duke
parents:
diff changeset
306 assert(gang()->task() == NULL, "No task binding");
a61af66fc99e Initial load
duke
parents:
diff changeset
307 // set_status(TERMINATED);
a61af66fc99e Initial load
duke
parents:
diff changeset
308 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
309 } else if (data.task() != NULL &&
a61af66fc99e Initial load
duke
parents:
diff changeset
310 data.sequence_number() != previous_sequence_number) {
a61af66fc99e Initial load
duke
parents:
diff changeset
311 // There is work to be done.
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // First check if we need to become active or if there
a61af66fc99e Initial load
duke
parents:
diff changeset
313 // are already the requisite number of workers
a61af66fc99e Initial load
duke
parents:
diff changeset
314 if (gang()->started_workers() == yf_gang()->active_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 // There are already enough workers, we do not need to
a61af66fc99e Initial load
duke
parents:
diff changeset
316 // to run; fall through and wait on monitor.
a61af66fc99e Initial load
duke
parents:
diff changeset
317 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // We need to pitch in and do the work.
a61af66fc99e Initial load
duke
parents:
diff changeset
319 assert(gang()->started_workers() < yf_gang()->active_workers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
320 "Unexpected state");
a61af66fc99e Initial load
duke
parents:
diff changeset
321 id = gang()->started_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
322 gang()->internal_note_start();
a61af66fc99e Initial load
duke
parents:
diff changeset
323 // Now, release the gang mutex and do the work.
a61af66fc99e Initial load
duke
parents:
diff changeset
324 {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 MutexUnlockerEx mul(gang_monitor, Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
326 data.task()->work(id); // This might include yielding
a61af66fc99e Initial load
duke
parents:
diff changeset
327 }
a61af66fc99e Initial load
duke
parents:
diff changeset
328 // Reacquire monitor and note completion of this worker
a61af66fc99e Initial load
duke
parents:
diff changeset
329 gang()->internal_note_finish();
a61af66fc99e Initial load
duke
parents:
diff changeset
330 // Update status of task based on whether all workers have
a61af66fc99e Initial load
duke
parents:
diff changeset
331 // finished or some have yielded
a61af66fc99e Initial load
duke
parents:
diff changeset
332 assert(data.task() == gang()->task(), "Confused task binding");
a61af66fc99e Initial load
duke
parents:
diff changeset
333 if (gang()->finished_workers() == yf_gang()->active_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 switch (data.yf_task()->status()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
335 case ABORTING: {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 data.yf_task()->set_status(ABORTED);
a61af66fc99e Initial load
duke
parents:
diff changeset
337 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339 case ACTIVE:
a61af66fc99e Initial load
duke
parents:
diff changeset
340 case COMPLETING: {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 data.yf_task()->set_status(COMPLETED);
a61af66fc99e Initial load
duke
parents:
diff changeset
342 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
345 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
346 }
a61af66fc99e Initial load
duke
parents:
diff changeset
347 gang_monitor->notify_all(); // Notify overseer
a61af66fc99e Initial load
duke
parents:
diff changeset
348 } else { // at least one worker is still working or yielded
a61af66fc99e Initial load
duke
parents:
diff changeset
349 assert(gang()->finished_workers() < yf_gang()->active_workers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
350 "Counts inconsistent");
a61af66fc99e Initial load
duke
parents:
diff changeset
351 switch (data.yf_task()->status()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 case ACTIVE: {
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // first, but not only thread to complete
a61af66fc99e Initial load
duke
parents:
diff changeset
354 data.yf_task()->set_status(COMPLETING);
a61af66fc99e Initial load
duke
parents:
diff changeset
355 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357 case YIELDING: {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 if (gang()->finished_workers() + yf_gang()->yielded_workers()
a61af66fc99e Initial load
duke
parents:
diff changeset
359 == yf_gang()->active_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
360 data.yf_task()->set_status(YIELDED);
a61af66fc99e Initial load
duke
parents:
diff changeset
361 gang_monitor->notify_all(); // notify overseer
a61af66fc99e Initial load
duke
parents:
diff changeset
362 }
a61af66fc99e Initial load
duke
parents:
diff changeset
363 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
364 }
a61af66fc99e Initial load
duke
parents:
diff changeset
365 case ABORTING:
a61af66fc99e Initial load
duke
parents:
diff changeset
366 case COMPLETING: {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 break; // nothing to do
a61af66fc99e Initial load
duke
parents:
diff changeset
368 }
a61af66fc99e Initial load
duke
parents:
diff changeset
369 default: // everything else: INACTIVE, YIELDED, ABORTED, COMPLETED
a61af66fc99e Initial load
duke
parents:
diff changeset
370 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
371 }
a61af66fc99e Initial load
duke
parents:
diff changeset
372 }
a61af66fc99e Initial load
duke
parents:
diff changeset
373 }
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375 // Remember the sequence number
a61af66fc99e Initial load
duke
parents:
diff changeset
376 previous_sequence_number = data.sequence_number();
a61af66fc99e Initial load
duke
parents:
diff changeset
377 // Wait for more work
a61af66fc99e Initial load
duke
parents:
diff changeset
378 gang_monitor->wait(Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
379 }
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }