4 * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package.
9 * This contains the implementation of the wrapper functions used to interface
10 * the java object with the native functions of the MSG API.
13 #include "msg/private.h"
14 #include "java/jxbt_context.h"
16 #include "jmsg_process.h"
17 #include "jmsg_host.h"
18 #include "jmsg_task.h"
19 #include "jmsg_parallel_task.h"
20 #include "jmsg_channel.h"
21 #include "jxbt_utilities.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
25 /* header for windows */
36 static DWORD __current_thread_id = 0;
38 int is_main_thread() {
39 return (GetCurrentThreadId() == __current_thread_id);
44 static pthread_t __current_thread_id = 0;
46 int is_main_thread() {
47 return (pthread_self() == __current_thread_id);
53 * The MSG process connected functions implementation.
56 JNIEXPORT void JNICALL
57 Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg, jobject jhost) {
58 jobject jprocess; /* the global reference to the java process instance */
59 jstring jname; /* the name of the java process instance */
60 const char* name; /* the C name of the process */
61 m_process_t process; /* the native process to create */
64 DEBUG4("Java_simgrid_msg_Msg_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
65 env,cls,jprocess_arg,jhost);
66 /* get the name of the java process */
67 jname = jprocess_get_name(jprocess_arg,env);
70 jxbt_throw_null(env,xbt_strdup("Internal error: Process name cannot be NULL"));
74 /* allocate the data of the simulation */
75 process = xbt_new0(s_m_process_t,1);
76 process->simdata = xbt_new0(s_simdata_process_t,1);
78 /* create a global java process instance */
79 jprocess = jprocess_new_global_ref(jprocess_arg,env);
82 free(process->simdata);
84 jxbt_throw_jni(env, "Can't get a global ref to the java process");
88 /* bind the java process instance to the native host */
89 jprocess_bind(jprocess,process,env);
91 /* build the C name of the process */
92 name = (*env)->GetStringUTFChars(env, jname, 0);
93 process->name = xbt_strdup(name);
94 (*env)->ReleaseStringUTFChars(env, jname, name);
96 process->simdata->m_host = jhost_get_native(env,jhost);
97 if( ! (process->simdata->m_host) ) { /* not binded */
98 free(process->simdata);
101 jxbt_throw_notbound(env,"host",jhost);
104 process->simdata->PID = msg_global->PID++;
106 /* create a new context */
107 DEBUG8("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
108 process->name,process->simdata->m_host->name,
109 process->simdata->PID,process,
110 process->simdata, process->simdata->m_host, process->simdata->m_host->simdata,
113 SIMIX_jprocess_create(process->name,
114 process->simdata->m_host->simdata->s_host,
115 /*data*/ (void*)process,
117 &process->simdata->s_process);
118 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
121 if (SIMIX_process_self()) { /* someone created me */
122 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
124 process->simdata->PPID = -1;
127 process->simdata->last_errno = MSG_OK;
131 /* add the process in the list of the process of the host */
132 xbt_fifo_unshift(host->simdata->process_list, process);
134 self = msg_global->current_process;
136 process->simdata->context->env = env;
138 /* start the java process */
139 xbt_context_start(process->simdata->context);
141 msg_global->current_process = self;
144 /* add the process to the list of the processes of the simulation */
145 xbt_fifo_unshift(msg_global->process_list, process);
147 /* add the process to the list of the processes to run in the simulation */
148 // xbt_fifo_unshift(msg_global->process_to_run, process);
150 // PAJE_PROCESS_NEW(process);
154 JNIEXPORT void JNICALL
155 Java_simgrid_msg_Msg_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
156 m_process_t process = jprocess_to_native_process(jprocess,env);
159 jxbt_throw_notbound(env,"process",jprocess);
163 /* try to suspend the process */
164 if(MSG_OK != MSG_process_suspend(process))
165 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
168 JNIEXPORT void JNICALL
169 Java_simgrid_msg_Msg_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
170 m_process_t process = jprocess_to_native_process(jprocess,env);
173 jxbt_throw_notbound(env,"process",jprocess);
177 /* try to resume the process */
178 if(MSG_OK != MSG_process_resume(process))
179 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
182 JNIEXPORT jboolean JNICALL
183 Java_simgrid_msg_Msg_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
184 m_process_t process = jprocess_to_native_process(jprocess,env);
187 jxbt_throw_notbound(env,"process",jprocess);
191 /* true is the process is suspended, false otherwise */
192 return (jboolean)MSG_process_is_suspended(process);
195 JNIEXPORT void JNICALL
196 Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
197 /* get the native instances from the java ones */
198 m_process_t process = jprocess_to_native_process(jprocess,env);
201 jxbt_throw_notbound(env,"process",jprocess);
205 /* delete the global reference */
206 jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env);
208 /* kill the native process (this wrapper is call by the destructor of the java
211 MSG_process_kill(process);
214 JNIEXPORT jobject JNICALL
215 Java_simgrid_msg_Msg_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
216 /* get the native instances from the java ones */
217 m_process_t process = jprocess_to_native_process(jprocess,env);
221 jxbt_throw_notbound(env,"process",jprocess);
225 host = MSG_process_get_host(process);
228 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
232 /* return the global reference to the java host instance */
233 return (jobject)host->data;
237 JNIEXPORT jobject JNICALL
238 Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) {
239 m_process_t process = MSG_process_from_PID(PID);
242 jxbt_throw_native(env, bprintf("MSG_process_from_PID(%d) failed",PID));
246 if(!SIMIX_process_get_jprocess(process->simdata->s_process)) {
247 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
251 return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process);
255 JNIEXPORT jint JNICALL
256 Java_simgrid_msg_Msg_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
257 m_process_t process = jprocess_to_native_process(jprocess,env);
260 jxbt_throw_notbound(env,"process",jprocess);
264 return (jint)MSG_process_get_PID(process);
268 JNIEXPORT jint JNICALL
269 Java_simgrid_msg_Msg_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
270 m_process_t process = jprocess_to_native_process(jprocess,env);
273 jxbt_throw_notbound(env,"process",jprocess);
277 return (jint)MSG_process_get_PPID(process);
280 JNIEXPORT jobject JNICALL
281 Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) {
282 m_process_t process = MSG_process_self();
286 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
290 jprocess = SIMIX_process_get_jprocess(process->simdata->s_process);
293 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
299 JNIEXPORT jint JNICALL
300 Java_simgrid_msg_Msg_processSelfPID(JNIEnv* env, jclass cls) {
301 return (jint)MSG_process_self_PID();
305 JNIEXPORT jint JNICALL
306 Java_simgrid_msg_Msg_processSelfPPID(JNIEnv* env, jclass cls) {
307 return (jint)MSG_process_self_PPID();
310 JNIEXPORT void JNICALL
311 Java_simgrid_msg_Msg_processChangeHost(JNIEnv* env, jclass cls, jobject jprocess, jobject jhost){
312 m_host_t host = jhost_get_native(env,jhost);
313 m_process_t process = jprocess_to_native_process(jprocess,env);
316 jxbt_throw_notbound(env,"process",jprocess);
321 jxbt_throw_notbound(env,"host",jhost);
325 /* try to change the host of the process */
326 if(MSG_OK != MSG_process_change_host(process,host))
327 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
330 JNIEXPORT void JNICALL
331 Java_simgrid_msg_Msg_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
332 if(MSG_OK != MSG_process_sleep((double)seconds))
333 jxbt_throw_native(env,
334 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
338 /***************************************************************************************
339 * The MSG host connected functions implementation. *
340 ***************************************************************************************/
342 JNIEXPORT jobject JNICALL
343 Java_simgrid_msg_Msg_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
344 m_host_t host; /* native host */
345 jobject jhost; /* global reference to the java host instance returned */
347 /* get the C string from the java string*/
348 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
350 /* get the host by name (the hosts are created during the grid resolution) */
351 host = MSG_get_host_by_name(name);
352 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
354 (*env)->ReleaseStringUTFChars(env, jname, name);
356 if(!host) {/* invalid name */
357 jxbt_throw_host_not_found(env,name);
361 if(!host->data) { /* native host not associated yet with java host */
363 /* instanciate a new java host */
364 jhost = jhost_new_instance(env);
367 jxbt_throw_jni(env,"java host instantiation failed");
371 /* get a global reference to the newly created host */
372 jhost = jhost_ref(env,jhost);
375 jxbt_throw_jni(env,"new global ref allocation failed");
379 /* bind the java host and the native host */
380 jhost_bind(jhost,host,env);
382 /* the native host data field is set with the global reference to the
383 * java host returned by this function
385 host->data = (void*)jhost;
388 /* return the global reference to the java host instance */
389 return (jobject)host->data;
392 JNIEXPORT jstring JNICALL
393 Java_simgrid_msg_Msg_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
394 m_host_t host = jhost_get_native(env,jhost);
397 jxbt_throw_notbound(env,"host",jhost);
401 return (*env)->NewStringUTF(env,host->name);
404 JNIEXPORT jint JNICALL
405 Java_simgrid_msg_Msg_hostGetNumber(JNIEnv* env, jclass cls) {
406 return (jint)MSG_get_host_number();
409 JNIEXPORT jobject JNICALL
410 Java_simgrid_msg_Msg_hostSelf(JNIEnv* env, jclass cls) {
413 m_host_t host = MSG_host_self();
416 /* the native host not yet associated with the java host instance */
418 /* instanciate a new java host instance */
419 jhost = jhost_new_instance(env);
422 jxbt_throw_jni(env,"java host instantiation failed");
426 /* get a global reference to the newly created host */
427 jhost = jhost_ref(env,jhost);
430 jxbt_throw_jni(env,"global ref allocation failed");
434 /* Bind & store it */
435 jhost_bind(jhost,host,env);
436 host->data = (void*)jhost;
438 jhost = (jobject)host->data;
444 JNIEXPORT jdouble JNICALL
445 Java_simgrid_msg_Msg_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
446 m_host_t host = jhost_get_native(env,jhost);
449 jxbt_throw_notbound(env,"host",jhost);
453 return (jdouble)MSG_get_host_speed(host);
456 JNIEXPORT jint JNICALL
457 Java_simgrid_msg_Msg_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
458 m_host_t host = jhost_get_native(env,jhost);
461 jxbt_throw_notbound(env,"host",jhost);
465 return (jint)MSG_get_host_msgload(host);
469 JNIEXPORT jboolean JNICALL
470 Java_simgrid_msg_Msg_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
471 m_host_t host = jhost_get_native(env,jhost);
474 jxbt_throw_notbound(env,"host",jhost);
478 return (jboolean)MSG_host_is_avail(host);
482 /***************************************************************************************
483 * The MSG task connected functions implementation. *
484 ***************************************************************************************/
486 JNIEXPORT void JNICALL
487 Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
488 jdouble jcomputeDuration, jdouble jmessageSize) {
489 m_task_t task; /* the native task to create */
490 const char* name; /* the name of the task */
492 if(jcomputeDuration < 0) {
493 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
494 (double)jcomputeDuration));
498 if(jmessageSize < 0) {
499 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
500 (double)jmessageSize));
505 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
509 /* get the C string from the java string*/
510 name = (*env)->GetStringUTFChars(env, jname, 0);
512 /* create the task */
513 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
515 (*env)->ReleaseStringUTFChars(env, jname, name);
517 /* bind & store the task */
518 jtask_bind(jtask,task,env);
520 /* allocate a new global reference to the java task instance */
521 task->data = (void*)jtask_new_global_ref(jtask,env);
524 jxbt_throw_jni(env,"global ref allocation failed");
527 JNIEXPORT void JNICALL
528 Java_simgrid_msg_Msg_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask_arg, jstring jname,
529 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
531 m_task_t task; /* the native parallel task to create */
532 const char* name; /* the name of the task */
533 jobject jtask; /* the global reference to the java parallel task instance */
536 double* computeDurations;
537 double* messageSizes;
538 jdouble *jcomputeDurations;
539 jdouble *jmessageSizes;
545 if (!jcomputeDurations_arg) {
546 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
550 if(!jmessageSizes_arg){
551 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
556 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
560 host_count = (int)(*env)->GetArrayLength(env,jhosts);
563 hosts = (m_host_t*)calloc(host_count,sizeof(m_host_t));
564 computeDurations = (double*)calloc(host_count,sizeof(double));
565 messageSizes = (double*)calloc(host_count,sizeof(double));
567 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
568 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
570 for(index = 0; index < host_count; index++) {
571 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
572 hosts[index] = jhost_get_native(env,jhost);
573 computeDurations[index] = jcomputeDurations[index];
574 messageSizes[index] = jmessageSizes[index];
577 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
578 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
581 /* get the C string from the java string*/
582 name = (*env)->GetStringUTFChars(env, jname, 0);
584 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
586 (*env)->ReleaseStringUTFChars(env, jname, name);
588 /* allocate a new global reference to the java task instance */
589 jtask = jparallel_task_ref(env,jtask_arg);
591 /* associate the java task object and the native task */
592 jparallel_task_bind(jtask,task,env);
594 task->data = (void*)jtask;
597 jxbt_throw_jni(env,"global ref allocation failed");
600 JNIEXPORT jobject JNICALL
601 Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
604 m_task_t task = jtask_to_native_task(jtask,env);
607 jxbt_throw_notbound(env,"task",jtask);
611 process = MSG_task_get_sender(task);
612 return SIMIX_process_get_jprocess(process->simdata->s_process);
615 JNIEXPORT jobject JNICALL
616 Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
620 task = jparallel_task_to_native_parallel_task(jtask,env);
623 jxbt_throw_notbound(env,"task",jtask);
627 process = MSG_task_get_sender(task);
629 return SIMIX_process_get_jprocess(process->simdata->s_process);
632 JNIEXPORT jobject JNICALL
633 Java_simgrid_msg_Msg_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
635 m_task_t task = jtask_to_native_task(jtask,env);
638 jxbt_throw_notbound(env,"task",jtask);
642 host = MSG_task_get_source(task);
645 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
649 return (jobject)host->data;
652 JNIEXPORT jobject JNICALL
653 Java_simgrid_msg_Msg_parallelTaskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
654 m_task_t task = jparallel_task_to_native_parallel_task(jtask,env);
658 jxbt_throw_notbound(env,"task",jtask);
662 host = MSG_task_get_source(task);
665 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
669 return (jobject)host->data;
673 JNIEXPORT jstring JNICALL
674 Java_simgrid_msg_Msg_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
675 m_task_t task = jtask_to_native_task(jtask,env);
678 jxbt_throw_notbound(env,"task",jtask);
682 return (*env)->NewStringUTF(env,task->name);
685 JNIEXPORT jstring JNICALL
686 Java_simgrid_msg_Msg_parallelTaskGetName(JNIEnv* env, jclass cls, jobject jtask) {
687 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
690 jxbt_throw_notbound(env,"task",jtask);
694 return (*env)->NewStringUTF(env,ptask->name);
697 JNIEXPORT void JNICALL
698 Java_simgrid_msg_Msg_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
699 m_task_t ptask = jtask_to_native_task(jtask,env);
702 jxbt_throw_notbound(env,"task",jtask);
706 if(MSG_OK != MSG_task_cancel(ptask))
707 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
710 JNIEXPORT void JNICALL
711 Java_simgrid_msg_Msg_parallelTaskCancel(JNIEnv* env, jclass cls, jobject jtask) {
712 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
715 jxbt_throw_notbound(env,"task",jtask);
719 if(MSG_OK != MSG_task_cancel(ptask))
720 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
724 JNIEXPORT jdouble JNICALL
725 Java_simgrid_msg_Msg_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
726 m_task_t ptask = jtask_to_native_task(jtask,env);
729 jxbt_throw_notbound(env,"task",jtask);
732 return (jdouble)MSG_task_get_compute_duration(ptask);
735 JNIEXPORT jdouble JNICALL
736 Java_simgrid_msg_Msg_parallelTaskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
737 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
740 jxbt_throw_notbound(env,"task",jtask);
743 return (jdouble)MSG_task_get_compute_duration(ptask);
746 JNIEXPORT jdouble JNICALL
747 Java_simgrid_msg_Msg_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
748 m_task_t ptask = jtask_to_native_task(jtask,env);
751 jxbt_throw_notbound(env,"task",jtask);
754 return (jdouble)MSG_task_get_remaining_computation(ptask);
757 JNIEXPORT jdouble JNICALL
758 Java_simgrid_msg_Msg_paralleTaskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
759 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
762 jxbt_throw_notbound(env,"task",jtask);
765 return (jdouble)MSG_task_get_remaining_computation(ptask);
768 JNIEXPORT void JNICALL
769 Java_simgrid_msg_Msg_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
770 m_task_t task = jtask_to_native_task(jtask,env);
773 jxbt_throw_notbound(env,"task",jtask);
776 MSG_task_set_priority(task,(double)priority);
779 JNIEXPORT void JNICALL
780 Java_simgrid_msg_Msg_parallelTaskSetPriority(JNIEnv* env, jclass cls,
781 jobject jtask, jdouble priority) {
782 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
785 jxbt_throw_notbound(env,"task",jtask);
788 MSG_task_set_priority(ptask,(double)priority);
792 JNIEXPORT void JNICALL
793 Java_simgrid_msg_Msg_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
795 /* get the native task */
796 m_task_t task = jtask_to_native_task(jtask_arg,env);
800 jxbt_throw_notbound(env,"task",jtask);
803 jtask = (jobject)task->data;
805 if(MSG_OK != MSG_task_destroy(task))
806 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
808 /* delete the global reference to the java task object */
809 jtask_delete_global_ref(jtask,env);
812 JNIEXPORT void JNICALL
813 Java_simgrid_msg_Msg_parallelTaskDestroy(JNIEnv* env, jclass cls, jobject jtask) {
814 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
817 jxbt_throw_notbound(env,"task",jtask);
821 /* unbind jobj & native one */
822 jparallel_task_bind(jtask,0,env);
824 /* delete the global reference to the java parallel task object */
825 jparallel_task_unref(env,jtask);
827 /* free allocated memory */
828 if(ptask->simdata->comm_amount)
829 free(ptask->simdata->comm_amount);
831 if(ptask->simdata->host_list)
832 free(ptask->simdata->host_list);
834 if(MSG_OK != MSG_task_destroy(ptask))
835 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
838 JNIEXPORT void JNICALL
839 Java_simgrid_msg_Msg_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
840 m_task_t task = jtask_to_native_task(jtask,env);
843 jxbt_throw_notbound(env,"task",jtask);
847 if(MSG_OK != MSG_task_execute(task))
848 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
851 JNIEXPORT void JNICALL
852 Java_simgrid_msg_Msg_parallelTaskExecute(JNIEnv* env, jclass cls, jobject jtask) {
853 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
856 jxbt_throw_notbound(env,"task",jtask);
860 if(MSG_OK != MSG_parallel_task_execute(ptask))
861 jxbt_throw_native(env, xbt_strdup("MSG_parallel_task_execute() failed"));
864 /***************************************************************************************
865 * The MSG channel connected functions implementation. *
866 ***************************************************************************************/
868 JNIEXPORT jobject JNICALL
869 Java_simgrid_msg_Msg_channelGet(JNIEnv* env, jclass cls, jobject jchannel) {
870 m_task_t task = NULL;
872 if(MSG_OK != MSG_task_get(&task,(int)jchannel_get_id(jchannel,env))) {
873 jxbt_throw_native(env, xbt_strdup("MSG_task_get() failed"));
877 return (jobject)task->data;
880 JNIEXPORT jobject JNICALL
881 Java_simgrid_msg_Msg_channelGetWithTimeout(JNIEnv* env, jclass cls,
882 jobject jchannel, jdouble jtimeout) {
883 m_task_t task = NULL;
884 int id = (int)jchannel_get_id(jchannel,env);
886 if(MSG_OK != MSG_task_get_with_time_out(&task,id,(double)jtimeout)) {
887 jxbt_throw_native(env, xbt_strdup("MSG_task_get_with_time_out() failed"));
891 return (jobject)task->data;
894 JNIEXPORT jobject JNICALL
895 Java_simgrid_msg_Msg_channelGetFromHost(JNIEnv* env, jclass cls,
896 jobject jchannel, jobject jhost) {
897 m_host_t host = jhost_get_native(env,jhost);
898 m_task_t task = NULL;
899 int id = (int)jchannel_get_id(jchannel,env);
902 jxbt_throw_notbound(env,"host",jhost);
905 if(MSG_OK != MSG_task_get_from_host(&task,id,host)){
906 jxbt_throw_native(env, xbt_strdup("MSG_task_get_from_host() failed"));
910 jxbt_throw_notbound(env,"task",task);
914 return (jobject)task->data;
917 JNIEXPORT jboolean JNICALL
918 Java_simgrid_msg_Msg_channelHasPendingCommunication(JNIEnv* env, jclass cls, jobject jchannel) {
919 int id = (int)jchannel_get_id(jchannel,env);
921 return (jboolean)MSG_task_Iprobe(id);
924 JNIEXPORT jint JNICALL
925 Java_simgrid_msg_Msg_channelGetCommunicatingProcess(JNIEnv* env, jclass cls, jobject jchannel) {
926 int id =jchannel_get_id(jchannel,env);
928 return (jint)MSG_task_probe_from(id);
931 JNIEXPORT jint JNICALL
932 Java_simgrid_msg_Msg_channelGetHostWaitingTasks(JNIEnv* env, jclass cls,
933 jobject jchannel, jobject jhost) {
934 int id = (int)jchannel_get_id(jchannel,env);
935 m_host_t host = jhost_get_native(env,jhost);
938 jxbt_throw_notbound(env,"host",jhost);
942 return (jint)MSG_task_probe_from_host(id,host);
945 JNIEXPORT void JNICALL
946 Java_simgrid_msg_Msg_channelPut(JNIEnv* env, jclass cls,
947 jobject jchannel, jobject jtask, jobject jhost) {
949 if(MSG_OK != MSG_task_put(jtask_to_native_task(jtask,env),
950 jhost_get_native(env,jhost),
951 (int)jchannel_get_id(jchannel,env)))
952 jxbt_throw_native(env, xbt_strdup("MSG_task_put() failed"));
955 JNIEXPORT void JNICALL
956 Java_simgrid_msg_Msg_channelPutWithTimeout(JNIEnv* env, jclass cls,
957 jobject jchannel, jobject jtask, jobject jhost,
959 m_task_t task = jtask_to_native_task(jtask,env);
960 int id = (int)jchannel_get_id(jchannel,env);
961 m_host_t host = jhost_get_native(env,jhost);
964 jxbt_throw_notbound(env,"host",jhost);
968 jxbt_throw_notbound(env,"task",jtask);
972 if(MSG_OK != MSG_task_put_with_timeout(task,host,id,(double)jtimeout))
973 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
976 JNIEXPORT void JNICALL
977 Java_simgrid_msg_Msg_channelPutBounded(JNIEnv* env, jclass cls,
978 jobject jchannel, jobject jtask, jobject jhost,
980 m_task_t task = jtask_to_native_task(jtask,env);
981 int id = (int)jchannel_get_id(jchannel,env);
982 m_host_t host = jhost_get_native(env,jhost);
985 jxbt_throw_notbound(env,"host",jhost);
989 jxbt_throw_notbound(env,"task",jtask);
993 if(MSG_OK != MSG_task_put_bounded(task,host,id,(double)jmaxRate))
994 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
997 JNIEXPORT jint JNICALL
998 Java_simgrid_msg_Msg_channelWait(JNIEnv* env, jclass cls, jobject jchannel, jdouble timeout) {
1000 int id = (int)jchannel_get_id(jchannel,env);
1002 if(MSG_OK != MSG_channel_select_from(id,(double)timeout,&PID)) {
1003 jxbt_throw_native(env, xbt_strdup("MSG_channel_select_from() failed"));
1010 JNIEXPORT jint JNICALL
1011 Java_simgrid_msg_Msg_channelGetNumber(JNIEnv* env, jclass cls) {
1012 return (jint)MSG_get_channel_number();
1015 JNIEXPORT void JNICALL
1016 Java_simgrid_msg_Msg_channelSetNumber(JNIEnv* env , jclass cls,jint channelNumber) {
1017 MSG_set_channel_number((int)channelNumber);
1020 JNIEXPORT jint JNICALL
1021 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
1022 return (jint)MSG_get_errno();
1025 JNIEXPORT jdouble JNICALL
1026 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
1027 return (jdouble)MSG_get_clock();
1031 JNIEXPORT void JNICALL
1032 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
1041 argc = (int)(*env)->GetArrayLength(env,jargs);
1045 argv = (char**)calloc(argc,sizeof(char*));
1047 argv[0] = strdup("java");
1049 for(index = 0; index < argc -1; index++) {
1050 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
1052 tmp = (*env)->GetStringUTFChars(env, jval, 0);
1054 argv[index +1] = strdup(tmp);
1056 (*env)->ReleaseStringUTFChars(env, jval, tmp);
1059 MSG_global_init(&argc,argv);
1061 for(index = 0; index < argc; index++)
1067 __current_thread_id = GetCurrentThreadId();
1069 __current_thread_id = pthread_self();
1074 JNIEXPORT void JNICALL
1075 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
1076 xbt_fifo_item_t item = NULL;
1077 m_host_t host = NULL;
1080 /* Run everything */
1081 if(MSG_OK != MSG_main())
1082 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1084 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
1085 SIMIX_display_process_status();
1086 exit(0); /* FIXME */
1088 DEBUG0("Clean java world");
1089 /* Cleanup java hosts */
1090 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
1091 jhost = (jobject)host->data;
1094 jhost_unref(env,jhost);
1096 DEBUG0("Clean native world");
1097 /* cleanup native stuff */
1098 if(MSG_OK != MSG_clean())
1099 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1103 JNIEXPORT jint JNICALL
1104 Java_simgrid_msg_Msg_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
1105 return (jint)MSG_process_killall((int)jresetPID);
1108 JNIEXPORT void JNICALL
1109 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
1111 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1113 MSG_create_environment(platformFile);
1115 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1118 JNIEXPORT void JNICALL
1119 Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) {
1120 m_process_t m_process = jprocess_to_native_process(jprocess,env);
1121 smx_process_t s_process;
1123 xbt_os_mutex_t ctx_mutex, creation_mutex;
1124 xbt_os_cond_t ctx_cond, creation_cond;
1126 DEBUG3("Msg_waitSignal(m_process=%p %s/%s)",
1127 m_process,m_process->name,m_process->simdata->m_host->name);
1129 jxbt_throw_notbound(env,"process",jprocess);
1133 s_process = m_process->simdata->s_process;
1135 if (s_process == NULL) {
1136 jxbt_throw_notbound(env,"SIMIX process",jprocess);
1140 ctx_mutex = SIMIX_process_get_jmutex(s_process);
1141 ctx_cond = SIMIX_process_get_jcond(s_process);
1143 creation_mutex = xbt_creation_mutex_get();
1144 creation_cond = xbt_creation_cond_get();
1146 xbt_os_mutex_lock(creation_mutex);
1147 xbt_os_mutex_lock(ctx_mutex);
1148 xbt_os_cond_signal( creation_cond );
1149 xbt_os_mutex_unlock( creation_mutex );
1150 xbt_os_cond_wait(ctx_cond, ctx_mutex);
1151 xbt_os_mutex_unlock(ctx_mutex);
1154 JNIEXPORT void JNICALL
1155 Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
1156 m_process_t process = jprocess_to_native_process(jprocess,env);
1159 jxbt_throw_notbound(env,"process",jprocess);
1162 MSG_process_kill(process);
1165 JNIEXPORT void JNICALL
1166 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
1167 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1169 MSG_paje_output(pajeFile);
1171 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1175 JNIEXPORT void JNICALL
1176 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
1177 const char* s = (*env)->GetStringUTFChars(env,js,0);
1179 (*env)->ReleaseStringUTFChars(env, js, s);
1182 JNIEXPORT jobjectArray JNICALL
1183 Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) {
1185 jobjectArray jtable;
1190 int count = xbt_fifo_size(msg_global->host);
1191 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
1193 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
1199 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
1202 jxbt_throw_jni(env,"Hosts table allocation failed");
1206 for(index = 0; index < count; index++) {
1207 host = table[index];
1208 jhost = (jobject)(host->data);
1211 jname = (*env)->NewStringUTF(env,host->name);
1213 jhost = Java_simgrid_msg_Msg_hostGetByName(env,cls_arg,jname);
1214 /* FIXME: leak of jname ? */
1217 (*env)->SetObjectArrayElement(env, jtable, index, jhost);