Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not include C files. Instead, add them to the project file. Hopefully, this will...
[simgrid.git] / src / java / jmsg.c
index d27abbf43d151a6d2ac7f89cd856bce9e844f9b3..9d7c85adea9c7f2ad45de8c63ddc85abc35f080b 100644 (file)
@@ -12,7 +12,8 @@
 #include "msg/msg.h"
 #include "msg/private.h"
 #include "simix/private.h"
-#include "java/jxbt_context.h"
+#include "xbt/xbt_jcontext.h"
+#include "xbt/xbt_context_factory.h"
 
 #include "jmsg_process.h"
 #include "jmsg_host.h"
 #include "jmsg_channel.h"
 #include "jxbt_utilities.h"
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
-
-/* header for windows */
-#ifdef WIN32
-#include <windows.h>
-#else
-#include <pthread.h>
-#endif
-
 #include "jmsg.h"
 
-static JavaVM * __java_vm = NULL;
-
-
-#ifdef WIN32
-  static DWORD __current_thread_id = 0;
-
-  int is_main_thread() {
-    return (GetCurrentThreadId() == __current_thread_id);
-  }
-
-#else /* !WIN32 */
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-  static pthread_t __current_thread_id = 0;
+static JavaVM * __java_vm = NULL;
 
-  int is_main_thread() {
-    return (pthread_self() == __current_thread_id);
-  }
-#endif
+static jobject
+native_to_java_process(m_process_t process);
 
 JavaVM *
 get_java_VM(void) {
@@ -65,6 +45,12 @@ get_current_thread_env(void) {
         return env;
 }
 
+static jobject
+native_to_java_process(m_process_t process)
+{
+       return ((xbt_jcontext_t)(process->simdata->s_process->simdata->context))->jprocess;     
+}
+
 
 /*
  * The MSG process connected functions implementation.                                 
@@ -132,6 +118,8 @@ Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg
                        /*data*/ (void*)process,
                        jprocess,env,
                        &process->simdata->s_process);
+
+  
   DEBUG1("context created (s_process=%p)",process->simdata->s_process);
 
 
@@ -200,7 +188,7 @@ Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
   }
 
   /* delete the global reference */
-  jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env);
+  jprocess_delete_global_ref(native_to_java_process(process),env);
        
   /* kill the native process (this wrapper is call by the destructor of the java 
    * process instance)
@@ -240,12 +228,12 @@ Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) {
     return NULL;
   }
 
-  if(!SIMIX_process_get_jprocess(process->simdata->s_process)) {
+  if(!native_to_java_process(process)) {
     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
     return NULL;
   }
 
-  return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process);
+  return (jobject)(native_to_java_process(process));
 }
 
 
@@ -284,7 +272,7 @@ Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) {
     return NULL;
   }
 
-  jprocess = SIMIX_process_get_jprocess(process->simdata->s_process);
+  jprocess = native_to_java_process(process);
 
   if(!jprocess)
     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
@@ -607,7 +595,7 @@ Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
   }
        
   process = MSG_task_get_sender(task);
-  return SIMIX_process_get_jprocess(process->simdata->s_process);
+  return (jobject)native_to_java_process(process);
 }
 
 JNIEXPORT jobject JNICALL 
@@ -624,7 +612,7 @@ Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jt
        
   process = MSG_task_get_sender(task);
        
-  return SIMIX_process_get_jprocess(process->simdata->s_process);
+  return (jobject)native_to_java_process(process);
 }
 
 JNIEXPORT jobject JNICALL 
@@ -1061,13 +1049,7 @@ Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
        
   free(argv);
 
-#ifdef WIN32
-  __current_thread_id = GetCurrentThreadId();
-#else
-  __current_thread_id = pthread_self();
-#endif
-
-(*env)->GetJavaVM(env,&__java_vm);
+ (*env)->GetJavaVM(env,&__java_vm);
        
 }
 
@@ -1117,12 +1099,6 @@ Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatform
 JNIEXPORT void JNICALL 
 Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) {
 
-  xbt_os_mutex_t creation_mutex = xbt_creation_mutex_get();
-  xbt_os_cond_t creation_cond = xbt_creation_cond_get();
-  xbt_os_mutex_lock(creation_mutex);
-  xbt_os_cond_signal( creation_cond );
-  xbt_os_mutex_unlock( creation_mutex );
-
 }
 
 JNIEXPORT void JNICALL 
@@ -1135,7 +1111,7 @@ Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
     return;
   }
 
-  jcontext_exit(process->simdata->s_process->simdata->context,0,get_current_thread_env());
+  xbt_context_stop(0);
 }
 
 JNIEXPORT void JNICALL 
@@ -1196,4 +1172,20 @@ Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) {
   return jtable;
 }
 
+
+JNIEXPORT void JNICALL 
+Java_simgrid_msg_Msg_selectContextFactory(JNIEnv * env, jclass class,jstring jname)
+{
+       int rv;
+       
+       /* get the C string from the java string*/
+       const char* name = (*env)->GetStringUTFChars(env, jname, 0);
+
+       rv = xbt_context_select_factory(name);
+               
+       (*env)->ReleaseStringUTFChars(env, jname, name);
+       
+       if(rv)
+               jxbt_throw_native(env, xbt_strdup("xbt_select_context_factory() failed"));       
+}