annotate src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.hpp @ 269:850fdf70db2b

Merge
author jmasa
date Mon, 28 Jul 2008 15:30:23 -0700
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 //
a61af66fc99e Initial load
duke
parents:
diff changeset
26 // The GCTaskManager is a queue of GCTasks, and accessors
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // to allow the queue to be accessed from many threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
28 //
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // Forward declarations of types defined in this file.
a61af66fc99e Initial load
duke
parents:
diff changeset
31 class GCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 class GCTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 class SynchronizedGCTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 class GCTaskManager;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 class NotifyDoneClosure;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // Some useful subclasses of GCTask. You can also make up your own.
a61af66fc99e Initial load
duke
parents:
diff changeset
37 class NoopGCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 class BarrierGCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 class ReleasingBarrierGCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 class NotifyingBarrierGCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 class WaitForBarrierGCTask;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // A free list of Monitor*'s.
a61af66fc99e Initial load
duke
parents:
diff changeset
43 class MonitorSupply;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // Forward declarations of classes referenced in this file via pointer.
a61af66fc99e Initial load
duke
parents:
diff changeset
46 class GCTaskThread;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 class Mutex;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 class Monitor;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 class ThreadClosure;
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // The abstract base GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
52 class GCTask : public ResourceObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
53 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // Known kinds of GCTasks, for predicates.
a61af66fc99e Initial load
duke
parents:
diff changeset
55 class Kind : AllStatic {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
57 enum kind {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 unknown_task,
a61af66fc99e Initial load
duke
parents:
diff changeset
59 ordinary_task,
a61af66fc99e Initial load
duke
parents:
diff changeset
60 barrier_task,
a61af66fc99e Initial load
duke
parents:
diff changeset
61 noop_task
a61af66fc99e Initial load
duke
parents:
diff changeset
62 };
a61af66fc99e Initial load
duke
parents:
diff changeset
63 static const char* to_string(kind value);
a61af66fc99e Initial load
duke
parents:
diff changeset
64 };
a61af66fc99e Initial load
duke
parents:
diff changeset
65 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
67 const Kind::kind _kind; // For runtime type checking.
a61af66fc99e Initial load
duke
parents:
diff changeset
68 const uint _affinity; // Which worker should run task.
a61af66fc99e Initial load
duke
parents:
diff changeset
69 GCTask* _newer; // Tasks are on doubly-linked ...
a61af66fc99e Initial load
duke
parents:
diff changeset
70 GCTask* _older; // ... lists.
a61af66fc99e Initial load
duke
parents:
diff changeset
71 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
72 virtual char* name() { return (char *)"task"; }
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // Abstract do_it method
a61af66fc99e Initial load
duke
parents:
diff changeset
75 virtual void do_it(GCTaskManager* manager, uint which) = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 // Accessors
a61af66fc99e Initial load
duke
parents:
diff changeset
77 Kind::kind kind() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 return _kind;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 uint affinity() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 return _affinity;
a61af66fc99e Initial load
duke
parents:
diff changeset
82 }
a61af66fc99e Initial load
duke
parents:
diff changeset
83 GCTask* newer() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 return _newer;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 void set_newer(GCTask* n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 _newer = n;
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89 GCTask* older() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 return _older;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92 void set_older(GCTask* p) {
a61af66fc99e Initial load
duke
parents:
diff changeset
93 _older = p;
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95 // Predicates.
a61af66fc99e Initial load
duke
parents:
diff changeset
96 bool is_ordinary_task() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 return kind()==Kind::ordinary_task;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 bool is_barrier_task() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return kind()==Kind::barrier_task;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102 bool is_noop_task() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 return kind()==Kind::noop_task;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 void print(const char* message) const PRODUCT_RETURN;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
107 // Constructors: Only create subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // An ordinary GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
109 GCTask();
a61af66fc99e Initial load
duke
parents:
diff changeset
110 // A GCTask of a particular kind, usually barrier or noop.
a61af66fc99e Initial load
duke
parents:
diff changeset
111 GCTask(Kind::kind kind);
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // An ordinary GCTask with an affinity.
a61af66fc99e Initial load
duke
parents:
diff changeset
113 GCTask(uint affinity);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // A GCTask of a particular kind, with and affinity.
a61af66fc99e Initial load
duke
parents:
diff changeset
115 GCTask(Kind::kind kind, uint affinity);
a61af66fc99e Initial load
duke
parents:
diff changeset
116 // We want a virtual destructor because virtual methods,
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // but since ResourceObj's don't have their destructors
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // called, we don't have one at all. Instead we have
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // this method, which gets called by subclasses to clean up.
a61af66fc99e Initial load
duke
parents:
diff changeset
120 virtual void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
121 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
122 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
123 };
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // A doubly-linked list of GCTasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // The list is not synchronized, because sometimes we want to
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // build up a list and then make it available to other threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // See also: SynchronizedGCTaskQueue.
a61af66fc99e Initial load
duke
parents:
diff changeset
129 class GCTaskQueue : public ResourceObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
132 GCTask* _insert_end; // Tasks are enqueued at this end.
a61af66fc99e Initial load
duke
parents:
diff changeset
133 GCTask* _remove_end; // Tasks are dequeued from this end.
a61af66fc99e Initial load
duke
parents:
diff changeset
134 uint _length; // The current length of the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
135 const bool _is_c_heap_obj; // Is this a CHeapObj?
a61af66fc99e Initial load
duke
parents:
diff changeset
136 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // Create as ResourceObj.
a61af66fc99e Initial load
duke
parents:
diff changeset
139 static GCTaskQueue* create();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // Create as CHeapObj.
a61af66fc99e Initial load
duke
parents:
diff changeset
141 static GCTaskQueue* create_on_c_heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // Destroyer.
a61af66fc99e Initial load
duke
parents:
diff changeset
143 static void destroy(GCTaskQueue* that);
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // These just examine the state of the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
146 bool is_empty() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 assert(((insert_end() == NULL && remove_end() == NULL) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
148 (insert_end() != NULL && remove_end() != NULL)),
a61af66fc99e Initial load
duke
parents:
diff changeset
149 "insert_end and remove_end don't match");
a61af66fc99e Initial load
duke
parents:
diff changeset
150 return insert_end() == NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 uint length() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 return _length;
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // Enqueue one task.
a61af66fc99e Initial load
duke
parents:
diff changeset
157 void enqueue(GCTask* task);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 // Enqueue a list of tasks. Empties the argument list.
a61af66fc99e Initial load
duke
parents:
diff changeset
159 void enqueue(GCTaskQueue* list);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // Dequeue one task.
a61af66fc99e Initial load
duke
parents:
diff changeset
161 GCTask* dequeue();
a61af66fc99e Initial load
duke
parents:
diff changeset
162 // Dequeue one task, preferring one with affinity.
a61af66fc99e Initial load
duke
parents:
diff changeset
163 GCTask* dequeue(uint affinity);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
166 GCTaskQueue(bool on_c_heap);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // Because ResourceMark doesn't call destructors.
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // This method cleans up like one.
a61af66fc99e Initial load
duke
parents:
diff changeset
170 virtual void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
172 GCTask* insert_end() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 return _insert_end;
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175 void set_insert_end(GCTask* value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 _insert_end = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178 GCTask* remove_end() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 return _remove_end;
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 void set_remove_end(GCTask* value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 _remove_end = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 void increment_length() {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 _length += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187 void decrement_length() {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 _length -= 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 void set_length(uint value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 _length = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193 bool is_c_heap_obj() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return _is_c_heap_obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
197 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
198 GCTask* remove(); // Remove from remove end.
a61af66fc99e Initial load
duke
parents:
diff changeset
199 GCTask* remove(GCTask* task); // Remove from the middle.
a61af66fc99e Initial load
duke
parents:
diff changeset
200 void print(const char* message) const PRODUCT_RETURN;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 };
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // A GCTaskQueue that can be synchronized.
a61af66fc99e Initial load
duke
parents:
diff changeset
204 // This "has-a" GCTaskQueue and a mutex to do the exclusion.
a61af66fc99e Initial load
duke
parents:
diff changeset
205 class SynchronizedGCTaskQueue : public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
206 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
208 GCTaskQueue* _unsynchronized_queue; // Has-a unsynchronized queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
209 Monitor * _lock; // Lock to control access.
a61af66fc99e Initial load
duke
parents:
diff changeset
210 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
212 static SynchronizedGCTaskQueue* create(GCTaskQueue* queue, Monitor * lock) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 return new SynchronizedGCTaskQueue(queue, lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 static void destroy(SynchronizedGCTaskQueue* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 delete that;
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220 // Accessors
a61af66fc99e Initial load
duke
parents:
diff changeset
221 GCTaskQueue* unsynchronized_queue() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
222 return _unsynchronized_queue;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }
a61af66fc99e Initial load
duke
parents:
diff changeset
224 Monitor * lock() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 return _lock;
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227 // GCTaskQueue wrapper methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
228 // These check that you hold the lock
a61af66fc99e Initial load
duke
parents:
diff changeset
229 // and then call the method on the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
230 bool is_empty() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
231 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
232 return unsynchronized_queue()->is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
233 }
a61af66fc99e Initial load
duke
parents:
diff changeset
234 void enqueue(GCTask* task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
235 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
236 unsynchronized_queue()->enqueue(task);
a61af66fc99e Initial load
duke
parents:
diff changeset
237 }
a61af66fc99e Initial load
duke
parents:
diff changeset
238 void enqueue(GCTaskQueue* list) {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
240 unsynchronized_queue()->enqueue(list);
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242 GCTask* dequeue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
244 return unsynchronized_queue()->dequeue();
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246 GCTask* dequeue(uint affinity) {
a61af66fc99e Initial load
duke
parents:
diff changeset
247 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
248 return unsynchronized_queue()->dequeue(affinity);
a61af66fc99e Initial load
duke
parents:
diff changeset
249 }
a61af66fc99e Initial load
duke
parents:
diff changeset
250 uint length() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 guarantee(own_lock(), "don't own the lock");
a61af66fc99e Initial load
duke
parents:
diff changeset
252 return unsynchronized_queue()->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
253 }
a61af66fc99e Initial load
duke
parents:
diff changeset
254 // For guarantees.
a61af66fc99e Initial load
duke
parents:
diff changeset
255 bool own_lock() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return lock()->owned_by_self();
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
260 SynchronizedGCTaskQueue(GCTaskQueue* queue, Monitor * lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // Destructor. Not virtual because no virtuals.
a61af66fc99e Initial load
duke
parents:
diff changeset
262 ~SynchronizedGCTaskQueue();
a61af66fc99e Initial load
duke
parents:
diff changeset
263 };
a61af66fc99e Initial load
duke
parents:
diff changeset
264
a61af66fc99e Initial load
duke
parents:
diff changeset
265 // This is an abstract base class for getting notifications
a61af66fc99e Initial load
duke
parents:
diff changeset
266 // when a GCTaskManager is done.
a61af66fc99e Initial load
duke
parents:
diff changeset
267 class NotifyDoneClosure : public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // The notification callback method.
a61af66fc99e Initial load
duke
parents:
diff changeset
270 virtual void notify(GCTaskManager* manager) = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
271 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
272 // Constructor.
a61af66fc99e Initial load
duke
parents:
diff changeset
273 NotifyDoneClosure() {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276 // Virtual destructor because virtual methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
277 virtual ~NotifyDoneClosure() {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
280 };
a61af66fc99e Initial load
duke
parents:
diff changeset
281
a61af66fc99e Initial load
duke
parents:
diff changeset
282 class GCTaskManager : public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
283 friend class ParCompactionManager;
a61af66fc99e Initial load
duke
parents:
diff changeset
284 friend class PSParallelCompact;
a61af66fc99e Initial load
duke
parents:
diff changeset
285 friend class PSScavenge;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 friend class PSRefProcTaskExecutor;
a61af66fc99e Initial load
duke
parents:
diff changeset
287 friend class RefProcTaskExecutor;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
289 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
290 NotifyDoneClosure* _ndc; // Notify on completion.
a61af66fc99e Initial load
duke
parents:
diff changeset
291 const uint _workers; // Number of workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
292 Monitor* _monitor; // Notification of changes.
a61af66fc99e Initial load
duke
parents:
diff changeset
293 SynchronizedGCTaskQueue* _queue; // Queue of tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
294 GCTaskThread** _thread; // Array of worker threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
295 uint _busy_workers; // Number of busy workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
296 uint _blocking_worker; // The worker that's blocking.
a61af66fc99e Initial load
duke
parents:
diff changeset
297 bool* _resource_flag; // Array of flag per threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
298 uint _delivered_tasks; // Count of delivered tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
299 uint _completed_tasks; // Count of completed tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
300 uint _barriers; // Count of barrier tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
301 uint _emptied_queue; // Times we emptied the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
302 NoopGCTask* _noop_task; // The NoopGCTask instance.
a61af66fc99e Initial load
duke
parents:
diff changeset
303 uint _noop_tasks; // Count of noop tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
304 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
305 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
306 static GCTaskManager* create(uint workers) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 return new GCTaskManager(workers);
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309 static GCTaskManager* create(uint workers, NotifyDoneClosure* ndc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
310 return new GCTaskManager(workers, ndc);
a61af66fc99e Initial load
duke
parents:
diff changeset
311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
312 static void destroy(GCTaskManager* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 delete that;
a61af66fc99e Initial load
duke
parents:
diff changeset
315 }
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
318 uint busy_workers() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 return _busy_workers;
a61af66fc99e Initial load
duke
parents:
diff changeset
320 }
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // Pun between Monitor* and Mutex*
a61af66fc99e Initial load
duke
parents:
diff changeset
322 Monitor* monitor() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 return _monitor;
a61af66fc99e Initial load
duke
parents:
diff changeset
324 }
a61af66fc99e Initial load
duke
parents:
diff changeset
325 Monitor * lock() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 return _monitor;
a61af66fc99e Initial load
duke
parents:
diff changeset
327 }
a61af66fc99e Initial load
duke
parents:
diff changeset
328 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
329 // Add the argument task to be run.
a61af66fc99e Initial load
duke
parents:
diff changeset
330 void add_task(GCTask* task);
a61af66fc99e Initial load
duke
parents:
diff changeset
331 // Add a list of tasks. Removes task from the argument list.
a61af66fc99e Initial load
duke
parents:
diff changeset
332 void add_list(GCTaskQueue* list);
a61af66fc99e Initial load
duke
parents:
diff changeset
333 // Claim a task for argument worker.
a61af66fc99e Initial load
duke
parents:
diff changeset
334 GCTask* get_task(uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
335 // Note the completion of a task by the argument worker.
a61af66fc99e Initial load
duke
parents:
diff changeset
336 void note_completion(uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // Is the queue blocked from handing out new tasks?
a61af66fc99e Initial load
duke
parents:
diff changeset
338 bool is_blocked() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 return (blocking_worker() != sentinel_worker());
a61af66fc99e Initial load
duke
parents:
diff changeset
340 }
a61af66fc99e Initial load
duke
parents:
diff changeset
341 // Request that all workers release their resources.
a61af66fc99e Initial load
duke
parents:
diff changeset
342 void release_all_resources();
a61af66fc99e Initial load
duke
parents:
diff changeset
343 // Ask if a particular worker should release its resources.
a61af66fc99e Initial load
duke
parents:
diff changeset
344 bool should_release_resources(uint which); // Predicate.
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // Note the release of resources by the argument worker.
a61af66fc99e Initial load
duke
parents:
diff changeset
346 void note_release(uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
347 // Constants.
a61af66fc99e Initial load
duke
parents:
diff changeset
348 // A sentinel worker identifier.
a61af66fc99e Initial load
duke
parents:
diff changeset
349 static uint sentinel_worker() {
a61af66fc99e Initial load
duke
parents:
diff changeset
350 return (uint) -1; // Why isn't there a max_uint?
a61af66fc99e Initial load
duke
parents:
diff changeset
351 }
a61af66fc99e Initial load
duke
parents:
diff changeset
352
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // Execute the task queue and wait for the completion.
a61af66fc99e Initial load
duke
parents:
diff changeset
354 void execute_and_wait(GCTaskQueue* list);
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356 void print_task_time_stamps();
a61af66fc99e Initial load
duke
parents:
diff changeset
357 void print_threads_on(outputStream* st);
a61af66fc99e Initial load
duke
parents:
diff changeset
358 void threads_do(ThreadClosure* tc);
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // Constructors. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // Create a GCTaskManager with the appropriate number of workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 GCTaskManager(uint workers);
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // Create a GCTaskManager that calls back when there's no more work.
a61af66fc99e Initial load
duke
parents:
diff changeset
365 GCTaskManager(uint workers, NotifyDoneClosure* ndc);
a61af66fc99e Initial load
duke
parents:
diff changeset
366 // Make virtual if necessary.
a61af66fc99e Initial load
duke
parents:
diff changeset
367 ~GCTaskManager();
a61af66fc99e Initial load
duke
parents:
diff changeset
368 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
369 uint workers() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 return _workers;
a61af66fc99e Initial load
duke
parents:
diff changeset
371 }
a61af66fc99e Initial load
duke
parents:
diff changeset
372 NotifyDoneClosure* notify_done_closure() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 return _ndc;
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375 SynchronizedGCTaskQueue* queue() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 return _queue;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 NoopGCTask* noop_task() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
379 return _noop_task;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }
a61af66fc99e Initial load
duke
parents:
diff changeset
381 // Bounds-checking per-thread data accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
382 GCTaskThread* thread(uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
383 void set_thread(uint which, GCTaskThread* value);
a61af66fc99e Initial load
duke
parents:
diff changeset
384 bool resource_flag(uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
385 void set_resource_flag(uint which, bool value);
a61af66fc99e Initial load
duke
parents:
diff changeset
386 // Modifier methods with some semantics.
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // Is any worker blocking handing out new tasks?
a61af66fc99e Initial load
duke
parents:
diff changeset
388 uint blocking_worker() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 return _blocking_worker;
a61af66fc99e Initial load
duke
parents:
diff changeset
390 }
a61af66fc99e Initial load
duke
parents:
diff changeset
391 void set_blocking_worker(uint value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
392 _blocking_worker = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
393 }
a61af66fc99e Initial load
duke
parents:
diff changeset
394 void set_unblocked() {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 set_blocking_worker(sentinel_worker());
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // Count of busy workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
398 void reset_busy_workers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 _busy_workers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401 uint increment_busy_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
402 uint decrement_busy_workers();
a61af66fc99e Initial load
duke
parents:
diff changeset
403 // Count of tasks delivered to workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
404 uint delivered_tasks() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
405 return _delivered_tasks;
a61af66fc99e Initial load
duke
parents:
diff changeset
406 }
a61af66fc99e Initial load
duke
parents:
diff changeset
407 void increment_delivered_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
408 _delivered_tasks += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
409 }
a61af66fc99e Initial load
duke
parents:
diff changeset
410 void reset_delivered_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
411 _delivered_tasks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
412 }
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // Count of tasks completed by workers.
a61af66fc99e Initial load
duke
parents:
diff changeset
414 uint completed_tasks() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
415 return _completed_tasks;
a61af66fc99e Initial load
duke
parents:
diff changeset
416 }
a61af66fc99e Initial load
duke
parents:
diff changeset
417 void increment_completed_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
418 _completed_tasks += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
419 }
a61af66fc99e Initial load
duke
parents:
diff changeset
420 void reset_completed_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 _completed_tasks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
422 }
a61af66fc99e Initial load
duke
parents:
diff changeset
423 // Count of barrier tasks completed.
a61af66fc99e Initial load
duke
parents:
diff changeset
424 uint barriers() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 return _barriers;
a61af66fc99e Initial load
duke
parents:
diff changeset
426 }
a61af66fc99e Initial load
duke
parents:
diff changeset
427 void increment_barriers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
428 _barriers += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430 void reset_barriers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
431 _barriers = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
432 }
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // Count of how many times the queue has emptied.
a61af66fc99e Initial load
duke
parents:
diff changeset
434 uint emptied_queue() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 return _emptied_queue;
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
a61af66fc99e Initial load
duke
parents:
diff changeset
437 void increment_emptied_queue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
438 _emptied_queue += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
439 }
a61af66fc99e Initial load
duke
parents:
diff changeset
440 void reset_emptied_queue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
441 _emptied_queue = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
442 }
a61af66fc99e Initial load
duke
parents:
diff changeset
443 // Count of the number of noop tasks we've handed out,
a61af66fc99e Initial load
duke
parents:
diff changeset
444 // e.g., to handle resource release requests.
a61af66fc99e Initial load
duke
parents:
diff changeset
445 uint noop_tasks() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
446 return _noop_tasks;
a61af66fc99e Initial load
duke
parents:
diff changeset
447 }
a61af66fc99e Initial load
duke
parents:
diff changeset
448 void increment_noop_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 _noop_tasks += 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
450 }
a61af66fc99e Initial load
duke
parents:
diff changeset
451 void reset_noop_tasks() {
a61af66fc99e Initial load
duke
parents:
diff changeset
452 _noop_tasks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
453 }
a61af66fc99e Initial load
duke
parents:
diff changeset
454 // Other methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
455 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
456 };
a61af66fc99e Initial load
duke
parents:
diff changeset
457
a61af66fc99e Initial load
duke
parents:
diff changeset
458 //
a61af66fc99e Initial load
duke
parents:
diff changeset
459 // Some exemplary GCTasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
460 //
a61af66fc99e Initial load
duke
parents:
diff changeset
461
a61af66fc99e Initial load
duke
parents:
diff changeset
462 // A noop task that does nothing,
a61af66fc99e Initial load
duke
parents:
diff changeset
463 // except take us around the GCTaskThread loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
464 class NoopGCTask : public GCTask {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
466 const bool _is_c_heap_obj; // Is this a CHeapObj?
a61af66fc99e Initial load
duke
parents:
diff changeset
467 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
469 static NoopGCTask* create();
a61af66fc99e Initial load
duke
parents:
diff changeset
470 static NoopGCTask* create_on_c_heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
471 static void destroy(NoopGCTask* that);
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // Methods from GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
473 void do_it(GCTaskManager* manager, uint which) {
a61af66fc99e Initial load
duke
parents:
diff changeset
474 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
475 }
a61af66fc99e Initial load
duke
parents:
diff changeset
476 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
477 // Constructor.
a61af66fc99e Initial load
duke
parents:
diff changeset
478 NoopGCTask(bool on_c_heap) :
a61af66fc99e Initial load
duke
parents:
diff changeset
479 GCTask(GCTask::Kind::noop_task),
a61af66fc99e Initial load
duke
parents:
diff changeset
480 _is_c_heap_obj(on_c_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
481 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
484 void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
485 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
486 bool is_c_heap_obj() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
487 return _is_c_heap_obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
488 }
a61af66fc99e Initial load
duke
parents:
diff changeset
489 };
a61af66fc99e Initial load
duke
parents:
diff changeset
490
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // A BarrierGCTask blocks other tasks from starting,
a61af66fc99e Initial load
duke
parents:
diff changeset
492 // and waits until it is the only task running.
a61af66fc99e Initial load
duke
parents:
diff changeset
493 class BarrierGCTask : public GCTask {
a61af66fc99e Initial load
duke
parents:
diff changeset
494 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
495 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
496 static BarrierGCTask* create() {
a61af66fc99e Initial load
duke
parents:
diff changeset
497 return new BarrierGCTask();
a61af66fc99e Initial load
duke
parents:
diff changeset
498 }
a61af66fc99e Initial load
duke
parents:
diff changeset
499 static void destroy(BarrierGCTask* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
500 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
502 delete that;
a61af66fc99e Initial load
duke
parents:
diff changeset
503 }
a61af66fc99e Initial load
duke
parents:
diff changeset
504 }
a61af66fc99e Initial load
duke
parents:
diff changeset
505 // Methods from GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
506 void do_it(GCTaskManager* manager, uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
507 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
508 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
509 BarrierGCTask() :
a61af66fc99e Initial load
duke
parents:
diff changeset
510 GCTask(GCTask::Kind::barrier_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
511 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
512 }
a61af66fc99e Initial load
duke
parents:
diff changeset
513 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
514 void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
515 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
516 // Wait for this to be the only task running.
a61af66fc99e Initial load
duke
parents:
diff changeset
517 void do_it_internal(GCTaskManager* manager, uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
518 };
a61af66fc99e Initial load
duke
parents:
diff changeset
519
a61af66fc99e Initial load
duke
parents:
diff changeset
520 // A ReleasingBarrierGCTask is a BarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
521 // that tells all the tasks to release their resource areas.
a61af66fc99e Initial load
duke
parents:
diff changeset
522 class ReleasingBarrierGCTask : public BarrierGCTask {
a61af66fc99e Initial load
duke
parents:
diff changeset
523 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
524 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
525 static ReleasingBarrierGCTask* create() {
a61af66fc99e Initial load
duke
parents:
diff changeset
526 return new ReleasingBarrierGCTask();
a61af66fc99e Initial load
duke
parents:
diff changeset
527 }
a61af66fc99e Initial load
duke
parents:
diff changeset
528 static void destroy(ReleasingBarrierGCTask* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
530 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
531 delete that;
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533 }
a61af66fc99e Initial load
duke
parents:
diff changeset
534 // Methods from GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
535 void do_it(GCTaskManager* manager, uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
536 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
537 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
538 ReleasingBarrierGCTask() :
a61af66fc99e Initial load
duke
parents:
diff changeset
539 BarrierGCTask() {
a61af66fc99e Initial load
duke
parents:
diff changeset
540 // Nothing to do.
a61af66fc99e Initial load
duke
parents:
diff changeset
541 }
a61af66fc99e Initial load
duke
parents:
diff changeset
542 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
543 void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
544 };
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 // A NotifyingBarrierGCTask is a BarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // that calls a notification method when it is the only task running.
a61af66fc99e Initial load
duke
parents:
diff changeset
548 class NotifyingBarrierGCTask : public BarrierGCTask {
a61af66fc99e Initial load
duke
parents:
diff changeset
549 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
550 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
551 NotifyDoneClosure* _ndc; // The callback object.
a61af66fc99e Initial load
duke
parents:
diff changeset
552 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
554 static NotifyingBarrierGCTask* create(NotifyDoneClosure* ndc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
555 return new NotifyingBarrierGCTask(ndc);
a61af66fc99e Initial load
duke
parents:
diff changeset
556 }
a61af66fc99e Initial load
duke
parents:
diff changeset
557 static void destroy(NotifyingBarrierGCTask* that) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 if (that != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
559 that->destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
560 delete that;
a61af66fc99e Initial load
duke
parents:
diff changeset
561 }
a61af66fc99e Initial load
duke
parents:
diff changeset
562 }
a61af66fc99e Initial load
duke
parents:
diff changeset
563 // Methods from GCTask.
a61af66fc99e Initial load
duke
parents:
diff changeset
564 void do_it(GCTaskManager* manager, uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
565 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
566 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
567 NotifyingBarrierGCTask(NotifyDoneClosure* ndc) :
a61af66fc99e Initial load
duke
parents:
diff changeset
568 BarrierGCTask(),
a61af66fc99e Initial load
duke
parents:
diff changeset
569 _ndc(ndc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
570 assert(notify_done_closure() != NULL, "can't notify on NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
573 void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
574 // Accessor.
a61af66fc99e Initial load
duke
parents:
diff changeset
575 NotifyDoneClosure* notify_done_closure() const { return _ndc; }
a61af66fc99e Initial load
duke
parents:
diff changeset
576 };
a61af66fc99e Initial load
duke
parents:
diff changeset
577
a61af66fc99e Initial load
duke
parents:
diff changeset
578 // A WaitForBarrierGCTask is a BarrierGCTask
a61af66fc99e Initial load
duke
parents:
diff changeset
579 // with a method you can call to wait until
a61af66fc99e Initial load
duke
parents:
diff changeset
580 // the BarrierGCTask is done.
a61af66fc99e Initial load
duke
parents:
diff changeset
581 // This may cover many of the uses of NotifyingBarrierGCTasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
582 class WaitForBarrierGCTask : public BarrierGCTask {
a61af66fc99e Initial load
duke
parents:
diff changeset
583 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
584 // Instance state.
a61af66fc99e Initial load
duke
parents:
diff changeset
585 Monitor* _monitor; // Guard and notify changes.
a61af66fc99e Initial load
duke
parents:
diff changeset
586 bool _should_wait; // true=>wait, false=>proceed.
a61af66fc99e Initial load
duke
parents:
diff changeset
587 const bool _is_c_heap_obj; // Was allocated on the heap.
a61af66fc99e Initial load
duke
parents:
diff changeset
588 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
589 virtual char* name() { return (char *) "waitfor-barrier-task"; }
a61af66fc99e Initial load
duke
parents:
diff changeset
590
a61af66fc99e Initial load
duke
parents:
diff changeset
591 // Factory create and destroy methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
592 static WaitForBarrierGCTask* create();
a61af66fc99e Initial load
duke
parents:
diff changeset
593 static WaitForBarrierGCTask* create_on_c_heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
594 static void destroy(WaitForBarrierGCTask* that);
a61af66fc99e Initial load
duke
parents:
diff changeset
595 // Methods.
a61af66fc99e Initial load
duke
parents:
diff changeset
596 void do_it(GCTaskManager* manager, uint which);
a61af66fc99e Initial load
duke
parents:
diff changeset
597 void wait_for();
a61af66fc99e Initial load
duke
parents:
diff changeset
598 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
599 // Constructor. Clients use factory, but there might be subclasses.
a61af66fc99e Initial load
duke
parents:
diff changeset
600 WaitForBarrierGCTask(bool on_c_heap);
a61af66fc99e Initial load
duke
parents:
diff changeset
601 // Destructor-like method.
a61af66fc99e Initial load
duke
parents:
diff changeset
602 void destruct();
a61af66fc99e Initial load
duke
parents:
diff changeset
603 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
604 Monitor* monitor() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
605 return _monitor;
a61af66fc99e Initial load
duke
parents:
diff changeset
606 }
a61af66fc99e Initial load
duke
parents:
diff changeset
607 bool should_wait() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
608 return _should_wait;
a61af66fc99e Initial load
duke
parents:
diff changeset
609 }
a61af66fc99e Initial load
duke
parents:
diff changeset
610 void set_should_wait(bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
611 _should_wait = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
612 }
a61af66fc99e Initial load
duke
parents:
diff changeset
613 bool is_c_heap_obj() {
a61af66fc99e Initial load
duke
parents:
diff changeset
614 return _is_c_heap_obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
615 }
a61af66fc99e Initial load
duke
parents:
diff changeset
616 };
a61af66fc99e Initial load
duke
parents:
diff changeset
617
a61af66fc99e Initial load
duke
parents:
diff changeset
618 class MonitorSupply : public AllStatic {
a61af66fc99e Initial load
duke
parents:
diff changeset
619 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
620 // State.
a61af66fc99e Initial load
duke
parents:
diff changeset
621 // Control multi-threaded access.
a61af66fc99e Initial load
duke
parents:
diff changeset
622 static Mutex* _lock;
a61af66fc99e Initial load
duke
parents:
diff changeset
623 // The list of available Monitor*'s.
a61af66fc99e Initial load
duke
parents:
diff changeset
624 static GrowableArray<Monitor*>* _freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
625 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
626 // Reserve a Monitor*.
a61af66fc99e Initial load
duke
parents:
diff changeset
627 static Monitor* reserve();
a61af66fc99e Initial load
duke
parents:
diff changeset
628 // Release a Monitor*.
a61af66fc99e Initial load
duke
parents:
diff changeset
629 static void release(Monitor* instance);
a61af66fc99e Initial load
duke
parents:
diff changeset
630 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
631 // Accessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
632 static Mutex* lock() {
a61af66fc99e Initial load
duke
parents:
diff changeset
633 return _lock;
a61af66fc99e Initial load
duke
parents:
diff changeset
634 }
a61af66fc99e Initial load
duke
parents:
diff changeset
635 static GrowableArray<Monitor*>* freelist() {
a61af66fc99e Initial load
duke
parents:
diff changeset
636 return _freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
637 }
a61af66fc99e Initial load
duke
parents:
diff changeset
638 };