You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by va...@apache.org on 2006/11/17 15:19:47 UTC

svn commit: r476148 - in /incubator/harmony/enhanced/drlvm/trunk/vm: include/open/ thread/src/

Author: varlax
Date: Fri Nov 17 06:19:46 2006
New Revision: 476148

URL: http://svn.apache.org/viewvc?view=rev&rev=476148
Log:
Applied HARMONY-1789 + HARMONY-2217 Improved interrupt() implementation
Tested on SUSE9-32, Win32.

Modified:
    incubator/harmony/enhanced/drlvm/trunk/vm/include/open/hythread_ext.h
    incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c
    incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_fat_monitor.c
    incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_interrupt.c
    incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_park.c
    incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_private.h

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/include/open/hythread_ext.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/include/open/hythread_ext.h?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/include/open/hythread_ext.h (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/include/open/hythread_ext.h Fri Nov 17 06:19:46 2006
@@ -311,6 +311,7 @@
 #define TM_THREAD_STATE_SLEEPING JVMTI_THREAD_STATE_SLEEPING // 0x0040  Thread is sleeping -- Thread.sleep(long).  
 #define TM_THREAD_STATE_IN_MONITOR_WAIT JVMTI_THREAD_STATE_IN_OBJECT_WAIT // 0x0100  Thread is waiting on an object monitor -- Object.wait.  
 #define TM_THREAD_STATE_PARKED JVMTI_THREAD_STATE_PARKED // 0x0200  Thread is parked, for example: LockSupport.park, LockSupport.parkUtil and LockSupport.parkNanos.  
+#define TM_THREAD_STATE_UNPARKED  0x0800 // 0x0800  Thread is unparked, to track staled unparks;
 #define TM_THREAD_STATE_SUSPENDED JVMTI_THREAD_STATE_SUSPENDED // 0x100000  Thread suspended. java.lang.Thread.suspend() or a JVMTI suspend function (such as SuspendThread) has been called on the thread. If this bit is set, the other bits refer to the thread state before suspension.  
 #define TM_THREAD_STATE_INTERRUPTED JVMTI_THREAD_STATE_INTERRUPTED // 0x200000  Thread has been interrupted.  
 #define TM_THREAD_STATE_IN_NATIVE JVMTI_THREAD_STATE_IN_NATIVE // 0x400000  Thread is in native code--that is, a native method is running which has not called back into the VM or Java programming language code. 

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c Fri Nov 17 06:19:46 2006
@@ -426,7 +426,12 @@
     // Report error in case current thread is not attached
     if (!thread) return TM_ERROR_UNATTACHED_THREAD;
     
-    status = sem_wait_impl(thread->sleep_event, millis, nanos, interruptable);
+    hymutex_lock(thread->mutex);
+    thread->state |= TM_THREAD_STATE_SLEEPING;
+    status = condvar_wait_impl(thread->condition, thread->mutex, millis, nanos, interruptable);
+    thread->state &= ~TM_THREAD_STATE_SLEEPING;
+    hymutex_unlock(thread->mutex);
+
     return (status == TM_ERROR_INTERRUPT && interruptable) ? TM_ERROR_INTERRUPT : TM_ERROR_NONE;
 }
 
@@ -644,9 +649,9 @@
     assert (status == TM_ERROR_NONE);
     status = hysem_create(&ptr->resume_event, 0, 1);
     assert (status == TM_ERROR_NONE);
-    status = hysem_create(&ptr->park_event, 0, 1);
+    status = hymutex_create(&ptr->mutex, TM_MUTEX_NESTED);
     assert (status == TM_ERROR_NONE);
-    status = hysem_create(&ptr->sleep_event, 0, 1);
+    status = hycond_create(&ptr->condition);
     assert (status == TM_ERROR_NONE);
     
     ptr->state = TM_THREAD_STATE_ALLOCATED;
