src/hotspot/share/runtime/jniHandles.cpp
author pliden
Tue, 12 Jun 2018 17:40:28 +0200
changeset 50525 767cdb97f103
parent 50113 caf115bb98ad
child 50599 ecc2af326b5f
permissions -rw-r--r--
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental) Reviewed-by: pliden, stefank, eosterlund, ehelin, sjohanss, rbackman, coleenp, ihse, jgeorge, lmesnik, rkennke Contributed-by: per.liden@oracle.com, stefan.karlsson@oracle.com, erik.osterlund@oracle.com, mikael.gerdin@oracle.com, kim.barrett@oracle.com, nils.eliasson@oracle.com, rickard.backman@oracle.com, rwestrel@redhat.com, coleen.phillimore@oracle.com, robbin.ehn@oracle.com, gerard.ziemski@oracle.com, hugh.wilkinson@intel.com, sandhya.viswanathan@intel.com, bill.npo.wheeler@intel.com, vinay.k.awasthi@intel.com, yasuenag@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5896
diff changeset
    25
#include "precompiled.hpp"
48816
3495d6050efe 8194898: Move OopStorage inline definitions to an .inline.hpp
kbarrett
parents: 48808
diff changeset
    26
#include "gc/shared/oopStorage.inline.hpp"
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 33148
diff changeset
    27
#include "logging/log.hpp"
37106
0856b64af754 8151539: Remove duplicate AlwaysTrueClosures
stefank
parents: 36398
diff changeset
    28
#include "memory/iterator.hpp"
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
    29
#include "oops/access.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5896
diff changeset
    30
#include "oops/oop.inline.hpp"
49393
93fe2fc5c093 8199472: Fix non-PCH build after JDK-8199319
simonis
parents: 49347
diff changeset
    31
#include "runtime/handles.inline.hpp"
49192
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
    32
#include "runtime/jniHandles.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5896
diff changeset
    33
#include "runtime/mutexLocker.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 13728
diff changeset
    34
