hotspot/src/share/vm/gc/g1/suspendibleThreadSet.cpp
author tschatzl
Wed, 25 Nov 2015 14:43:29 +0100
changeset 34300 6075c1e0e913
parent 30764 fec48bf5a827
child 37188 390e3a2e7cee
permissions -rw-r--r--
8136679: JFR event for adaptive IHOP Reviewed-by: tbenson, mgerdin, sangheki, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     1
/*
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     4
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     8
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    13
 * accompanied this code).
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    14
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    18
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    21
 * questions.
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    22
 *
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    23
 */
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    24
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    25
#include "precompiled.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30610
diff changeset
    26
#include "gc/g1/suspendibleThreadSet.hpp"
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    27
#include "runtime/mutexLocker.hpp"
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    28
#include "runtime/thread.inline.hpp"
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    29
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    30
uint   SuspendibleThreadSet::_nthreads          = 0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    31
uint   SuspendibleThreadSet::_nthreads_stopped  = 0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    32
bool   SuspendibleThreadSet::_suspend_all       = false;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    33
double SuspendibleThreadSet::_suspend_all_start = 0.0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    34
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    35
void SuspendibleThreadSet::join() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    36
  assert(!Thread::current()->is_suspendible_thread(), "Thread already joined");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    37
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    38
  while (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    39
    ml.wait(Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    40
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    41
  _nthreads++;
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    42
  DEBUG_ONLY(Thread::current()->set_suspendible_thread();)
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    43
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    44
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    45
void SuspendibleThreadSet::leave() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    46
  assert(Thread::current()->is_suspendible_thread(), "Thread not joined");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    47
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    48
  assert(_nthreads > 0, "Invalid");
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    49
  DEBUG_ONLY(Thread::current()->clear_suspendible_thread();)
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    50
  _nthreads--;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    51
  if (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    52
    ml.notify_all();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    53
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    54
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    55
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    56
void SuspendibleThreadSet::yield() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    57
  assert(Thread::current()->is_suspendible_thread(), "Must have joined");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    58
  if (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    59
    MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    60
    if (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    61
      _nthreads_stopped++;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    62
      if (_nthreads_stopped == _nthreads) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    63
        if (ConcGCYieldTimeout > 0) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    64
          double now = os::elapsedTime();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    65
          guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    66
        }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    67
      }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    68
      ml.notify_all();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    69
      while (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    70
        ml.wait(Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    71
      }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    72
      assert(_nthreads_stopped > 0, "Invalid");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    73
      _nthreads_stopped--;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    74
      ml.notify_all();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    75
    }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    76
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    77
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    78
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    79
void SuspendibleThreadSet::synchronize() {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    80
  assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    81
  if (ConcGCYieldTimeout > 0) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    82
    _suspend_all_start = os::elapsedTime();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    83
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    84
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    85
  assert(!_suspend_all, "Only one at a time");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    86
  _suspend_all = true;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    87
  while (_nthreads_stopped < _nthreads) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    88
    ml.wait(Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    89
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    90
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    91
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    92
void SuspendibleThreadSet::desynchronize() {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    93
  assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    94
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    95
  assert(_nthreads_stopped == _nthreads, "Invalid");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    96
  _suspend_all = false;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    97
  ml.notify_all();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    98
}