@@ -674,10 +679,6 @@
     status = hylatch_set(thread->safe_region_event, 1);
     assert (status == TM_ERROR_NONE);
     status = hysem_set(thread->resume_event, 0);
-    assert (status == TM_ERROR_NONE);
-    status = hysem_set(thread->park_event, 0);
-    assert (status == TM_ERROR_NONE);
-    status = hysem_set(thread->sleep_event, 0);
     assert (status == TM_ERROR_NONE);
     
     thread->state = TM_THREAD_STATE_ALLOCATED;

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_fat_monitor.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_fat_monitor.c?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_fat_monitor.c (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_fat_monitor.c Fri Nov 17 06:19:46 2006
@@ -174,7 +174,6 @@
         mon_ptr->last_wait=tm_self_tls;
 #endif
     
-    mon_ptr->wait_count++;
         saved_recursion = mon_ptr->recursion_count;
         
     assert(saved_recursion>=0);
@@ -199,10 +198,20 @@
         if (status != TM_ERROR_NONE) return status;
     set_suspend_disable(saved_disable_count);
 
-#else 
-    //printf("starting wait: %x, %x \n", mon_ptr->condition, hythread_self());
-        status =  condvar_wait_impl(mon_ptr->condition, mon_ptr->mutex, ms, nano, interruptable);
-    //printf("finishing wait: %x, %x \n", mon_ptr->condition, hythread_self());
+#else
+    mon_ptr->wait_count++;
+    hymutex_lock(self->mutex);
+    self->current_condition = mon_ptr->condition;
+    self->state |= TM_THREAD_STATE_IN_MONITOR_WAIT;
+    hymutex_unlock(self->mutex);
+
+    status =  condvar_wait_impl(mon_ptr->condition, mon_ptr->mutex, ms, nano, interruptable);
+
+    hymutex_lock(self->mutex);
+    self->state &= ~TM_THREAD_STATE_IN_MONITOR_WAIT;
+    self->current_condition = NULL;
+    hymutex_unlock(self->mutex);
+    mon_ptr->wait_count--;
 #endif
         if(self->suspend_request) {
             hymutex_unlock(mon_ptr->mutex);
@@ -308,7 +317,6 @@
     if(mon_ptr->owner != tm_self_tls) {
         return TM_ERROR_ILLEGAL_STATE;
     }
-    mon_ptr->wait_count = 0;
 #ifdef NO_COND_VARS
         mon_ptr->notify_flag=1;
         return TM_ERROR_NONE;
@@ -334,9 +342,6 @@
 IDATA VMCALL hythread_monitor_notify(hythread_monitor_t mon_ptr) {      
     if(mon_ptr->owner != tm_self_tls) {
         return TM_ERROR_ILLEGAL_STATE;
-    }
-    if (mon_ptr->wait_count > 0){
-        mon_ptr->wait_count--;
     }
 #ifdef NO_COND_VARS
         mon_ptr->notify_flag=1;

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_interrupt.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_interrupt.c?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_interrupt.c (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_interrupt.c Fri Nov 17 06:19:46 2006
@@ -40,13 +40,25 @@
  */
 void VMCALL hythread_interrupt(hythread_t thread) {
     IDATA status;
+    hymutex_lock(thread->mutex);
     thread->state |= TM_THREAD_STATE_INTERRUPTED;
     
-    // If thread was doing any kind of wait, notify it.
-    if (thread->current_condition) {
-            status=hycond_notify_all(thread->current_condition);   
-                        assert (status == TM_ERROR_NONE);
+    if (thread == tm_self_tls) {
+        hymutex_unlock(thread->mutex);
+        return;
     }
+
+    if (thread->state
+            & (TM_THREAD_STATE_PARKED | TM_THREAD_STATE_SLEEPING
+                | TM_THREAD_STATE_IN_MONITOR_WAIT)) {
+        // If thread was doing any kind of wait, notify it.
+        if (thread->current_condition) {
+            status = hycond_notify_all(thread->current_condition);
+            assert(status == TM_ERROR_NONE);
+        }
+    }
+
+    hymutex_unlock(thread->mutex);
 }
 
 /** 
@@ -56,9 +68,12 @@
  * @returns TM_ERROR_INTERRUPT if thread was interruped, TM_ERROR_NONE otherwise
  */
 UDATA VMCALL hythread_clear_interrupted_other(hythread_t thread) {
-        int interrupted = thread->state & TM_THREAD_STATE_INTERRUPTED;
-    thread->state &= ~TM_THREAD_STATE_INTERRUPTED;    
-        return interrupted?TM_ERROR_INTERRUPT:TM_ERROR_NONE;
+    int interrupted;
+    hymutex_lock(thread->mutex);
+    interrupted = thread->state & TM_THREAD_STATE_INTERRUPTED;
+    thread->state &= ~TM_THREAD_STATE_INTERRUPTED;
+    hymutex_unlock(thread->mutex);
+    return interrupted ? TM_ERROR_INTERRUPT : TM_ERROR_NONE;
 }
 
 /**

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_park.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_park.c?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_park.c (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_park.c Fri Nov 17 06:19:46 2006
@@ -47,16 +47,28 @@
  * @see hythread_unpark
  */
 IDATA VMCALL hythread_park(I_64 millis, IDATA nanos) {
-     IDATA status;
+     IDATA status = 0;
      hythread_t t = tm_self_tls;     
      assert(t);
-     status = hysem_wait_interruptable(t->park_event, millis, nanos);
+
+     hymutex_lock(t->mutex);
+
+     if (t->state & TM_THREAD_STATE_UNPARKED) {
+        t->state &= ~TM_THREAD_STATE_UNPARKED;
+        hymutex_unlock(t->mutex);
+        return (t->state & TM_THREAD_STATE_INTERRUPTED) ? TM_ERROR_INTERRUPT : TM_ERROR_NONE;
+     }
+
+     t->state |= TM_THREAD_STATE_PARKED;
+     status = hycond_wait_interruptable(t->condition, t->mutex, millis, nanos);
+     t->state &= ~TM_THREAD_STATE_PARKED;
+
      //the status should be restored for j.u.c.LockSupport
-     ////
      if (status == TM_ERROR_INTERRUPT) {
          t->state |= TM_THREAD_STATE_INTERRUPTED;
      }
 
+     hymutex_unlock(t->mutex);
      return status;
 }
 
@@ -72,11 +84,18 @@
  * @see hythread_park
  */
 void VMCALL hythread_unpark(hythread_t thread) {
-    IDATA UNUSED status;
     if(thread ==  NULL) {
         return;
     }
     
-    status = hysem_post(thread->park_event);
-        assert(status == TM_ERROR_NONE);
+    hymutex_lock(thread->mutex);
+
+    if (thread->state & TM_THREAD_STATE_PARKED) {
+        thread->state &= ~TM_THREAD_STATE_PARKED;
+        hycond_notify_all(thread->condition);
+    } else {
+        thread->state |= TM_THREAD_STATE_UNPARKED;
+    }
+
+    hymutex_unlock(thread->mutex);
 }

Modified: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_private.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_private.h?view=diff&rev=476148&r1=476147&r2=476148
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_private.h (original)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_private.h Fri Nov 17 06:19:46 2006
@@ -250,15 +250,15 @@
 
 // Synchronization stuff
 
-    /**
-     * Event reserved for parking threads.
+    /*
+     * Thread local lock, used to serialize thread state;
      */
-    hysem_t park_event;
-    
-    /**
-     * Event reserved for sleeping threads.
+    hymutex_t mutex;
+
+    /*
+     * Conditional variable used to implement wait function for sleep/park;
      */
-    hysem_t sleep_event;    
+    hycond_t condition;
 
     /**
      * Event reserved for threads that invoke join.
@@ -269,8 +269,7 @@
      * Current conditional variable thread is waiting on (used for interrupting)
      */
     hycond_t current_condition;
-           
-              
+
 // State
 
     /**