OSDN Git Service

* include/ext/atomicity.h
[pf3gnuchains/gcc-fork.git] / libjava / interpret-run.cc
index f951981..0591953 100644 (file)
@@ -29,6 +29,10 @@ details.  */
   _Jv_InterpFrame frame_desc (meth, thread);
 #endif
 
+#ifdef DIRECT_THREADED
+  ThreadCountAdjuster adj (meth, &frame_desc);
+#endif // DIRECT_THREADED
+
   _Jv_word stack[meth->max_stack];
   _Jv_word *sp = stack;
 
@@ -361,32 +365,58 @@ details.  */
     }                                                                  \
   while (0)
 
+// We fail to rewrite a breakpoint if there is another thread
+// currently executing this method.  This is a bug, but there's
+// nothing else we can do that doesn't cause a data race.
 #undef REWRITE_INSN
 #define REWRITE_INSN(INSN,SLOT,VALUE)                                  \
-  do {                                                                 \
-    if (pc[-2].insn == breakpoint_insn->insn)                          \
-      {                                                                        \
-       using namespace ::gnu::gcj::jvmti;                              \
-       jlocation location = meth->insn_index (pc - 2);                 \
-       _Jv_RewriteBreakpointInsn (meth->self, location, (pc_t) INSN);  \
-      }                                                                        \
-    else                                                               \
-      pc[-2].insn = INSN;                                              \
+  do                                                                   \
+    {                                                                  \
+      _Jv_MutexLock (&rewrite_insn_mutex);                             \
+      if (meth->thread_count <= 1)                                     \
+       {                                                               \
+         if (pc[-2].insn == breakpoint_insn->insn)                     \
+           {                                                           \
+             using namespace ::gnu::gcj::jvmti;                        \
+             jlocation location = meth->insn_index (pc - 2);           \
+             _Jv_RewriteBreakpointInsn (meth->self, location, (pc_t) INSN); \
+           }                                                           \
+         else                                                          \
+           pc[-2].insn = INSN;                                         \
                                                                        \
-    pc[-1].SLOT = VALUE;                                               \
-  }                                                                    \
+         pc[-1].SLOT = VALUE;                                          \
+       }                                                               \
+      _Jv_MutexUnlock (&rewrite_insn_mutex);                           \
+    }                                                                  \
   while (0)
 
-#else
+#undef INTERP_REPORT_EXCEPTION
+#define INTERP_REPORT_EXCEPTION(Jthrowable) REPORT_EXCEPTION (Jthrowable)
+#else // !DEBUG
 #undef NEXT_INSN
 #define NEXT_INSN goto *((pc++)->insn)
+
+// Rewriting a multi-word instruction in the presence of multiple
+// threads is a data race if a thread reads part of an instruction
+// while some other thread is rewriting that instruction.  We detect
+// more than one thread executing a method and don't rewrite the
+// instruction.  A thread entering a method blocks on
+// rewrite_insn_mutex until the write is complete.
 #define REWRITE_INSN(INSN,SLOT,VALUE)          \
   do {                                         \
-    pc[-2].insn = INSN;                                \
-    pc[-1].SLOT = VALUE;                       \
+    _Jv_MutexLock (&rewrite_insn_mutex);       \
+    if (meth->thread_count <= 1)               \
+      {                                                \
+       pc[-2].insn = INSN;                     \
+       pc[-1].SLOT = VALUE;                    \
+      }                                                \
+    _Jv_MutexUnlock (&rewrite_insn_mutex);     \
   }                                            \
   while (0)
-#endif
+
+#undef INTERP_REPORT_EXCEPTION
+#define INTERP_REPORT_EXCEPTION(Jthrowable) /* not needed when not debugging */
+#endif // !DEBUG
 
 #define INTVAL() ((pc++)->int_val)
 #define AVAL() ((pc++)->datum)
