duke@435: /* duke@435: * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // duke@435: // Prioritized queue of VM operations. duke@435: // duke@435: // Encapsulates both queue management and duke@435: // and priority policy duke@435: // duke@435: class VMOperationQueue : public CHeapObj { duke@435: private: duke@435: enum Priorities { duke@435: SafepointPriority, // Highest priority (operation executed at a safepoint) duke@435: MediumPriority, // Medium priority duke@435: nof_priorities duke@435: }; duke@435: duke@435: // We maintain a doubled linked list, with explicit count. duke@435: int _queue_length[nof_priorities]; duke@435: int _queue_counter; duke@435: VM_Operation* _queue [nof_priorities]; duke@435: // we also allow the vmThread to register the ops it has drained so we duke@435: // can scan them from oops_do duke@435: VM_Operation* _drain_list; duke@435: duke@435: // Double-linked non-empty list insert. duke@435: void insert(VM_Operation* q,VM_Operation* n); duke@435: void unlink(VM_Operation* q); duke@435: duke@435: // Basic queue manipulation duke@435: bool queue_empty (int prio); duke@435: void queue_add_front (int prio, VM_Operation *op); duke@435: void queue_add_back (int prio, VM_Operation *op); duke@435: VM_Operation* queue_remove_front(int prio); duke@435: void queue_oops_do(int queue, OopClosure* f); duke@435: void drain_list_oops_do(OopClosure* f); duke@435: VM_Operation* queue_drain(int prio); duke@435: // lock-free query: may return the wrong answer but must not break duke@435: bool queue_peek(int prio) { return _queue_length[prio] > 0; } duke@435: duke@435: public: duke@435: VMOperationQueue(); duke@435: duke@435: // Highlevel operations. Encapsulates policy duke@435: bool add(VM_Operation *op); duke@435: VM_Operation* remove_next(); // Returns next or null duke@435: VM_Operation* remove_next_at_safepoint_priority() { return queue_remove_front(SafepointPriority); } duke@435: VM_Operation* drain_at_safepoint_priority() { return queue_drain(SafepointPriority); } duke@435: void set_drain_list(VM_Operation* list) { _drain_list = list; } duke@435: bool peek_at_safepoint_priority() { return queue_peek(SafepointPriority); } duke@435: duke@435: // GC support duke@435: void oops_do(OopClosure* f); duke@435: duke@435: void verify_queue(int prio) PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: // duke@435: // A single VMThread (the primordial thread) spawns all other threads duke@435: // and is itself used by other threads to offload heavy vm operations duke@435: // like scavenge, garbage_collect etc. duke@435: // duke@435: minqi@1554: class VMThread: public NamedThread { duke@435: private: duke@435: static ThreadPriority _current_priority; duke@435: duke@435: static bool _should_terminate; duke@435: static bool _terminated; duke@435: static Monitor * _terminate_lock; duke@435: static PerfCounter* _perf_accumulated_vm_operation_time; duke@435: duke@435: void evaluate_operation(VM_Operation* op); duke@435: public: duke@435: // Constructor duke@435: VMThread(); duke@435: duke@435: // Tester duke@435: bool is_VM_thread() const { return true; } duke@435: bool is_GC_thread() const { return true; } duke@435: duke@435: // The ever running loop for the VMThread duke@435: void loop(); duke@435: duke@435: // Called to stop the VM thread duke@435: static void wait_for_vm_thread_exit(); duke@435: static bool should_terminate() { return _should_terminate; } duke@435: static bool is_terminated() { return _terminated == true; } duke@435: duke@435: // Execution of vm operation duke@435: static void execute(VM_Operation* op); duke@435: duke@435: // Returns the current vm operation if any. duke@435: static VM_Operation* vm_operation() { return _cur_vm_operation; } duke@435: duke@435: // Returns the single instance of VMThread. duke@435: static VMThread* vm_thread() { return _vm_thread; } duke@435: duke@435: // GC support jrose@1424: void oops_do(OopClosure* f, CodeBlobClosure* cf); duke@435: duke@435: // Debugging duke@435: void print_on(outputStream* st) const; duke@435: void print() const { print_on(tty); } duke@435: void verify(); duke@435: duke@435: // Performance measurement duke@435: static PerfCounter* perf_accumulated_vm_operation_time() { return _perf_accumulated_vm_operation_time; } duke@435: duke@435: // Entry for starting vm thread duke@435: virtual void run(); duke@435: duke@435: // Creations/Destructions duke@435: static void create(); duke@435: static void destroy(); duke@435: duke@435: private: duke@435: // VM_Operation support duke@435: static VM_Operation* _cur_vm_operation; // Current VM operation duke@435: static VMOperationQueue* _vm_queue; // Queue (w/ policy) of VM operations duke@435: duke@435: // Pointer to single-instance of VM thread duke@435: static VMThread* _vm_thread; duke@435: };