harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r434076 [9/18] - in /incubator/harmony/enhanced/drlvm/trunk: build/make/components/ build/make/components/vm/ build/make/targets/ build/patches/lnx/ build/patches/lnx/APR/ build/patches/lnx/APR/threadproc/ build/patches/lnx/APR/threadproc/u...
Date Wed, 23 Aug 2006 16:49:21 GMT
Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_basic.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_basic.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_basic.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_basic.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,677 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Nikolay Kuznetsov
+ * @version $Revision: 1.1.2.21 $
+ */  
+
+/**
+ * @file thread_java_basic.c
+ * @brief Key threading operations like thread creation and pointer conversion.
+ */
+
+#include <open/jthread.h>
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+#include "jni.h"
+
+#undef LOG_DOMAIN
+#define LOG_DOMAIN "tm.java"
+
+void stop_callback(void);
+jmethodID getRunMethod(JNIEnv *env);
+
+typedef struct  {
+    JNIEnv *jenv;
+    jthread thread;
+    jboolean daemon;
+        jvmtiEnv *tiEnv;
+        jvmtiStartFunction tiProc;
+        void *tiProcArgs;
+} wrapper_proc_data;
+
+
+IDATA associate_native_and_java_thread(JNIEnv* env, jthread java_thread, hythread_t  tm_native_thread, jobject thread_ref);
+
+/**
+ * Creates new Java thread.
+ *
+ * The newly created thread will immediately start to execute the <code>run()</code>
+ * method of the appropriate <code>thread</code> object.
+ *
+ * @param[in] env JNI environment that will be associated with the created Java thread
+ * @param[in] java_thread Java thread object with which new thread must be associated.
+ * @param[in] attrs thread attributes.
+ * @sa java.lang.Thread.run()
+ */
+IDATA jthread_create(JNIEnv* env, jthread java_thread, jthread_threadattr_t *attrs) {
+
+        IDATA status;
+        status = jthread_create_with_function(env,java_thread,attrs,NULL,NULL);
+        return status;
+}
+
+int wrapper_proc(void *arg) {
+
+    IDATA status,status1;
+    wrapper_proc_data *data = (wrapper_proc_data *)arg;
+    JNIEnv *env = data->jenv;
+    jvmtiEnv *tiEnv = data->tiEnv;
+    jvmtiStartFunction tiProc  = data->tiProc;
+    void *tiProcArgs =  data->tiProcArgs;
+
+    TRACE(("TM: Java thread started: id=%d OS_handle=%p", hythread_self()->thread_id, apr_os_thread_current()));
+    //status = hythread_global_lock();
+    //assert (status == TM_ERROR_NONE);
+    status=vm_attach();
+    if(status!=TM_ERROR_NONE)
+    {
+        if (!data->daemon){
+            status1 = countdown_nondaemon_threads();
+            assert (status1 == TM_ERROR_NONE);
+        }
+        return status;
+    }
+    jvmti_send_thread_start_end_event(1);
+    //status = hythread_global_unlock();
+    //  assert (status == TM_ERROR_NONE);
+        if(tiProc!=NULL)
+        {
+                tiProc(tiEnv, env, tiProcArgs);
+        }
+        else
+        {
+                (*env) -> CallVoidMethodA(env, data->thread, getRunMethod(env), NULL);//for jthread_create();
+        }
+
+        jvmti_send_thread_start_end_event(0);
+    (*env) -> DeleteGlobalRef(env, data->thread);
+    TRACE(("TM: Java thread finished: id=%d OS_handle=%p", hythread_self()->thread_id, apr_os_thread_current()));
+        assert(hythread_is_suspend_enabled());
+    status=vm_detach();
+    if(status!=TM_ERROR_NONE)
+    {
+        if (!data->daemon){
+            status1 = countdown_nondaemon_threads();
+            assert (status1 == TM_ERROR_NONE);
+        }
+        return status;
+    }
+    assert(hythread_is_suspend_enabled());
+        if (!data->daemon){
+                status = countdown_nondaemon_threads();
+                assert (status == TM_ERROR_NONE);
+        }
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Creates new Java thread with specific execution function.
+ *
+ * This function differs from <code>jthread_create</code> such that
+ * the newly created thread, instead of invoking Java execution engine,
+ * would start to directly execute the specific native function pointed by the <code>proc</code>.
+ * This method of thread creation would be useful for creating TI agent threads (i.e. Java threads
+ * which always execute only native code).
+ *
+ * @param[in] env JNI environment that will be associated with the created Java thread
+ * @param[in] java_thread Java thread object with which new thread must be associated.
+ * @param[in] attrs thread attributes.
+ * @param[in] proc the start function to be executed in this thread.
+ * @param[in] arg The argument to the start function. Is passed as an array.
+ * @sa JVMTI::RunAgentThread()
+ */
+IDATA jthread_create_with_function(JNIEnv *env, jthread java_thread, jthread_threadattr_t *attrs,jvmtiStartFunction proc, const void* arg)
+{
+        hythread_t tm_native_thread = NULL;
+    jvmti_thread_t tm_java_thread = NULL;
+    wrapper_proc_data *data;
+        IDATA status;
+        apr_status_t apr_status;
+        apr_pool_t * pool;
+
+        if (env == NULL || java_thread == NULL || attrs == NULL){
+            return TM_ERROR_NULL_POINTER;
+        }
+    tm_native_thread = vm_jthread_get_tm_data(java_thread);
+        
+    //This is for irregular use. In ordinary live valid jthread instance
+    //contains weak reference associated with it and native thread to reuse 
+    //if any
+    ////
+
+    if (tm_native_thread==NULL)
+        {
+       if(!jthread_thread_init(NULL,env,java_thread, NULL, 0))
+		 {
+			 return TM_ERROR_OUT_OF_MEMORY;
+		 }
+                tm_native_thread = vm_jthread_get_tm_data(java_thread);
+        }
+    tm_java_thread = hythread_get_private_data(tm_native_thread);
+    assert(tm_java_thread);    
+    apr_status = apr_pool_create(&pool, 0);
+	if (apr_status != APR_SUCCESS) {
+		return CONVERT_ERROR(apr_status);
+	}
+    data = apr_palloc(pool, sizeof(wrapper_proc_data));
+        if(data == NULL) {
+                return TM_ERROR_OUT_OF_MEMORY;
+        }
+
+    // Prepare argumets for wrapper proc
+    data->jenv = env;
+    data->thread = tm_java_thread->thread_object; 
+    data->daemon = attrs->daemon;
+        data->tiEnv  = attrs->jvmti_env;
+    data->tiProc = proc;
+    data->tiProcArgs = (void *)arg;
+    
+    // create native thread with wrapper_proc
+        if (!attrs->daemon){
+         status = increase_nondaemon_threads_count();
+                 if (status != TM_ERROR_NONE) return status;
+        }
+        status = hythread_create(&tm_native_thread, (attrs->stacksize)?attrs->stacksize:1024000,
+                              attrs->priority, 0, wrapper_proc, data);
+    if ((!attrs->daemon)&&(status != TM_ERROR_NONE)){
+        countdown_nondaemon_threads();
+    }
+    TRACE(("TM: Created thread: id=%d", tm_native_thread->thread_id));
+
+        return status;
+}
+
+/**
+ * Attaches the current native thread to Java VM.
+ *
+ * This function will create a control structure for Java thread
+ * and associate it with the current native thread. Nothing happens
+ * if this thread is already attached.
+ *
+ * @param[in] env JNI environment that will be associated with the attached Java thread
+ * @param[in] java_thread Java thread object with which the current native thread must be associated.
+ * @sa JNI::AttachCurrentThread ()
+ */
+IDATA jthread_attach(JNIEnv* env, jthread java_thread) {        
+
+    hythread_t tm_native_thread;
+    IDATA status;
+        status = hythread_attach(NULL);
+    if (status != TM_ERROR_NONE){
+        return status;
+    }
+        tm_native_thread = hythread_self();
+        //I wonder if we need it.
+    //since jthread already created, thus association is already done,
+    //see jthread_init
+    //VVVVVVVVVVVVVVVVVVVVVV
+    status=associate_native_and_java_thread(env, java_thread, tm_native_thread, NULL);
+    if (status != TM_ERROR_NONE){
+        return status;
+    }
+    TRACE(("TM: Current thread attached to jthread=%p", java_thread));
+    status=vm_attach();
+        return status;
+}
+
+/**
+ * Associates the Java thread with the native thread.
+ *
+ * @param[in] ret_thread not used
+ * @param[in] env JNI environment that will be associated with the created Java thread
+ * @param[in] java_thread the Java thread for the association
+ * @param[in] weak_ref java.lang.WeakReference to the <code>java_thread</code> that used for native resource deletion
+ * @param[in] old_thread the native thread for the association
+ * @return the native thread
+ */
+jlong jthread_thread_init(jvmti_thread_t *ret_thread, JNIEnv* env, jthread java_thread, jobject weak_ref, jlong old_thread) {
+    hythread_t     tm_native_thread = NULL;
+    jvmti_thread_t tmj_thread;
+        IDATA status;
+
+    if (old_thread) {
+        tm_native_thread = (hythread_t)((IDATA)old_thread);
+        tmj_thread = (jvmti_thread_t)hythread_get_private_data(tm_native_thread);
+        //delete used weak reference
+        if (tmj_thread->thread_ref) (*env)->DeleteGlobalRef(env, tmj_thread->thread_ref);
+        
+    }
+        status = hythread_struct_init(&tm_native_thread, NULL);
+        if(status != TM_ERROR_NONE)
+		{
+			return 0;
+		}
+        status=associate_native_and_java_thread(env, java_thread, tm_native_thread, weak_ref);
+        if(status != TM_ERROR_NONE)
+		{
+			return 0;
+		}
+    return (jlong)((IDATA)tm_native_thread);
+}
+
+/**
+ * Detaches the selected thread from java VM.
+ *
+ * This function will release any resources associated with the given thread.
+ *
+ * @param[in] java_thread Java thread to be detached
+ */
+IDATA jthread_detach(jthread java_thread) {
+    jvmti_thread_t tm_java_thread;
+    hythread_t tm_native_thread;
+     
+        // Check input arg
+    assert(java_thread);
+        TRACE(("TM: jthread_detach %x", hythread_self()));
+
+        tm_native_thread = vm_jthread_get_tm_data(java_thread);
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+
+        // Remove tm_thread_t pointer from java.lang.Thread object
+    vm_jthread_set_tm_data(java_thread, NULL);
+ 
+        vm_detach();     
+
+    // Deallocate tm_java_thread 
+    apr_pool_destroy(tm_java_thread->pool);
+
+    // Remove tm_jthread_t pointer from *tm_native_thread      
+    /*
+    status = hythread_set_private_data(tm_native_thread, NULL);
+    if (status != TM_ERROR_NONE){
+        return status;
+    }*/
+        assert(hythread_is_suspend_enabled());
+    return TM_ERROR_NONE;    
+}
+
+IDATA associate_native_and_java_thread(JNIEnv* env, jthread java_thread, hythread_t tm_native_thread, jobject thread_ref)
+{
+        IDATA status;
+    apr_status_t apr_status;
+    apr_pool_t *pool;
+        jvmti_thread_t tm_java_thread;
+        if ((env == NULL) || (java_thread == NULL)||(tm_native_thread==NULL)){
+        return TM_ERROR_NULL_POINTER;
+    }
+    
+
+    tm_java_thread = hythread_get_private_data(tm_native_thread);
+    if (!tm_java_thread) {
+        apr_status = apr_pool_create(&pool, 0);
+            if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+        if (pool==NULL) return TM_ERROR_OUT_OF_MEMORY;
+        tm_java_thread = apr_palloc(pool, sizeof(JVMTIThread));
+            if(tm_java_thread == NULL) {
+                    return TM_ERROR_OUT_OF_MEMORY;
+            }
+    
+        tm_java_thread->pool = pool;
+
+        status = hythread_set_private_data(tm_native_thread, tm_java_thread);
+        if (status != TM_ERROR_NONE){
+            return status;
+        }
+    }
+    
+    tm_java_thread->jenv = env;
+    tm_java_thread->thread_object = (*env)->NewGlobalRef(env,java_thread);
+    tm_java_thread->thread_ref    = (thread_ref)?(*env)->NewGlobalRef(env, thread_ref):NULL; 
+    tm_java_thread->contended_monitor = 0;
+    tm_java_thread->wait_monitor = 0;
+    tm_java_thread->owned_monitors = 0;
+    tm_java_thread->owned_monitors_nmb = 0;
+    tm_java_thread->jvmti_local_storage.env = 0;
+    tm_java_thread->jvmti_local_storage.data = 0;
+
+    // Associate java_thread with tm_thread      
+    vm_jthread_set_tm_data(java_thread, tm_native_thread);
+    
+        return TM_ERROR_NONE;
+}
+
+/**
+ * Waits till the <code>thread</code> is finished.
+ *
+ * @param[in] java_thread a thread to wait for
+ * @return TM_THREAD_TIMEOUT or TM_THREAD_INTERRUPTED or 0 in case thread
+ * was successfully joined.
+ * @sa java.lang.Thread.join()
+ */
+IDATA jthread_join(jthread java_thread) {
+
+    hythread_t  tm_native_thread;
+        IDATA status;
+
+        if (java_thread == NULL){
+            return TM_ERROR_NULL_POINTER;
+        }
+    tm_native_thread = jthread_get_native_thread(java_thread); 
+    status = hythread_join_interruptable(tm_native_thread, 0, 0);
+    TRACE(("TM: jthread %d joined %d", hythread_self()->thread_id, tm_native_thread->thread_id));
+
+        return status;
+}
+
+/**
+ * Waits till the <code>thread</code> is finished with specific timeout.
+ *
+ * @param[in] java_thread a thread to wait for
+ * @param[in] millis timeout in milliseconds to wait
+ * @param[in] nanos timeout in nanoseconds to wait
+ * @return TM_THREAD_TIMEOUT or TM_THREAD_INTERRUPTED or 0 in case thread
+ * was successfully joined.
+ * @sa java.lang.Thread.join()
+ */
+IDATA jthread_timed_join(jthread java_thread, jlong millis, jint nanos) {
+        
+    hythread_t  tm_native_thread;
+        IDATA status;
+
+        if (java_thread == NULL){
+            return TM_ERROR_NULL_POINTER;
+        }
+    tm_native_thread = jthread_get_native_thread(java_thread);
+        if(!tm_native_thread) {
+                return TM_ERROR_NONE;
+        }
+    status = hythread_join_interruptable(tm_native_thread, millis, nanos);
+    TRACE(("TM: jthread %d joined %d", hythread_self()->thread_id, tm_native_thread->thread_id));
+
+    return status;
+}
+
+/**
+ * Lets an another thread to pass.
+ * @sa java.lang.Thread.yield()
+ */
+IDATA jthread_yield(){
+    hythread_yield();
+    return TM_ERROR_NONE;
+}
+
+/*
+ * Callback which is executed in the target thread at safe point 
+ * whenever Thread.stop() method is called.
+ */
+void stop_callback() {  
+    hythread_t tm_native_thread;
+    jvmti_thread_t tm_java_thread;
+    JNIEnv *env;
+        jobject excn;
+        IDATA status;
+    
+    tm_native_thread = hythread_self();
+    hythread_global_lock();
+        if (tm_native_thread->suspend_request > 0){
+        tm_native_thread->suspend_request = 1;
+        hythread_resume(tm_native_thread);
+        }
+    hythread_global_unlock();
+    hythread_resume(tm_native_thread);
+        // Clear callback (this is one-time event)
+    status=set_safepoint_callback(tm_native_thread, NULL);
+    assert (status == TM_ERROR_NONE);
+
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+    excn = tm_java_thread->stop_exception;
+        env = tm_java_thread->jenv;
+        (*env) -> Throw(env, excn);
+}
+
+/**
+ * Stops the execution of the given <code>thread</code> and forces
+ * ThreadDeath exception to be thrown in it.
+ *
+ * @param[in] java_thread thread to be stopped
+ * @sa java.lang.Thread.stop()
+ */
+IDATA jthread_stop(jthread java_thread) {
+    jclass clazz;
+        JNIEnv *env;
+    jvmti_thread_t tm_java_thread; 
+    hythread_t tm_native_thread;
+
+    tm_native_thread = vm_jthread_get_tm_data(java_thread);
+    tm_java_thread = hythread_get_private_data(tm_native_thread);
+        env = tm_java_thread->jenv;
+    clazz = (*env)->FindClass(env, "java/lang/ThreadDeath");
+        return jthread_exception_stop(java_thread, clazz);
+}
+
+/**
+ * Stops the execution of the given <code>thread</code> and forces
+ * the <code>throwable</code> exception to be thrown in it.
+ *
+ * @param[in] java_thread thread to be stopped
+ * @param[in] excn exception to be thrown
+ * @sa java.lang.Thread.stop()
+ */
+IDATA jthread_exception_stop(jthread java_thread, jobject excn) {
+
+    jvmti_thread_t tm_java_thread;
+    hythread_t tm_native_thread;
+    IDATA status;
+        JNIEnv* env;
+
+        tm_native_thread = jthread_get_native_thread(java_thread);
+
+    hythread_suspend_other(tm_native_thread);
+
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+        
+    // Install safepoint callback that would throw exception
+        env = tm_java_thread->jenv;
+    tm_java_thread->stop_exception = (*env)->NewGlobalRef(env,excn);
+    status=set_safepoint_callback(tm_native_thread, stop_callback);
+    if (status != TM_ERROR_NONE) return status;
+    // Let thread do callback inside safe point loop
+    status=hysem_post(tm_native_thread->resume_event);
+    // resume will be called by stop_callback
+    // hythread_resume(tm_native_thread);
+    ////        
+
+        return status;
+}
+
+/**
+ * Causes the current <code>thread</code> to sleep for at least the specified time.
+ * This call doesn't clear the interrupted flag.
+ *
+ * @param[in] millis timeout in milliseconds
+ * @param[in] nanos timeout in nanoseconds
+ *
+ * @return  returns 0 on success or negative value on failure,
+ * or TM_THREAD_INTERRUPTED in case thread was interrupted during sleep.
+ * @sa java.lang.Thread.sleep()
+ */
+IDATA jthread_sleep(jlong millis, jint nanos) {
+
+    hythread_t tm_native_thread = hythread_self();
+        IDATA status;
+
+    tm_native_thread->state &= ~TM_THREAD_STATE_RUNNABLE;
+        tm_native_thread->state |= TM_THREAD_STATE_WAITING | TM_THREAD_STATE_SLEEPING |
+                                   TM_THREAD_STATE_WAITING_WITH_TIMEOUT;
+
+    status = hythread_sleep_interruptable(millis, nanos); 
+
+        tm_native_thread->state &= ~(TM_THREAD_STATE_WAITING | TM_THREAD_STATE_SLEEPING |
+                                     TM_THREAD_STATE_WAITING_WITH_TIMEOUT);
+    tm_native_thread->state |= TM_THREAD_STATE_RUNNABLE;
+        if (status == TM_ERROR_INTERRUPT) {
+        TRACE(("TM: sleep interrupted status received, thread: %p", hythread_self()));
+    }    
+    return status;
+}
+
+/**
+ * Returns JNI environment associated with the given jthread, or NULL if there is none.
+ *
+ * The NULL value means the jthread object is not yet associated with native thread,
+ * or appropriate native thread has already died and deattached.
+ * 
+ * @param[in] java_thread java.lang.Thread object
+ */
+JNIEnv *jthread_get_JNI_env(jthread java_thread) {
+
+        hythread_t tm_native_thread;
+        jvmti_thread_t tm_java_thread;
+
+    if (java_thread == NULL){
+        return NULL;
+        }
+        tm_native_thread = jthread_get_native_thread(java_thread);
+    if (tm_native_thread == NULL){
+        return NULL;
+        }
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+    if (tm_java_thread == NULL){
+        return NULL;
+        }
+    return tm_java_thread->jenv;
+}
+/**
+ * Returns thread Id for the given <code>thread</code>.
+ *
+ * Thread ID must be unique for all Java threads.
+ * Can be reused after thread is finished.
+ *
+ * @return thread ID
+ * @sa java.lang.Thread.getId()
+ */
+jlong jthread_get_id(jthread java_thread) {
+
+    hythread_t tm_native_thread;
+
+        tm_native_thread = jthread_get_native_thread(java_thread);
+    assert(tm_native_thread);
+
+        return hythread_get_id(tm_native_thread);
+}
+
+/**
+ * Returns jthread given the thread ID.
+ *
+ * @param[in] thread_id thread ID
+ * @return jthread for the given ID, or NULL if there are no such.
+ */
+jthread jthread_get_thread(jlong thread_id) {
+
+        hythread_t tm_native_thread;
+        jvmti_thread_t tm_java_thread;
+        jthread java_thread;
+    
+    tm_native_thread = hythread_get_thread((jint)thread_id);
+    if (tm_native_thread == NULL){
+        return NULL;
+        }
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+        java_thread = tm_java_thread->thread_object;
+    assert(java_thread);
+        return java_thread;
+}
+
+/**
+ * Returns native thread associated with the given Java <code>thread</code>.
+ *
+ * @return native thread
+ */
+hythread_t  jthread_get_native_thread(jthread thread) {
+
+        assert(thread);
+
+    return vm_jthread_get_tm_data(thread);        
+}
+
+/**
+ * Returns Java thread associated with the given native <code>thread</code>.
+ *
+ * @return Java thread
+ */
+jthread jthread_get_java_thread(hythread_t tm_native_thread) {
+
+        jvmti_thread_t tm_java_thread;
+
+    if (tm_native_thread == NULL){
+        TRACE(("TM: native thread is NULL"));
+        return NULL;
+        }
+        tm_java_thread = hythread_get_private_data(tm_native_thread);
+
+    if (tm_java_thread == NULL){
+        TRACE(("TM: tmj thread is NULL"));
+        return NULL;
+        }
+
+    return tm_java_thread->thread_object;
+}
+/**
+ * Returns jthread associated with the current thread.
+ *
+ * @return jthread associated with the current thread, 
+ * or NULL if the current native thread is not attached to JVM.
+ */
+jthread jthread_self(void) {
+
+    return jthread_get_java_thread(hythread_self());
+}
+
+/**
+ * Cancels all java threads. This method being used at VM shutdown
+ * to terminate all java threads.
+ */ 
+IDATA jthread_cancel_all() {
+    return hythread_cancel_all(NULL);
+}
+
+/*
+ *  Auxiliary function to throw java.lang.InterruptedException
+ */
+
+void throw_interrupted_exception(void){
+
+    jvmti_thread_t tm_java_thread;
+    hythread_t tm_native_thread;
+    jclass clazz;
+        JNIEnv *env;
+        TRACE(("interrupted_exception thrown"));
+    tm_native_thread = hythread_self();
+    tm_java_thread = hythread_get_private_data(tm_native_thread);
+        env = tm_java_thread->jenv;
+        clazz = (*env) -> FindClass(env, "java/lang/InterruptedException");
+        (*env) -> ThrowNew(env, clazz, "Park() is interrupted");
+}
+
+jmethodID getRunMethod(JNIEnv *env) {
+    jclass clazz;
+    static jmethodID run_method = NULL;
+        IDATA status;
+    
+    status=acquire_start_lock();
+        assert (status == TM_ERROR_NONE);
+    //printf("run method find enter\n");
+    if (!run_method) {
+        clazz = (*env) -> FindClass(env, "java/lang/Thread");
+        run_method = (*env) -> GetMethodID(env, clazz, "runImpl", "()V");
+    }
+    status=release_start_lock();
+    //printf("run method find exit\n");
+    assert (status == TM_ERROR_NONE);
+    return run_method;
+}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_basic.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_interrupt.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_interrupt.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_interrupt.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_interrupt.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,72 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Sergey Petrovsky
+ * @version $Revision: 1.1.2.5 $
+ */  
+
+/**
+ * @file thread_java_interrupt.c
+ * @brief Java thread interruption related functions
+ */
+
+#include <open/jthread.h>
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+
+/**
+ * Interrupt a <code>thread</code>.
+ *
+ * If the <code>thread</code>is currently blocked (i.e. waiting on a monitor_wait or sleeping)
+ * resume the <code>thread</code>and cause it to return from the blocking function with
+ * TM_THREAD_INTERRUPTED.
+ *
+ * @param[in] java_thread a thread to be interrupted
+ * @sa java.lang.Thread.interrupt()
+ */
+IDATA VMCALL jthread_interrupt(jthread java_thread) {
+    hythread_t tm_native_thread = jthread_get_native_thread(java_thread); 
+    hythread_interrupt(tm_native_thread);
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Returns <code>true</code> if the thread <code>thread</code> is interrupted.
+ *
+ * Otherwise returns <code>false</code>.
+ *
+ * @param[in] java_thread thread to be checked
+ * @return <code>true</code> if the thread <code>thread</code>
+ * is interrupted; <code>false</code> otherwise.
+ * @sa java.lang.Thread.isInterrupted()
+ */
+jboolean jthread_is_interrupted(jthread java_thread) {   
+        hythread_t tm_native_thread = jthread_get_native_thread(java_thread);
+    return hythread_interrupted(tm_native_thread) > 0;
+   
+}
+
+/**
+ * Clears the interruption flag for the specific <code>thread</code>.
+ *
+ * @param[in] java_thread where to clear interrupt flag
+ * @sa java.lang.Thread.interrupted()
+ */
+IDATA VMCALL jthread_clear_interrupted(jthread java_thread) {
+    hythread_t tm_native_thread = jthread_get_native_thread(java_thread);
+    return hythread_clear_interrupted_other(tm_native_thread);
+}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_interrupt.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_iterator.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_iterator.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_iterator.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_iterator.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,100 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Dmitry Demin
+ * @version $Revision: 1.1.2.8 $
+ */  
+
+/**
+ * @file thread_java_iterator.c
+ * @brief Java thread iterator related functions
+ */
+
+#include <open/ti_thread.h>
+#include <open/hythread_ext.h>
+#include <open/jthread.h>
+#include "thread_private.h"
+
+
+/**
+ * Creates the iterator that can be used to walk over java threads.
+ */
+jthread_iterator_t VMCALL jthread_iterator_create(void) {
+    hythread_group_t java_thread_group = get_java_thread_group();
+    return (jthread_iterator_t)hythread_iterator_create(java_thread_group);
+}
+
+/**
+ * Releases the iterator.
+ * 
+ * @param[in] it iterator
+ */
+IDATA VMCALL jthread_iterator_release(jthread_iterator_t *it) {
+    return hythread_iterator_release((hythread_iterator_t *)it);
+}
+
+/**
+ * Resets the iterator such that it will start from the beginning.
+ * 
+ * @param[in] it iterator
+ */
+IDATA VMCALL jthread_iterator_reset(jthread_iterator_t *it) {
+    return hythread_iterator_reset((hythread_iterator_t *)it);   
+}
+
+/**
+ * Returns the next jthread using the given iterator.
+ * 
+ * @param[in] it iterator
+ */
+jthread VMCALL jthread_iterator_next(jthread_iterator_t *it) {
+        hythread_t tm_native_thread;
+        jvmti_thread_t tm_java_thread;
+        tm_native_thread = hythread_iterator_next((hythread_iterator_t *)it);
+        while(tm_native_thread!=NULL)
+        {
+            tm_java_thread = hythread_get_private_data(tm_native_thread);
+                if (tm_java_thread){
+                        return (jthread)tm_java_thread->thread_object;
+                }
+                tm_native_thread = hythread_iterator_next((hythread_iterator_t *)it);
+        }
+        return NULL;
+} 
+
+/**
+ * Returns the the number of Java threads.
+ * 
+ * @param[in] iterator
+ */
+int32 VMCALL jthread_iterator_size(jthread_iterator_t iterator) {
+        jthread res;
+        IDATA status;
+        int count=0;
+        status=jthread_iterator_reset(&iterator);
+        assert (status == TM_ERROR_NONE);
+        res = jthread_iterator_next(&iterator);
+        while(res!=NULL){
+                count++;        
+                res = jthread_iterator_next(&iterator);
+        }
+        status=jthread_iterator_reset(&iterator);
+        assert (status == TM_ERROR_NONE);
+        return count;
+}
+
+

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_iterator.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_monitors.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_monitors.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_monitors.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_monitors.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,461 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/** 
+ * @author Artem Aliev
+ * @version $Revision: 1.1.2.16 $
+ */  
+
+/**
+ * @file thread_java_monitors.c
+ * @brief Java thread monitors related functions
+ */
+
+#undef LOG_DOMAIN
+#define LOG_DOMAIN "tm.monitor"
+#include <open/jthread.h>
+#include <open/hythread_ext.h>
+#include <open/thread_externals.h>
+#include "thread_private.h"
+#include "jni.h"
+
+void add_owned_monitor(jobject monitor);
+void remove_owned_monitor(jobject monitor);
+void set_contended_monitor(jobject monitor);
+void set_wait_monitor(jobject monitor);
+
+/**
+ *  Initializes Java monitor.
+ *
+ *  Monitor is a recursive lock with one conditional variable associated with it.
+ *  Implementation may use the knowledge of internal object layout in order to allocate lock
+ *  and conditional variable in the most efficient manner.
+ *
+ *  @param[in] monitor object where monitor needs to be initilalized.
+ */
+IDATA VMCALL jthread_monitor_init(jobject monitor) {        
+    
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    assert(monitor);
+    
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    status = hythread_thin_monitor_create(lockword); 
+    hythread_suspend_enable();
+    return status;
+}
+
+
+
+/**
+ * Gains the ownership over monitor.
+ *
+ * Current thread blocks if the specified monitor is owned by other thread.
+ *
+ * @param[in] monitor object where monitor is located
+ * @sa JNI::MonitorEnter()
+ */
+IDATA VMCALL jthread_monitor_enter(jobject monitor) {        
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    // should be moved to event handler
+      apr_time_t enter_begin;
+    jvmti_thread_t tm_java_thread;
+    hythread_t tm_native_thread;
+    ////////
+
+    assert(monitor);
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    status = hythread_thin_monitor_try_enter(lockword);
+    if(status != TM_ERROR_EBUSY) {
+        hythread_suspend_enable();
+        if (ti_is_enabled()){
+            add_owned_monitor(monitor);
+        }
+        return status;
+    }
+
+    // should be moved to event handler
+    if (ti_is_enabled()){
+         enter_begin = apr_time_now();
+		 tm_native_thread = hythread_self();
+         tm_native_thread->state &= ~TM_THREAD_STATE_RUNNABLE;
+         tm_native_thread->state |= TM_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
+    }
+    /////////
+#ifdef LOCK_RESERVATION
+// busy unreserve lock before blocking and inflating
+    while (TM_ERROR_NONE !=unreserve_lock(lockword)) {
+        hythread_yield();
+        hythread_safe_point();
+         lockword = vm_object_get_lockword_addr(monitor);
+    }
+    status = hythread_thin_monitor_try_enter(lockword);
+    if(status != TM_ERROR_EBUSY) {
+        goto entered;
+    }
+#endif //LOCK_RESERVATION
+    
+    // busy wait and inflate
+    // reload poiter after safepoints
+    
+    if (ti_is_enabled()){
+        hythread_suspend_enable();
+        set_contended_monitor(monitor);
+        jvmti_send_contended_enter_or_entered_monitor_event(monitor, 1);
+        hythread_suspend_disable();
+
+    }
+
+    lockword = vm_object_get_lockword_addr(monitor);
+    while ((status = hythread_thin_monitor_try_enter(lockword)) == TM_ERROR_EBUSY) {
+        hythread_safe_point();
+        lockword = vm_object_get_lockword_addr(monitor);
+ 
+        if (is_fat_lock(*lockword)) {
+            status = hythread_thin_monitor_enter(lockword);
+             if (status != TM_ERROR_NONE) {
+                 hythread_suspend_enable();
+                 assert(0);
+                 return status;
+             }
+             goto entered; 
+        }
+        hythread_yield();
+    }
+    assert(status == TM_ERROR_NONE);
+    if(!is_fat_lock(*lockword)) {
+        inflate_lock(lockword);
+    }
+// do all ti staff here
+entered:
+    hythread_suspend_enable();
+    jvmti_send_contended_enter_or_entered_monitor_event(monitor, 0);
+    if (ti_is_enabled()){
+        add_owned_monitor(monitor);
+        // should be moved to event handler
+        tm_java_thread = hythread_get_private_data(hythread_self());
+        tm_java_thread->blocked_time += apr_time_now()- enter_begin;
+        tm_native_thread->state &= ~TM_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
+        tm_native_thread->state |= TM_THREAD_STATE_RUNNABLE;
+        /////////
+    }
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Attempt to gain the ownership over monitor without blocking.
+ *
+ * @param[in] monitor object where monitor is located
+ */
+IDATA VMCALL jthread_monitor_try_enter(jobject monitor) {        
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    assert(monitor);
+
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    status = hythread_thin_monitor_try_enter(lockword);
+    
+    hythread_suspend_enable();
+    if (status == TM_ERROR_NONE && ti_is_enabled()){
+        add_owned_monitor(monitor);
+    }
+    return status;
+}
+
+/**
+ * Releases the ownership over monitor.
+ *
+ * @param[in] monitor monitor
+ * @sa JNI::MonitorExit()
+ */
+IDATA VMCALL jthread_monitor_exit(jobject monitor) {
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    assert(monitor);
+
+    hythread_suspend_disable();
+
+    lockword = vm_object_get_lockword_addr(monitor);
+    status =  hythread_thin_monitor_exit(lockword);
+    hythread_suspend_enable();
+     if (status == TM_ERROR_NONE && ti_is_enabled()){
+        remove_owned_monitor(monitor);
+    }
+    if (status == TM_ERROR_ILLEGAL_STATE) {
+        jthread_throw_exception("java/lang/IllegalMonitorStateException", "Illegal monitor state");
+    }
+    return status;
+}
+
+/**
+ * Notifies one thread waiting on the monitor.
+ *
+ * Only single thread waiting on the
+ * object's monitor is waked up.
+ * Nothing happens if no threads are waiting on the monitor.
+ *
+ * @param[in] monitor object where monitor is located
+ * @sa java.lang.Object.notify() 
+ */
+IDATA VMCALL jthread_monitor_notify(jobject monitor) {    
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    assert(monitor);
+
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    status = hythread_thin_monitor_notify(lockword);
+    hythread_suspend_enable();
+
+    return status;
+}
+
+/**
+ * Notifies all threads which are waiting on the monitor.
+ *
+ * Each thread from the set of threads waiting on the
+ * object's monitor is waked up.
+ *
+ * @param[in] monitor object where monitor is located
+ * @sa java.lang.Object.notifyAll() 
+ */
+IDATA VMCALL jthread_monitor_notify_all(jobject monitor) {    
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    assert(monitor);
+
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    status = hythread_thin_monitor_notify_all(lockword);
+    hythread_suspend_enable();
+
+    return status;
+}
+
+/**
+ * Wait on the <code>object</code>'s monitor.
+ *
+ * This function instructs the current thread to be scheduled off 
+ * the processor and wait on the monitor until the following occurs: 
+ * <UL>
+ * <LI>another thread invokes <code>thread_notify(object)</code>
+ * and VM chooses this thread to wake up;
+ * <LI>another thread invokes <code>thread_notifyAll(object);</code>
+ * <LI>another thread invokes <code>thread_interrupt(thread);</code>
+ * </UL>
+ *
+ * @param[in] monitor object where monitor is located
+ * @sa java.lang.Object.wait()
+ * @return 
+ */
+IDATA VMCALL jthread_monitor_wait(jobject monitor) {
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+    // should be moved to event handler
+    apr_time_t wait_begin;
+    jvmti_thread_t tm_java_thread;
+    ///////
+
+    assert(monitor);
+
+    if (ti_is_enabled()) {
+        set_wait_monitor(monitor);
+        jvmti_send_wait_monitor_event(monitor, (jlong)0);
+        // should be moved to event handler
+        wait_begin = apr_time_now();
+        ////////
+    }
+
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    if (!is_fat_lock(*lockword)) {
+                if (!owns_thin_lock(hythread_self(), *lockword)) {
+            TRACE(("ILLEGAL_STATE wait %x\n", lockword));
+                        hythread_suspend_enable();
+            return TM_ERROR_ILLEGAL_STATE;  
+        }    
+        inflate_lock(lockword);
+    }
+     if (ti_is_enabled()){
+        remove_owned_monitor(monitor);
+    }
+    status = hythread_thin_monitor_wait_interruptable(lockword, 0, 0); 
+    hythread_suspend_enable();
+     if (ti_is_enabled()){
+        add_owned_monitor(monitor);
+        jvmti_send_waited_monitor_event(monitor, (jboolean)0);
+        // should be moved to event handler
+        tm_java_thread = hythread_get_private_data(hythread_self());
+        tm_java_thread->waited_time += apr_time_now()- wait_begin;
+        /////////
+    }
+    return status;
+}
+
+/**
+ * Wait on the <code>object</code>'s monitor with the specified timeout.
+ *
+ * This function instructs the current thread to be scheduled off 
+ * the processor and wait on the monitor until the following occurs: 
+ * <UL>
+ * <LI>another thread invokes <code>thread_notify(object)</code>
+ * and VM chooses this thread to wake up;
+ * <LI>another thread invokes <code>thread_notifyAll(object);</code>
+ * <LI>another thread invokes <code>thread_interrupt(thread);</code>
+ * <LI>real time elapsed from the wating begin is
+ * greater or equal the timeout specified.
+ * </UL>
+ *
+ * @param[in] monitor object where monitor is located
+ * @param[in] millis time to wait (in milliseconds)
+ * @param[in] nanos time to wait (in nanoseconds)
+ * @sa java.lang.Object.wait()
+ */
+IDATA VMCALL jthread_monitor_timed_wait(jobject monitor, jlong millis, jint nanos) {
+    hythread_thin_monitor_t *lockword;
+    IDATA status;
+	hythread_t tm_native_thread;
+
+    assert(monitor);
+    jvmti_send_wait_monitor_event(monitor, millis);
+
+    hythread_suspend_disable();
+    lockword = vm_object_get_lockword_addr(monitor);
+    if (!is_fat_lock(*lockword)) {
+        if (!owns_thin_lock(hythread_self(), *lockword)) {
+                    TRACE(("ILLEGAL_STATE wait %x\n", lockword));
+                                hythread_suspend_enable();
+                return TM_ERROR_ILLEGAL_STATE;  
+        }    
+        inflate_lock(lockword);
+    }
+      if (ti_is_enabled()){
+        remove_owned_monitor(monitor);
+ 		tm_native_thread = hythread_self();
+        tm_native_thread->state &= ~TM_THREAD_STATE_RUNNABLE;
+        tm_native_thread->state |= TM_THREAD_STATE_WAITING |
+			                       TM_THREAD_STATE_IN_MONITOR_WAIT;
+		if((millis > 0) || (nanos > 0)) { 
+            tm_native_thread->state |= TM_THREAD_STATE_WAITING_WITH_TIMEOUT;
+		} else {
+            tm_native_thread->state |= TM_THREAD_STATE_WAITING_INDEFINITELY;
+		}
+    }
+    status = hythread_thin_monitor_wait_interruptable(lockword, millis, nanos);
+
+     if (ti_is_enabled()){
+        add_owned_monitor(monitor);
+        tm_native_thread->state &= ~(TM_THREAD_STATE_WAITING | 
+			                         TM_THREAD_STATE_IN_MONITOR_WAIT);
+		if((millis > 0) || (nanos > 0)) { 
+            tm_native_thread->state &= ~TM_THREAD_STATE_WAITING_WITH_TIMEOUT;
+		} else {
+            tm_native_thread->state &= ~TM_THREAD_STATE_WAITING_INDEFINITELY;
+    }
+        tm_native_thread->state |= TM_THREAD_STATE_RUNNABLE;
+	}
+    hythread_suspend_enable();
+
+    jvmti_send_waited_monitor_event(monitor, (status == APR_TIMEUP)?(jboolean)1:(jboolean)0);
+
+    return status;
+}
+
+void add_owned_monitor(jobject monitor){
+    hythread_t tm_native_thread = hythread_self();
+    jvmti_thread_t tm_java_thread = hythread_get_private_data(tm_native_thread);
+    int disable_status;
+    TRACE(("TM: add owned monitor: %x", monitor));
+    if (!tm_java_thread) return;
+    
+    disable_status = reset_suspend_disable();
+    if (tm_java_thread->contended_monitor) {
+        (*(tm_java_thread->jenv))->DeleteGlobalRef(tm_java_thread->jenv, tm_java_thread->contended_monitor);
+        tm_java_thread->contended_monitor = NULL;
+    }
+
+    if (tm_java_thread->wait_monitor) {
+        (*(tm_java_thread->jenv))->DeleteGlobalRef(tm_java_thread->jenv, tm_java_thread->wait_monitor);
+        tm_java_thread->wait_monitor = NULL;
+    }
+    
+    
+    if (!tm_java_thread->owned_monitors){
+         tm_java_thread->owned_monitors = apr_pcalloc(tm_java_thread->pool,
+                            MAX_OWNED_MONITOR_NUMBER * sizeof(jobject));
+         assert(tm_java_thread->owned_monitors);
+         tm_java_thread->owned_monitors_nmb = 0;
+    }
+    assert(tm_java_thread->owned_monitors_nmb < MAX_OWNED_MONITOR_NUMBER);
+  
+    tm_java_thread->owned_monitors[tm_java_thread->owned_monitors_nmb] 
+        = (*(tm_java_thread->jenv))->NewGlobalRef(tm_java_thread->jenv, monitor);
+    set_suspend_disable(disable_status);
+    tm_java_thread->owned_monitors_nmb++;
+}
+
+void remove_owned_monitor(jobject monitor){
+    int i,j, disable_status;
+    hythread_t tm_native_thread = hythread_self();
+    jvmti_thread_t tm_java_thread = hythread_get_private_data(tm_native_thread);
+    
+    TRACE(("TM: remove owned monitor: %x", monitor));
+    
+    if (!tm_java_thread) return;
+    for (i = tm_java_thread->owned_monitors_nmb - 1; i >= 0; i--){
+        if (vm_objects_are_equal(tm_java_thread->owned_monitors[i], monitor)){
+            disable_status = reset_suspend_disable();
+            (*(tm_java_thread->jenv))->DeleteGlobalRef(tm_java_thread->jenv, tm_java_thread->owned_monitors[i]);
+            set_suspend_disable(disable_status);
+            for (j = i; j < tm_java_thread->owned_monitors_nmb - 1; j++){
+                tm_java_thread->owned_monitors[j] = tm_java_thread->owned_monitors[j + 1];
+            }
+            tm_java_thread->owned_monitors_nmb--;
+            return;
+        }
+    }
+    //assert(0); monitor - it is no valid monitor
+}
+
+void set_contended_monitor(jobject monitor){
+    hythread_t tm_native_thread = hythread_self();
+    IDATA suspend_status;
+    
+    jvmti_thread_t tm_java_thread = hythread_get_private_data(tm_native_thread);
+
+    if (!tm_java_thread) return;
+    
+    suspend_status = reset_suspend_disable();
+    tm_java_thread->contended_monitor = (*(tm_java_thread->jenv))->NewGlobalRef(tm_java_thread->jenv, monitor);
+    set_suspend_disable(suspend_status);
+}
+
+void set_wait_monitor(jobject monitor){
+    hythread_t tm_native_thread = hythread_self();
+    IDATA suspend_status;
+    
+    jvmti_thread_t tm_java_thread = hythread_get_private_data(tm_native_thread);
+
+    if (!tm_java_thread) return;
+
+    suspend_status = reset_suspend_disable();
+    assert (suspend_status == TM_ERROR_NONE);
+    tm_java_thread->wait_monitor = (*(tm_java_thread->jenv))->NewGlobalRef(tm_java_thread->jenv, monitor);
+    set_suspend_disable(suspend_status);
+}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_monitors.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_park.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_park.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_park.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_park.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,81 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Sergey Petrovsky
+ * @version $Revision: 1.1.2.6 $
+ */  
+
+/**
+ * @file thread_java_park.c
+ * @brief Java thread park/unpark functions
+ */
+
+#include <open/jthread.h>
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+
+/**
+ * Parks the current thread.
+ *
+ * Stops the current thread from executing until it is unparked or interrupted.
+ * Unlike wait or sleep, the interrupted flag is NOT cleared by this API.
+ *
+ * @sa java.util.concurrent.locks.LockSupport.park() 
+ */
+IDATA VMCALL jthread_park() {
+    return hythread_park(0,0);
+}
+
+/**
+ * Parks the current thread with the specified timeout.
+ *
+ * Stops the current thread from executing until it is unparked, interrupted,
+ * or the specified timeout elapses.
+ * Unlike wait or sleep, the interrupted flag is NOT cleared by this API.
+ *
+ * @param[in] millis timeout in milliseconds
+ * @param[in] nanos timeout in nanoseconds
+ * @sa java.util.concurrent.locks.LockSupport.park() 
+ */
+IDATA VMCALL jthread_timed_park(jlong millis, jint nanos) {
+    return hythread_park((I_64)millis, (IDATA)nanos);
+}
+
+/**
+ * Unparks the given thread.
+ *
+ * If the thread is parked, it will return from park.
+ * If the thread is not parked, its 'UNPARKED' flag will be set, and it will
+ * return immediately the next time it is parked.
+ *
+ * Note that unparks are not counted. Unparking a thread once is the same as
+ * unparking it n times.
+ *
+ * @param[in] java_thread thread that needs to be unparked
+ * @sa java.util.concurrent.locks.LockSupport.unpark() 
+ */
+IDATA VMCALL jthread_unpark(jthread java_thread) {
+
+        hythread_t tm_native_thread;
+
+        assert(java_thread);
+    tm_native_thread = jthread_get_native_thread(java_thread);
+    
+    hythread_unpark(tm_native_thread);
+    
+    return TM_ERROR_NONE;
+}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_park.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_suspend.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_suspend.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_suspend.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_suspend.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,116 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Sergey Petrovsky
+ * @version $Revision: 1.1.2.5 $
+ */  
+
+/**
+ * @file thread_java_suspend.c
+ * @brief Java thread suspend/resume functions
+ */
+
+#include <open/jthread.h>
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+
+/**
+ * Resumes the suspended thread <code>thread</code> execution.
+ *
+ * This function cancels the effect of all previous
+ * <code>thread_suspend(thread)</code> calls such that
+ * the thread <code>thread</code> may proceed with execution.
+ *
+ * @param[in] java_thread thread to be resumed
+ * @sa java.lang.Thread.resume(), JVMTI::ResumeThread()
+ */
+IDATA VMCALL jthread_resume(jthread java_thread) {
+    hythread_t tm_native_thread = vm_jthread_get_tm_data(java_thread);
+        hythread_resume(tm_native_thread);
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Resumes the suspended threads from the list.
+ *
+ * @param[out] results list of error codes for resume result
+ * @param[in] count number of threads in the list
+ * @param[in] thread_list list of threads to be resumed
+ * @sa JVMTI::ResumeThreadList()
+ */
+IDATA VMCALL jthread_resume_all(jvmtiError* results, jint count, const jthread* thread_list) {
+    
+        int i;
+
+        for (i = 0; i < count; i++){
+                results[i] = (jvmtiError)jthread_resume(thread_list[i]);
+        }
+        return TM_ERROR_NONE;
+}
+
+/**
+ * Suspends the <code>thread</code> execution.
+ *
+ * The execution of <code>java_thread</code> is suspended till
+ * <code>jthread_resume(java_thread)</code> is called from another thread.
+ *
+ * Note: this function implements safe suspend based on the safe points and
+ * regions. This means that:
+ * <ul>
+ *   <li>If the thread is in Java code,
+ *        it runs till the safe point and puts itself into a wait state.
+ *   <li>If the thread is running native code, it runs till
+ *        the end of safe region and then puts itself into a wait state,
+ * </ul>
+ *
+ * @param[in] java_thread thread to be suspended
+ * @sa java.lang.Thread.suspend(), JVMTI::SuspendThread()
+ */
+IDATA VMCALL jthread_suspend(jthread java_thread) {
+        hythread_t tm_native_thread = vm_jthread_get_tm_data(java_thread);
+        hythread_suspend_other(tm_native_thread);
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Suspends the threads from the list.
+ *
+ * The <code>thread</code> thread's execution is suspended till
+ * <code>thread_resume(thread)</code> is called from another thread.
+ *
+ * Note: this function implements safe suspend based on the safe points and
+ * regions. This means that:
+ * <ul>
+ *   <li>If the thread is in Java code,
+ *        it runs till the safe point and puts itself into wait state.
+ *   <li>If the thread is running native code, it runs till
+ *        the end of safe region and then puts itself into a wait state,
+ * </ul>
+ *
+ * @param[out] results list of error codes for suspension result
+ * @param[in] count number of threads in the list
+ * @param[in] thread_list list of threads to be suspended
+ * @sa JVMTI::SuspendThreadList()
+ */
+IDATA VMCALL jthread_suspend_all(jvmtiError* results, jint count, const jthread* thread_list) {
+        int i;
+        for (i = 0; i < count; i++){
+                results[i] = (jvmtiError)jthread_suspend(thread_list[i]);
+        }
+    return TM_ERROR_NONE;
+}
+

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_java_suspend.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_attrs.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_attrs.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_attrs.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_attrs.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,60 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Nikolay Kuznetsov
+ * @version $Revision: 1.1.2.7 $
+ */  
+
+/**
+ * @file thread_native_attrs.c
+ * @brief Hythread priority related finctions
+ */
+
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+
+/**
+ * Set a thread's execution priority.
+ * 
+ * @param[in] thread a thread
+ * @param[in] priority
+ * Use the following symbolic constants for priorities:<br>
+ *                              HYTHREAD_PRIORITY_MAX<br>
+ *                              HYTHREAD_PRIORITY_USER_MAX<br>
+ *                              HYTHREAD_PRIORITY_NORMAL<br>
+ *                              HYTHREAD_PRIORITY_USER_MIN<br>
+ *                              HYTHREAD_PRIORITY_MIN<br>
+ * 
+ * @returns 0 on success or negative value on failure (priority wasn't changed)
+ * 
+ * 
+ */
+IDATA VMCALL hythread_set_priority(hythread_t thread, UDATA priority){
+    apr_status_t apr_status = apr_thread_set_priority(thread->os_handle, priority); 
+        if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+    thread->priority = priority;
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Returns the priority for the <code>thread</code>.
+ *
+ * @param[in] thread those attribute is read
+ */
+UDATA VMCALL hythread_get_priority(hythread_t thread) {
+    return thread->priority;
+}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_attrs.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,727 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Nikolay Kuznetsov
+ * @version $Revision: 1.1.2.13 $
+ */  
+
+/**
+ * @file thread_native_basic.c
+ * @brief hythread basic functions
+ */
+
+#undef LOG_DOMAIN
+#define LOG_DOMAIN "tm.native"
+
+#include <open/hythread_ext.h>
+#include "thread_private.h"
+
+extern hythread_group_t TM_DEFAULT_GROUP;
+static void* APR_THREAD_FUNC thread_start_proc(apr_thread_t* thd, void *p_args);
+static hythread_t init_thread(hythread_group_t group);
+static void reset_thread(hythread_t thread);
+//#define APR_TLS_USE 1
+
+#define NAKED __declspec( naked )
+
+#if !defined (APR_TLS_USE ) && !defined (FS14_TLS_USE)
+#ifdef PLATFORM_POSIX
+__thread hythread_t tm_self_tls = NULL;
+#else
+__declspec(thread) hythread_t tm_self_tls = NULL;
+
+#endif
+#endif
+
+#define MAX_ID 1000000
+hythread_t fast_thread_array[MAX_ID];
+int next_id = 1;
+/*
+IDATA add_to_fast_thread_array(hythread_t thread,int id)
+{
+	if (id>=MAX_ID)
+	{
+		if (MAX_ID<1000)
+	    {
+			MAX_ID=1000;
+			fast_thread_array=(hythread_t *)malloc(MAX_ID*sizeof(hythread_t));
+	    }
+		else
+		{
+			MAX_ID*=2;
+			fast_thread_array=(hythread_t *)realloc(fast_thread_array,MAX_ID*sizeof(hythread_t));
+		}
+		if (fast_thread_array==NULL)
+			return TM_ERROR_OUT_OF_MEMORY;
+	}
+	fast_thread_array[id]=thread;
+	return TM_ERROR_NONE;
+}*/
+static void thread_set_self(hythread_t thread);
+
+/**
+ * Creates a new thread.
+ *
+ * @param[out] new_thread The newly created thread.
+ * @param[in] group thread group, or NULL; in case of NULL this thread will go to the default group.
+ * @param[in] attr threadattr to use to determine how to create the thread, or NULL for default attributes
+ * @param[in] func function to run in the new thread
+ * @param[in] data argument to be passed to starting function
+ * @sa apr_thread_create()
+ */
+IDATA VMCALL hythread_create_with_group(hythread_t *ret_thread, hythread_group_t group, UDATA stacksize, UDATA priority, UDATA suspend, hythread_entrypoint_t func, void *data) {
+    apr_threadattr_t *apr_attrs;
+    hythread_t  new_thread;
+    apr_status_t apr_status;
+   
+    if (!ret_thread || !(*ret_thread)) {
+        // Allocate & init thread structure
+        new_thread = init_thread(group); 
+		if(new_thread==NULL){
+			return TM_ERROR_OUT_OF_MEMORY;
+		}
+    } else {
+        new_thread = (*ret_thread);
+    }
+
+    if (stacksize) {
+        apr_threadattr_create(&apr_attrs, new_thread->pool);
+        apr_threadattr_stacksize_set(apr_attrs, stacksize);
+        new_thread->apr_attrs  = apr_attrs;
+    } else {
+        new_thread->apr_attrs = NULL;
+    }
+
+    new_thread->priority = priority ? priority : HYTHREAD_PRIORITY_NORMAL;
+    //new_thread->suspend_request = suspend ? 1 : 0;
+    
+    // Set up thread body procedure 
+    new_thread->start_proc = func;
+    new_thread->start_proc_args = data;
+    new_thread->state = TM_THREAD_STATE_ALIVE; 
+    // Create APR thread using the given attributes;
+    apr_status = apr_thread_create(&(new_thread->os_handle),    // new thread OS handle 
+            new_thread->apr_attrs,                          // thread attr created here
+            thread_start_proc,                              //
+            (void *)new_thread,                             //thread_proc attrs 
+            new_thread->pool); 
+   
+    // Store the pointer to the resulting thread 
+    if(ret_thread) {
+        *ret_thread = new_thread;
+    }
+    return CONVERT_ERROR(apr_status);
+}
+
+/**
+ * Create a new OS thread.
+ * 
+ * The created thread is attached to the threading library.<br>
+ * <br>
+ * Unlike POSIX, this doesn't require an attributes structure.
+ * Instead, any interesting attributes (e.g. stacksize) are
+ * passed in with the arguments.
+ *
+ * @param[out] ret_thread a pointer to a hythread_t which will point to the thread (if successfully created)
+ * @param[in] stacksize the size of the new thread's stack (bytes)<br>
+ *                      0 indicates use default size
+ * @param[in] priority priorities range from HYTHREAD_PRIORITY_MIN to HYTHREAD_PRIORITY_MAX (inclusive)
+ * @param[in] suspend set to non-zero to create the thread in a suspended state.
+ * @param[in] func pointer to the function which the thread will run
+ * @param[in] data a value to pass to the entrypoint function
+ *
+ * @return  0 on success or negative value on failure
+ *
+ * @see hythread_exit, hythread_resume
+ */
+IDATA VMCALL hythread_create(hythread_t *ret_thread, UDATA stacksize, UDATA priority, UDATA suspend, hythread_entrypoint_t func, void *data) {
+    return hythread_create_with_group(ret_thread, NULL, stacksize, priority, suspend, func, data);
+}
+
+/**
+ * Registers the current OS thread with the threading subsystem.
+ *
+ * @param[in] handle thread to register
+ * @param[in] group thread group, or NULL; in case of NULL this thread will go to the default group
+ */
+IDATA VMCALL hythread_attach_to_group(hythread_t *handle, hythread_group_t group) {
+    hythread_t thread;
+    apr_thread_t *os_handle = NULL; 
+    apr_os_thread_t *os_thread;
+    apr_status_t apr_status; 
+
+    // Do nothing and return if the thread is already attached
+    thread = tm_self_tls;
+    if (thread) {
+        if (handle) {
+            *handle = thread;
+        }
+        return TM_ERROR_NONE;
+    }
+    thread = init_thread(group); // allocate & init thread structure
+	if (thread==NULL){
+		return TM_ERROR_OUT_OF_MEMORY;
+	}
+    os_thread = apr_palloc(thread->pool, sizeof(apr_os_thread_t));
+	if(os_thread == NULL) {
+        return TM_ERROR_OUT_OF_MEMORY;
+	} 
+    *os_thread = apr_os_thread_current();
+    apr_status = apr_os_thread_put(&os_handle, os_thread, thread->pool);
+        if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+    
+    thread->os_handle = os_handle;
+    thread_set_self(thread);
+    
+    thread->state = TM_THREAD_STATE_ALIVE;
+    assert(thread == tm_self_tls);
+    TRACE(("TM: native attached: native: %p ",  tm_self_tls));
+
+    if (handle) {
+        *handle = thread;
+    }
+    
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Attach an OS thread to the threading library.
+ *
+ * Create a new hythread_t to represent the existing OS thread.
+ * Attaching a thread is required when a thread was created
+ * outside of the Hy threading library wants to use any of the
+ * Hy threading library functionality.
+ *
+ * If the OS thread is already attached, handle is set to point
+ * to the existing hythread_t.
+ *
+ * @param[out] handle pointer to a hythread_t to be set (will be ignored if null)
+ * @return  0 on success or negative value on failure
+ *
+ * @see hythread_detach
+ */
+IDATA VMCALL hythread_attach(hythread_t *handle) {
+    return hythread_attach_to_group(handle, NULL);
+}
+
+/**
+ * Detaches a thread from the threading library.
+ * 
+ * @note Assumes that the thread being detached is already attached.<br>
+ * 
+ * If the thread is an attached thread, then detach should only be called by the thread
+ * itself. Internal resources associated with the thread are freed.
+ * 
+ * If the thread is already dead, this call will destroy it.
+ * 
+ * @param[in] thread a hythread_t representing the thread to be detached.
+ * If this is NULL, the current thread is detached.
+ * @return none
+ * 
+ * @see hythread_attach
+ */
+void VMCALL hythread_detach(hythread_t thread) {
+    // Acquire global TM lock to prevent concurrent acccess to thread list
+        IDATA status;
+    status = hythread_global_lock(NULL);
+        assert (status == TM_ERROR_NONE);
+    
+    if (!(thread = tm_self_tls)) {
+        status = hythread_global_unlock(NULL);
+                assert (status == TM_ERROR_NONE);
+        return;
+    }
+    
+        status = thread_destroy(thread);       // Remove thread from the list of thread 
+    assert (status == TM_ERROR_NONE);
+        thread_set_self(NULL); 
+    status = hythread_global_unlock(NULL); 
+        assert (status == TM_ERROR_NONE);
+}
+/**
+ * Waits until the selected thread finishes execution.
+ *
+ * @param[in] t thread to join
+ */
+IDATA VMCALL hythread_join(hythread_t t) { 
+    return hylatch_wait(t->join_event);
+}
+/**
+ * Waits until the selected thread finishes with specific timeout.
+ *
+ * @param[in] t a thread to wait for
+ * @param[in] millis timeout in milliseconds to wait
+ * @param[in] nanos timeout in nanoseconds to wait
+ * @return TM_THREAD_TIMEOUT or 0 in case thread
+ * was successfully joined.
+ */
+IDATA VMCALL hythread_join_timed(hythread_t t, I_64 millis, IDATA nanos) { 
+    return hylatch_wait_timed(t->join_event, millis, nanos);
+}
+
+/**
+ * Waits until the selected thread finishes with specific timeout.
+ *
+ * @param[in] t a thread to wait for
+ * @param[in] millis timeout in milliseconds to wait
+ * @param[in] nanos timeout in nanoseconds to wait
+ * @return TM_THREAD_TIMEOUT or TM_THREAD_INTERRUPTED or 0 in case thread
+ * was successfully joined.
+ */
+IDATA VMCALL hythread_join_interruptable(hythread_t t, I_64 millis, IDATA nanos) { 
+    return hylatch_wait_interruptable(t->join_event, millis, nanos);
+}
+
+/** 
+ * Yield the processor.
+ * 
+ * @return none
+ */
+void VMCALL hythread_yield() {
+    //apr_thread_yield returns void 
+    apr_thread_yield();
+}
+/** 
+ * Return the hythread_t for the current thread.
+ * 
+ * @note Must be called only by an attached thread
+ * 
+ * @return hythread_t for the current thread
+ *
+ * @see hythread_attach
+ * 
+ */
+#ifdef APR_TLS_USE
+/**
+ * Return the hythread_t for the current thread.
+ *
+ * @note Must be called only by an attached thread
+ *
+ * @return hythread_t for the current thread
+ *
+ * @see hythread_attach
+ *
+ */
+extern "C"
+hythread_t hythread_self() {
+    hythread_t  thread;
+    apr_status_t UNUSED apr_status;
+    
+    // Extract hythread_t from TLS
+        apr_status = apr_threadkey_private_get((void **)(&thread), TM_THREAD_KEY);            
+    assert(apr_status == APR_SUCCESS);
+    
+    return thread;
+}
+#else 
+#ifdef FS14_TLS_USE
+/**
+ * Return the hythread_t for the current thread.
+ *
+ * @note Must be called only by an attached thread
+ *
+ * @return hythread_t for the current thread
+ *
+ * @see hythread_attach
+ *
+ */
+NAKED hythread_t hythread_self() {
+    _asm { mov eax, fs:[0x14]
+           ret;
+    }
+    //return tm_self_tls;
+}
+
+static void thread_set_self(hythread_t  thread) {
+    //tm_self_tls = thread;
+    _asm{
+        mov eax, thread
+        mov fs:[0x14], eax
+    }
+}
+#else
+/**
+ * Return the hythread_t for the current thread.
+ *
+ * @note Must be called only by an attached thread
+ *
+ * @return hythread_t for the current thread
+ *
+ * @see hythread_attach
+ *
+ */
+hythread_t hythread_self() {
+    return tm_self_tls;
+}
+
+static void thread_set_self(hythread_t  thread) {
+    tm_self_tls = thread;
+}
+#endif
+#endif
+
+/*
+ */
+IDATA thread_sleep_impl(I_64 millis, IDATA nanos, IDATA interruptable) {
+    IDATA status;
+    
+    hythread_t thread = tm_self_tls;
+    
+    // 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);
+    return (status == TM_ERROR_INTERRUPT && interruptable) ? TM_ERROR_INTERRUPT : TM_ERROR_NONE;
+}
+
+/** 
+ * Suspend the current thread from executing 
+ * for at least the specified time.
+ *
+ * @param[in] millis
+ * @param[in] nanos 
+ * @return  0 on success<br>
+ *    HYTHREAD_INVALID_ARGUMENT if the arguments are invalid<br>
+ *    HYTHREAD_INTERRUPTED if the sleep was interrupted
+ *
+ * @see hythread_sleep
+ */
+IDATA VMCALL hythread_sleep_interruptable(I_64 millis, IDATA nanos) {    
+    return thread_sleep_impl(millis, nanos, WAIT_INTERRUPTABLE);
+}
+
+/** 
+ * Suspend the current thread from executing 
+ * for at least the specified time.
+ *
+ * @param[in] millis minimum number of milliseconds to sleep
+ * @return  0 on success<br> HYTHREAD_INVALID_ARGUMENT if millis < 0
+ *
+ * @see hythread_sleep_interruptable
+ */
+IDATA VMCALL hythread_sleep(int64 millis) {
+    return thread_sleep_impl(millis, 0, WAIT_NONINTERRUPTABLE);
+}
+
+/**
+ * Returns the id of the specific thread.
+ * 
+ * @return  0 on success
+ */
+IDATA VMCALL hythread_get_id(hythread_t t) {
+    assert(t);
+    return (IDATA)t->thread_id;
+}
+
+/**
+ * Returns the id of the current thread.
+ * @return  0 on success
+ */
+IDATA VMCALL hythread_get_self_id() {
+    return (IDATA)tm_self_tls->thread_id;
+}
+/**
+ * Returns the thread given the specific id.
+ */
+hythread_t VMCALL hythread_get_thread(IDATA id) {
+    return fast_thread_array[id];
+}
+
+/**
+ * Returns thread private data.
+ * 
+ * @param[in] t thread those private data to get
+ * @return pointer to thread private data
+ */
+void* VMCALL hythread_get_private_data(hythread_t  t){
+    assert(t);
+    return t->private_data;
+}
+/**
+ * Sets the thread private data. 
+ *
+ * @param t thread
+ * @param data pointer to private data
+ */
+IDATA VMCALL hythread_set_private_data(hythread_t  t, void* data) {
+    assert(t);
+    t->private_data = data;
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Get thread group. 
+ *
+ * @param[out] group hythread_group_t* pointer to group
+ * @param[in] thread hythread_t thread
+ * @return  0 on success
+ */
+IDATA VMCALL hythread_get_group(hythread_group_t *group, hythread_t thread) {
+    (*group) = thread->group;
+    return TM_ERROR_NONE;
+}
+
+//==============================================================================
+// Private functions
+/*
+ */
+IDATA thread_destroy(hythread_t thread) {
+    
+    // Acquire global TM lock to prevent concurrent acccess to thread list    
+    IDATA status;
+    status =hythread_global_lock(NULL);
+    if (status != TM_ERROR_NONE) return status;
+    thread->prev->next = thread->next;
+    thread->next->prev = thread->prev;
+    thread->group->threads_count--;
+
+    fast_thread_array[thread->thread_id] = NULL;
+
+    status =hythread_global_unlock(NULL);
+    if (status != TM_ERROR_NONE) return status;
+    return TM_ERROR_NONE;
+}
+/*
+ */
+IDATA allocate_thread(hythread_t thread, hythread_group_t group) {
+    int id = 0;
+    hythread_t cur, prev;
+        IDATA status;
+    //thread_list points to the dummy thread, which is not actual thread, just 
+    //a auxiliary thread structure to maintain the thread list
+    /////
+    
+    // Acquire global TM lock to prevent concurrent acccess to thread list
+    status =hythread_global_lock(NULL); 
+        if (status != TM_ERROR_NONE) return status;
+    
+    // Append the thread to the list of threads
+    cur  = group->thread_list->next;
+    if (thread->thread_id) {
+        id   = thread->thread_id;
+    } else {
+        id   = ++next_id;
+    }
+	if (id>=MAX_ID){
+		hythread_global_unlock(NULL);
+		return TM_ERROR_OUT_OF_MEMORY;
+    }
+    prev = cur->prev;
+    
+    thread->next = cur;
+    thread->prev = prev;
+    prev->next = cur->prev = thread;
+    thread->thread_id = id;
+    fast_thread_array[id] = thread;
+    /*status=add_to_fast_thread_array(thread,id);
+    if (status != TM_ERROR_NONE) return status;*/
+    status=hythread_global_unlock(NULL);
+    if (status != TM_ERROR_NONE) return status;
+    return TM_ERROR_NONE;
+    
+}
+
+/** 
+ * Terminates a running thread.
+ * 
+ * @note This should only be used as a last resort.  The system may be in
+ * an unpredictable state once a thread is cancelled.  In addition, the thread
+ * may not even stop running if it refuses to cancel.
+ * 
+ * @param[in] thread a thread to be terminated 
+ * @return none
+ */
+void VMCALL hythread_cancel(hythread_t thread) {
+    apr_thread_cancel(thread->os_handle);
+}
+
+/** 
+ * Terminates all running threads in the given group.
+ * 
+ * @param[in] group thread group
+ * @see hythread_cancell
+ */
+IDATA VMCALL hythread_cancel_all(hythread_group_t group) {
+    hythread_iterator_t iter;
+    hythread_t next;
+    hythread_t self = tm_self_tls;
+
+    if (!group) {
+        group = TM_DEFAULT_GROUP;
+    }
+    
+    iter = hythread_iterator_create (group);
+    while((next = hythread_iterator_next (&iter)) != NULL) {
+                if(next != self) {
+                        hythread_cancel(next);
+            //since this method being used at shutdown it does not
+            //males any sence to exit on error, but continue terminating threads
+                }       
+        }
+
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Allocates and initializes a new thread_t structure.
+ *
+ */
+IDATA VMCALL hythread_struct_init(hythread_t *ret_thread, hythread_group_t group) {
+    if (*ret_thread) {
+        reset_thread(*ret_thread);
+    } else {
+        (*ret_thread) = init_thread(group);
+    }
+    if ((*ret_thread)==NULL)
+	{
+		return TM_ERROR_OUT_OF_MEMORY;
+	}
+    return TM_ERROR_NONE;
+}
+
+/*
+ * Allocates and initializes a new thread_t structure 
+ *
+ * @return created and initialized thread_t structure
+ */
+static hythread_t init_thread(hythread_group_t group) {
+    apr_pool_t *pool;
+        apr_status_t apr_status;
+    hythread_t ptr;
+        IDATA status;
+
+    if (!group) {
+        group = TM_DEFAULT_GROUP;
+    }
+
+    apr_status = apr_pool_create(&pool, group->pool);
+    if((apr_status!=APR_SUCCESS)||(pool==NULL))
+	    return NULL;
+    ptr = (hythread_t )apr_pcalloc(pool, sizeof(HyThread));
+	if (ptr==NULL)
+		return NULL;
+    ptr->pool       = pool;
+    ptr->group      = group;
+    ptr->os_handle  = NULL;
+    ptr->next       = ptr->prev = ptr;
+    ptr->priority   = HYTHREAD_PRIORITY_NORMAL;
+    // not implemented
+    //ptr->big_thread_local_storage = (void **)apr_pcalloc(pool, sizeof(void*)*tm_tls_capacity);
+    
+    // Suspension
+    ptr->suspend_request = 0;
+    ptr->suspend_disable_count = 0;
+    status = hylatch_create(&ptr->join_event, 1);
+        assert (status == TM_ERROR_NONE);
+    status = hylatch_create(&ptr->safe_region_event, 1);        
+        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);
+        assert (status == TM_ERROR_NONE);
+    status = hysem_create(&ptr->sleep_event, 0, 1);
+        assert (status == TM_ERROR_NONE);
+    
+    ptr->state = TM_THREAD_STATE_ALLOCATED;
+    status = allocate_thread(ptr, group);
+	if (status !=TM_ERROR_NONE)
+	{
+		return NULL;
+	}
+    group->threads_count++;
+
+    return ptr;
+}
+
+static void reset_thread(hythread_t thread) {
+    apr_status_t apr_status;
+        IDATA status;
+    apr_thread_join(&apr_status, thread->os_handle);
+        assert(!apr_status);
+    thread->os_handle  = NULL;
+    thread->priority   = HYTHREAD_PRIORITY_NORMAL;
+    // not implemented
+    //ptr->big_thread_local_storage = (void **)apr_pcalloc(pool, sizeof(void*)*tm_tls_capacity);
+    thread->next       = thread->prev = thread;
+    // Suspension
+    thread->suspend_request = 0;
+    thread->suspend_disable_count = 0;
+    status = hylatch_set(thread->join_event, 1);
+        assert (status == TM_ERROR_NONE);
+    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;
+    status =allocate_thread(thread, thread->group);
+	if (status!=TM_ERROR_NONE)
+	{
+		thread=NULL;
+	}
+    thread->group->threads_count++;
+}
+
+// Wrapper around user thread start proc. Used to perform some duty jobs 
+// right after thread is started.
+//////
+static void* APR_THREAD_FUNC thread_start_proc(apr_thread_t* thd, void *p_args) {
+        IDATA status;
+    hythread_t thread = (hythread_t )p_args;
+
+    TRACE(("TM: native thread started: native: %p tm: %p", apr_os_thread_current(), thread));
+    // Also, should it be executed under TM global lock?
+    thread->os_handle = thd; // DELETE?
+    thread_set_self(thread);
+    status = hythread_set_priority(thread, thread->priority);
+        //assert (status == TM_ERROR_NONE);//now we down - fixme
+    thread->state |= TM_THREAD_STATE_RUNNABLE;
+
+    // Do actual call of the thread body supplied by the user
+    thread->start_proc(thread->start_proc_args);
+    
+    thread->state = TM_THREAD_STATE_TERMINATED;
+    // Send join event to those threads who called join on this thread
+    hylatch_count_down(thread->join_event);
+    status = thread_destroy(thread);       // Remove thread from the list of thread 
+    assert (status == TM_ERROR_NONE);
+    
+    // Cleanup TLS after thread completes
+    thread_set_self(NULL); 
+    return (void *)apr_thread_exit(thd, APR_SUCCESS);
+}
+
+apr_pool_t* get_local_pool() {
+  hythread_t self = tm_self_tls;
+  if(self == NULL) {
+      return TM_POOL;
+  } else {
+      return self->pool;
+  }
+}
+ 
+/**
+  * TODO: implement this function to reduce memory leaks.
+  */
+IDATA local_pool_cleanup_register(void* func, void* data) {
+   return TM_ERROR_NONE;
+}
+

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_basic.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_condvar.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_condvar.c?rev=434076&view=auto
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_condvar.c (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_condvar.c Wed Aug 23 09:48:41 2006
@@ -0,0 +1,176 @@
+/*
+ *  Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/** 
+ * @author Nikolay Kuznetsov
+ * @version $Revision: 1.1.2.8 $
+ */  
+
+/**
+ * @file thread_native_condvar.c
+ * @brief Hythread condvar related functions
+ */
+
+#include "thread_private.h"
+#include <open/hythread_ext.h>
+
+
+/** @name Conditional variable
+ */
+//@{
+
+/**
+ * Creates and initializes condition variable.
+ *
+ * @param[out] cond the memory address where the newly created condition variable
+ * will be stored.
+ * @sa apr_thread_cond_create()
+ */
+IDATA VMCALL hycond_create (hycond_t *cond) {
+    apr_pool_t *pool = get_local_pool(); 
+    apr_status_t apr_status = apr_thread_cond_create((apr_thread_cond_t**)cond, pool);
+    if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+    return TM_ERROR_NONE;
+}
+
+/*
+ */
+IDATA condvar_wait_impl(hycond_t cond, hymutex_t mutex, I_64 ms, IDATA nano, IDATA interruptable) {
+        apr_status_t apr_status;
+        int disable_count;
+        hythread_t this_thread;
+    
+    this_thread = tm_self_tls;    
+    
+    // Store provided cond into current thread cond
+    this_thread->current_condition = interruptable ? cond : NULL;
+
+        // check interrupted flag
+    if (interruptable && (this_thread->state & TM_THREAD_STATE_INTERRUPTED)) {
+        // clean interrupted flag
+        this_thread->state &= (~TM_THREAD_STATE_INTERRUPTED);
+                return TM_ERROR_INTERRUPT;
+    }
+
+        disable_count = reset_suspend_disable(); 
+        // Delegate to OS wait
+    apr_status = (!ms && !nano)?
+        apr_thread_cond_wait((apr_thread_cond_t*)cond, (apr_thread_mutex_t*)mutex):
+        apr_thread_cond_timedwait ((apr_thread_cond_t*)cond, (apr_thread_mutex_t*)mutex, ms*1000+ nano / 1000);
+        
+    set_suspend_disable(disable_count);
+
+        this_thread->current_condition = NULL;
+   
+    // check interrupted flag
+    if (interruptable &&  (this_thread->state & TM_THREAD_STATE_INTERRUPTED)) {
+        // clean interrupted flag
+        this_thread->state &= (~TM_THREAD_STATE_INTERRUPTED);
+                return TM_ERROR_INTERRUPT;
+    }
+
+        return CONVERT_ERROR(apr_status);
+
+}
+
+/**
+ * Instructs the current thread to wait until it is signaled to wake up.
+ * 
+ * @param[in] cond the condition variable on which to block
+ * @param[in] mutex the mutex that must be locked upon entering this function
+ * @sa apr_thread_cond_wait()
+ * @return  
+ *      TM_NO_ERROR on success 
+ */
+IDATA VMCALL hycond_wait(hycond_t cond, hymutex_t mutex) {
+    return condvar_wait_impl(cond, mutex, 0, 0, 0);
+}
+
+/**
+ * Instructs the current thread to wait until signaled to wake up or
+ * the specified timeout is elapsed.
+ *
+ * @param[in] cond the condition variable on which to block.
+ * @param[in] mutex the mutex that must be locked upon entering this function
+ * @param[in] ms amount of time in milliseconds to wait
+ * @param[in] nano amount of time in nanoseconds to wait
+ * @sa apr_thread_cond_timedwait()
+ * @return  
+ *      TM_NO_ERROR on success 
+ */
+IDATA VMCALL hycond_wait_timed(hycond_t cond, hymutex_t mutex, I_64 ms, IDATA nano) {
+    return condvar_wait_impl(cond, mutex, ms, nano, 0);
+}
+
+/**
+ * Instructs the current thread to wait until signaled to wake up or
+ * the specified timeout is elapsed.
+ *
+ * @param[in] cond the condition variable on which to block.
+ * @param[in] mutex the mutex that must be locked upon entering this function
+ * @param[in] ms amount of time in milliseconds to wait
+ * @param[in] nano amount of time in nanoseconds to wait
+ * @sa apr_thread_cond_timedwait()
+ * @return  
+ *      TM_NO_ERROR on success 
+ *      TM_THREAD_INTERRUPTED in case thread was interrupted during wait.
+ */
+IDATA VMCALL hycond_wait_interruptable(hycond_t cond, hymutex_t mutex, I_64 ms, IDATA nano) {
+    return condvar_wait_impl(cond, mutex, ms, nano, WAIT_INTERRUPTABLE);
+}
+
+/**
+ * Signals a single thread that is blocking on the given condition variable to wake up. 
+ *
+ * @param[in] cond the condition variable on which to produce the signal.
+ * @sa apr_thread_cond_signal()
+ */
+IDATA VMCALL hycond_notify (hycond_t cond) {
+    apr_status_t apr_status = apr_thread_cond_signal((apr_thread_cond_t*)cond);
+    if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+    return TM_ERROR_NONE;
+}
+
+/**
+ * Signals all threads blocking on the given condition variable.
+ * 
+ * @param[in] cond the condition variable on which to produce the broadcast.
+ * @sa apr_thread_cond_broadcast()
+ */
+IDATA VMCALL hycond_notify_all (hycond_t cond) {
+        apr_status_t apr_status = apr_thread_cond_broadcast((apr_thread_cond_t*)cond);
+        if (apr_status != APR_SUCCESS) return CONVERT_ERROR(apr_status);
+    return TM_ERROR_NONE;   
+}
+
+/**
+ * Destroys the condition variable and releases the associated memory.
+ *
+ * @param[in] cond the condition variable to destroy
+ * @sa apr_thread_cond_destroy()
+ */
+IDATA VMCALL hycond_destroy (hycond_t cond) {
+        apr_status_t apr_status;
+    apr_pool_t *pool = apr_thread_cond_pool_get ((apr_thread_cond_t*)cond);
+    if(pool != get_local_pool()) {
+          return local_pool_cleanup_register(hycond_destroy, cond);
+    }
+    apr_status=apr_thread_cond_destroy((apr_thread_cond_t*)cond);
+    return CONVERT_ERROR(apr_status);
+
+}
+
+//@}

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/thread/src/thread_native_condvar.c
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message