hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.cpp
author xlu
Mon, 06 Apr 2009 15:47:39 -0700
changeset 2526 39a58a50be35
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6699669: Hotspot server leaves synchronized block with monitor in bad state Summary: Remove usage of _highest_lock field in Thread so that is_lock_owned won't depend on the correct update of that field. Reviewed-by: never, dice, acorn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_concurrentMarkSweepThread.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// ======= Concurrent Mark Sweep Thread ========
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// The CMS thread is created when Concurrent Mark Sweep is used in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// older of two generations in a generational memory system.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
ConcurrentMarkSweepThread*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
     ConcurrentMarkSweepThread::_cmst     = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
bool ConcurrentMarkSweepThread::_should_terminate = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
int  ConcurrentMarkSweepThread::_CMS_flag         = CMS_nil;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
volatile jint ConcurrentMarkSweepThread::_pending_yields      = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
volatile jint ConcurrentMarkSweepThread::_pending_decrements  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
volatile bool ConcurrentMarkSweepThread::_icms_enabled   = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
volatile bool ConcurrentMarkSweepThread::_should_run     = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// When icms is enabled, the icms thread is stopped until explicitly
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// started.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
volatile bool ConcurrentMarkSweepThread::_should_stop    = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
SurrogateLockerThread*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
     ConcurrentMarkSweepThread::_slt = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
SurrogateLockerThread::SLT_msg_type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
     ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
