concurrentGCThread.cpp 9.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24 25 26 27 28 29 30 31 32 33
 *
 */

// CopyrightVersion 1.2

# include "incls/_precompiled.incl"
# include "incls/_concurrentGCThread.cpp.incl"

int  ConcurrentGCThread::_CGC_flag            = CGC_nil;

SuspendibleThreadSet ConcurrentGCThread::_sts;

34 35
ConcurrentGCThread::ConcurrentGCThread() :
  _should_terminate(false), _has_terminated(false) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  _sts.initialize();
};

void ConcurrentGCThread::stopWorldAndDo(VoidClosure* op) {
  MutexLockerEx x(Heap_lock,
                  Mutex::_no_safepoint_check_flag);
  // warning("CGC: about to try stopping world");
  SafepointSynchronize::begin();
  // warning("CGC: successfully stopped world");
  op->do_void();
  SafepointSynchronize::end();
  // warning("CGC: successfully restarted world");
}

void ConcurrentGCThread::safepoint_synchronize() {
  _sts.suspend_all();
}

void ConcurrentGCThread::safepoint_desynchronize() {
  _sts.resume_all();
}

void ConcurrentGCThread::create_and_start() {
  if (os::create_thread(this, os::cgc_thread)) {
    // XXX: need to set this to low priority
    // unless "agressive mode" set; priority
    // should be just less than that of VMThread.
    os::set_priority(this, NearMaxPriority);
    if (!_should_terminate && !DisableStartThread) {
      os::start_thread(this);
    }
  }
}

void ConcurrentGCThread::initialize_in_thread() {
  this->record_stack_base_and_size();
  this->initialize_thread_local_storage();
  this->set_active_handles(JNIHandleBlock::allocate_block());
  // From this time Thread::current() should be working.
  assert(this == Thread::current(), "just checking");
}

void ConcurrentGCThread::wait_for_universe_init() {
  MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
  while (!is_init_completed() && !_should_terminate) {
    CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200);
  }
}

void ConcurrentGCThread::terminate() {
  // Signal that it is terminated
  {
    MutexLockerEx mu(Terminator_lock,
                     Mutex::_no_safepoint_check_flag);
    _has_terminated = true;
    Terminator_lock->notify();
  }

  // Thread destructor usually does this..
  ThreadLocalStorage::set_thread(NULL);
}


void SuspendibleThreadSet::initialize_work() {
  MutexLocker x(STS_init_lock);
  if (!_initialized) {
    _m             = new Monitor(Mutex::leaf,
                                 "SuspendibleThreadSetLock", true);
    _async         = 0;
    _async_stop    = false;
    _async_stopped = 0;
    _initialized   = true;
  }
}

void SuspendibleThreadSet::join() {
  initialize();
  MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
  while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
  _async++;
  assert(_async > 0, "Huh.");
}

void SuspendibleThreadSet::leave() {
  assert(_initialized, "Must be initialized.");
  MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
  _async--;
  assert(_async >= 0, "Huh.");
  if (_async_stop) _m->notify_all();
}

void SuspendibleThreadSet::yield(const char* id) {
  assert(_initialized, "Must be initialized.");
  if (_async_stop) {
    MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
    if (_async_stop) {
      _async_stopped++;
      assert(_async_stopped > 0, "Huh.");
      if (_async_stopped == _async) {
        if (ConcGCYieldTimeout > 0) {
          double now = os::elapsedTime();
          guarantee((now - _suspend_all_start) * 1000.0 <
                    (double)ConcGCYieldTimeout,
                    "Long delay; whodunit?");
        }
      }
      _m->notify_all();
      while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
      _async_stopped--;
      assert(_async >= 0, "Huh");
      _m->notify_all();
    }
  }
}

void SuspendibleThreadSet::suspend_all() {
  initialize();  // If necessary.
  if (ConcGCYieldTimeout > 0) {
    _suspend_all_start = os::elapsedTime();
  }
  MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
  assert(!_async_stop, "Only one at a time.");
  _async_stop = true;
  while (_async_stopped < _async) _m->wait(Mutex::_no_safepoint_check_flag);
}

void SuspendibleThreadSet::resume_all() {
  assert(_initialized, "Must be initialized.");
  MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
  assert(_async_stopped == _async, "Huh.");
  _async_stop = false;
  _m->notify_all();
}

