annotate src/share/vm/utilities/workgroup.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 8b10f48633dc
children 92da084fefc9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
2 * Copyright (c) 2001, 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: 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"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
26 #include "memory/allocation.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
27 #include "memory/allocation.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
28 #include "runtime/os.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1833
diff changeset
29 #include "utilities/workgroup.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // Definitions of WorkGang methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 AbstractWorkGang::AbstractWorkGang(const char* name,
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
34 bool are_GC_task_threads,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
35 bool are_ConcurrentGC_threads) :
0
a61af66fc99e Initial load
duke
parents:
diff changeset
36 _name(name),
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
37 _are_GC_task_threads(are_GC_task_threads),
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
38 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
39
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
40 assert(!(are_GC_task_threads && are_ConcurrentGC_threads),
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
41 "They cannot both be STW GC and Concurrent threads" );
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
42
0
a61af66fc99e Initial load
duke
parents:
diff changeset
43 // Other initialization.
a61af66fc99e Initial load
duke
parents:
diff changeset
44 _monitor = new Monitor(/* priority */ Mutex::leaf,
a61af66fc99e Initial load
duke
parents:
diff changeset
45 /* name */ "WorkGroup monitor",
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
46 /* allow_vm_block */ are_GC_task_threads);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
47 assert(monitor() != NULL, "Failed to allocate monitor");
a61af66fc99e Initial load
duke
parents:
diff changeset
48 _terminate = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 _task = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 _sequence_number = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 _started_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 _finished_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 WorkGang::WorkGang(const char* name,
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
56 int workers,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
57 bool are_GC_task_threads,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
58 bool are_ConcurrentGC_threads) :
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
59 AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // Save arguments.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 _total_workers = workers;
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
62 }
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
63
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
64 GangWorker* WorkGang::allocate_worker(int which) {
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
65 GangWorker* new_worker = new GangWorker(this, which);
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
66 return new_worker;
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
67 }
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
68
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
69 // The current implementation will exit if the allocation
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
70 // of any worker fails. Still, return a boolean so that
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
71 // a future implementation can possibly do a partial
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
72 // initialization of the workers and report such to the
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
73 // caller.
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
74 bool WorkGang::initialize_workers() {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
75
0
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if (TraceWorkGang) {
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
77 tty->print_cr("Constructing work gang %s with %d threads",
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
78 name(),
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
79 total_workers());
0
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
81 _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers());
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
82 if (gang_workers() == NULL) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
83 vm_exit_out_of_memory(0, "Cannot create GangWorker array.");
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
84 return false;
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
85 }
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
86 os::ThreadType worker_type;
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
87 if (are_ConcurrentGC_threads()) {
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
88 worker_type = os::cgc_thread;
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
89 } else {
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
90 worker_type = os::pgc_thread;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
91 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
92 for (int worker = 0; worker < total_workers(); worker += 1) {
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
93 GangWorker* new_worker = allocate_worker(worker);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
94 assert(new_worker != NULL, "Failed to allocate GangWorker");
a61af66fc99e Initial load
duke
parents:
diff changeset
95 _gang_workers[worker] = new_worker;
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
96 if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
97 vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
98 return false;
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
99 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
100 if (!DisableStartThread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 os::start_thread(new_worker);
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
104 return true;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 AbstractWorkGang::~AbstractWorkGang() {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 tty->print_cr("Destructing work gang %s", name());
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 stop(); // stop all the workers
a61af66fc99e Initial load
duke
parents:
diff changeset
112 for (int worker = 0; worker < total_workers(); worker += 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 delete gang_worker(worker);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 delete gang_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
116 delete monitor();
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 GangWorker* AbstractWorkGang::gang_worker(int i) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // Array index bounds checking.
a61af66fc99e Initial load
duke
parents:
diff changeset
121 GangWorker* result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 assert(gang_workers() != NULL, "No workers for indexing");
a61af66fc99e Initial load
duke
parents:
diff changeset
123 assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
124 result = _gang_workers[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
125 assert(result != NULL, "Indexing to null worker");
a61af66fc99e Initial load
duke
parents:
diff changeset
126 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 void WorkGang::run_task(AbstractGangTask* task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // This thread is executed by the VM thread which does not block
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // on ordinary MutexLocker's.
a61af66fc99e Initial load
duke
parents:
diff changeset
132 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 tty->print_cr("Running work gang %s task %s", name(), task->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // Tell all the workers to run a task.
a61af66fc99e Initial load
duke
parents:
diff changeset
137 assert(task != NULL, "Running a null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // Initialize.
a61af66fc99e Initial load
duke
parents:
diff changeset
139 _task = task;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 _sequence_number += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 _started_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
142 _finished_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
143 // Tell the workers to get to work.
a61af66fc99e Initial load
duke
parents:
diff changeset
144 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // Wait for them to be finished
a61af66fc99e Initial load
duke
parents:
diff changeset
146 while (finished_workers() < total_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
149 name(), finished_workers(), total_workers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
150 _sequence_number);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 monitor()->wait(/* no_safepoint_check */ true);
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154 _task = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
155 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 tty->print_cr("/nFinished work gang %s: %d/%d sequence %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
157 name(), finished_workers(), total_workers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
158 _sequence_number);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161
a61af66fc99e Initial load
duke
parents:
diff changeset
162 void AbstractWorkGang::stop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 // Tell all workers to terminate, then wait for them to become inactive.
a61af66fc99e Initial load
duke
parents:
diff changeset
164 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 tty->print_cr("Stopping work gang %s task %s", name(), task()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168 _task = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
169 _terminate = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 monitor()->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
171 while (finished_workers() < total_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 tty->print_cr("Waiting in work gang %s: %d/%d finished",
a61af66fc99e Initial load
duke
parents:
diff changeset
174 name(), finished_workers(), total_workers());
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176 monitor()->wait(/* no_safepoint_check */ true);
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178 }
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 void AbstractWorkGang::internal_worker_poll(WorkData* data) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 assert(monitor()->owned_by_self(), "worker_poll is an internal method");
a61af66fc99e Initial load
duke
parents:
diff changeset
182 assert(data != NULL, "worker data is null");
a61af66fc99e Initial load
duke
parents:
diff changeset
183 data->set_terminate(terminate());
a61af66fc99e Initial load
duke
parents:
diff changeset
184 data->set_task(task());
a61af66fc99e Initial load
duke
parents:
diff changeset
185 data->set_sequence_number(sequence_number());
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 void AbstractWorkGang::internal_note_start() {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 assert(monitor()->owned_by_self(), "note_finish is an internal method");
a61af66fc99e Initial load
duke
parents:
diff changeset
190 _started_workers += 1;
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 AbstractWorkGang::internal_note_finish() {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 assert(monitor()->owned_by_self(), "note_finish is an internal method");
a61af66fc99e Initial load
duke
parents:
diff changeset
195 _finished_workers += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 uint num_thr = total_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
200 for (uint i = 0; i < num_thr; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 gang_worker(i)->print_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
202 st->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204 }
a61af66fc99e Initial load
duke
parents:
diff changeset
205
a61af66fc99e Initial load
duke
parents:
diff changeset
206 void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 assert(tc != NULL, "Null ThreadClosure");
a61af66fc99e Initial load
duke
parents:
diff changeset
208 uint num_thr = total_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
209 for (uint i = 0; i < num_thr; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 tc->do_thread(gang_worker(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }
a61af66fc99e Initial load
duke
parents:
diff changeset
213
a61af66fc99e Initial load
duke
parents:
diff changeset
214 // GangWorker methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 _gang = gang;
a61af66fc99e Initial load
duke
parents:
diff changeset
218 set_id(id);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 set_name("Gang worker#%d (%s)", id, gang->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
220 }
a61af66fc99e Initial load
duke
parents:
diff changeset
221
a61af66fc99e Initial load
duke
parents:
diff changeset
222 void GangWorker::run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
224 loop();
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 void GangWorker::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 this->initialize_thread_local_storage();
a61af66fc99e Initial load
duke
parents:
diff changeset
229 assert(_gang != NULL, "No gang to run in");
a61af66fc99e Initial load
duke
parents:
diff changeset
230 os::set_priority(this, NearMaxPriority);
a61af66fc99e Initial load
duke
parents:
diff changeset
231 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 tty->print_cr("Running gang worker for gang %s id %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
233 gang()->name(), id());
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 // The VM thread should not execute here because MutexLocker's are used
a61af66fc99e Initial load
duke
parents:
diff changeset
236 // as (opposed to MutexLockerEx's).
a61af66fc99e Initial load
duke
parents:
diff changeset
237 assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
a61af66fc99e Initial load
duke
parents:
diff changeset
238 " of a work gang");
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240
a61af66fc99e Initial load
duke
parents:
diff changeset
241 void GangWorker::loop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 int previous_sequence_number = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
243 Monitor* gang_monitor = gang()->monitor();
a61af66fc99e Initial load
duke
parents:
diff changeset
244 for ( ; /* !terminate() */; ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 WorkData data;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 int part; // Initialized below.
a61af66fc99e Initial load
duke
parents:
diff changeset
247 {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // Grab the gang mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
249 MutexLocker ml(gang_monitor);
a61af66fc99e Initial load
duke
parents:
diff changeset
250 // Wait for something to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
251 // Polling outside the while { wait } avoids missed notifies
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // in the outer loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
253 gang()->internal_worker_poll(&data);
a61af66fc99e Initial load
duke
parents:
diff changeset
254 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 tty->print("Polled outside for work in gang %s worker %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
256 gang()->name(), id());
a61af66fc99e Initial load
duke
parents:
diff changeset
257 tty->print(" terminate: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
258 data.terminate() ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
259 tty->print(" sequence: %d (prev: %d)",
a61af66fc99e Initial load
duke
parents:
diff changeset
260 data.sequence_number(), previous_sequence_number);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 if (data.task() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 tty->print(" task: %s", data.task()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
263 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 tty->print(" task: NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 for ( ; /* break or return */; ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // Terminate if requested.
a61af66fc99e Initial load
duke
parents:
diff changeset
270 if (data.terminate()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
271 gang()->internal_note_finish();
a61af66fc99e Initial load
duke
parents:
diff changeset
272 gang_monitor->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
273 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
274 }
a61af66fc99e Initial load
duke
parents:
diff changeset
275 // Check for new work.
a61af66fc99e Initial load
duke
parents:
diff changeset
276 if ((data.task() != NULL) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
277 (data.sequence_number() != previous_sequence_number)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 gang()->internal_note_start();
a61af66fc99e Initial load
duke
parents:
diff changeset
279 gang_monitor->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
280 part = gang()->started_workers() - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
282 }
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
284 gang_monitor->wait(/* no_safepoint_check */ true);
a61af66fc99e Initial load
duke
parents:
diff changeset
285 gang()->internal_worker_poll(&data);
a61af66fc99e Initial load
duke
parents:
diff changeset
286 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 tty->print("Polled inside for work in gang %s worker %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
288 gang()->name(), id());
a61af66fc99e Initial load
duke
parents:
diff changeset
289 tty->print(" terminate: %s",
a61af66fc99e Initial load
duke
parents:
diff changeset
290 data.terminate() ? "true" : "false");
a61af66fc99e Initial load
duke
parents:
diff changeset
291 tty->print(" sequence: %d (prev: %d)",
a61af66fc99e Initial load
duke
parents:
diff changeset
292 data.sequence_number(), previous_sequence_number);
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if (data.task() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 tty->print(" task: %s", data.task()->name());
a61af66fc99e Initial load
duke
parents:
diff changeset
295 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
296 tty->print(" task: NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
297 }
a61af66fc99e Initial load
duke
parents:
diff changeset
298 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301 // Drop gang mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
302 }
a61af66fc99e Initial load
duke
parents:
diff changeset
303 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 tty->print("Work for work gang %s id %d task %s part %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
305 gang()->name(), id(), data.task()->name(), part);
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 assert(data.task() != NULL, "Got null task");
a61af66fc99e Initial load
duke
parents:
diff changeset
308 data.task()->work(part);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 {
a61af66fc99e Initial load
duke
parents:
diff changeset
310 if (TraceWorkGang) {
a61af66fc99e Initial load
duke
parents:
diff changeset
311 tty->print("Finish for work gang %s id %d task %s part %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
312 gang()->name(), id(), data.task()->name(), part);
a61af66fc99e Initial load
duke
parents:
diff changeset
313 }
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // Grab the gang mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
315 MutexLocker ml(gang_monitor);
a61af66fc99e Initial load
duke
parents:
diff changeset
316 gang()->internal_note_finish();
a61af66fc99e Initial load
duke
parents:
diff changeset
317 // Tell the gang you are done.
a61af66fc99e Initial load
duke
parents:
diff changeset
318 gang_monitor->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
319 // Drop the gang mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
320 }
a61af66fc99e Initial load
duke
parents:
diff changeset
321 previous_sequence_number = data.sequence_number();
a61af66fc99e Initial load
duke
parents:
diff changeset
322 }
a61af66fc99e Initial load
duke
parents:
diff changeset
323 }
a61af66fc99e Initial load
duke
parents:
diff changeset
324
a61af66fc99e Initial load
duke
parents:
diff changeset
325 bool GangWorker::is_GC_task_thread() const {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
326 return gang()->are_GC_task_threads();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
327 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
328
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
329 bool GangWorker::is_ConcurrentGC_thread() const {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
330 return gang()->are_ConcurrentGC_threads();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
331 }
a61af66fc99e Initial load
duke
parents:
diff changeset
332
a61af66fc99e Initial load
duke
parents:
diff changeset
333 void GangWorker::print_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 st->print("\"%s\" ", name());
a61af66fc99e Initial load
duke
parents:
diff changeset
335 Thread::print_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
336 st->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
337 }
a61af66fc99e Initial load
duke
parents:
diff changeset
338
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // Printing methods
a61af66fc99e Initial load
duke
parents:
diff changeset
340
a61af66fc99e Initial load
duke
parents:
diff changeset
341 const char* AbstractWorkGang::name() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
342 return _name;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344
a61af66fc99e Initial load
duke
parents:
diff changeset
345 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
346
a61af66fc99e Initial load
duke
parents:
diff changeset
347 const char* AbstractGangTask::name() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
348 return _name;
a61af66fc99e Initial load
duke
parents:
diff changeset
349 }
a61af66fc99e Initial load
duke
parents:
diff changeset
350
a61af66fc99e Initial load
duke
parents:
diff changeset
351 #endif /* PRODUCT */
a61af66fc99e Initial load
duke
parents:
diff changeset
352
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // *** WorkGangBarrierSync
a61af66fc99e Initial load
duke
parents:
diff changeset
354
a61af66fc99e Initial load
duke
parents:
diff changeset
355 WorkGangBarrierSync::WorkGangBarrierSync()
a61af66fc99e Initial load
duke
parents:
diff changeset
356 : _monitor(Mutex::safepoint, "work gang barrier sync", true),
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
357 _n_workers(0), _n_completed(0), _should_reset(false) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
358 }
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 WorkGangBarrierSync::WorkGangBarrierSync(int n_workers, const char* name)
a61af66fc99e Initial load
duke
parents:
diff changeset
361 : _monitor(Mutex::safepoint, name, true),
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
362 _n_workers(n_workers), _n_completed(0), _should_reset(false) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
363 }
a61af66fc99e Initial load
duke
parents:
diff changeset
364
a61af66fc99e Initial load
duke
parents:
diff changeset
365 void WorkGangBarrierSync::set_n_workers(int n_workers) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 _n_workers = n_workers;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 _n_completed = 0;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
368 _should_reset = false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
369 }
a61af66fc99e Initial load
duke
parents:
diff changeset
370
a61af66fc99e Initial load
duke
parents:
diff changeset
371 void WorkGangBarrierSync::enter() {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
373 if (should_reset()) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
374 // The should_reset() was set and we are the first worker to enter
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
375 // the sync barrier. We will zero the n_completed() count which
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
376 // effectively resets the barrier.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
377 zero_completed();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
378 set_should_reset(false);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
379 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
380 inc_completed();
a61af66fc99e Initial load
duke
parents:
diff changeset
381 if (n_completed() == n_workers()) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
382 // At this point we would like to reset the barrier to be ready in
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
383 // case it is used again. However, we cannot set n_completed() to
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
384 // 0, even after the notify_all(), given that some other workers
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
385 // might still be waiting for n_completed() to become ==
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
386 // n_workers(). So, if we set n_completed() to 0, those workers
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
387 // will get stuck (as they will wake up, see that n_completed() !=
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
388 // n_workers() and go back to sleep). Instead, we raise the
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
389 // should_reset() flag and the barrier will be reset the first
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
390 // time a worker enters it again.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
391 set_should_reset(true);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
392 monitor()->notify_all();
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
393 } else {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
394 while (n_completed() != n_workers()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 monitor()->wait(/* no_safepoint_check */ true);
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397 }
a61af66fc99e Initial load
duke
parents:
diff changeset
398 }
a61af66fc99e Initial load
duke
parents:
diff changeset
399
a61af66fc99e Initial load
duke
parents:
diff changeset
400 // SubTasksDone functions.
a61af66fc99e Initial load
duke
parents:
diff changeset
401
a61af66fc99e Initial load
duke
parents:
diff changeset
402 SubTasksDone::SubTasksDone(int n) :
a61af66fc99e Initial load
duke
parents:
diff changeset
403 _n_tasks(n), _n_threads(1), _tasks(NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
404 _tasks = NEW_C_HEAP_ARRAY(jint, n);
a61af66fc99e Initial load
duke
parents:
diff changeset
405 guarantee(_tasks != NULL, "alloc failure");
a61af66fc99e Initial load
duke
parents:
diff changeset
406 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
407 }
a61af66fc99e Initial load
duke
parents:
diff changeset
408
a61af66fc99e Initial load
duke
parents:
diff changeset
409 bool SubTasksDone::valid() {
a61af66fc99e Initial load
duke
parents:
diff changeset
410 return _tasks != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
411 }
a61af66fc99e Initial load
duke
parents:
diff changeset
412
1833
8b10f48633dc 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 1552
diff changeset
413 void SubTasksDone::set_n_threads(int t) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
414 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
415 assert(_claimed == 0 || _threads_completed == _n_threads,
a61af66fc99e Initial load
duke
parents:
diff changeset
416 "should not be called while tasks are being processed!");
a61af66fc99e Initial load
duke
parents:
diff changeset
417 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
418 _n_threads = (t == 0 ? 1 : t);
a61af66fc99e Initial load
duke
parents:
diff changeset
419 }
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 void SubTasksDone::clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 for (int i = 0; i < _n_tasks; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 _tasks[i] = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
424 }
a61af66fc99e Initial load
duke
parents:
diff changeset
425 _threads_completed = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
426 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
427 _claimed = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430
a61af66fc99e Initial load
duke
parents:
diff changeset
431 bool SubTasksDone::is_task_claimed(int t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
432 assert(0 <= t && t < _n_tasks, "bad task id.");
a61af66fc99e Initial load
duke
parents:
diff changeset
433 jint old = _tasks[t];
a61af66fc99e Initial load
duke
parents:
diff changeset
434 if (old == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 old = Atomic::cmpxchg(1, &_tasks[t], 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
a61af66fc99e Initial load
duke
parents:
diff changeset
437 assert(_tasks[t] == 1, "What else?");
a61af66fc99e Initial load
duke
parents:
diff changeset
438 bool res = old != 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
439 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
440 if (!res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
441 assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
a61af66fc99e Initial load
duke
parents:
diff changeset
442 Atomic::inc(&_claimed);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 }
a61af66fc99e Initial load
duke
parents:
diff changeset
444 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
445 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 }
a61af66fc99e Initial load
duke
parents:
diff changeset
447
a61af66fc99e Initial load
duke
parents:
diff changeset
448 void SubTasksDone::all_tasks_completed() {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 jint observed = _threads_completed;
a61af66fc99e Initial load
duke
parents:
diff changeset
450 jint old;
a61af66fc99e Initial load
duke
parents:
diff changeset
451 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
452 old = observed;
a61af66fc99e Initial load
duke
parents:
diff changeset
453 observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
a61af66fc99e Initial load
duke
parents:
diff changeset
454 } while (observed != old);
a61af66fc99e Initial load
duke
parents:
diff changeset
455 // If this was the last thread checking in, clear the tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
456 if (observed+1 == _n_threads) clear();
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 SubTasksDone::~SubTasksDone() {
a61af66fc99e Initial load
duke
parents:
diff changeset
461 if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
a61af66fc99e Initial load
duke
parents:
diff changeset
462 }
a61af66fc99e Initial load
duke
parents:
diff changeset
463
a61af66fc99e Initial load
duke
parents:
diff changeset
464 // *** SequentialSubTasksDone
a61af66fc99e Initial load
duke
parents:
diff changeset
465
a61af66fc99e Initial load
duke
parents:
diff changeset
466 void SequentialSubTasksDone::clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 _n_tasks = _n_claimed = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 _n_threads = _n_completed = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
469 }
a61af66fc99e Initial load
duke
parents:
diff changeset
470
a61af66fc99e Initial load
duke
parents:
diff changeset
471 bool SequentialSubTasksDone::valid() {
a61af66fc99e Initial load
duke
parents:
diff changeset
472 return _n_threads > 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
473 }
a61af66fc99e Initial load
duke
parents:
diff changeset
474
a61af66fc99e Initial load
duke
parents:
diff changeset
475 bool SequentialSubTasksDone::is_task_claimed(int& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
476 jint* n_claimed_ptr = &_n_claimed;
a61af66fc99e Initial load
duke
parents:
diff changeset
477 t = *n_claimed_ptr;
a61af66fc99e Initial load
duke
parents:
diff changeset
478 while (t < _n_tasks) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
a61af66fc99e Initial load
duke
parents:
diff changeset
480 if (res == t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
481 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483 t = *n_claimed_ptr;
a61af66fc99e Initial load
duke
parents:
diff changeset
484 }
a61af66fc99e Initial load
duke
parents:
diff changeset
485 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
486 }
a61af66fc99e Initial load
duke
parents:
diff changeset
487
a61af66fc99e Initial load
duke
parents:
diff changeset
488 bool SequentialSubTasksDone::all_tasks_completed() {
a61af66fc99e Initial load
duke
parents:
diff changeset
489 jint* n_completed_ptr = &_n_completed;
a61af66fc99e Initial load
duke
parents:
diff changeset
490 jint complete = *n_completed_ptr;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
492 jint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
a61af66fc99e Initial load
duke
parents:
diff changeset
493 if (res == complete) {
a61af66fc99e Initial load
duke
parents:
diff changeset
494 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 }
a61af66fc99e Initial load
duke
parents:
diff changeset
496 complete = res;
a61af66fc99e Initial load
duke
parents:
diff changeset
497 }
a61af66fc99e Initial load
duke
parents:
diff changeset
498 if (complete+1 == _n_threads) {
a61af66fc99e Initial load
duke
parents:
diff changeset
499 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
500 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
501 }
a61af66fc99e Initial load
duke
parents:
diff changeset
502 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
503 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
504
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
505 bool FreeIdSet::_stat_init = false;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
506 FreeIdSet* FreeIdSet::_sets[NSets];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
507 bool FreeIdSet::_safepoint;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
508
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
509 FreeIdSet::FreeIdSet(int sz, Monitor* mon) :
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
510 _sz(sz), _mon(mon), _hd(0), _waiters(0), _index(-1), _claimed(0)
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
511 {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
512 _ids = new int[sz];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
513 for (int i = 0; i < sz; i++) _ids[i] = i+1;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
514 _ids[sz-1] = end_of_list; // end of list.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
515 if (_stat_init) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
516 for (int j = 0; j < NSets; j++) _sets[j] = NULL;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
517 _stat_init = true;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
518 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
519 // Add to sets. (This should happen while the system is still single-threaded.)
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
520 for (int j = 0; j < NSets; j++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
521 if (_sets[j] == NULL) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
522 _sets[j] = this;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
523 _index = j;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
524 break;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
525 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
526 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
527 guarantee(_index != -1, "Too many FreeIdSets in use!");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
528 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
529
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
530 FreeIdSet::~FreeIdSet() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
531 _sets[_index] = NULL;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
532 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
533
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
534 void FreeIdSet::set_safepoint(bool b) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
535 _safepoint = b;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
536 if (b) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
537 for (int j = 0; j < NSets; j++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
538 if (_sets[j] != NULL && _sets[j]->_waiters > 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
539 Monitor* mon = _sets[j]->_mon;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
540 mon->lock_without_safepoint_check();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
541 mon->notify_all();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
542 mon->unlock();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
543 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
544 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
545 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
546 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
547
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
548 #define FID_STATS 0
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
549
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
550 int FreeIdSet::claim_par_id() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
551 #if FID_STATS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
552 thread_t tslf = thr_self();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
553 tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
554 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
555 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
556 while (!_safepoint && _hd == end_of_list) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
557 _waiters++;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
558 #if FID_STATS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
559 if (_waiters > 5) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
560 tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n",
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
561 tslf, _waiters, _claimed);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
562 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
563 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
564 _mon->wait(Mutex::_no_safepoint_check_flag);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
565 _waiters--;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
566 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
567 if (_hd == end_of_list) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
568 #if FID_STATS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
569 tty->print("claim_par_id[%d]: returning EOL.\n", tslf);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
570 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
571 return -1;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
572 } else {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
573 int res = _hd;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
574 _hd = _ids[res];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
575 _ids[res] = claimed; // For debugging.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
576 _claimed++;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
577 #if FID_STATS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
578 tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n",
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
579 tslf, res, _claimed);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
580 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
581 return res;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
582 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
583 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
584
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
585 bool FreeIdSet::claim_perm_id(int i) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
586 assert(0 <= i && i < _sz, "Out of range.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
587 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
588 int prev = end_of_list;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
589 int cur = _hd;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
590 while (cur != end_of_list) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
591 if (cur == i) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
592 if (prev == end_of_list) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
593 _hd = _ids[cur];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
594 } else {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
595 _ids[prev] = _ids[cur];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
596 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
597 _ids[cur] = claimed;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
598 _claimed++;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
599 return true;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
600 } else {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
601 prev = cur;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
602 cur = _ids[cur];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
603 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
604 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
605 return false;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
606
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
607 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
608
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
609 void FreeIdSet::release_par_id(int id) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
610 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
611 assert(_ids[id] == claimed, "Precondition.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
612 _ids[id] = _hd;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
613 _hd = id;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
614 _claimed--;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
615 #if FID_STATS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
616 tty->print("[%d] release_par_id(%d), waiters =%d, claimed = %d.\n",
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
617 thr_self(), id, _waiters, _claimed);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
618 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
619 if (_waiters > 0)
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
620 // Notify all would be safer, but this is OK, right?
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
621 _mon->notify_all();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
622 }