< prev index next >

src/share/vm/prims/whitebox.cpp

Print this page

        

@@ -813,30 +813,63 @@
   }
   NMethodSweeper::possibly_sweep();
 }
 
 JavaThread* WhiteBox::create_sweeper_thread(TRAPS) {
+  Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, THREAD);
+  instanceKlassHandle klass(THREAD, k);
+  instanceHandle thread_oop = klass->allocate_instance_handle(THREAD);
+
+  Handle name = java_lang_String::create_from_str("WB Sweeper thread", THREAD);
+
+  // Initialize thread_oop to put it into the system threadGroup
+  Handle thread_group(THREAD, Universe::system_thread_group());
+  JavaValue result(T_VOID);
+  JavaCalls::call_special(&result,
+                          thread_oop,
+                          klass,
+                          vmSymbols::object_initializer_name(),
+                          vmSymbols::threadgroup_string_void_signature(),
+                          thread_group,
+                          name,
+                          THREAD);
+
+  KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
+  JavaCalls::call_special(&result,
+                          thread_group,
+                          group,
+                          vmSymbols::add_method_name(),
+                          vmSymbols::thread_void_signature(),
+                          thread_oop,             // ARG 1
+                          THREAD);
+
+  {
+    MutexLocker mu(Threads_lock);
+
   // create sweeper thread w/ custom entry -- one iteration instead of loop
   CodeCacheSweeperThread* sweeper_thread = new CodeCacheSweeperThread();
   sweeper_thread->set_entry_point(&WhiteBox::sweeper_thread_entry);
 
-  // create j.l.Thread object and associate it w/ sweeper thread
-  {
-    // inherit deamon property from current thread
-    bool is_daemon = java_lang_Thread::is_daemon(JavaThread::current()->threadObj());
+    // check that thread and osthread were created
+    if (sweeper_thread == NULL || sweeper_thread->osthread() == NULL) {
+      vm_exit_during_initialization("java.lang.OutOfMemoryError",
+                                    os::native_thread_creation_failed_msg());
+    }
 
-    HandleMark hm(THREAD);
-    Handle thread_group(THREAD, Universe::system_thread_group());
-    const char* name = "WB Sweeper thread";
-    sweeper_thread->allocate_threadObj(thread_group, name, is_daemon, THREAD);
+    java_lang_Thread::set_thread(thread_oop(), sweeper_thread);
+    // inherit daemon property from current thread
+    if (java_lang_Thread::is_daemon(JavaThread::current()->threadObj())) {
+      java_lang_Thread::set_daemon(thread_oop());
   }
 
-  {
-    MutexLocker mu(Threads_lock, THREAD);
+    // Make sure that both the thread_oop is set and the thread is added to
+    // the threads list before a safepoint can happen. Otherwise, a GC may
+    // move the thread object without updating the oop.
+    sweeper_thread->set_threadObj(thread_oop());
     Threads::add(sweeper_thread);
-  }
   return sweeper_thread;
+  }
 }
 
 WB_ENTRY(jobject, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
   JavaThread* sweeper_thread = WhiteBox::create_sweeper_thread(Thread::current());
   if (sweeper_thread == NULL) {
< prev index next >