static void _sltLoop(JavaThread* thread, TRAPS) {
  SurrogateLockerThread* slt = (SurrogateLockerThread*)thread;
  slt->loop();
}

SurrogateLockerThread::SurrogateLockerThread() :
  JavaThread(&_sltLoop),
  _monitor(Mutex::nonleaf, "SLTMonitor"),
  _buffer(empty)
{}

SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) {
  klassOop k =
    SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(),
                                      true, CHECK_NULL);
  instanceKlassHandle klass (THREAD, k);
  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);

188
  const char thread_name[] = "Surrogate Locker Thread (Concurrent GC)";
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL);

  // Initialize thread_oop to put it into the system threadGroup
  Handle thread_group (THREAD, Universe::system_thread_group());
  JavaValue result(T_VOID);
  JavaCalls::call_special(&result, thread_oop,
                          klass,
                          vmSymbolHandles::object_initializer_name(),
                          vmSymbolHandles::threadgroup_string_void_signature(),
                          thread_group,
                          string,
                          CHECK_NULL);

  SurrogateLockerThread* res;
  {
    MutexLocker mu(Threads_lock);
    res = new SurrogateLockerThread();

    // At this point it may be possible that no osthread was created for the
    // JavaThread due to lack of memory. We would have to throw an exception
    // in that case. However, since this must work and we do not allow
    // exceptions anyway, check and abort if this fails.
    if (res == NULL || res->osthread() == NULL) {
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
                                    "unable to create new native thread");
    }
    java_lang_Thread::set_thread(thread_oop(), res);
    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
    java_lang_Thread::set_daemon(thread_oop());

    res->set_threadObj(thread_oop());
    Threads::add(res);
    Thread::start(res);
  }
  os::yield(); // This seems to help with initial start-up of SLT
  return res;
}

void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) {
  MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
  assert(_buffer == empty, "Should be empty");
  assert(msg != empty, "empty message");
  _buffer = msg;
  while (_buffer != empty) {
    _monitor.notify();
    _monitor.wait(Mutex::_no_safepoint_check_flag);
  }
}

// ======= Surrogate Locker Thread =============

void SurrogateLockerThread::loop() {
  BasicLock pll_basic_lock;
  SLT_msg_type msg;
  debug_only(unsigned int owned = 0;)

  while (/* !isTerminated() */ 1) {
    {
      MutexLocker x(&_monitor);
      // Since we are a JavaThread, we can't be here at a safepoint.
      assert(!SafepointSynchronize::is_at_safepoint(),
             "SLT is a JavaThread");
      // wait for msg buffer to become non-empty
      while (_buffer == empty) {
        _monitor.notify();
        _monitor.wait();
      }
      msg = _buffer;
    }
    switch(msg) {
      case acquirePLL: {
        instanceRefKlass::acquire_pending_list_lock(&pll_basic_lock);
        debug_only(owned++;)
        break;
      }
      case releaseAndNotifyPLL: {
        assert(owned > 0, "Don't have PLL");
        instanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock);
        debug_only(owned--;)
        break;
      }
      case empty:
      default: {
        guarantee(false,"Unexpected message in _buffer");
        break;
      }
    }
    {
      MutexLocker x(&_monitor);
      // Since we are a JavaThread, we can't be here at a safepoint.
      assert(!SafepointSynchronize::is_at_safepoint(),
             "SLT is a JavaThread");
      _buffer = empty;
      _monitor.notify();
    }
  }
  assert(!_monitor.owned_by_self(), "Should unlock before exit.");
}


// ===== STS Access From Outside CGCT =====

void ConcurrentGCThread::stsYield(const char* id) {
  assert( Thread::current()->is_ConcurrentGC_thread(),
          "only a conc GC thread can call this" );
  _sts.yield(id);
}

bool ConcurrentGCThread::stsShouldYield() {
  assert( Thread::current()->is_ConcurrentGC_thread(),
          "only a conc GC thread can call this" );
  return _sts.should_yield();
}

void ConcurrentGCThread::stsJoin() {
  assert( Thread::current()->is_ConcurrentGC_thread(),
          "only a conc GC thread can call this" );
  _sts.join();
}

void ConcurrentGCThread::stsLeave() {
  assert( Thread::current()->is_ConcurrentGC_thread(),
          "only a conc GC thread can call this" );
  _sts.leave();
}