1 /*
   2  * Copyright (c) 2001, 2014, 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 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "gc_implementation/shared/concurrentGCThread.hpp"
  28 #include "oops/instanceRefKlass.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/init.hpp"
  31 #include "runtime/interfaceSupport.hpp"
  32 #include "runtime/java.hpp"
  33 #include "runtime/javaCalls.hpp"
  34 #include "runtime/os.hpp"
  35 
  36 // CopyrightVersion 1.2
  37 
  38 int  ConcurrentGCThread::_CGC_flag            = CGC_nil;
  39 
  40 ConcurrentGCThread::ConcurrentGCThread() :
  41   _should_terminate(false), _has_terminated(false) {
  42 };
  43 
  44 void ConcurrentGCThread::create_and_start() {
  45   if (os::create_thread(this, os::cgc_thread)) {
  46     // XXX: need to set this to low priority
  47     // unless "aggressive mode" set; priority
  48     // should be just less than that of VMThread.
  49     os::set_priority(this, NearMaxPriority);
  50     if (!_should_terminate && !DisableStartThread) {
  51       os::start_thread(this);
  52     }
  53   }
  54 }
  55 
  56 void ConcurrentGCThread::initialize_in_thread() {
  57   this->record_stack_base_and_size();
  58   this->initialize_thread_local_storage();
  59   this->set_active_handles(JNIHandleBlock::allocate_block());
  60   // From this time Thread::current() should be working.
  61   assert(this == Thread::current(), "just checking");
  62 }
  63 
  64 void ConcurrentGCThread::wait_for_universe_init() {
  65   MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
  66   while (!is_init_completed() && !_should_terminate) {
  67     CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200);
  68   }
  69 }
  70 
  71 void ConcurrentGCThread::terminate() {
  72   // Signal that it is terminated
  73   {
  74     MutexLockerEx mu(Terminator_lock,
  75                      Mutex::_no_safepoint_check_flag);
  76     _has_terminated = true;
  77     Terminator_lock->notify();
  78   }
  79 
  80   // Thread destructor usually does this..
  81   ThreadLocalStorage::set_thread(NULL);
  82 }
  83 
  84 static void _sltLoop(JavaThread* thread, TRAPS) {
  85   SurrogateLockerThread* slt = (SurrogateLockerThread*)thread;
  86   slt->loop();
  87 }
  88 
  89 SurrogateLockerThread::SurrogateLockerThread() :
  90   JavaThread(&_sltLoop),
  91   _monitor(Mutex::nonleaf, "SLTMonitor", false,
  92            Monitor::_safepoint_check_sometimes),
  93   _buffer(empty)
  94 {}
  95 
  96 SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) {
  97   Klass* k =
  98     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
  99                                       true, CHECK_NULL);
 100   instanceKlassHandle klass (THREAD, k);
 101   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
 102 
 103   const char thread_name[] = "Surrogate Locker Thread (Concurrent GC)";
 104   Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL);
 105 
 106   // Initialize thread_oop to put it into the system threadGroup
 107   Handle thread_group (THREAD, Universe::system_thread_group());
 108   JavaValue result(T_VOID);
 109   JavaCalls::call_special(&result, thread_oop,
 110                           klass,
 111                           vmSymbols::object_initializer_name(),
 112                           vmSymbols::threadgroup_string_void_signature(),
 113                           thread_group,
 114                           string,
 115                           CHECK_NULL);
 116 
 117   SurrogateLockerThread* res;
 118   {
 119     MutexLocker mu(Threads_lock);
 120     res = new SurrogateLockerThread();
 121 
 122     // At this point it may be possible that no osthread was created for the
 123     // JavaThread due to lack of memory. We would have to throw an exception
 124     // in that case. However, since this must work and we do not allow
 125     // exceptions anyway, check and abort if this fails.
 126     if (res == NULL || res->osthread() == NULL) {
 127       vm_exit_during_initialization("java.lang.OutOfMemoryError",
 128                                     os::native_thread_creation_failed_msg());
 129     }
 130     java_lang_Thread::set_thread(thread_oop(), res);
 131     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 132     java_lang_Thread::set_daemon(thread_oop());
 133 
 134     res->set_threadObj(thread_oop());
 135     Threads::add(res);
 136     Thread::start(res);
 137   }
 138   os::naked_yield(); // This seems to help with initial start-up of SLT
 139   return res;
 140 }
 141 
 142 void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) {
 143   MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
 144   assert(_buffer == empty, "Should be empty");
 145   assert(msg != empty, "empty message");
 146   assert(!Heap_lock->owned_by_self(), "Heap_lock owned by requesting thread");
 147 
 148   _buffer = msg;
 149   while (_buffer != empty) {
 150     _monitor.notify();
 151     _monitor.wait(Mutex::_no_safepoint_check_flag);
 152   }
 153 }
 154 
 155 // ======= Surrogate Locker Thread =============
 156 
 157 void SurrogateLockerThread::loop() {
 158   BasicLock pll_basic_lock;
 159   SLT_msg_type msg;
 160   debug_only(unsigned int owned = 0;)
 161 
 162   while (/* !isTerminated() */ 1) {
 163     {
 164       MutexLocker x(&_monitor);
 165       // Since we are a JavaThread, we can't be here at a safepoint.
 166       assert(!SafepointSynchronize::is_at_safepoint(),
 167              "SLT is a JavaThread");
 168       // wait for msg buffer to become non-empty
 169       while (_buffer == empty) {
 170         _monitor.notify();
 171         _monitor.wait();
 172       }
 173       msg = _buffer;
 174     }
 175     switch(msg) {
 176       case acquirePLL: {
 177         InstanceRefKlass::acquire_pending_list_lock(&pll_basic_lock);
 178         debug_only(owned++;)
 179         break;
 180       }
 181       case releaseAndNotifyPLL: {
 182         assert(owned > 0, "Don't have PLL");
 183         InstanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock);
 184         debug_only(owned--;)
 185         break;
 186       }
 187       case empty:
 188       default: {
 189         guarantee(false,"Unexpected message in _buffer");
 190         break;
 191       }
 192     }
 193     {
 194       MutexLocker x(&_monitor);
 195       // Since we are a JavaThread, we can't be here at a safepoint.
 196       assert(!SafepointSynchronize::is_at_safepoint(),
 197              "SLT is a JavaThread");
 198       _buffer = empty;
 199       _monitor.notify();
 200     }
 201   }
 202   assert(!_monitor.owned_by_self(), "Should unlock before exit.");
 203 }