src/hotspot/share/runtime/threadSMR.cpp
author dcubed
Wed, 22 Nov 2017 17:54:50 -0800
changeset 48105 8d15b1369c7a
child 48312 2a1413298af0
permissions -rw-r--r--
8167108: inconsistent handling of SR_lock can lead to crashes Summary: Add Thread Safe Memory Reclamation (Thread-SMR) mechanism. Reviewed-by: coleenp, dcubed, dholmes, eosterlund, gthornbr, kbarrett, rehn, sspitsyn, stefank Contributed-by: daniel.daugherty@oracle.com, erik.osterlund@oracle.com, robbin.ehn@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     1
/*
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     4
 *
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     8
 *
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    13
 * accompanied this code).
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    14
 *
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    18
 *
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    21
 * questions.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    22
 *
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    23
 */
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    24
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    25
#include "precompiled.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    26
#include "memory/allocation.inline.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    27
#include "runtime/thread.inline.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    28
#include "runtime/threadSMR.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    29
#include "services/threadService.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    30
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    31
// 'entries + 1' so we always have at least one entry.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    32
ThreadsList::ThreadsList(int entries) : _length(entries), _threads(NEW_C_HEAP_ARRAY(JavaThread*, entries + 1, mtThread)), _next_list(NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    33
  *(JavaThread**)(_threads + entries) = NULL;  // Make sure the extra entry is NULL.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    34
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    35
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    36
ThreadsList::~ThreadsList() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    37
  FREE_C_HEAP_ARRAY(JavaThread*, _threads);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    38
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    39
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    40
ThreadsListSetter::~ThreadsListSetter() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    41
  if (_target_needs_release) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    42
    // The hazard ptr in the target needs to be released.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    43
    Threads::release_stable_list(_target);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    44
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    45
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    46
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    47
void ThreadsListSetter::set() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    48
  assert(_target->get_threads_hazard_ptr() == NULL, "hazard ptr should not already be set");
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    49
  (void) Threads::acquire_stable_list(_target, /* is_ThreadsListSetter */ true);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    50
  _target_needs_release = true;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    51
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    52
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    53
ThreadsListHandle::ThreadsListHandle(Thread *self) : _list(Threads::acquire_stable_list(self, /* is_ThreadsListSetter */ false)), _self(self) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    54
  assert(self == Thread::current(), "sanity check");
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    55
  if (EnableThreadSMRStatistics) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    56
    _timer.start();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    57
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    58
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    59
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    60
ThreadsListHandle::~ThreadsListHandle() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    61
  Threads::release_stable_list(_self);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    62
  if (EnableThreadSMRStatistics) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    63
    _timer.stop();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    64
    uint millis = (uint)_timer.milliseconds();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    65
    Threads::inc_smr_tlh_cnt();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    66
    Threads::add_smr_tlh_times(millis);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    67
    Threads::update_smr_tlh_time_max(millis);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    68
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    69
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    70
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    71
// Convert an internal thread reference to a JavaThread found on the
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    72
// associated ThreadsList. This ThreadsListHandle "protects" the
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    73
// returned JavaThread *.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    74
//
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    75
// If thread_oop_p is not NULL, then the caller wants to use the oop
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    76
// after this call so the oop is returned. On success, *jt_pp is set
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    77
// to the converted JavaThread * and true is returned. On error,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    78
// returns false.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    79
//
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    80
bool ThreadsListHandle::cv_internal_thread_to_JavaThread(jobject jthread,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    81
                                                         JavaThread ** jt_pp,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    82
                                                         oop * thread_oop_p) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    83
  assert(this->list() != NULL, "must have a ThreadsList");
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    84
  assert(jt_pp != NULL, "must have a return JavaThread pointer");
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    85
  // thread_oop_p is optional so no assert()
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    86
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    87
  // The JVM_* interfaces don't allow a NULL thread parameter; JVM/TI
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    88
  // allows a NULL thread parameter to signify "current thread" which
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    89
  // allows us to avoid calling cv_external_thread_to_JavaThread().
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    90
  // The JVM_* interfaces have no such leeway.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    91
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    92
  oop thread_oop = JNIHandles::resolve_non_null(jthread);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    93
  // Looks like an oop at this point.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    94
  if (thread_oop_p != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    95
    // Return the oop to the caller; the caller may still want
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    96
    // the oop even if this function returns false.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    97
    *thread_oop_p = thread_oop;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    98
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
    99
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   100
  JavaThread *java_thread = java_lang_Thread::thread(thread_oop);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   101
  if (java_thread == NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   102
    // The java.lang.Thread does not contain a JavaThread * so it has
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   103
    // not yet run or it has died.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   104
    return false;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   105
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   106
  // Looks like a live JavaThread at this point.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   107
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   108
  if (java_thread != JavaThread::current()) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   109
    // jthread is not for the current JavaThread so have to verify
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   110
    // the JavaThread * against the ThreadsList.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   111
    if (EnableThreadSMRExtraValidityChecks && !includes(java_thread)) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   112
      // Not on the JavaThreads list so it is not alive.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   113
      return false;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   114
    }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   115
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   116
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   117
  // Return a live JavaThread that is "protected" by the
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   118
  // ThreadsListHandle in the caller.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   119
  *jt_pp = java_thread;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   120
  return true;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents:
diff changeset
   121
}