< prev index next >

src/hotspot/share/runtime/jniHandles.cpp

Print this page

  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 

 25 #include "gc/shared/collectedHeap.hpp"
 26 #include "gc/shared/oopStorage.inline.hpp"
 27 #include "gc/shared/oopStorageSet.hpp"
 28 #include "logging/log.hpp"
 29 #include "memory/iterator.hpp"
 30 #include "memory/universe.hpp"
 31 #include "oops/access.inline.hpp"
 32 #include "oops/oop.inline.hpp"
 33 #include "runtime/handles.inline.hpp"

 34 #include "runtime/javaThread.inline.hpp"
 35 #include "runtime/jniHandles.inline.hpp"
 36 #include "runtime/mutexLocker.hpp"
 37 #include "utilities/align.hpp"
 38 #include "utilities/debug.hpp"
 39 
 40 OopStorage* JNIHandles::global_handles() {
 41   return _global_handles;
 42 }
 43 
 44 OopStorage* JNIHandles::weak_global_handles() {
 45   return _weak_global_handles;
 46 }
 47 
 48 // Serviceability agent support.
 49 OopStorage* JNIHandles::_global_handles = nullptr;
 50 OopStorage* JNIHandles::_weak_global_handles = nullptr;
 51 
 52 void jni_handles_init() {
 53   JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);

265     guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
266   }
267   virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
268 };
269 
270 void JNIHandles::verify() {
271   VerifyJNIHandles verify_handle;
272 
273   oops_do(&verify_handle);
274   weak_oops_do(&verify_handle);
275 }
276 
277 // This method is implemented here to avoid circular includes between
278 // jniHandles.hpp and thread.hpp.
279 bool JNIHandles::current_thread_in_native() {
280   Thread* thread = Thread::current();
281   return (thread->is_Java_thread() &&
282           JavaThread::cast(thread)->thread_state() == _thread_in_native);
283 }
284 






































285 int JNIHandleBlock::_blocks_allocated = 0;
286 
287 static inline bool is_tagged_free_list(uintptr_t value) {
288   return (value & 1u) != 0;
289 }
290 
291 static inline uintptr_t tag_free_list(uintptr_t value) {
292   return value | 1u;
293 }
294 
295 static inline uintptr_t untag_free_list(uintptr_t value) {
296   return value & ~(uintptr_t)1u;
297 }
298 
299 // There is a freelist of handles running through the JNIHandleBlock
300 // with a tagged next pointer, distinguishing these next pointers from
301 // oops. The freelist handling currently relies on the size of oops
302 // being the same as a native pointer. If this ever changes, then
303 // this freelist handling must change too.
304 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));

  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "classfile/vmSymbols.hpp"
 26 #include "gc/shared/collectedHeap.hpp"
 27 #include "gc/shared/oopStorage.inline.hpp"
 28 #include "gc/shared/oopStorageSet.hpp"
 29 #include "logging/log.hpp"
 30 #include "memory/iterator.hpp"
 31 #include "memory/universe.hpp"
 32 #include "oops/access.inline.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/handles.inline.hpp"
 35 #include "runtime/javaCalls.hpp"
 36 #include "runtime/javaThread.inline.hpp"
 37 #include "runtime/jniHandles.inline.hpp"
 38 #include "runtime/mutexLocker.hpp"
 39 #include "utilities/align.hpp"
 40 #include "utilities/debug.hpp"
 41 
 42 OopStorage* JNIHandles::global_handles() {
 43   return _global_handles;
 44 }
 45 
 46 OopStorage* JNIHandles::weak_global_handles() {
 47   return _weak_global_handles;
 48 }
 49 
 50 // Serviceability agent support.
 51 OopStorage* JNIHandles::_global_handles = nullptr;
 52 OopStorage* JNIHandles::_weak_global_handles = nullptr;
 53 
 54 void jni_handles_init() {
 55   JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);

267     guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
268   }
269   virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
270 };
271 
272 void JNIHandles::verify() {
273   VerifyJNIHandles verify_handle;
274 
275   oops_do(&verify_handle);
276   weak_oops_do(&verify_handle);
277 }
278 
279 // This method is implemented here to avoid circular includes between
280 // jniHandles.hpp and thread.hpp.
281 bool JNIHandles::current_thread_in_native() {
282   Thread* thread = Thread::current();
283   return (thread->is_Java_thread() &&
284           JavaThread::cast(thread)->thread_state() == _thread_in_native);
285 }
286 
287 bool JNIHandles::is_same_object(jobject handle1, jobject handle2) {
288   oop obj1 = resolve_no_keepalive(handle1);
289   oop obj2 = resolve_no_keepalive(handle2);
290 
291   bool ret = obj1 == obj2;
292 
293   if (EnableValhalla) {
294     if (!ret && obj1 != nullptr && obj2 != nullptr && obj1->klass() == obj2->klass() && obj1->klass()->is_inline_klass()) {
295       // The two references are different, they are not null and they are both inline types,
296       // a full substitutability test is required, calling ValueObjectMethods.isSubstitutable()
297       // (similarly to InterpreterRuntime::is_substitutable)
298       JavaThread* THREAD = JavaThread::current();
299       Handle ha(THREAD, obj1);
300       Handle hb(THREAD, obj2);
301       JavaValue result(T_BOOLEAN);
302       JavaCallArguments args;
303       args.push_oop(ha);
304       args.push_oop(hb);
305       methodHandle method(THREAD, Universe::is_substitutable_method());
306       JavaCalls::call(&result, method, &args, THREAD);
307       if (HAS_PENDING_EXCEPTION) {
308         // Something really bad happened because isSubstitutable() should not throw exceptions
309         // If it is an error, just let it propagate
310         // If it is an exception, wrap it into an InternalError
311         if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
312           Handle e(THREAD, PENDING_EXCEPTION);
313           CLEAR_PENDING_EXCEPTION;
314           THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
315         }
316       }
317       ret = result.get_jboolean();
318     }
319   }
320 
321   return ret;
322 }
323 
324 
325 int JNIHandleBlock::_blocks_allocated = 0;
326 
327 static inline bool is_tagged_free_list(uintptr_t value) {
328   return (value & 1u) != 0;
329 }
330 
331 static inline uintptr_t tag_free_list(uintptr_t value) {
332   return value | 1u;
333 }
334 
335 static inline uintptr_t untag_free_list(uintptr_t value) {
336   return value & ~(uintptr_t)1u;
337 }
338 
339 // There is a freelist of handles running through the JNIHandleBlock
340 // with a tagged next pointer, distinguishing these next pointers from
341 // oops. The freelist handling currently relies on the size of oops
342 // being the same as a native pointer. If this ever changes, then
343 // this freelist handling must change too.
344 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
< prev index next >