src/hotspot/share/gc/shared/suspendibleThreadSet.cpp
author stefank
Thu, 22 Feb 2018 18:36:32 +0100
changeset 49048 4e8c86b75428
parent 47701 be620a591379
child 54623 1126f0607c70
permissions -rw-r--r--
8198525: Move _size_policy out of GenCollectorPolicy into GenCollectedHeap Reviewed-by: pliden, sjohanss
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
     1
/*
47647
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2017, 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"
47647
64dba69fc528 8189276: Make SuspendibleThreadSet and related code available to other GCs
rkennke
parents: 47216
diff changeset
    26
#include "gc/shared/suspendibleThreadSet.hpp"
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    27
#include "runtime/mutexLocker.hpp"
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    28
#include "runtime/semaphore.hpp"
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    29
#include "runtime/thread.inline.hpp"
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    30
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    31
uint   SuspendibleThreadSet::_nthreads          = 0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    32
uint   SuspendibleThreadSet::_nthreads_stopped  = 0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    33
bool   SuspendibleThreadSet::_suspend_all       = false;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    34
double SuspendibleThreadSet::_suspend_all_start = 0.0;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    35
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    36
static Semaphore* _synchronize_wakeup = NULL;
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    37
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    38
void SuspendibleThreadSet_init() {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    39
  assert(_synchronize_wakeup == NULL, "STS already initialized");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    40
  _synchronize_wakeup = new Semaphore();
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    41
}
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    42
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    43
bool SuspendibleThreadSet::is_synchronized() {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    44
  assert_lock_strong(STS_lock);
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    45
  assert(_nthreads_stopped <= _nthreads, "invariant");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    46
  return _nthreads_stopped == _nthreads;
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    47
}
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    48
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    49
void SuspendibleThreadSet::join() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    50
  assert(!Thread::current()->is_suspendible_thread(), "Thread already joined");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    51
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    52
  while (_suspend_all) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    53
    ml.wait(Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    54
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    55
  _nthreads++;
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    56
  DEBUG_ONLY(Thread::current()->set_suspendible_thread();)
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    57
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    58
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    59
void SuspendibleThreadSet::leave() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    60
  assert(Thread::current()->is_suspendible_thread(), "Thread not joined");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    61
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    62
  assert(_nthreads > 0, "Invalid");
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    63
  DEBUG_ONLY(Thread::current()->clear_suspendible_thread();)
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    64
  _nthreads--;
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    65
  if (_suspend_all && is_synchronized()) {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    66
    // This leave completes a request, so inform the requestor.
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    67
    _synchronize_wakeup->signal();
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    68
  }
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    69
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    70
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    71
void SuspendibleThreadSet::yield() {
30610
d4f41f692503 7006810: G1: Introduce peace-of-mind checking in the Suspendible Thread Set
jprovino
parents: 24094
diff changeset
    72
  assert(Thread::current()->is_suspendible_thread(), "Must have joined");
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    73
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    74
  if (_suspend_all) {
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    75
    _nthreads_stopped++;
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    76
    if (is_synchronized()) {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    77
      if (ConcGCYieldTimeout > 0) {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    78
        double now = os::elapsedTime();
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    79
        guarantee((now - _suspend_all_start) * 1000.0 < (double)ConcGCYieldTimeout, "Long delay");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    80
      }
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    81
      // This yield completes the request, so inform the requestor.
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    82
      _synchronize_wakeup->signal();
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    83
    }
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    84
    while (_suspend_all) {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    85
      ml.wait(Mutex::_no_safepoint_check_flag);
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    86
    }
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    87
    assert(_nthreads_stopped > 0, "Invalid");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    88
    _nthreads_stopped--;
24094
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::synchronize() {
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
  if (ConcGCYieldTimeout > 0) {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    95
    _suspend_all_start = os::elapsedTime();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
    96
  }
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    97
  {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    98
    MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
    99
    assert(!_suspend_all, "Only one at a time");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   100
    _suspend_all = true;
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   101
    if (is_synchronized()) {
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   102
      return;
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   103
    }
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   104
  } // Release lock before semaphore wait.
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   105
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   106
  // Semaphore initial count is zero.  To reach here, there must be at
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   107
  // least one not yielded thread in the set, e.g. is_synchronized()
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   108
  // was false before the lock was released.  A thread in the set will
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   109
  // signal the semaphore iff it is the last to yield or leave while
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   110
  // there is an active suspend request.  So there will be exactly one
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   111
  // signal, which will increment the semaphore count to one, which
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   112
  // will then be consumed by this wait, returning it to zero.  No
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   113
  // thread can exit yield or enter the set until desynchronize is
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   114
  // called, so there are no further opportunities for the semaphore
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   115
  // being signaled until we get back here again for some later
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   116
  // synchronize call.  Hence, there is no need to re-check for
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   117
  // is_synchronized after the wait; it will always be true there.
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   118
  _synchronize_wakeup->wait();
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   119
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   120
#ifdef ASSERT
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   121
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   122
  assert(_suspend_all, "STS not synchronizing");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   123
  assert(is_synchronized(), "STS not synchronized");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   124
#endif
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   125
}
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   126
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   127
void SuspendibleThreadSet::desynchronize() {
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   128
  assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   129
  MonitorLockerEx ml(STS_lock, Mutex::_no_safepoint_check_flag);
37188
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   130
  assert(_suspend_all, "STS not synchronizing");
390e3a2e7cee 8152196: SuspendibleThreadSet::yield scales poorly
kbarrett
parents: 30764
diff changeset
   131
  assert(is_synchronized(), "STS not synchronized");
24094
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   132
  _suspend_all = false;
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   133
  ml.notify_all();
5dbf1f44de18 8039147: Cleanup SuspendibleThreadSet
pliden
parents:
diff changeset
   134
}