< prev index next >

src/share/vm/prims/jni.cpp

Print this page




4137     res = JNI_OK;
4138     return res;
4139   } else {
4140     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4141     res = JNI_ERR;
4142     return res;
4143   }
4144 }
4145 
4146 
4147 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4148   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4149 
4150   // Check below commented out from JDK1.2fcs as well
4151   /*
4152   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4153     return JNI_EVERSION;
4154   }
4155   */
4156 
4157   Thread* t = ThreadLocalStorage::get_thread_slow();
4158   if (t != NULL) {
4159     // If the thread has been attached this operation is a no-op
4160     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4161     return JNI_OK;
4162   }
4163 
4164   // Create a thread and mark it as attaching so it will be skipped by the
4165   // ThreadsListEnumerator - see CR 6404306
4166   JavaThread* thread = new JavaThread(true);
4167 
4168   // Set correct safepoint info. The thread is going to call into Java when
4169   // initializing the Java level thread object. Hence, the correct state must
4170   // be set in order for the Safepoint code to deal with it correctly.
4171   thread->set_thread_state(_thread_in_vm);
4172   // Must do this before initialize_thread_local_storage
4173   thread->record_stack_base_and_size();
4174 
4175   thread->initialize_thread_local_storage();
4176 
4177   if (!os::create_attached_thread(thread)) {
4178     delete thread;
4179     return JNI_ERR;
4180   }
4181   // Enable stack overflow checks
4182   thread->create_stack_guard_pages();
4183 
4184   thread->initialize_tlab();
4185 
4186   thread->cache_global_variables();
4187 
4188   // Crucial that we do not have a safepoint check for this thread, since it has
4189   // not been added to the Thread list yet.
4190   { Threads_lock->lock_without_safepoint_check();
4191     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4192     // avoid this thread trying to do a GC before it is added to the thread-list
4193     thread->set_active_handles(JNIHandleBlock::allocate_block());
4194     Threads::add(thread, daemon);
4195     Threads_lock->unlock();


4262 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4263   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4264   if (!vm_created) {
4265   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4266     return JNI_ERR;
4267   }
4268 
4269   JNIWrapper("AttachCurrentThread");
4270   jint ret = attach_current_thread(vm, penv, _args, false);
4271   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4272   return ret;
4273 }
4274 
4275 
4276 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4277   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4278   VM_Exit::block_if_vm_exited();
4279 
4280   JNIWrapper("DetachCurrentThread");
4281 
4282   // If the thread has been deattacted the operations is a no-op
4283   if (ThreadLocalStorage::thread() == NULL) {
4284   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4285     return JNI_OK;
4286   }
4287 
4288   JavaThread* thread = JavaThread::current();
4289   if (thread->has_last_Java_frame()) {
4290   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4291     // Can't detach a thread that's running java, that can't work.
4292     return JNI_ERR;
4293   }
4294 
4295   // Safepoint support. Have to do call-back to safepoint code, if in the
4296   // middel of a safepoint operation
4297   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4298 
4299   // XXX: Note that JavaThread::exit() call below removes the guards on the
4300   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4301   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4302   // of the guards is visible in jni_AttachCurrentThread above,
4303   // the removal of the guards is buried below in JavaThread::exit()


4320   jint ret = JNI_ERR;
4321   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4322 
4323   if (!vm_created) {
4324     *penv = NULL;
4325     ret = JNI_EDETACHED;
4326     return ret;
4327   }
4328 
4329   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4330     return ret;
4331   }
4332 
4333 #ifndef JVMPI_VERSION_1
4334 // need these in order to be polite about older agents
4335 #define JVMPI_VERSION_1   ((jint)0x10000001)
4336 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4337 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4338 #endif // !JVMPI_VERSION_1
4339 
4340   Thread* thread = ThreadLocalStorage::thread();
4341   if (thread != NULL && thread->is_Java_thread()) {
4342     if (Threads::is_supported_jni_version_including_1_1(version)) {
4343       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4344       ret = JNI_OK;
4345       return ret;
4346 
4347     } else if (version == JVMPI_VERSION_1 ||
4348                version == JVMPI_VERSION_1_1 ||
4349                version == JVMPI_VERSION_1_2) {
4350       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4351       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4352       ret = JNI_EVERSION;
4353       return ret;
4354     } else if (JvmtiExport::is_jvmdi_version(version)) {
4355       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4356       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4357       ret = JNI_EVERSION;
4358       return ret;
4359     } else {
4360       *penv = NULL;




4137     res = JNI_OK;
4138     return res;
4139   } else {
4140     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4141     res = JNI_ERR;
4142     return res;
4143   }
4144 }
4145 
4146 
4147 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4148   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4149 
4150   // Check below commented out from JDK1.2fcs as well
4151   /*
4152   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4153     return JNI_EVERSION;
4154   }
4155   */
4156 
4157   Thread* t = Thread::current_or_null();
4158   if (t != NULL) {
4159     // If the thread has been attached this operation is a no-op
4160     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4161     return JNI_OK;
4162   }
4163 
4164   // Create a thread and mark it as attaching so it will be skipped by the
4165   // ThreadsListEnumerator - see CR 6404306
4166   JavaThread* thread = new JavaThread(true);
4167 
4168   // Set correct safepoint info. The thread is going to call into Java when
4169   // initializing the Java level thread object. Hence, the correct state must
4170   // be set in order for the Safepoint code to deal with it correctly.
4171   thread->set_thread_state(_thread_in_vm);

