< prev index next >

src/share/vm/prims/jni.cpp

Print this page




4169     res = JNI_OK;
4170     return res;
4171   } else {
4172     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4173     res = JNI_ERR;
4174     return res;
4175   }
4176 }
4177 
4178 
4179 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4180   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4181 
4182   // Check below commented out from JDK1.2fcs as well
4183   /*
4184   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4185     return JNI_EVERSION;
4186   }
4187   */
4188 
4189   Thread* t = ThreadLocalStorage::get_thread_slow();
4190   if (t != NULL) {
4191     // If the thread has been attached this operation is a no-op
4192     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4193     return JNI_OK;
4194   }
4195 
4196   // Create a thread and mark it as attaching so it will be skipped by the
4197   // ThreadsListEnumerator - see CR 6404306
4198   JavaThread* thread = new JavaThread(true);
4199 
4200   // Set correct safepoint info. The thread is going to call into Java when
4201   // initializing the Java level thread object. Hence, the correct state must
4202   // be set in order for the Safepoint code to deal with it correctly.
4203   thread->set_thread_state(_thread_in_vm);
4204   // Must do this before initialize_thread_local_storage
4205   thread->record_stack_base_and_size();
4206 
4207   thread->initialize_thread_local_storage();
4208 
4209   if (!os::create_attached_thread(thread)) {
4210     delete thread;
4211     return JNI_ERR;
4212   }
4213   // Enable stack overflow checks
4214   thread->create_stack_guard_pages();
4215 
4216   thread->initialize_tlab();
4217 
4218   thread->cache_global_variables();
4219 
4220   // Crucial that we do not have a safepoint check for this thread, since it has
4221   // not been added to the Thread list yet.
4222   { Threads_lock->lock_without_safepoint_check();
4223     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4224     // avoid this thread trying to do a GC before it is added to the thread-list
4225     thread->set_active_handles(JNIHandleBlock::allocate_block());
4226     Threads::add(thread, daemon);
4227     Threads_lock->unlock();


4294 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4295   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4296   if (!vm_created) {
4297   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4298     return JNI_ERR;
4299   }
4300 
4301   JNIWrapper("AttachCurrentThread");
4302   jint ret = attach_current_thread(vm, penv, _args, false);
4303   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4304   return ret;
4305 }
4306 
4307 
4308 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4309   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4310   VM_Exit::block_if_vm_exited();
4311 
4312   JNIWrapper("DetachCurrentThread");
4313 
4314   // If the thread has been deattacted the operations is a no-op
4315   if (ThreadLocalStorage::thread() == NULL) {
4316   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4317     return JNI_OK;
4318   }
4319 
4320   JavaThread* thread = JavaThread::current();
4321   if (thread->has_last_Java_frame()) {
4322   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4323     // Can't detach a thread that's running java, that can't work.
4324     return JNI_ERR;
4325   }
4326 
4327   // Safepoint support. Have to do call-back to safepoint code, if in the
4328   // middel of a safepoint operation
4329   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4330 
4331   // XXX: Note that JavaThread::exit() call below removes the guards on the
4332   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4333   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4334   // of the guards is visible in jni_AttachCurrentThread above,
4335   // the removal of the guards is buried below in JavaThread::exit()


4352   jint ret = JNI_ERR;
4353   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4354 
4355   if (!vm_created) {
4356     *penv = NULL;
4357     ret = JNI_EDETACHED;
4358     return ret;
4359   }
4360 
4361   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4362     return ret;
4363   }
4364 
4365 #ifndef JVMPI_VERSION_1
4366 // need these in order to be polite about older agents
4367 #define JVMPI_VERSION_1   ((jint)0x10000001)
4368 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4369 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4370 #endif // !JVMPI_VERSION_1
4371 
4372   Thread* thread = ThreadLocalStorage::thread();
4373   if (thread != NULL && thread->is_Java_thread()) {
4374     if (Threads::is_supported_jni_version_including_1_1(version)) {
4375       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4376       ret = JNI_OK;
4377       return ret;
4378 
4379     } else if (version == JVMPI_VERSION_1 ||
4380                version == JVMPI_VERSION_1_1 ||
4381                version == JVMPI_VERSION_1_2) {
4382       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4383       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4384       ret = JNI_EVERSION;
4385       return ret;
4386     } else if (JvmtiExport::is_jvmdi_version(version)) {
4387       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4388       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4389       ret = JNI_EVERSION;
4390       return ret;
4391     } else {
4392       *penv = NULL;




4169     res = JNI_OK;
4170     return res;
4171   } else {
4172     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4173     res = JNI_ERR;
4174     return res;
4175   }
4176 }
4177 
4178 
4179 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4180   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4181 
4182   // Check below commented out from JDK1.2fcs as well
4183   /*
4184   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4185     return JNI_EVERSION;
4186   }
4187   */
4188 
4189   Thread* t = Thread::current_or_null();
4190   if (t != NULL) {
4191     // If the thread has been attached this operation is a no-op
4192     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4193     return JNI_OK;
4194   }
4195 
4196   // Create a thread and mark it as attaching so it will be skipped by the
4197   // ThreadsListEnumerator - see CR 6404306
4198   JavaThread* thread = new JavaThread(true);
4199 
4200   // Set correct safepoint info. The thread is going to call into Java when
4201   // initializing the Java level thread object. Hence, the correct state must
4202   // be set in order for the Safepoint code to deal with it correctly.
4203   thread->set_thread_state(_thread_in_vm);

