1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_VMTHREAD_HPP
  26 #define SHARE_VM_RUNTIME_VMTHREAD_HPP
  27 
  28 #include "runtime/perfData.hpp"
  29 #include "runtime/vm_operations.hpp"
  30 #ifdef TARGET_OS_FAMILY_linux
  31 # include "thread_linux.inline.hpp"
  32 #endif
  33 #ifdef TARGET_OS_FAMILY_solaris
  34 # include "thread_solaris.inline.hpp"
  35 #endif
  36 #ifdef TARGET_OS_FAMILY_windows
  37 # include "thread_windows.inline.hpp"
  38 #endif
  39 
  40 //
  41 // Prioritized queue of VM operations.
  42 //
  43 // Encapsulates both queue management and
  44 // and priority policy
  45 //
  46 class VMOperationQueue : public CHeapObj {
  47  private:
  48   enum Priorities {
  49      SafepointPriority, // Highest priority (operation executed at a safepoint)
  50      MediumPriority,    // Medium priority
  51      nof_priorities
  52   };
  53 
  54   // We maintain a doubled linked list, with explicit count.
  55   int           _queue_length[nof_priorities];
  56   int           _queue_counter;
  57   VM_Operation* _queue       [nof_priorities];
  58   // we also allow the vmThread to register the ops it has drained so we
  59   // can scan them from oops_do
  60   VM_Operation* _drain_list;
  61 
  62   // Double-linked non-empty list insert.
  63   void insert(VM_Operation* q,VM_Operation* n);
  64   void unlink(VM_Operation* q);
  65 
  66   // Basic queue manipulation
  67   bool queue_empty                (int prio);
  68   void queue_add_front            (int prio, VM_Operation *op);
  69   void queue_add_back             (int prio, VM_Operation *op);
  70   VM_Operation* queue_remove_front(int prio);
  71   void queue_oops_do(int queue, OopClosure* f);
  72   void drain_list_oops_do(OopClosure* f);
  73   VM_Operation* queue_drain(int prio);
  74   // lock-free query: may return the wrong answer but must not break
  75   bool queue_peek(int prio) { return _queue_length[prio] > 0; }
  76 
  77  public:
  78   VMOperationQueue();
  79 
  80   // Highlevel operations. Encapsulates policy
  81   bool add(VM_Operation *op);
  82   VM_Operation* remove_next();                        // Returns next or null
  83   VM_Operation* remove_next_at_safepoint_priority()   { return queue_remove_front(SafepointPriority); }
  84   VM_Operation* drain_at_safepoint_priority() { return queue_drain(SafepointPriority); }
  85   void set_drain_list(VM_Operation* list) { _drain_list = list; }
  86   bool peek_at_safepoint_priority() { return queue_peek(SafepointPriority); }
  87 
  88   // GC support
  89   void oops_do(OopClosure* f);
  90 
  91   void verify_queue(int prio) PRODUCT_RETURN;
  92 };
  93 
  94 
  95 //
  96 // A single VMThread (the primordial thread) spawns all other threads
  97 // and is itself used by other threads to offload heavy vm operations
  98 // like scavenge, garbage_collect etc.
  99 //
 100 
 101 class VMThread: public NamedThread {
 102  private:
 103   static ThreadPriority _current_priority;
 104 
 105   static bool _should_terminate;
 106   static bool _terminated;
 107   static Monitor * _terminate_lock;
 108   static PerfCounter* _perf_accumulated_vm_operation_time;
 109 
 110   void evaluate_operation(VM_Operation* op);
 111  public:
 112   // Constructor
 113   VMThread();
 114 
 115   // Tester
 116   bool is_VM_thread() const                      { return true; }
 117   bool is_GC_thread() const                      { return true; }
 118 
 119   // The ever running loop for the VMThread
 120   void loop();
 121 
 122   // Called to stop the VM thread
 123   static void wait_for_vm_thread_exit();
 124   static bool should_terminate()                  { return _should_terminate; }
 125   static bool is_terminated()                     { return _terminated == true; }
 126 
 127   // Execution of vm operation
 128   static void execute(VM_Operation* op);
 129 
 130   // Returns the current vm operation if any.
 131   static VM_Operation* vm_operation()             { return _cur_vm_operation;   }
 132 
 133   // Returns the single instance of VMThread.
 134   static VMThread* vm_thread()                    { return _vm_thread; }
 135 
 136   // GC support
 137   void oops_do(OopClosure* f, CodeBlobClosure* cf);
 138 
 139   // Debugging
 140   void print_on(outputStream* st) const;
 141   void print() const                              { print_on(tty); }
 142   void verify();
 143 
 144   // Performance measurement
 145   static PerfCounter* perf_accumulated_vm_operation_time()               { return _perf_accumulated_vm_operation_time; }
 146 
 147   // Entry for starting vm thread
 148   virtual void run();
 149 
 150   // Creations/Destructions
 151   static void create();
 152   static void destroy();
 153 
 154  private:
 155   // VM_Operation support
 156   static VM_Operation*     _cur_vm_operation;   // Current VM operation
 157   static VMOperationQueue* _vm_queue;           // Queue (w/ policy) of VM operations
 158 
 159   // Pointer to single-instance of VM thread
 160   static VMThread*     _vm_thread;
 161 };
 162 
 163 #endif // SHARE_VM_RUNTIME_VMTHREAD_HPP