1 /*
   2  * Copyright (c) 2001, 2016, 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/shared/concurrentGCThread.hpp"
  28 #include "oops/instanceRefKlass.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/init.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/javaCalls.hpp"
  33 #include "runtime/os.hpp"
  34 
  35 ConcurrentGCThread::ConcurrentGCThread() :
  36   _should_terminate(false), _has_terminated(false) {
  37 };
  38 
  39 void ConcurrentGCThread::create_and_start(ThreadPriority prio) {
  40   if (os::create_thread(this, os::cgc_thread)) {
  41     // XXX: need to set this to low priority
  42     // unless "aggressive mode" set; priority
  43     // should be just less than that of VMThread.
  44     os::set_priority(this, prio);
  45     if (!_should_terminate) {
  46       os::start_thread(this);
  47     }
  48   }
  49 }
  50 
  51 void ConcurrentGCThread::initialize_in_thread() {
  52   this->initialize_named_thread();
  53   this->set_active_handles(JNIHandleBlock::allocate_block());
  54   // From this time Thread::current() should be working.
  55   assert(this == Thread::current(), "just checking");
  56 }
  57 
  58 void ConcurrentGCThread::wait_for_universe_init() {
  59   MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
  60   while (!is_init_completed() && !_should_terminate) {
  61     CGC_lock->wait(Mutex::_no_safepoint_check_flag, 1);
  62   }
  63 }
  64 
  65 void ConcurrentGCThread::terminate() {
  66   assert(_should_terminate, "Should only be called on terminate request.");
  67   // Signal that it is terminated
  68   {
  69     MutexLockerEx mu(Terminator_lock,
  70                      Mutex::_no_safepoint_check_flag);
  71     _has_terminated = true;
  72     Terminator_lock->notify();
  73   }
  74 }
  75 
  76 void ConcurrentGCThread::run() {
  77   initialize_in_thread();
  78   wait_for_universe_init();
  79 
  80   run_service();
  81 
  82   terminate();
  83 }
  84 
  85 void ConcurrentGCThread::stop() {
  86   // it is ok to take late safepoints here, if needed
  87   {
  88     MutexLockerEx mu(Terminator_lock);
  89     assert(!_has_terminated,   "stop should only be called once");
  90     assert(!_should_terminate, "stop should only be called once");
  91     _should_terminate = true;
  92   }
  93 
  94   stop_service();
  95 
  96   {
  97     MutexLockerEx mu(Terminator_lock);
  98     while (!_has_terminated) {
  99       Terminator_lock->wait();
 100     }
 101   }
 102 }
--- EOF ---