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