Monitor*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
     ConcurrentMarkSweepThread::_sltMonitor = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  : ConcurrentGCThread() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  assert(_cmst == NULL, "CMS thread already created");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  _cmst = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  assert(_collector == NULL, "Collector already set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  _collector = collector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  set_name("Concurrent Mark-Sweep GC Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  if (os::create_thread(this, os::cgc_thread)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    // XXX: need to set this to low priority
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    // unless "agressive mode" set; priority
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    // should be just less than that of VMThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    os::set_priority(this, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      os::start_thread(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  _sltMonitor = SLT_lock;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  set_icms_enabled(CMSIncrementalMode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
void ConcurrentMarkSweepThread::run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  assert(this == cmst(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  this->record_stack_base_and_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  this->initialize_thread_local_storage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  this->set_active_handles(JNIHandleBlock::allocate_block());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // From this time Thread::current() should be working.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  assert(this == Thread::current(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  // Wait until Universe::is_fully_initialized()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    CMSLoopCountWarn loopX("CMS::run", "waiting for "
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
                           "Universe::is_fully_initialized()", 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    MutexLockerEx x(CGC_lock, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    set_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    // Wait until Universe is initialized and all initialization is completed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    while (!is_init_completed() && !Universe::is_fully_initialized() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
           !_should_terminate) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      CGC_lock->wait(true, 200);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      loopX.tick();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    // Wait until the surrogate locker thread that will do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    // pending list locking on our behalf has been created.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    // We cannot start the SLT thread ourselves since we need
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    // to be a JavaThread to do so.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    while (_slt == NULL && !_should_terminate) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      CGC_lock->wait(true, 200);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
      loopY.tick();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    clear_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  while (!_should_terminate) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    sleepBeforeNextCycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    if (_should_terminate) break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    _collector->collect_in_background(false);  // !clear_all_soft_refs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  assert(_should_terminate, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // Check that the state of any protocol for synchronization
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  // between background (CMS) and foreground collector is "clean"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // (i.e. will not potentially block the foreground collector,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  // requiring action by us).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  verify_ok_to_terminate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  // Signal that it is terminated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    MutexLockerEx mu(Terminator_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                     Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    assert(_cmst == this, "Weird!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    _cmst = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    Terminator_lock->notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  // Thread destructor usually does this..
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  ThreadLocalStorage::set_thread(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
           cms_thread_wants_cms_token()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
         "Must renounce all worldly possessions and desires for nirvana");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  _collector->verify_ok_to_terminate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
// create and start a new ConcurrentMarkSweep Thread for given CMS generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  if (!_should_terminate) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    assert(cmst() == NULL, "start() called twice?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    assert(cmst() == th, "Where did the just-created CMS thread go?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    return th;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
void ConcurrentMarkSweepThread::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  if (CMSIncrementalMode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    // Disable incremental mode and wake up the thread so it notices the change.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    disable_icms();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    start_icms();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  // it is ok to take late safepoints here, if needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    MutexLockerEx x(Terminator_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    _should_terminate = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  { // Now post a notify on CGC_lock so as to nudge
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    // CMS thread(s) that might be slumbering in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    // sleepBeforeNextCycle.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    CGC_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  { // Now wait until (all) CMS thread(s) have exited
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    MutexLockerEx x(Terminator_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    while(cmst() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      Terminator_lock->wait();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  assert(tc != NULL, "Null ThreadClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  if (_cmst != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    tc->do_thread(_cmst);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  assert(Universe::is_fully_initialized(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
         "Called too early, make sure heap is fully initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  if (_collector != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    AbstractWorkGang* gang = _collector->conc_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    if (gang != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      gang->threads_do(tc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
void ConcurrentMarkSweepThread::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  st->print("\"%s\" ", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  Thread::print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  if (_cmst != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    _cmst->print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  if (_collector != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    AbstractWorkGang* gang = _collector->conc_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    if (gang != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      gang->print_worker_threads_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  assert(UseConcMarkSweepGC, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
                  Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  if (!is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    assert(Thread::current()->is_VM_thread(), "Not a VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    CMSSynchronousYieldRequest yr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    while (CMS_flag_is_set(CMS_cms_has_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      // indicate that we want to get the token
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      set_CMS_flag(CMS_vm_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      CGC_lock->wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    // claim the token and proceed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    clear_CMS_flag(CMS_vm_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    set_CMS_flag(CMS_vm_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    assert(Thread::current()->is_ConcurrentGC_thread(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
           "Not a CMS thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    // The following barrier assumes there's only one CMS thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    // This will need to be modified is there are more CMS threads than one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      set_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      CGC_lock->wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    // claim the token
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    clear_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    set_CMS_flag(CMS_cms_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  assert(UseConcMarkSweepGC, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
                  Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  if (!is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    assert(Thread::current()->is_VM_thread(), "Not a VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    assert(CMS_flag_is_set(CMS_vm_has_token), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    clear_CMS_flag(CMS_vm_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    if (CMS_flag_is_set(CMS_cms_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      // wake-up a waiting CMS thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      CGC_lock->notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
           "Should have been cleared");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    assert(Thread::current()->is_ConcurrentGC_thread(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
           "Not a CMS thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    assert(CMS_flag_is_set(CMS_cms_has_token), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    clear_CMS_flag(CMS_cms_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    if (CMS_flag_is_set(CMS_vm_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
      // wake-up a waiting VM thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
      CGC_lock->notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
           "Should have been cleared");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// Wait until the next synchronous GC or a timeout, whichever is earlier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
void ConcurrentMarkSweepThread::wait_on_cms_lock(long t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
                  Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  set_CMS_flag(CMS_cms_wants_token);   // to provoke notifies
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  CGC_lock->wait(Mutex::_no_safepoint_check_flag, t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  clear_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
         "Should not be set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  while (!_should_terminate) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    if (CMSIncrementalMode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      icms_wait();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      // Wait until the next synchronous GC or a timeout, whichever is earlier
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
      wait_on_cms_lock(CMSWaitDuration);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    // Check if we should start a CMS collection cycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    if (_collector->shouldConcurrentCollect()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    // .. collection criterion not yet met, let's go back
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    // and wait some more
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
// Incremental CMS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
void ConcurrentMarkSweepThread::start_icms() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  trace_state("start_icms");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  _should_run = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  iCMS_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
void ConcurrentMarkSweepThread::stop_icms() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  if (!_should_stop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    trace_state("stop_icms");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    _should_stop = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    _should_run = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    asynchronous_yield_request();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    iCMS_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
void ConcurrentMarkSweepThread::icms_wait() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  if (_should_stop && icms_enabled()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    trace_state("pause_icms");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    _collector->stats().stop_cms_timer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    while(!_should_run && icms_enabled()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
      iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    _collector->stats().start_cms_timer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    _should_stop = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    trace_state("pause_icms end");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// Note: this method, although exported by the ConcurrentMarkSweepThread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
// which is a non-JavaThread, can only be called by a JavaThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
// Currently this is done at vm creation time (post-vm-init) by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// main/Primordial (Java)Thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
// XXX Consider changing this in the future to allow the CMS thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
// itself to create this thread?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  assert(_slt == NULL, "SLT already created");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  _slt = SurrogateLockerThread::make(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
}