4204   thread->record_stack_base_and_size();
4205   thread->initialize_thread_current();

4206 
4207   if (!os::create_attached_thread(thread)) {
4208     delete thread;
4209     return JNI_ERR;
4210   }
4211   // Enable stack overflow checks
4212   thread->create_stack_guard_pages();
4213 
4214   thread->initialize_tlab();
4215 
4216   thread->cache_global_variables();
4217 
4218   // Crucial that we do not have a safepoint check for this thread, since it has
4219   // not been added to the Thread list yet.
4220   { Threads_lock->lock_without_safepoint_check();
4221     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4222     // avoid this thread trying to do a GC before it is added to the thread-list
4223     thread->set_active_handles(JNIHandleBlock::allocate_block());
4224     Threads::add(thread, daemon);
4225     Threads_lock->unlock();


4292 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4293   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4294   if (!vm_created) {
4295   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4296     return JNI_ERR;
4297   }
4298 
4299   JNIWrapper("AttachCurrentThread");
4300   jint ret = attach_current_thread(vm, penv, _args, false);
4301   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4302   return ret;
4303 }
4304 
4305 
4306 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4307   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4308   VM_Exit::block_if_vm_exited();
4309 
4310   JNIWrapper("DetachCurrentThread");
4311 
4312   // If the thread has already been detached the operation is a no-op
4313   if (Thread::current_or_null() == NULL) {
4314   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4315     return JNI_OK;
4316   }
4317 
4318   JavaThread* thread = JavaThread::current();
4319   if (thread->has_last_Java_frame()) {
4320   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4321     // Can't detach a thread that's running java, that can't work.
4322     return JNI_ERR;
4323   }
4324 
4325   // Safepoint support. Have to do call-back to safepoint code, if in the
4326   // middel of a safepoint operation
4327   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4328 
4329   // XXX: Note that JavaThread::exit() call below removes the guards on the
4330   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4331   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4332   // of the guards is visible in jni_AttachCurrentThread above,
4333   // the removal of the guards is buried below in JavaThread::exit()


4350   jint ret = JNI_ERR;
4351   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4352 
4353   if (!vm_created) {
4354     *penv = NULL;
4355     ret = JNI_EDETACHED;
4356     return ret;
4357   }
4358 
4359   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4360     return ret;
4361   }
4362 
4363 #ifndef JVMPI_VERSION_1
4364 // need these in order to be polite about older agents
4365 #define JVMPI_VERSION_1   ((jint)0x10000001)
4366 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4367 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4368 #endif // !JVMPI_VERSION_1
4369 
4370   Thread* thread = Thread::current_or_null();
4371   if (thread != NULL && thread->is_Java_thread()) {
4372     if (Threads::is_supported_jni_version_including_1_1(version)) {
4373       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4374       ret = JNI_OK;
4375       return ret;
4376 
4377     } else if (version == JVMPI_VERSION_1 ||
4378                version == JVMPI_VERSION_1_1 ||
4379                version == JVMPI_VERSION_1_2) {
4380       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4381       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4382       ret = JNI_EVERSION;
4383       return ret;
4384     } else if (JvmtiExport::is_jvmdi_version(version)) {
4385       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4386       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4387       ret = JNI_EVERSION;
4388       return ret;
4389     } else {
4390       *penv = NULL;


< prev index next >