annotate src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.cpp @ 1972:f95d63e2154a

6989984: Use standard include model for Hospot Summary: Replaced MakeDeps and the includeDB files with more standardized solutions. Reviewed-by: coleenp, kvn, kamg
author stefank
date Tue, 23 Nov 2010 13:22:55 -0800
parents c18cbe5936b8
children bca17e38de00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
2 * Copyright (c) 2002, 2010, 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: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
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: 0
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: 1552
diff changeset
25 #include "precompiled.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
26 #include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
27 #include "gc_implementation/parallelScavenge/gcTaskThread.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
28 #include "memory/allocation.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
29 #include "memory/allocation.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
30 #include "runtime/mutex.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
31 #include "runtime/mutexLocker.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 //
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // GCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
35 //
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 const char* GCTask::Kind::to_string(kind value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 const char* result = "unknown GCTask kind";
a61af66fc99e Initial load
duke
parents:
diff changeset
39 switch (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
41 result = "unknown GCTask kind";
a61af66fc99e Initial load
duke
parents:
diff changeset
42 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 case unknown_task:
a61af66fc99e Initial load
duke
parents:
diff changeset
44 result = "unknown task";
a61af66fc99e Initial load
duke
parents:
diff changeset
45 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 case ordinary_task:
a61af66fc99e Initial load
duke
parents:
diff changeset
47 result = "ordinary task";
a61af66fc99e Initial load
duke
parents:
diff changeset
48 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 case barrier_task:
a61af66fc99e Initial load
duke
parents:
diff changeset
50 result = "barrier task";
a61af66fc99e Initial load
duke
parents:
diff changeset
51 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 case noop_task:
a61af66fc99e Initial load
duke
parents:
diff changeset
53 result = "noop task";
a61af66fc99e Initial load
duke
parents:
diff changeset
54 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 };
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 GCTask::GCTask() :
a61af66fc99e Initial load
duke
parents:
diff changeset
60 _kind(Kind::ordinary_task),
a61af66fc99e Initial load
duke
parents:
diff changeset
61 _affinity(GCTaskManager::sentinel_worker()){
a61af66fc99e Initial load
duke
parents:
diff changeset
62 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
63 }
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 GCTask::GCTask(Kind::kind kind) :
a61af66fc99e Initial load
duke
parents:
diff changeset
66 _kind(kind),
a61af66fc99e Initial load
duke
parents:
diff changeset
67 _affinity(GCTaskManager::sentinel_worker()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
69 }
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 GCTask::GCTask(uint affinity) :
a61af66fc99e Initial load
duke
parents:
diff changeset
72 _kind(Kind::ordinary_task),
a61af66fc99e Initial load
duke
parents:
diff changeset
73 _affinity(affinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 GCTask::GCTask(Kind::kind kind, uint affinity) :
a61af66fc99e Initial load
duke
parents:
diff changeset
78 _kind(kind),
a61af66fc99e Initial load
duke
parents:
diff changeset
79 _affinity(affinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
80 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 void GCTask::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 _older = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 _newer = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 void GCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 assert(older() == NULL, "shouldn't have an older task");
a61af66fc99e Initial load
duke
parents:
diff changeset
90 assert(newer() == NULL, "shouldn't have a newer task");
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 NOT_PRODUCT(
a61af66fc99e Initial load
duke
parents:
diff changeset
95 void GCTask::print(const char* message) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 tty->print(INTPTR_FORMAT " <- " INTPTR_FORMAT "(%u) -> " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
97 newer(), this, affinity(), older());
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 )
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 //
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // GCTaskQueue
a61af66fc99e Initial load
duke
parents:
diff changeset
103 //
a61af66fc99e Initial load
duke
parents:
diff changeset
104
a61af66fc99e Initial load
duke
parents:
diff changeset
105 GCTaskQueue* GCTaskQueue::create() {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 GCTaskQueue* result = new GCTaskQueue(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 tty->print_cr("GCTaskQueue::create()"
a61af66fc99e Initial load
duke
parents:
diff changeset
109 " returns " INTPTR_FORMAT, result);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 GCTaskQueue* GCTaskQueue::create_on_c_heap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 GCTaskQueue* result = new(ResourceObj::C_HEAP) GCTaskQueue(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
116 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 tty->print_cr("GCTaskQueue::create_on_c_heap()"
a61af66fc99e Initial load
duke
parents:
diff changeset
118 " returns " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
119 result);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 }
a61af66fc99e Initial load
duke
parents:
diff changeset
121 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 GCTaskQueue::GCTaskQueue(bool on_c_heap) :
a61af66fc99e Initial load
duke
parents:
diff changeset
125 _is_c_heap_obj(on_c_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
129 " GCTaskQueue::GCTaskQueue() constructor",
a61af66fc99e Initial load
duke
parents:
diff changeset
130 this);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 void GCTaskQueue::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 void GCTaskQueue::destroy(GCTaskQueue* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
141 " GCTaskQueue::destroy()"
a61af66fc99e Initial load
duke
parents:
diff changeset
142 " is_c_heap_obj: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
143 that,
a61af66fc99e Initial load
duke
parents:
diff changeset
144 that->is_c_heap_obj() ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // That instance may have been allocated as a CHeapObj,
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // in which case we have to free it explicitly.
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
150 assert(that->is_empty(), "should be empty");
a61af66fc99e Initial load
duke
parents:
diff changeset
151 if (that->is_c_heap_obj()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 FreeHeap(that);
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 }
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 void GCTaskQueue::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 set_insert_end(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 set_remove_end(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 set_length(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 // Enqueue one task.
a61af66fc99e Initial load
duke
parents:
diff changeset
164 void GCTaskQueue::enqueue(GCTask* task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
167 " GCTaskQueue::enqueue(task: "
a61af66fc99e Initial load
duke
parents:
diff changeset
168 INTPTR_FORMAT ")",
a61af66fc99e Initial load
duke
parents:
diff changeset
169 this, task);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 print("before:");
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172 assert(task != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
173 assert(task->older() == NULL, "shouldn't be on queue");
a61af66fc99e Initial load
duke
parents:
diff changeset
174 assert(task->newer() == NULL, "shouldn't be on queue");
a61af66fc99e Initial load
duke
parents:
diff changeset
175 task->set_newer(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 task->set_older(insert_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if (is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 set_remove_end(task);
a61af66fc99e Initial load
duke
parents:
diff changeset
179 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 insert_end()->set_newer(task);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182 set_insert_end(task);
a61af66fc99e Initial load
duke
parents:
diff changeset
183 increment_length();
a61af66fc99e Initial load
duke
parents:
diff changeset
184 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 print("after:");
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // Enqueue a whole list of tasks. Empties the argument list.
a61af66fc99e Initial load
duke
parents:
diff changeset
190 void GCTaskQueue::enqueue(GCTaskQueue* list) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
193 " GCTaskQueue::enqueue(list: "
a61af66fc99e Initial load
duke
parents:
diff changeset
194 INTPTR_FORMAT ")",
a61af66fc99e Initial load
duke
parents:
diff changeset
195 this);
a61af66fc99e Initial load
duke
parents:
diff changeset
196 print("before:");
a61af66fc99e Initial load
duke
parents:
diff changeset
197 list->print("list:");
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 if (list->is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // Enqueuing the empty list: nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
201 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203 uint list_length = list->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // Enqueuing to empty list: just acquire elements.
a61af66fc99e Initial load
duke
parents:
diff changeset
206 set_insert_end(list->insert_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
207 set_remove_end(list->remove_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
208 set_length(list_length);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // Prepend argument list to our queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
211 list->remove_end()->set_older(insert_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
212 insert_end()->set_newer(list->remove_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
213 set_insert_end(list->insert_end());
a61af66fc99e Initial load
duke
parents:
diff changeset
214 // empty the argument list.
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 set_length(length() + list_length);
a61af66fc99e Initial load
duke
parents:
diff changeset
217 list->initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
218 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 print("after:");
a61af66fc99e Initial load
duke
parents:
diff changeset
220 list->print("list:");
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223
a61af66fc99e Initial load
duke
parents:
diff changeset
224 // Dequeue one task.
a61af66fc99e Initial load
duke
parents:
diff changeset
225 GCTask* GCTaskQueue::dequeue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
227 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
228 " GCTaskQueue::dequeue()", this);
a61af66fc99e Initial load
duke
parents:
diff changeset
229 print("before:");
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231 assert(!is_empty(), "shouldn't dequeue from empty list");
a61af66fc99e Initial load
duke
parents:
diff changeset
232 GCTask* result = remove();
a61af66fc99e Initial load
duke
parents:
diff changeset
233 assert(result != NULL, "shouldn't have NULL task");
a61af66fc99e Initial load
duke
parents:
diff changeset
234 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
235 tty->print_cr(" return: " INTPTR_FORMAT, result);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 print("after:");
a61af66fc99e Initial load
duke
parents:
diff changeset
237 }
a61af66fc99e Initial load
duke
parents:
diff changeset
238 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240
a61af66fc99e Initial load
duke
parents:
diff changeset
241 // Dequeue one task, preferring one with affinity.
a61af66fc99e Initial load
duke
parents:
diff changeset
242 GCTask* GCTaskQueue::dequeue(uint affinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
245 " GCTaskQueue::dequeue(%u)", this, affinity);
a61af66fc99e Initial load
duke
parents:
diff changeset
246 print("before:");
a61af66fc99e Initial load
duke
parents:
diff changeset
247 }
a61af66fc99e Initial load
duke
parents:
diff changeset
248 assert(!is_empty(), "shouldn't dequeue from empty list");
a61af66fc99e Initial load
duke
parents:
diff changeset
249 // Look down to the next barrier for a task with this affinity.
a61af66fc99e Initial load
duke
parents:
diff changeset
250 GCTask* result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
251 for (GCTask* element = remove_end();
a61af66fc99e Initial load
duke
parents:
diff changeset
252 element != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
253 element = element->newer()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 if (element->is_barrier_task()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 // Don't consider barrier tasks, nor past them.
a61af66fc99e Initial load
duke
parents:
diff changeset
256 result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259 if (element->affinity() == affinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 result = remove(element);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
262 }
a61af66fc99e Initial load
duke
parents:
diff changeset
263 }
a61af66fc99e Initial load
duke
parents:
diff changeset
264 // If we didn't find anything with affinity, just take the next task.
a61af66fc99e Initial load
duke
parents:
diff changeset
265 if (result == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 result = remove();
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 if (TraceGCTaskQueue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 tty->print_cr(" return: " INTPTR_FORMAT, result);
a61af66fc99e Initial load
duke
parents:
diff changeset
270 print("after:");
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
273 }
a61af66fc99e Initial load
duke
parents:
diff changeset
274
a61af66fc99e Initial load
duke
parents:
diff changeset
275 GCTask* GCTaskQueue::remove() {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 // Dequeue from remove end.
a61af66fc99e Initial load
duke
parents:
diff changeset
277 GCTask* result = remove_end();
a61af66fc99e Initial load
duke
parents:
diff changeset
278 assert(result != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
279 assert(result->older() == NULL, "not the remove_end");
a61af66fc99e Initial load
duke
parents:
diff changeset
280 set_remove_end(result->newer());
a61af66fc99e Initial load
duke
parents:
diff changeset
281 if (remove_end() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 assert(insert_end() == result, "not a singleton");
a61af66fc99e Initial load
duke
parents:
diff changeset
283 set_insert_end(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
284 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 remove_end()->set_older(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287 result->set_newer(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
288 decrement_length();
a61af66fc99e Initial load
duke
parents:
diff changeset
289 assert(result->newer() == NULL, "shouldn't be on queue");
a61af66fc99e Initial load
duke
parents:
diff changeset
290 assert(result->older() == NULL, "shouldn't be on queue");
a61af66fc99e Initial load
duke
parents:
diff changeset
291 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293
a61af66fc99e Initial load
duke
parents:
diff changeset
294 GCTask* GCTaskQueue::remove(GCTask* task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 // This is slightly more work, and has slightly fewer asserts
a61af66fc99e Initial load
duke
parents:
diff changeset
296 // than removing from the remove end.
a61af66fc99e Initial load
duke
parents:
diff changeset
297 assert(task != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
298 GCTask* result = task;
a61af66fc99e Initial load
duke
parents:
diff changeset
299 if (result->newer() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
300 result->newer()->set_older(result->older());
a61af66fc99e Initial load
duke
parents:
diff changeset
301 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 assert(insert_end() == result, "not youngest");
a61af66fc99e Initial load
duke
parents:
diff changeset
303 set_insert_end(result->older());
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if (result->older() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 result->older()->set_newer(result->newer());
a61af66fc99e Initial load
duke
parents:
diff changeset
307 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
308 assert(remove_end() == result, "not oldest");
a61af66fc99e Initial load
duke
parents:
diff changeset
309 set_remove_end(result->newer());
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311 result->set_newer(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
312 result->set_older(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
313 decrement_length();
a61af66fc99e Initial load
duke
parents:
diff changeset
314 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
315 }
a61af66fc99e Initial load
duke
parents:
diff changeset
316
a61af66fc99e Initial load
duke
parents:
diff changeset
317 NOT_PRODUCT(
a61af66fc99e Initial load
duke
parents:
diff changeset
318 void GCTaskQueue::print(const char* message) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 tty->print_cr("[" INTPTR_FORMAT "] GCTaskQueue:"
a61af66fc99e Initial load
duke
parents:
diff changeset
320 " insert_end: " INTPTR_FORMAT
a61af66fc99e Initial load
duke
parents:
diff changeset
321 " remove_end: " INTPTR_FORMAT
a61af66fc99e Initial load
duke
parents:
diff changeset
322 " %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
323 this, insert_end(), remove_end(), message);
a61af66fc99e Initial load
duke
parents:
diff changeset
324 for (GCTask* element = insert_end();
a61af66fc99e Initial load
duke
parents:
diff changeset
325 element != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
326 element = element->older()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 element->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
328 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331 )
a61af66fc99e Initial load
duke
parents:
diff changeset
332
a61af66fc99e Initial load
duke
parents:
diff changeset
333 //
a61af66fc99e Initial load
duke
parents:
diff changeset
334 // SynchronizedGCTaskQueue
a61af66fc99e Initial load
duke
parents:
diff changeset
335 //
a61af66fc99e Initial load
duke
parents:
diff changeset
336
a61af66fc99e Initial load
duke
parents:
diff changeset
337 SynchronizedGCTaskQueue::SynchronizedGCTaskQueue(GCTaskQueue* queue_arg,
a61af66fc99e Initial load
duke
parents:
diff changeset
338 Monitor * lock_arg) :
a61af66fc99e Initial load
duke
parents:
diff changeset
339 _unsynchronized_queue(queue_arg),
a61af66fc99e Initial load
duke
parents:
diff changeset
340 _lock(lock_arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 assert(unsynchronized_queue() != NULL, "null queue");
a61af66fc99e Initial load
duke
parents:
diff changeset
342 assert(lock() != NULL, "null lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344
a61af66fc99e Initial load
duke
parents:
diff changeset
345 SynchronizedGCTaskQueue::~SynchronizedGCTaskQueue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
346 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
347 }
a61af66fc99e Initial load
duke
parents:
diff changeset
348
a61af66fc99e Initial load
duke
parents:
diff changeset
349 //
a61af66fc99e Initial load
duke
parents:
diff changeset
350 // GCTaskManager
a61af66fc99e Initial load
duke
parents:
diff changeset
351 //
a61af66fc99e Initial load
duke
parents:
diff changeset
352 GCTaskManager::GCTaskManager(uint workers) :
a61af66fc99e Initial load
duke
parents:
diff changeset
353 _workers(workers),
a61af66fc99e Initial load
duke
parents:
diff changeset
354 _ndc(NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
355 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357
a61af66fc99e Initial load
duke
parents:
diff changeset
358 GCTaskManager::GCTaskManager(uint workers, NotifyDoneClosure* ndc) :
a61af66fc99e Initial load
duke
parents:
diff changeset
359 _workers(workers),
a61af66fc99e Initial load
duke
parents:
diff changeset
360 _ndc(ndc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
362 }
a61af66fc99e Initial load
duke
parents:
diff changeset
363
a61af66fc99e Initial load
duke
parents:
diff changeset
364 void GCTaskManager::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
365 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 tty->print_cr("GCTaskManager::initialize: workers: %u", workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 assert(workers() != 0, "no workers");
a61af66fc99e Initial load
duke
parents:
diff changeset
369 _monitor = new Monitor(Mutex::barrier, // rank
a61af66fc99e Initial load
duke
parents:
diff changeset
370 "GCTaskManager monitor", // name
a61af66fc99e Initial load
duke
parents:
diff changeset
371 Mutex::_allow_vm_block_flag); // allow_vm_block
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // The queue for the GCTaskManager must be a CHeapObj.
a61af66fc99e Initial load
duke
parents:
diff changeset
373 GCTaskQueue* unsynchronized_queue = GCTaskQueue::create_on_c_heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
374 _queue = SynchronizedGCTaskQueue::create(unsynchronized_queue, lock());
a61af66fc99e Initial load
duke
parents:
diff changeset
375 _noop_task = NoopGCTask::create_on_c_heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
376 _resource_flag = NEW_C_HEAP_ARRAY(bool, workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
377 {
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // Set up worker threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
379 // Distribute the workers among the available processors,
a61af66fc99e Initial load
duke
parents:
diff changeset
380 // unless we were told not to, or if the os doesn't want to.
a61af66fc99e Initial load
duke
parents:
diff changeset
381 uint* processor_assignment = NEW_C_HEAP_ARRAY(uint, workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
382 if (!BindGCTaskThreadsToCPUs ||
a61af66fc99e Initial load
duke
parents:
diff changeset
383 !os::distribute_processes(workers(), processor_assignment)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
384 for (uint a = 0; a < workers(); a += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
385 processor_assignment[a] = sentinel_worker();
a61af66fc99e Initial load
duke
parents:
diff changeset
386 }
a61af66fc99e Initial load
duke
parents:
diff changeset
387 }
a61af66fc99e Initial load
duke
parents:
diff changeset
388 _thread = NEW_C_HEAP_ARRAY(GCTaskThread*, workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
389 for (uint t = 0; t < workers(); t += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 set_thread(t, GCTaskThread::create(this, t, processor_assignment[t]));
a61af66fc99e Initial load
duke
parents:
diff changeset
391 }
a61af66fc99e Initial load
duke
parents:
diff changeset
392 if (TraceGCTaskThread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 tty->print("GCTaskManager::initialize: distribution:");
a61af66fc99e Initial load
duke
parents:
diff changeset
394 for (uint t = 0; t < workers(); t += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 tty->print(" %u", processor_assignment[t]);
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
398 }
a61af66fc99e Initial load
duke
parents:
diff changeset
399 FREE_C_HEAP_ARRAY(uint, processor_assignment);
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401 reset_busy_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
402 set_unblocked();
a61af66fc99e Initial load
duke
parents:
diff changeset
403 for (uint w = 0; w < workers(); w += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
404 set_resource_flag(w, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
a61af66fc99e Initial load
duke
parents:
diff changeset
406 reset_delivered_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
407 reset_completed_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
408 reset_noop_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
409 reset_barriers();
a61af66fc99e Initial load
duke
parents:
diff changeset
410 reset_emptied_queue();
a61af66fc99e Initial load
duke
parents:
diff changeset
411 for (uint s = 0; s < workers(); s += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 thread(s)->start();
a61af66fc99e Initial load
duke
parents:
diff changeset
413 }
a61af66fc99e Initial load
duke
parents:
diff changeset
414 }
a61af66fc99e Initial load
duke
parents:
diff changeset
415
a61af66fc99e Initial load
duke
parents:
diff changeset
416 GCTaskManager::~GCTaskManager() {
a61af66fc99e Initial load
duke
parents:
diff changeset
417 assert(busy_workers() == 0, "still have busy workers");
a61af66fc99e Initial load
duke
parents:
diff changeset
418 assert(queue()->is_empty(), "still have queued work");
a61af66fc99e Initial load
duke
parents:
diff changeset
419 NoopGCTask::destroy(_noop_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
420 _noop_task = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
421 if (_thread != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 for (uint i = 0; i < workers(); i += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 GCTaskThread::destroy(thread(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
424 set_thread(i, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
426 FREE_C_HEAP_ARRAY(GCTaskThread*, _thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
427 _thread = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429 if (_resource_flag != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
430 FREE_C_HEAP_ARRAY(bool, _resource_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
431 _resource_flag = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
432 }
a61af66fc99e Initial load
duke
parents:
diff changeset
433 if (queue() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
434 GCTaskQueue* unsynchronized_queue = queue()->unsynchronized_queue();
a61af66fc99e Initial load
duke
parents:
diff changeset
435 GCTaskQueue::destroy(unsynchronized_queue);
a61af66fc99e Initial load
duke
parents:
diff changeset
436 SynchronizedGCTaskQueue::destroy(queue());
a61af66fc99e Initial load
duke
parents:
diff changeset
437 _queue = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
438 }
a61af66fc99e Initial load
duke
parents:
diff changeset
439 if (monitor() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
440 delete monitor();
a61af66fc99e Initial load
duke
parents:
diff changeset
441 _monitor = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
442 }
a61af66fc99e Initial load
duke
parents:
diff changeset
443 }
a61af66fc99e Initial load
duke
parents:
diff changeset
444
a61af66fc99e Initial load
duke
parents:
diff changeset
445 void GCTaskManager::print_task_time_stamps() {
a61af66fc99e Initial load
duke
parents:
diff changeset
446 for(uint i=0; i<ParallelGCThreads; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 GCTaskThread* t = thread(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
448 t->print_task_time_stamps();
a61af66fc99e Initial load
duke
parents:
diff changeset
449 }
a61af66fc99e Initial load
duke
parents:
diff changeset
450 }
a61af66fc99e Initial load
duke
parents:
diff changeset
451
a61af66fc99e Initial load
duke
parents:
diff changeset
452 void GCTaskManager::print_threads_on(outputStream* st) {
a61af66fc99e Initial load
duke
parents:
diff changeset
453 uint num_thr = workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
454 for (uint i = 0; i < num_thr; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
455 thread(i)->print_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
456 st->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
457 }
a61af66fc99e Initial load
duke
parents:
diff changeset
458 }
a61af66fc99e Initial load
duke
parents:
diff changeset
459
a61af66fc99e Initial load
duke
parents:
diff changeset
460 void GCTaskManager::threads_do(ThreadClosure* tc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
461 assert(tc != NULL, "Null ThreadClosure");
a61af66fc99e Initial load
duke
parents:
diff changeset
462 uint num_thr = workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
463 for (uint i = 0; i < num_thr; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
464 tc->do_thread(thread(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
465 }
a61af66fc99e Initial load
duke
parents:
diff changeset
466 }
a61af66fc99e Initial load
duke
parents:
diff changeset
467
a61af66fc99e Initial load
duke
parents:
diff changeset
468 GCTaskThread* GCTaskManager::thread(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
469 assert(which < workers(), "index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
470 assert(_thread[which] != NULL, "shouldn't have null thread");
a61af66fc99e Initial load
duke
parents:
diff changeset
471 return _thread[which];
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 void GCTaskManager::set_thread(uint which, GCTaskThread* value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
475 assert(which < workers(), "index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
476 assert(value != NULL, "shouldn't have null thread");
a61af66fc99e Initial load
duke
parents:
diff changeset
477 _thread[which] = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
478 }
a61af66fc99e Initial load
duke
parents:
diff changeset
479
a61af66fc99e Initial load
duke
parents:
diff changeset
480 void GCTaskManager::add_task(GCTask* task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
481 assert(task != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
482 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
483 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
484 tty->print_cr("GCTaskManager::add_task(" INTPTR_FORMAT " [%s])",
a61af66fc99e Initial load
duke
parents:
diff changeset
485 task, GCTask::Kind::to_string(task->kind()));
a61af66fc99e Initial load
duke
parents:
diff changeset
486 }
a61af66fc99e Initial load
duke
parents:
diff changeset
487 queue()->enqueue(task);
a61af66fc99e Initial load
duke
parents:
diff changeset
488 // Notify with the lock held to avoid missed notifies.
a61af66fc99e Initial load
duke
parents:
diff changeset
489 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
490 tty->print_cr(" GCTaskManager::add_task (%s)->notify_all",
a61af66fc99e Initial load
duke
parents:
diff changeset
491 monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
492 }
a61af66fc99e Initial load
duke
parents:
diff changeset
493 (void) monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
494 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
495 }
a61af66fc99e Initial load
duke
parents:
diff changeset
496
a61af66fc99e Initial load
duke
parents:
diff changeset
497 void GCTaskManager::add_list(GCTaskQueue* list) {
a61af66fc99e Initial load
duke
parents:
diff changeset
498 assert(list != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
499 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
500 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 tty->print_cr("GCTaskManager::add_list(%u)", list->length());
a61af66fc99e Initial load
duke
parents:
diff changeset
502 }
a61af66fc99e Initial load
duke
parents:
diff changeset
503 queue()->enqueue(list);
a61af66fc99e Initial load
duke
parents:
diff changeset
504 // Notify with the lock held to avoid missed notifies.
a61af66fc99e Initial load
duke
parents:
diff changeset
505 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
506 tty->print_cr(" GCTaskManager::add_list (%s)->notify_all",
a61af66fc99e Initial load
duke
parents:
diff changeset
507 monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
508 }
a61af66fc99e Initial load
duke
parents:
diff changeset
509 (void) monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
510 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
511 }
a61af66fc99e Initial load
duke
parents:
diff changeset
512
a61af66fc99e Initial load
duke
parents:
diff changeset
513 GCTask* GCTaskManager::get_task(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
514 GCTask* result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
515 // Grab the queue lock.
a61af66fc99e Initial load
duke
parents:
diff changeset
516 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
517 // Wait while the queue is block or
a61af66fc99e Initial load
duke
parents:
diff changeset
518 // there is nothing to do, except maybe release resources.
a61af66fc99e Initial load
duke
parents:
diff changeset
519 while (is_blocked() ||
a61af66fc99e Initial load
duke
parents:
diff changeset
520 (queue()->is_empty() && !should_release_resources(which))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
521 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
522 tty->print_cr("GCTaskManager::get_task(%u)"
a61af66fc99e Initial load
duke
parents:
diff changeset
523 " blocked: %s"
a61af66fc99e Initial load
duke
parents:
diff changeset
524 " empty: %s"
a61af66fc99e Initial load
duke
parents:
diff changeset
525 " release: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
526 which,
a61af66fc99e Initial load
duke
parents:
diff changeset
527 is_blocked() ? "true" : "false",
a61af66fc99e Initial load
duke
parents:
diff changeset
528 queue()->is_empty() ? "true" : "false",
a61af66fc99e Initial load
duke
parents:
diff changeset
529 should_release_resources(which) ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
530 tty->print_cr(" => (%s)->wait()",
a61af66fc99e Initial load
duke
parents:
diff changeset
531 monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533 monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
534 }
a61af66fc99e Initial load
duke
parents:
diff changeset
535 // We've reacquired the queue lock here.
a61af66fc99e Initial load
duke
parents:
diff changeset
536 // Figure out which condition caused us to exit the loop above.
a61af66fc99e Initial load
duke
parents:
diff changeset
537 if (!queue()->is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
538 if (UseGCTaskAffinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
539 result = queue()->dequeue(which);
a61af66fc99e Initial load
duke
parents:
diff changeset
540 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
541 result = queue()->dequeue();
a61af66fc99e Initial load
duke
parents:
diff changeset
542 }
a61af66fc99e Initial load
duke
parents:
diff changeset
543 if (result->is_barrier_task()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
544 assert(which != sentinel_worker(),
a61af66fc99e Initial load
duke
parents:
diff changeset
545 "blocker shouldn't be bogus");
a61af66fc99e Initial load
duke
parents:
diff changeset
546 set_blocking_worker(which);
a61af66fc99e Initial load
duke
parents:
diff changeset
547 }
a61af66fc99e Initial load
duke
parents:
diff changeset
548 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
549 // The queue is empty, but we were woken up.
a61af66fc99e Initial load
duke
parents:
diff changeset
550 // Just hand back a Noop task,
a61af66fc99e Initial load
duke
parents:
diff changeset
551 // in case someone wanted us to release resources, or whatever.
a61af66fc99e Initial load
duke
parents:
diff changeset
552 result = noop_task();
a61af66fc99e Initial load
duke
parents:
diff changeset
553 increment_noop_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
554 }
a61af66fc99e Initial load
duke
parents:
diff changeset
555 assert(result != NULL, "shouldn't have null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
556 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 tty->print_cr("GCTaskManager::get_task(%u) => " INTPTR_FORMAT " [%s]",
a61af66fc99e Initial load
duke
parents:
diff changeset
558 which, result, GCTask::Kind::to_string(result->kind()));
a61af66fc99e Initial load
duke
parents:
diff changeset
559 tty->print_cr(" %s", result->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
560 }
a61af66fc99e Initial load
duke
parents:
diff changeset
561 increment_busy_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
562 increment_delivered_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
563 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
564 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }
a61af66fc99e Initial load
duke
parents:
diff changeset
566
a61af66fc99e Initial load
duke
parents:
diff changeset
567 void GCTaskManager::note_completion(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
568 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
569 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
570 tty->print_cr("GCTaskManager::note_completion(%u)", which);
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572 // If we are blocked, check if the completing thread is the blocker.
a61af66fc99e Initial load
duke
parents:
diff changeset
573 if (blocking_worker() == which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
574 assert(blocking_worker() != sentinel_worker(),
a61af66fc99e Initial load
duke
parents:
diff changeset
575 "blocker shouldn't be bogus");
a61af66fc99e Initial load
duke
parents:
diff changeset
576 increment_barriers();
a61af66fc99e Initial load
duke
parents:
diff changeset
577 set_unblocked();
a61af66fc99e Initial load
duke
parents:
diff changeset
578 }
a61af66fc99e Initial load
duke
parents:
diff changeset
579 increment_completed_tasks();
a61af66fc99e Initial load
duke
parents:
diff changeset
580 uint active = decrement_busy_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
581 if ((active == 0) && (queue()->is_empty())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
582 increment_emptied_queue();
a61af66fc99e Initial load
duke
parents:
diff changeset
583 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
584 tty->print_cr(" GCTaskManager::note_completion(%u) done", which);
a61af66fc99e Initial load
duke
parents:
diff changeset
585 }
a61af66fc99e Initial load
duke
parents:
diff changeset
586 // Notify client that we are done.
a61af66fc99e Initial load
duke
parents:
diff changeset
587 NotifyDoneClosure* ndc = notify_done_closure();
a61af66fc99e Initial load
duke
parents:
diff changeset
588 if (ndc != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
589 ndc->notify(this);
a61af66fc99e Initial load
duke
parents:
diff changeset
590 }
a61af66fc99e Initial load
duke
parents:
diff changeset
591 }
a61af66fc99e Initial load
duke
parents:
diff changeset
592 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
593 tty->print_cr(" GCTaskManager::note_completion(%u) (%s)->notify_all",
a61af66fc99e Initial load
duke
parents:
diff changeset
594 which, monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
595 tty->print_cr(" "
a61af66fc99e Initial load
duke
parents:
diff changeset
596 " blocked: %s"
a61af66fc99e Initial load
duke
parents:
diff changeset
597 " empty: %s"
a61af66fc99e Initial load
duke
parents:
diff changeset
598 " release: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
599 is_blocked() ? "true" : "false",
a61af66fc99e Initial load
duke
parents:
diff changeset
600 queue()->is_empty() ? "true" : "false",
a61af66fc99e Initial load
duke
parents:
diff changeset
601 should_release_resources(which) ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
602 tty->print_cr(" "
a61af66fc99e Initial load
duke
parents:
diff changeset
603 " delivered: %u"
a61af66fc99e Initial load
duke
parents:
diff changeset
604 " completed: %u"
a61af66fc99e Initial load
duke
parents:
diff changeset
605 " barriers: %u"
a61af66fc99e Initial load
duke
parents:
diff changeset
606 " emptied: %u",
a61af66fc99e Initial load
duke
parents:
diff changeset
607 delivered_tasks(),
a61af66fc99e Initial load
duke
parents:
diff changeset
608 completed_tasks(),
a61af66fc99e Initial load
duke
parents:
diff changeset
609 barriers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
610 emptied_queue());
a61af66fc99e Initial load
duke
parents:
diff changeset
611 }
a61af66fc99e Initial load
duke
parents:
diff changeset
612 // Tell everyone that a task has completed.
a61af66fc99e Initial load
duke
parents:
diff changeset
613 (void) monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
614 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
615 }
a61af66fc99e Initial load
duke
parents:
diff changeset
616
a61af66fc99e Initial load
duke
parents:
diff changeset
617 uint GCTaskManager::increment_busy_workers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
618 assert(queue()->own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
619 _busy_workers += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
620 return _busy_workers;
a61af66fc99e Initial load
duke
parents:
diff changeset
621 }
a61af66fc99e Initial load
duke
parents:
diff changeset
622
a61af66fc99e Initial load
duke
parents:
diff changeset
623 uint GCTaskManager::decrement_busy_workers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
624 assert(queue()->own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
625 _busy_workers -= 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
626 return _busy_workers;
a61af66fc99e Initial load
duke
parents:
diff changeset
627 }
a61af66fc99e Initial load
duke
parents:
diff changeset
628
a61af66fc99e Initial load
duke
parents:
diff changeset
629 void GCTaskManager::release_all_resources() {
a61af66fc99e Initial load
duke
parents:
diff changeset
630 // If you want this to be done atomically, do it in a BarrierGCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
631 for (uint i = 0; i < workers(); i += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
632 set_resource_flag(i, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
633 }
a61af66fc99e Initial load
duke
parents:
diff changeset
634 }
a61af66fc99e Initial load
duke
parents:
diff changeset
635
a61af66fc99e Initial load
duke
parents:
diff changeset
636 bool GCTaskManager::should_release_resources(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
637 // This can be done without a lock because each thread reads one element.
a61af66fc99e Initial load
duke
parents:
diff changeset
638 return resource_flag(which);
a61af66fc99e Initial load
duke
parents:
diff changeset
639 }
a61af66fc99e Initial load
duke
parents:
diff changeset
640
a61af66fc99e Initial load
duke
parents:
diff changeset
641 void GCTaskManager::note_release(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
642 // This can be done without a lock because each thread writes one element.
a61af66fc99e Initial load
duke
parents:
diff changeset
643 set_resource_flag(which, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
644 }
a61af66fc99e Initial load
duke
parents:
diff changeset
645
a61af66fc99e Initial load
duke
parents:
diff changeset
646 void GCTaskManager::execute_and_wait(GCTaskQueue* list) {
a61af66fc99e Initial load
duke
parents:
diff changeset
647 WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
a61af66fc99e Initial load
duke
parents:
diff changeset
648 list->enqueue(fin);
a61af66fc99e Initial load
duke
parents:
diff changeset
649 add_list(list);
a61af66fc99e Initial load
duke
parents:
diff changeset
650 fin->wait_for();
a61af66fc99e Initial load
duke
parents:
diff changeset
651 // We have to release the barrier tasks!
a61af66fc99e Initial load
duke
parents:
diff changeset
652 WaitForBarrierGCTask::destroy(fin);
a61af66fc99e Initial load
duke
parents:
diff changeset
653 }
a61af66fc99e Initial load
duke
parents:
diff changeset
654
a61af66fc99e Initial load
duke
parents:
diff changeset
655 bool GCTaskManager::resource_flag(uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
656 assert(which < workers(), "index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
657 return _resource_flag[which];
a61af66fc99e Initial load
duke
parents:
diff changeset
658 }
a61af66fc99e Initial load
duke
parents:
diff changeset
659
a61af66fc99e Initial load
duke
parents:
diff changeset
660 void GCTaskManager::set_resource_flag(uint which, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
661 assert(which < workers(), "index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
662 _resource_flag[which] = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
663 }
a61af66fc99e Initial load
duke
parents:
diff changeset
664
a61af66fc99e Initial load
duke
parents:
diff changeset
665 //
a61af66fc99e Initial load
duke
parents:
diff changeset
666 // NoopGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
667 //
a61af66fc99e Initial load
duke
parents:
diff changeset
668
a61af66fc99e Initial load
duke
parents:
diff changeset
669 NoopGCTask* NoopGCTask::create() {
a61af66fc99e Initial load
duke
parents:
diff changeset
670 NoopGCTask* result = new NoopGCTask(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
671 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
672 }
a61af66fc99e Initial load
duke
parents:
diff changeset
673
a61af66fc99e Initial load
duke
parents:
diff changeset
674 NoopGCTask* NoopGCTask::create_on_c_heap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
675 NoopGCTask* result = new(ResourceObj::C_HEAP) NoopGCTask(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
676 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
677 }
a61af66fc99e Initial load
duke
parents:
diff changeset
678
a61af66fc99e Initial load
duke
parents:
diff changeset
679 void NoopGCTask::destroy(NoopGCTask* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
680 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
681 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
682 if (that->is_c_heap_obj()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
683 FreeHeap(that);
a61af66fc99e Initial load
duke
parents:
diff changeset
684 }
a61af66fc99e Initial load
duke
parents:
diff changeset
685 }
a61af66fc99e Initial load
duke
parents:
diff changeset
686 }
a61af66fc99e Initial load
duke
parents:
diff changeset
687
a61af66fc99e Initial load
duke
parents:
diff changeset
688 void NoopGCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
689 // This has to know it's superclass structure, just like the constructor.
a61af66fc99e Initial load
duke
parents:
diff changeset
690 this->GCTask::destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
691 // Nothing else to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
692 }
a61af66fc99e Initial load
duke
parents:
diff changeset
693
a61af66fc99e Initial load
duke
parents:
diff changeset
694 //
a61af66fc99e Initial load
duke
parents:
diff changeset
695 // BarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
696 //
a61af66fc99e Initial load
duke
parents:
diff changeset
697
a61af66fc99e Initial load
duke
parents:
diff changeset
698 void BarrierGCTask::do_it(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
699 // Wait for this to be the only busy worker.
a61af66fc99e Initial load
duke
parents:
diff changeset
700 // ??? I thought of having a StackObj class
a61af66fc99e Initial load
duke
parents:
diff changeset
701 // whose constructor would grab the lock and come to the barrier,
a61af66fc99e Initial load
duke
parents:
diff changeset
702 // and whose destructor would release the lock,
a61af66fc99e Initial load
duke
parents:
diff changeset
703 // but that seems like too much mechanism for two lines of code.
a61af66fc99e Initial load
duke
parents:
diff changeset
704 MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
705 do_it_internal(manager, which);
a61af66fc99e Initial load
duke
parents:
diff changeset
706 // Release manager->lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
707 }
a61af66fc99e Initial load
duke
parents:
diff changeset
708
a61af66fc99e Initial load
duke
parents:
diff changeset
709 void BarrierGCTask::do_it_internal(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
710 // Wait for this to be the only busy worker.
a61af66fc99e Initial load
duke
parents:
diff changeset
711 assert(manager->monitor()->owned_by_self(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
712 assert(manager->is_blocked(), "manager isn't blocked");
a61af66fc99e Initial load
duke
parents:
diff changeset
713 while (manager->busy_workers() > 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
714 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
715 tty->print_cr("BarrierGCTask::do_it(%u) waiting on %u workers",
a61af66fc99e Initial load
duke
parents:
diff changeset
716 which, manager->busy_workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
717 }
a61af66fc99e Initial load
duke
parents:
diff changeset
718 manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
719 }
a61af66fc99e Initial load
duke
parents:
diff changeset
720 }
a61af66fc99e Initial load
duke
parents:
diff changeset
721
a61af66fc99e Initial load
duke
parents:
diff changeset
722 void BarrierGCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
723 this->GCTask::destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
724 // Nothing else to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
725 }
a61af66fc99e Initial load
duke
parents:
diff changeset
726
a61af66fc99e Initial load
duke
parents:
diff changeset
727 //
a61af66fc99e Initial load
duke
parents:
diff changeset
728 // ReleasingBarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
729 //
a61af66fc99e Initial load
duke
parents:
diff changeset
730
a61af66fc99e Initial load
duke
parents:
diff changeset
731 void ReleasingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
732 MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
733 do_it_internal(manager, which);
a61af66fc99e Initial load
duke
parents:
diff changeset
734 manager->release_all_resources();
a61af66fc99e Initial load
duke
parents:
diff changeset
735 // Release manager->lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
736 }
a61af66fc99e Initial load
duke
parents:
diff changeset
737
a61af66fc99e Initial load
duke
parents:
diff changeset
738 void ReleasingBarrierGCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
739 this->BarrierGCTask::destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
740 // Nothing else to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
741 }
a61af66fc99e Initial load
duke
parents:
diff changeset
742
a61af66fc99e Initial load
duke
parents:
diff changeset
743 //
a61af66fc99e Initial load
duke
parents:
diff changeset
744 // NotifyingBarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
745 //
a61af66fc99e Initial load
duke
parents:
diff changeset
746
a61af66fc99e Initial load
duke
parents:
diff changeset
747 void NotifyingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
748 MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
749 do_it_internal(manager, which);
a61af66fc99e Initial load
duke
parents:
diff changeset
750 NotifyDoneClosure* ndc = notify_done_closure();
a61af66fc99e Initial load
duke
parents:
diff changeset
751 if (ndc != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
752 ndc->notify(manager);
a61af66fc99e Initial load
duke
parents:
diff changeset
753 }
a61af66fc99e Initial load
duke
parents:
diff changeset
754 // Release manager->lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
755 }
a61af66fc99e Initial load
duke
parents:
diff changeset
756
a61af66fc99e Initial load
duke
parents:
diff changeset
757 void NotifyingBarrierGCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
758 this->BarrierGCTask::destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
759 // Nothing else to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
760 }
a61af66fc99e Initial load
duke
parents:
diff changeset
761
a61af66fc99e Initial load
duke
parents:
diff changeset
762 //
a61af66fc99e Initial load
duke
parents:
diff changeset
763 // WaitForBarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
764 //
a61af66fc99e Initial load
duke
parents:
diff changeset
765 WaitForBarrierGCTask* WaitForBarrierGCTask::create() {
a61af66fc99e Initial load
duke
parents:
diff changeset
766 WaitForBarrierGCTask* result = new WaitForBarrierGCTask(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
767 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
768 }
a61af66fc99e Initial load
duke
parents:
diff changeset
769
a61af66fc99e Initial load
duke
parents:
diff changeset
770 WaitForBarrierGCTask* WaitForBarrierGCTask::create_on_c_heap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
771 WaitForBarrierGCTask* result = new WaitForBarrierGCTask(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
772 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
773 }
a61af66fc99e Initial load
duke
parents:
diff changeset
774
a61af66fc99e Initial load
duke
parents:
diff changeset
775 WaitForBarrierGCTask::WaitForBarrierGCTask(bool on_c_heap) :
a61af66fc99e Initial load
duke
parents:
diff changeset
776 _is_c_heap_obj(on_c_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
777 _monitor = MonitorSupply::reserve();
a61af66fc99e Initial load
duke
parents:
diff changeset
778 set_should_wait(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
779 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
780 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
781 " WaitForBarrierGCTask::WaitForBarrierGCTask()"
a61af66fc99e Initial load
duke
parents:
diff changeset
782 " monitor: " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
783 this, monitor());
a61af66fc99e Initial load
duke
parents:
diff changeset
784 }
a61af66fc99e Initial load
duke
parents:
diff changeset
785 }
a61af66fc99e Initial load
duke
parents:
diff changeset
786
a61af66fc99e Initial load
duke
parents:
diff changeset
787 void WaitForBarrierGCTask::destroy(WaitForBarrierGCTask* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
788 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
789 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
790 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
791 " WaitForBarrierGCTask::destroy()"
a61af66fc99e Initial load
duke
parents:
diff changeset
792 " is_c_heap_obj: %s"
a61af66fc99e Initial load
duke
parents:
diff changeset
793 " monitor: " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
794 that,
a61af66fc99e Initial load
duke
parents:
diff changeset
795 that->is_c_heap_obj() ? "true" : "false",
a61af66fc99e Initial load
duke
parents:
diff changeset
796 that->monitor());
a61af66fc99e Initial load
duke
parents:
diff changeset
797 }
a61af66fc99e Initial load
duke
parents:
diff changeset
798 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
799 if (that->is_c_heap_obj()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
800 FreeHeap(that);
a61af66fc99e Initial load
duke
parents:
diff changeset
801 }
a61af66fc99e Initial load
duke
parents:
diff changeset
802 }
a61af66fc99e Initial load
duke
parents:
diff changeset
803 }
a61af66fc99e Initial load
duke
parents:
diff changeset
804
a61af66fc99e Initial load
duke
parents:
diff changeset
805 void WaitForBarrierGCTask::destruct() {
a61af66fc99e Initial load
duke
parents:
diff changeset
806 assert(monitor() != NULL, "monitor should not be NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
807 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
808 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
809 " WaitForBarrierGCTask::destruct()"
a61af66fc99e Initial load
duke
parents:
diff changeset
810 " monitor: " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
811 this, monitor());
a61af66fc99e Initial load
duke
parents:
diff changeset
812 }
a61af66fc99e Initial load
duke
parents:
diff changeset
813 this->BarrierGCTask::destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
814 // Clean up that should be in the destructor,
a61af66fc99e Initial load
duke
parents:
diff changeset
815 // except that ResourceMarks don't call destructors.
a61af66fc99e Initial load
duke
parents:
diff changeset
816 if (monitor() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
817 MonitorSupply::release(monitor());
a61af66fc99e Initial load
duke
parents:
diff changeset
818 }
a61af66fc99e Initial load
duke
parents:
diff changeset
819 _monitor = (Monitor*) 0xDEAD000F;
a61af66fc99e Initial load
duke
parents:
diff changeset
820 }
a61af66fc99e Initial load
duke
parents:
diff changeset
821
a61af66fc99e Initial load
duke
parents:
diff changeset
822 void WaitForBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
823 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
824 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
825 " WaitForBarrierGCTask::do_it() waiting for idle"
a61af66fc99e Initial load
duke
parents:
diff changeset
826 " monitor: " INTPTR_FORMAT,
a61af66fc99e Initial load
duke
parents:
diff changeset
827 this, monitor());
a61af66fc99e Initial load
duke
parents:
diff changeset
828 }
a61af66fc99e Initial load
duke
parents:
diff changeset
829 {
a61af66fc99e Initial load
duke
parents:
diff changeset
830 // First, wait for the barrier to arrive.
a61af66fc99e Initial load
duke
parents:
diff changeset
831 MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
832 do_it_internal(manager, which);
a61af66fc99e Initial load
duke
parents:
diff changeset
833 // Release manager->lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
834 }
a61af66fc99e Initial load
duke
parents:
diff changeset
835 {
a61af66fc99e Initial load
duke
parents:
diff changeset
836 // Then notify the waiter.
a61af66fc99e Initial load
duke
parents:
diff changeset
837 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
838 set_should_wait(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
839 // Waiter doesn't miss the notify in the wait_for method
a61af66fc99e Initial load
duke
parents:
diff changeset
840 // since it checks the flag after grabbing the monitor.
a61af66fc99e Initial load
duke
parents:
diff changeset
841 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
842 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
843 " WaitForBarrierGCTask::do_it()"
a61af66fc99e Initial load
duke
parents:
diff changeset
844 " [" INTPTR_FORMAT "] (%s)->notify_all()",
a61af66fc99e Initial load
duke
parents:
diff changeset
845 this, monitor(), monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
846 }
a61af66fc99e Initial load
duke
parents:
diff changeset
847 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
848 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
849 }
a61af66fc99e Initial load
duke
parents:
diff changeset
850 }
a61af66fc99e Initial load
duke
parents:
diff changeset
851
a61af66fc99e Initial load
duke
parents:
diff changeset
852 void WaitForBarrierGCTask::wait_for() {
a61af66fc99e Initial load
duke
parents:
diff changeset
853 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
854 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
855 " WaitForBarrierGCTask::wait_for()"
a61af66fc99e Initial load
duke
parents:
diff changeset
856 " should_wait: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
857 this, should_wait() ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
858 }
a61af66fc99e Initial load
duke
parents:
diff changeset
859 {
a61af66fc99e Initial load
duke
parents:
diff changeset
860 // Grab the lock and check again.
a61af66fc99e Initial load
duke
parents:
diff changeset
861 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
862 while (should_wait()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
863 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
864 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
865 " WaitForBarrierGCTask::wait_for()"
a61af66fc99e Initial load
duke
parents:
diff changeset
866 " [" INTPTR_FORMAT "] (%s)->wait()",
a61af66fc99e Initial load
duke
parents:
diff changeset
867 this, monitor(), monitor()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
868 }
a61af66fc99e Initial load
duke
parents:
diff changeset
869 monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
870 }
a61af66fc99e Initial load
duke
parents:
diff changeset
871 // Reset the flag in case someone reuses this task.
a61af66fc99e Initial load
duke
parents:
diff changeset
872 set_should_wait(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
873 if (TraceGCTaskManager) {
a61af66fc99e Initial load
duke
parents:
diff changeset
874 tty->print_cr("[" INTPTR_FORMAT "]"
a61af66fc99e Initial load
duke
parents:
diff changeset
875 " WaitForBarrierGCTask::wait_for() returns"
a61af66fc99e Initial load
duke
parents:
diff changeset
876 " should_wait: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
877 this, should_wait() ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
878 }
a61af66fc99e Initial load
duke
parents:
diff changeset
879 // Release monitor().
a61af66fc99e Initial load
duke
parents:
diff changeset
880 }
a61af66fc99e Initial load
duke
parents:
diff changeset
881 }
a61af66fc99e Initial load
duke
parents:
diff changeset
882
a61af66fc99e Initial load
duke
parents:
diff changeset
883 Mutex* MonitorSupply::_lock = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
884 GrowableArray<Monitor*>* MonitorSupply::_freelist = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
885
a61af66fc99e Initial load
duke
parents:
diff changeset
886 Monitor* MonitorSupply::reserve() {
a61af66fc99e Initial load
duke
parents:
diff changeset
887 Monitor* result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
888 // Lazy initialization: possible race.
a61af66fc99e Initial load
duke
parents:
diff changeset
889 if (lock() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
890 _lock = new Mutex(Mutex::barrier, // rank
a61af66fc99e Initial load
duke
parents:
diff changeset
891 "MonitorSupply mutex", // name
a61af66fc99e Initial load
duke
parents:
diff changeset
892 Mutex::_allow_vm_block_flag); // allow_vm_block
a61af66fc99e Initial load
duke
parents:
diff changeset
893 }
a61af66fc99e Initial load
duke
parents:
diff changeset
894 {
a61af66fc99e Initial load
duke
parents:
diff changeset
895 MutexLockerEx ml(lock());
a61af66fc99e Initial load
duke
parents:
diff changeset
896 // Lazy initialization.
a61af66fc99e Initial load
duke
parents:
diff changeset
897 if (freelist() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
898 _freelist =
a61af66fc99e Initial load
duke
parents:
diff changeset
899 new(ResourceObj::C_HEAP) GrowableArray<Monitor*>(ParallelGCThreads,
a61af66fc99e Initial load
duke
parents:
diff changeset
900 true);
a61af66fc99e Initial load
duke
parents:
diff changeset
901 }
a61af66fc99e Initial load
duke
parents:
diff changeset
902 if (! freelist()->is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
903 result = freelist()->pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
904 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
905 result = new Monitor(Mutex::barrier, // rank
a61af66fc99e Initial load
duke
parents:
diff changeset
906 "MonitorSupply monitor", // name
a61af66fc99e Initial load
duke
parents:
diff changeset
907 Mutex::_allow_vm_block_flag); // allow_vm_block
a61af66fc99e Initial load
duke
parents:
diff changeset
908 }
a61af66fc99e Initial load
duke
parents:
diff changeset
909 guarantee(result != NULL, "shouldn't return NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
910 assert(!result->is_locked(), "shouldn't be locked");
a61af66fc99e Initial load
duke
parents:
diff changeset
911 // release lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
912 }
a61af66fc99e Initial load
duke
parents:
diff changeset
913 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
914 }
a61af66fc99e Initial load
duke
parents:
diff changeset
915
a61af66fc99e Initial load
duke
parents:
diff changeset
916 void MonitorSupply::release(Monitor* instance) {
a61af66fc99e Initial load
duke
parents:
diff changeset
917 assert(instance != NULL, "shouldn't release NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
918 assert(!instance->is_locked(), "shouldn't be locked");
a61af66fc99e Initial load
duke
parents:
diff changeset
919 {
a61af66fc99e Initial load
duke
parents:
diff changeset
920 MutexLockerEx ml(lock());
a61af66fc99e Initial load
duke
parents:
diff changeset
921 freelist()->push(instance);
a61af66fc99e Initial load
duke
parents:
diff changeset
922 // release lock().
a61af66fc99e Initial load
duke
parents:
diff changeset
923 }
a61af66fc99e Initial load
duke
parents:
diff changeset
924 }