4172   thread->record_stack_base_and_size();
4173   thread->initialize_thread_current();

4174 
4175   if (!os::create_attached_thread(thread)) {
4176     delete thread;
4177     return JNI_ERR;
4178   }
4179   // Enable stack overflow checks
4180   thread->create_stack_guard_pages();
4181 
4182   thread->initialize_tlab();
4183 
4184   thread->cache_global_variables();
4185 
4186   // Crucial that we do not have a safepoint check for this thread, since it has
4187   // not been added to the Thread list yet.
4188   { Threads_lock->lock_without_safepoint_check();
4189     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4190     // avoid this thread trying to do a GC before it is added to the thread-list
4191     thread->set_active_handles(JNIHandleBlock::allocate_block());
4192     Threads::add(thread, daemon);
4193     Threads_lock->unlock();


4260 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4261   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4262   if (!vm_created) {
4263   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4264     return JNI_ERR;
4265   }
4266 
4267   JNIWrapper("AttachCurrentThread");
4268   jint ret = attach_current_thread(vm, penv, _args, false);
4269   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4270   return ret;
4271 }
4272 
4273 
4274 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4275   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4276   VM_Exit::block_if_vm_exited();
4277 
4278   JNIWrapper("DetachCurrentThread");
4279 
4280   // If the thread has already been detached the operations is a no-op
4281   if (Thread::current_or_null() == NULL) {
4282   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4283     return JNI_OK;
4284   }
4285 
4286   JavaThread* thread = JavaThread::current();
4287   if (thread->has_last_Java_frame()) {
4288   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4289     // Can't detach a thread that's running java, that can't work.
4290     return JNI_ERR;
4291   }
4292 
4293   // Safepoint support. Have to do call-back to safepoint code, if in the
4294   // middel of a safepoint operation
4295   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4296 
4297   // XXX: Note that JavaThread::exit() call below removes the guards on the
4298   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4299   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4300   // of the guards is visible in jni_AttachCurrentThread above,
4301   // the removal of the guards is buried below in JavaThread::exit()


4318   jint ret = JNI_ERR;
4319   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4320 
4321   if (!vm_created) {
4322     *penv = NULL;
4323     ret = JNI_EDETACHED;
4324     return ret;
4325   }
4326 
4327   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4328     return ret;
4329   }
4330 
4331 #ifndef JVMPI_VERSION_1
4332 // need these in order to be polite about older agents
4333 #define JVMPI_VERSION_1   ((jint)0x10000001)
4334 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4335 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4336 #endif // !JVMPI_VERSION_1
4337 
4338   Thread* thread = Thread::current_or_null();
4339   if (thread != NULL && thread->is_Java_thread()) {
4340     if (Threads::is_supported_jni_version_including_1_1(version)) {
4341       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4342       ret = JNI_OK;
4343       return ret;
4344 
4345     } else if (version == JVMPI_VERSION_1 ||
4346                version == JVMPI_VERSION_1_1 ||
4347                version == JVMPI_VERSION_1_2) {
4348       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4349       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4350       ret = JNI_EVERSION;
4351       return ret;
4352     } else if (JvmtiExport::is_jvmdi_version(version)) {
4353       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4354       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4355       ret = JNI_EVERSION;
4356       return ret;
4357     } else {
4358       *penv = NULL;


< prev index next >