@@ -558,6 +588,7 @@ details.  */
          }
        else
          {
+           NULLCHECK (sp[0].o);
            jobject rcv = sp[0].o;
            _Jv_VTable *table = *(_Jv_VTable**) rcv;
            fun = (void (*)()) table->get_method (rmeth->method->index);
@@ -570,7 +601,7 @@ details.  */
       {
        /* here goes the magic again... */
        ffi_cif *cif = &rmeth->cif;
-       ffi_raw *raw = (ffi_raw*) sp;
+       INTERP_FFI_RAW_TYPE *raw = (INTERP_FFI_RAW_TYPE *) sp;
 
        _Jv_value rvalue;
 
@@ -2355,7 +2386,11 @@ details.  */
        /* VM spec, section 3.11.5 */
        if ((klass->getModifiers() & Modifier::ABSTRACT)
            || klass->isInterface())
-         throw new java::lang::InstantiationException;
+         {
+           jthrowable t = new java::lang::InstantiationException;
+           INTERP_REPORT_EXCEPTION (t);
+           throw t;
+         }
        jobject res = _Jv_AllocObject (klass);
        PUSHA (res);
 
@@ -2422,7 +2457,9 @@ details.  */
     insn_athrow:
       {
        jobject value = POPA();
-       throw static_cast<jthrowable>(value);
+       jthrowable t = static_cast<jthrowable> (value);
+       INTERP_REPORT_EXCEPTION (t);
+       throw t;
       }
       NEXT_INSN;
 
@@ -2608,26 +2645,21 @@ details.  */
 
     insn_breakpoint:
       {
-       JvAssert (JVMTI_REQUESTED_EVENT (Breakpoint));
-
-       // Send JVMTI notification
        using namespace ::java::lang;
        jmethodID method = meth->self;
        jlocation location = meth->insn_index (pc - 1);
-       Thread *thread = Thread::currentThread ();
-       JNIEnv *jni_env = _Jv_GetCurrentJNIEnv ();
 
-       // Save the insn here since the breakpoint could be removed
-       // before the JVMTI notification returns.
        using namespace gnu::gcj::jvmti;
        Breakpoint *bp
          = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
                                              location);
        JvAssert (bp != NULL);
+
+       // Save the insn here since the breakpoint could be removed
+       // before the JVMTI notification returns.
        pc_t opc = reinterpret_cast<pc_t> (bp->getInsn ());
 
-       _Jv_JVMTI_PostEvent (JVMTI_EVENT_BREAKPOINT, thread, jni_env,
-                            method, location);
+       bp->execute ();
 
        // Continue execution
 #ifdef DIRECT_THREADED
@@ -2639,10 +2671,6 @@ details.  */
     }
   catch (java::lang::Throwable *ex)
     {
-#ifdef DEBUG
-       // This needs to be done before the pc is changed.
-       jlong throw_loc = meth->insn_index (pc);
-#endif
       // Check if the exception is handled and, if so, set the pc to the start
       // of the appropriate catch block.
       if (meth->check_handler (&pc, meth, ex))
@@ -2650,27 +2678,19 @@ details.  */
           sp = stack;
           sp++->o = ex; // Push exception.
 #ifdef DEBUG
-          if (JVMTI_REQUESTED_EVENT (Exception))
+          if (JVMTI_REQUESTED_EVENT (ExceptionCatch))
             {
               using namespace gnu::gcj::jvmti;
-              jlong throw_meth = reinterpret_cast<jlong> (meth->get_method ());
+              jlong catch_meth = reinterpret_cast<jlong> (meth->get_method ());
               jlong catch_loc = meth->insn_index (pc);
-              ExceptionEvent::postExceptionEvent (thread, throw_meth,
-                                                  throw_loc, ex, throw_meth,
-                                                  catch_loc);
+             _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION_CATCH, thread,
+                                  _Jv_GetCurrentJNIEnv (), catch_meth,
+                                  catch_loc, ex);
             }
 #endif
           NEXT_INSN;
         }
-#ifdef DEBUG
-      if (JVMTI_REQUESTED_EVENT (Exception))
-        {
-          using namespace gnu::gcj::jvmti;
-          jlong throw_meth = reinterpret_cast<jlong> (meth->get_method ());
-          ExceptionEvent::postExceptionEvent (thread, throw_meth, throw_loc,
-                                              ex, NULL, NULL);
-        }
-#endif
+
       // No handler, so re-throw.
       throw ex;
     }