#include "runtime/thread.inline.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    35
#include "utilities/align.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    36
#include "utilities/debug.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    38
OopStorage* JNIHandles::_global_handles = NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    39
OopStorage* JNIHandles::_weak_global_handles = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    41
OopStorage* JNIHandles::global_handles() {
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    42
  assert(_global_handles != NULL, "Uninitialized JNI global handles");
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    43
  return _global_handles;
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    44
}
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    45
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    46
OopStorage* JNIHandles::weak_global_handles() {
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    47
  assert(_weak_global_handles != NULL, "Uninitialized JNI weak global handles");
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    48
  return _weak_global_handles;
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    49
}
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
    50
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
jobject JNIHandles::make_local(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    return NULL;                // ignore null handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    Thread* thread = Thread::current();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    57
    assert(oopDesc::is_oop(obj), "not an oop");
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
    58
    assert(!current_thread_in_native(), "must not be in native");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    return thread->active_handles()->allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// optimized versions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
jobject JNIHandles::make_local(Thread* thread, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    return NULL;                // ignore null handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  } else {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    70
    assert(oopDesc::is_oop(obj), "not an oop");
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
    71
    assert(thread->is_Java_thread(), "not a Java thread");
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
    72
    assert(!current_thread_in_native(), "must not be in native");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    return thread->active_handles()->allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    return NULL;                // ignore null handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    JavaThread* thread = JavaThread::thread_from_jni_environment(env);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    83
    assert(oopDesc::is_oop(obj), "not an oop");
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
    84
    assert(!current_thread_in_native(), "must not be in native");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    return thread->active_handles()->allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    90
static void report_handle_allocation_failure(AllocFailType alloc_failmode,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    91
                                             const char* handle_kind) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    92
  if (alloc_failmode == AllocFailStrategy::EXIT_OOM) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    93
    // Fake size value, since we don't know the min allocation size here.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    94
    vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    95
                          "Cannot create %s JNI handle", handle_kind);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    96
  } else {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    97
    assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    98
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
    99
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   100
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   101
jobject JNIHandles::make_global(Handle obj, AllocFailType alloc_failmode) {
5896
c60924a89622 6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents: 5547
diff changeset
   102
  assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   103
  assert(!current_thread_in_native(), "must not be in native");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  jobject res = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  if (!obj.is_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    // ignore null handles
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   107
    assert(oopDesc::is_oop(obj()), "not an oop");
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   108
    oop* ptr = global_handles()->allocate();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   109
    // Return NULL on allocation failure.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   110
    if (ptr != NULL) {
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   111
      assert(*ptr == NULL, "invariant");
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   112
      RootAccess<IN_CONCURRENT_ROOT>::oop_store(ptr, obj());
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   113
      res = reinterpret_cast<jobject>(ptr);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   114
    } else {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   115
      report_handle_allocation_failure(alloc_failmode, "global");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   116
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   125
jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) {
5896
c60924a89622 6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents: 5547
diff changeset
   126
  assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   127
  assert(!current_thread_in_native(), "must not be in native");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  jobject res = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  if (!obj.is_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    // ignore null handles
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   131
    assert(oopDesc::is_oop(obj()), "not an oop");
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   132
    oop* ptr = weak_global_handles()->allocate();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   133
    // Return NULL on allocation failure.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   134
    if (ptr != NULL) {
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   135
      assert(*ptr == NULL, "invariant");
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   136
      RootAccess<ON_PHANTOM_OOP_REF>::oop_store(ptr, obj());
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   137
      char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   138
      res = reinterpret_cast<jobject>(tptr);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   139
    } else {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   140
      report_handle_allocation_failure(alloc_failmode, "weak global");
44406
a46a6c4d1dd9 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents: 44093
diff changeset
   141
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
49192
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   148
// Resolve some erroneous cases to NULL, rather than treating them as
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   149
// possibly unchecked errors.  In particular, deleted handles are
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   150
// treated as NULL (though a deleted and later reallocated handle
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   151
// isn't detected).
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   152
oop JNIHandles::resolve_external_guard(jobject handle) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   153
  oop result = NULL;
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   154
  if (handle != NULL) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   155
    result = resolve_impl<true /* external_guard */ >(handle);
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   156
  }
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   157
  return result;
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   158
}
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48816
diff changeset
   159
44406
a46a6c4d1dd9 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents: 44093
diff changeset
   160
oop JNIHandles::resolve_jweak(jweak handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   161
  assert(handle != NULL, "precondition");
44406
a46a6c4d1dd9 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents: 44093
diff changeset
   162
  assert(is_jweak(handle), "precondition");
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   163
  return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(jweak_ptr(handle));
44406
a46a6c4d1dd9 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents: 44093
diff changeset
   164
}
a46a6c4d1dd9 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents: 44093
diff changeset
   165
47794
e84aa2c71241 8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents: 47779
diff changeset
   166
bool JNIHandles::is_global_weak_cleared(jweak handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   167
  assert(handle != NULL, "precondition");
47794
e84aa2c71241 8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents: 47779
diff changeset
   168
  assert(is_jweak(handle), "not a weak handle");
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   169
  oop* oop_ptr = jweak_ptr(handle);
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   170
  oop value = RootAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(oop_ptr);
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   171
  return value == NULL;
47794
e84aa2c71241 8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents: 47779
diff changeset
   172
}
e84aa2c71241 8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents: 47779
diff changeset
   173
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
void JNIHandles::destroy_global(jobject handle) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  if (handle != NULL) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   176
    assert(!is_jweak(handle), "wrong method for detroying jweak");
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   177
    oop* oop_ptr = jobject_ptr(handle);
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   178
    RootAccess<IN_CONCURRENT_ROOT>::oop_store(oop_ptr, (oop)NULL);
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   179
    global_handles()->release(oop_ptr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
void JNIHandles::destroy_weak_global(jobject handle) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  if (handle != NULL) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   186
    assert(is_jweak(handle), "JNI handle not jweak");
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   187
    oop* oop_ptr = jweak_ptr(handle);
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   188
    RootAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_ptr, (oop)NULL);
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   189
    weak_global_handles()->release(oop_ptr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
void JNIHandles::oops_do(OopClosure* f) {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   195
  global_handles()->oops_do(f);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
32817
acc2744fd84b 8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents: 32607
diff changeset
   199
void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   200
  weak_global_handles()->weak_oops_do(is_alive, f);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
37106
0856b64af754 8151539: Remove duplicate AlwaysTrueClosures
stefank
parents: 36398
diff changeset
   204
void JNIHandles::weak_oops_do(OopClosure* f) {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   205
  weak_global_handles()->weak_oops_do(f);
37106
0856b64af754 8151539: Remove duplicate AlwaysTrueClosures
stefank
parents: 36398
diff changeset
   206
}
0856b64af754 8151539: Remove duplicate AlwaysTrueClosures
stefank
parents: 36398
diff changeset
   207
0856b64af754 8151539: Remove duplicate AlwaysTrueClosures
stefank
parents: 36398
diff changeset
   208
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
void JNIHandles::initialize() {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   210
  _global_handles = new OopStorage("JNI Global",
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   211
                                   JNIGlobalAlloc_lock,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   212
                                   JNIGlobalActive_lock);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   213
  _weak_global_handles = new OopStorage("JNI Weak",
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   214
                                        JNIWeakAlloc_lock,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   215
                                        JNIWeakActive_lock);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   216
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   217
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   218
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   219
inline bool is_storage_handle(const OopStorage* storage, const oop* ptr) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   220
  return storage->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   221
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   222
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   223
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   224
jobjectRefType JNIHandles::handle_type(Thread* thread, jobject handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   225
  assert(handle != NULL, "precondition");
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   226
  jobjectRefType result = JNIInvalidRefType;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   227
  if (is_jweak(handle)) {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   228
    if (is_storage_handle(weak_global_handles(), jweak_ptr(handle))) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   229
      result = JNIWeakGlobalRefType;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   230
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   231
  } else {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   232
    switch (global_handles()->allocation_status(jobject_ptr(handle))) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   233
    case OopStorage::ALLOCATED_ENTRY:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   234
      result = JNIGlobalRefType;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   235
      break;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   236
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   237
    case OopStorage::UNALLOCATED_ENTRY:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   238
      break;                    // Invalid global handle
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   239
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   240
    case OopStorage::INVALID_ENTRY:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   241
      // Not in global storage.  Might be a local handle.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   242
      if (is_local_handle(thread, handle) ||
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   243
          (thread->is_Java_thread() &&
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   244
           is_frame_handle((JavaThread*)thread, handle))) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   245
        result = JNILocalRefType;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   246
      }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   247
      break;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   248
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   249
    default:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   250
      ShouldNotReachHere();
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   251
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   252
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   253
  return result;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   258
  assert(handle != NULL, "precondition");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  JNIHandleBlock* block = thread->active_handles();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  // Look back past possible native calls to jni_PushLocalFrame.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  while (block != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    if (block->chain_contains(handle)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    block = block->pop_frame_link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
// Determine if the handle is somewhere in the current thread's stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// We easily can't isolate any particular stack frame the handle might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
// come from, so we'll check the whole stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   276
bool JNIHandles::is_frame_handle(JavaThread* thr, jobject handle) {
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   277
  assert(handle != NULL, "precondition");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  // If there is no java frame, then this must be top level code, such
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // as the java command executable, in which case, this type of handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  // is not permitted.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  return (thr->has_last_Java_frame() &&
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   282
         (void*)handle < (void*)thr->stack_base() &&
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   283
         (void*)handle >= (void*)thr->last_Java_sp());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
bool JNIHandles::is_global_handle(jobject handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   288
  assert(handle != NULL, "precondition");
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   289
  return !is_jweak(handle) && is_storage_handle(global_handles(), jobject_ptr(handle));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
bool JNIHandles::is_weak_global_handle(jobject handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   294
  assert(handle != NULL, "precondition");
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   295
  return is_jweak(handle) && is_storage_handle(weak_global_handles(), jweak_ptr(handle));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   298
size_t JNIHandles::global_handle_memory_usage() {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   299
  return global_handles()->total_memory_usage();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   300
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   302
size_t JNIHandles::weak_global_handle_memory_usage() {
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   303
  return weak_global_handles()->total_memory_usage();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   304
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   305
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
// We assume this is called at a safepoint: no lock is needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
void JNIHandles::print_on(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   311
  st->print_cr("JNI global refs: " SIZE_FORMAT ", weak refs: " SIZE_FORMAT,
49909
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   312
               global_handles()->allocation_count(),
f276b348ec14 8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents: 49656
diff changeset
   313
               weak_global_handles()->allocation_count());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  st->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   318
class VerifyJNIHandles: public OopClosure {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
public:
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   320
  virtual void do_oop(oop* root) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    (*root)->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   323
  virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
void JNIHandles::verify() {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   327
  VerifyJNIHandles verify_handle;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  oops_do(&verify_handle);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50113
diff changeset
   330
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50113
diff changeset
   331
  // JNI weaks are handled concurrently in ZGC, so they can't be verified here
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50113
diff changeset
   332
  if (!UseZGC) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50113
diff changeset
   333
    weak_oops_do(&verify_handle);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50113
diff changeset
   334
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
48465
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   337
// This method is implemented here to avoid circular includes between
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   338
// jniHandles.hpp and thread.hpp.
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   339
bool JNIHandles::current_thread_in_native() {
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   340
  Thread* thread = Thread::current();
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   341
  return (thread->is_Java_thread() &&
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   342
          JavaThread::current()->thread_state() == _thread_in_native);
f82e79958beb 8167372: Add code to check for getting oops while thread is in native
hseigel
parents: 48147
diff changeset
   343
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
void jni_handles_init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  JNIHandles::initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
int             JNIHandleBlock::_blocks_allocated     = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
JNIHandleBlock* JNIHandleBlock::_block_free_list      = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
JNIHandleBlock* JNIHandleBlock::_block_list           = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   358
#ifdef ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
void JNIHandleBlock::zap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  // Zap block values
46703
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   361
  _top = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  for (int index = 0; index < block_size_in_oops; index++) {
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   363
    // NOT using Access here; just bare clobbering to NULL, since the
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   364
    // block no longer contains valid oops.
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   365
    _handles[index] = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
}
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   368
#endif // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  assert(thread == NULL || thread == Thread::current(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  JNIHandleBlock* block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  // Check the thread-local free list for a block so we don't
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  // have to acquire a mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  if (thread != NULL && thread->free_handle_block() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    block = thread->free_handle_block();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    thread->set_free_handle_block(block->_next);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    // locking with safepoint checking introduces a potential deadlock:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    // - another would hold Threads_lock (jni_AttachCurrentThread) and then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    MutexLockerEx ml(JNIHandleBlockFreeList_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
                     Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    if (_block_free_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      // Allocate new block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
      block = new JNIHandleBlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
      _blocks_allocated++;
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   390
      block->zap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
      #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
      // Link new block to list of all allocated blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      block->_block_list_link = _block_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
      _block_list = block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
      #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
      // Get block from free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
      block = _block_free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
      _block_free_list = _block_free_list->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  }
46703
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   402
  block->_top = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  block->_next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  block->_pop_frame_link = NULL;
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   405
  block->_planned_capacity = block_size_in_oops;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  debug_only(block->_last = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  debug_only(block->_free_list = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  debug_only(block->_allocate_before_rebuild = -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  return block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  assert(thread == NULL || thread == Thread::current(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  JNIHandleBlock* pop_frame_link = block->pop_frame_link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  // Put returned block at the beginning of the thread-local free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  // Note that if thread == NULL, we use it as an implicit argument that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  // we _don't_ want the block to be kept on the free_handle_block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  // See for instance JavaThread::exit().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  if (thread != NULL ) {
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   422
    block->zap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    JNIHandleBlock* freelist = thread->free_handle_block();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
    block->_pop_frame_link = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    thread->set_free_handle_block(block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
    // Add original freelist to end of chain
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    if ( freelist != NULL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
      while ( block->_next != NULL ) block = block->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
      block->_next = freelist;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
    block = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  if (block != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    // Return blocks to free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    // locking with safepoint checking introduces a potential deadlock:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    // - another would hold Threads_lock (jni_AttachCurrentThread) and then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    MutexLockerEx ml(JNIHandleBlockFreeList_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
                     Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    while (block != NULL) {
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   443
      block->zap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
      JNIHandleBlock* next = block->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      block->_next = _block_free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
      _block_free_list = block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
      block = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  if (pop_frame_link != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    // As a sanity check we release blocks pointed to by the pop_frame_link.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    // This should never happen (only if PopLocalFrame is not called the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    // correct number of times).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    release_block(pop_frame_link, thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
void JNIHandleBlock::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  JNIHandleBlock* current_chain = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  // Iterate over chain of blocks, followed by chains linked through the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  // pop frame links.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  while (current_chain != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    for (JNIHandleBlock* current = current_chain; current != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
         current = current->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
      assert(current == current_chain || current->pop_frame_link() == NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
        "only blocks first in chain should have pop frame link set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      for (int index = 0; index < current->_top; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
        oop* root = &(current->_handles)[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
        oop value = *root;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
        // traverse heap pointers only, not deleted handles or free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
        // pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
        if (value != NULL && Universe::heap()->is_in_reserved(value)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
          f->do_oop(root);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
      // the next handle block is valid only if current block is full
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
      if (current->_top < block_size_in_oops) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
    current_chain = current_chain->pop_frame_link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
jobject JNIHandleBlock::allocate_handle(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  assert(Universe::heap()->is_in_reserved(obj), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  if (_top == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    // This is the first allocation or the initial block got zapped when
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
    // entering a native function. If we have any following blocks they are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    // not valid anymore.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
    for (JNIHandleBlock* current = _next; current != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
         current = current->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
      assert(current->_last == NULL, "only first block should have _last set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
      assert(current->_free_list == NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
             "only first block should have _free_list set");
46703
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   498
      if (current->_top == 0) {
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   499
        // All blocks after the first clear trailing block are already cleared.
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   500
#ifdef ASSERT
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   501
        for (current = current->_next; current != NULL; current = current->_next) {
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   502
          assert(current->_top == 0, "trailing blocks must already be cleared");
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   503
        }
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   504
#endif
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   505
        break;
f152b500376e 8175318: Performance issue regarding local JNI references
kbarrett
parents: 46625
diff changeset
   506
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
      current->_top = 0;
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   508
      current->zap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
    // Clear initial block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
    _free_list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
    _allocate_before_rebuild = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
    _last = this;
48147
1b57d99567ed 8191870: Remove badJNIHandle
kbarrett
parents: 47794
diff changeset
   514
    zap();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  // Try last block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  if (_last->_top < block_size_in_oops) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
    oop* handle = &(_last->_handles)[_last->_top++];
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   520
    RootAccess<AS_DEST_NOT_INITIALIZED>::oop_store(handle, obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    return (jobject) handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  // Try free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  if (_free_list != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    oop* handle = _free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    _free_list = (oop*) *_free_list;
49656
be608cad0b2a 8195972: Refactor oops in JNI to use the Access API
kbarrett
parents: 49393
diff changeset
   528
    RootAccess<AS_DEST_NOT_INITIALIZED>::oop_store(handle, obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    return (jobject) handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  // Check if unused block follow last
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  if (_last->_next != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
    // update last and retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    _last = _last->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    return allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  // No space available, we have to rebuild free list or expand
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  if (_allocate_before_rebuild == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
      rebuild_free_list();        // updates _allocate_before_rebuild counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    // Append new block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    Thread* thread = Thread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
    Handle obj_handle(thread, obj);
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   545
    // This can block, so we need to preserve obj across call.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    _last->_next = JNIHandleBlock::allocate_block(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
    _last = _last->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
    _allocate_before_rebuild--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
    obj = obj_handle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
  return allocate_handle(obj);  // retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
void JNIHandleBlock::rebuild_free_list() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  int free = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  int blocks = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
    for (int index = 0; index < current->_top; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
      oop* handle = &(current->_handles)[index];
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   561
      if (*handle == NULL) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
        // this handle was cleared out by a delete call, reuse it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
        *handle = (oop) _free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
        _free_list = handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
        free++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    // we should not rebuild free list if there are unused handles at the end
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
    assert(current->_top == block_size_in_oops, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
    blocks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  // Heuristic: if more than half of the handles are free we rebuild next time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  // as well, otherwise we append a corresponding number of new blocks before
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  // attempting a free list rebuild again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  int total = blocks * block_size_in_oops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  int extra = total - 2*free;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  if (extra > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
    // Not as many free handles as we would like - compute number of new blocks to append
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
    _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
bool JNIHandleBlock::contains(jobject handle) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
bool JNIHandleBlock::chain_contains(jobject handle) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
    if (current->contains(handle)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   599
size_t JNIHandleBlock::length() const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   600
  size_t result = 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
    result++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   607
class CountJNIHandleClosure: public OopClosure {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   608
private:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   609
  int _count;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   610
public:
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   611
  CountJNIHandleClosure(): _count(0) {}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   612
  virtual void do_oop(oop* ooph) { _count++; }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   613
  virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   614
  int count() { return _count; }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   615
};
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   616
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   617
const size_t JNIHandleBlock::get_number_of_live_handles() {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   618
  CountJNIHandleClosure counter;
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   619
  oops_do(&counter);
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   620
  return counter.count();
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   621
}
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 24424
diff changeset
   622
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   623
// This method is not thread-safe, i.e., must be called while holding a lock on the
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
// structure.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   625
size_t JNIHandleBlock::memory_usage() const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  return length() * sizeof(JNIHandleBlock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   632
bool JNIHandles::is_local_handle(jobject handle) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   633
  return JNIHandleBlock::any_contains(handle);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   634
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents: 48465
diff changeset
   635
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
bool JNIHandleBlock::any_contains(jobject handle) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   637
  assert(handle != NULL, "precondition");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
    if (current->contains(handle)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
void JNIHandleBlock::print_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
  int used_blocks = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  int free_blocks = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  int used_handles = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
  int free_handles = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  JNIHandleBlock* block = _block_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
  while (block != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
    if (block->_top > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
      used_blocks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
      free_blocks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
    used_handles += block->_top;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    free_handles += (block_size_in_oops - block->_top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    block = block->_block_list_link;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  tty->print_cr("JNIHandleBlocks statistics");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
  tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  tty->print_cr("- blocks in use:    %d", used_blocks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  tty->print_cr("- blocks free:      %d", free_blocks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
  tty->print_cr("- handles in use:   %d", used_handles);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  tty->print_cr("- handles free:     %d", free_handles);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
#endif