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 __MSG_process_cleanup,
118 &process->simdata->s_process);
119 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
122 if (SIMIX_process_self()) { /* someone created me */
123 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
125 process->simdata->PPID = -1;
128 process->simdata->last_errno = MSG_OK;
132 /* add the process in the list of the process of the host */
133 xbt_fifo_unshift(host->simdata->process_list, process);
135 self = msg_global->current_process;
137 process->simdata->context->env = env;
139 /* start the java process */
140 xbt_context_start(process->simdata->context);
142 msg_global->current_process = self;
145 /* add the process to the list of the processes of the simulation */
146 xbt_fifo_unshift(msg_global->process_list, process);
148 /* add the process to the list of the processes to run in the simulation */
149 // xbt_fifo_unshift(msg_global->process_to_run, process);
151 // PAJE_PROCESS_NEW(process);
155 JNIEXPORT void JNICALL
156 Java_simgrid_msg_Msg_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
157 m_process_t process = jprocess_to_native_process(jprocess,env);
160 jxbt_throw_notbound(env,"process",jprocess);
164 /* try to suspend the process */
165 if(MSG_OK != MSG_process_suspend(process))
166 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
169 JNIEXPORT void JNICALL
170 Java_simgrid_msg_Msg_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
171 m_process_t process = jprocess_to_native_process(jprocess,env);
174 jxbt_throw_notbound(env,"process",jprocess);
178 /* try to resume the process */
179 if(MSG_OK != MSG_process_resume(process))
180 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
183 JNIEXPORT jboolean JNICALL
184 Java_simgrid_msg_Msg_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
185 m_process_t process = jprocess_to_native_process(jprocess,env);
188 jxbt_throw_notbound(env,"process",jprocess);
192 /* true is the process is suspended, false otherwise */
193 return (jboolean)MSG_process_is_suspended(process);
196 JNIEXPORT void JNICALL
197 Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
198 /* get the native instances from the java ones */
199 m_process_t process = jprocess_to_native_process(jprocess,env);
202 jxbt_throw_notbound(env,"process",jprocess);
206 /* delete the global reference */
207 jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env);
209 /* kill the native process (this wrapper is call by the destructor of the java
212 MSG_process_kill(process);
215 JNIEXPORT jobject JNICALL
216 Java_simgrid_msg_Msg_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
217 /* get the native instances from the java ones */
218 m_process_t process = jprocess_to_native_process(jprocess,env);
222 jxbt_throw_notbound(env,"process",jprocess);
226 host = MSG_process_get_host(process);
229 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
233 /* return the global reference to the java host instance */
234 return (jobject)host->data;
238 JNIEXPORT jobject JNICALL
239 Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) {
240 m_process_t process = MSG_process_from_PID(PID);
243 jxbt_throw_native(env, bprintf("MSG_process_from_PID(%d) failed",PID));
247 if(!SIMIX_process_get_jprocess(process->simdata->s_process)) {
248 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
252 return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process);
256 JNIEXPORT jint JNICALL
257 Java_simgrid_msg_Msg_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
258 m_process_t process = jprocess_to_native_process(jprocess,env);
261 jxbt_throw_notbound(env,"process",jprocess);
265 return (jint)MSG_process_get_PID(process);
269 JNIEXPORT jint JNICALL
270 Java_simgrid_msg_Msg_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
271 m_process_t process = jprocess_to_native_process(jprocess,env);
274 jxbt_throw_notbound(env,"process",jprocess);
278 return (jint)MSG_process_get_PPID(process);
281 JNIEXPORT jobject JNICALL
282 Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) {
283 m_process_t process = MSG_process_self();
287 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
291 jprocess = SIMIX_process_get_jprocess(process->simdata->s_process);
294 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
300 JNIEXPORT jint JNICALL
301 Java_simgrid_msg_Msg_processSelfPID(JNIEnv* env, jclass cls) {
302 return (jint)MSG_process_self_PID();
306 JNIEXPORT jint JNICALL
307 Java_simgrid_msg_Msg_processSelfPPID(JNIEnv* env, jclass cls) {
308 return (jint)MSG_process_self_PPID();
311 JNIEXPORT void JNICALL
312 Java_simgrid_msg_Msg_processChangeHost(JNIEnv* env, jclass cls, jobject jprocess, jobject jhost){
313 m_host_t host = jhost_get_native(env,jhost);
314 m_process_t process = jprocess_to_native_process(jprocess,env);
317 jxbt_throw_notbound(env,"process",jprocess);
322 jxbt_throw_notbound(env,"host",jhost);
326 /* try to change the host of the process */
327 if(MSG_OK != MSG_process_change_host(process,host))
328 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
331 JNIEXPORT void JNICALL
332 Java_simgrid_msg_Msg_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
333 if(MSG_OK != MSG_process_sleep((double)seconds))
334 jxbt_throw_native(env,
335 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
339 /***************************************************************************************
340 * The MSG host connected functions implementation. *
341 ***************************************************************************************/
343 JNIEXPORT jobject JNICALL
344 Java_simgrid_msg_Msg_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
345 m_host_t host; /* native host */
346 jobject jhost; /* global reference to the java host instance returned */
348 /* get the C string from the java string*/
349 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
351 /* get the host by name (the hosts are created during the grid resolution) */
352 host = MSG_get_host_by_name(name);
353 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
355 (*env)->ReleaseStringUTFChars(env, jname, name);
357 if(!host) {/* invalid name */
358 jxbt_throw_host_not_found(env,name);
362 if(!host->data) { /* native host not associated yet with java host */
364 /* instanciate a new java host */
365 jhost = jhost_new_instance(env);
368 jxbt_throw_jni(env,"java host instantiation failed");
372 /* get a global reference to the newly created host */
373 jhost = jhost_ref(env,jhost);
376 jxbt_throw_jni(env,"new global ref allocation failed");
380 /* bind the java host and the native host */
381 jhost_bind(jhost,host,env);
383 /* the native host data field is set with the global reference to the
384 * java host returned by this function
386 host->data = (void*)jhost;
389 /* return the global reference to the java host instance */
390 return (jobject)host->data;
393 JNIEXPORT jstring JNICALL
394 Java_simgrid_msg_Msg_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
395 m_host_t host = jhost_get_native(env,jhost);
398 jxbt_throw_notbound(env,"host",jhost);
402 return (*env)->NewStringUTF(env,host->name);
405 JNIEXPORT jint JNICALL
406 Java_simgrid_msg_Msg_hostGetNumber(JNIEnv* env, jclass cls) {
407 return (jint)MSG_get_host_number();
410 JNIEXPORT jobject JNICALL
411 Java_simgrid_msg_Msg_hostSelf(JNIEnv* env, jclass cls) {
414 m_host_t host = MSG_host_self();
417 /* the native host not yet associated with the java host instance */
419 /* instanciate a new java host instance */
420 jhost = jhost_new_instance(env);
423 jxbt_throw_jni(env,"java host instantiation failed");
427 /* get a global reference to the newly created host */
428 jhost = jhost_ref(env,jhost);
431 jxbt_throw_jni(env,"global ref allocation failed");
435 /* Bind & store it */
436 jhost_bind(jhost,host,env);
437 host->data = (void*)jhost;
439 jhost = (jobject)host->data;
445 JNIEXPORT jdouble JNICALL
446 Java_simgrid_msg_Msg_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
447 m_host_t host = jhost_get_native(env,jhost);
450 jxbt_throw_notbound(env,"host",jhost);
454 return (jdouble)MSG_get_host_speed(host);
457 JNIEXPORT jint JNICALL
458 Java_simgrid_msg_Msg_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
459 m_host_t host = jhost_get_native(env,jhost);
462 jxbt_throw_notbound(env,"host",jhost);
466 return (jint)MSG_get_host_msgload(host);
470 JNIEXPORT jboolean JNICALL
471 Java_simgrid_msg_Msg_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
472 m_host_t host = jhost_get_native(env,jhost);
475 jxbt_throw_notbound(env,"host",jhost);
479 return (jboolean)MSG_host_is_avail(host);
483 /***************************************************************************************
484 * The MSG task connected functions implementation. *
485 ***************************************************************************************/
487 JNIEXPORT void JNICALL
488 Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
489 jdouble jcomputeDuration, jdouble jmessageSize) {
490 m_task_t task; /* the native task to create */
491 const char* name; /* the name of the task */
493 if(jcomputeDuration < 0) {
494 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
495 (double)jcomputeDuration));
499 if(jmessageSize < 0) {
500 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
501 (double)jmessageSize));
506 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
510 /* get the C string from the java string*/
511 name = (*env)->GetStringUTFChars(env, jname, 0);
513 /* create the task */
514 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
516 (*env)->ReleaseStringUTFChars(env, jname, name);
518 /* bind & store the task */
519 jtask_bind(jtask,task,env);
521 /* allocate a new global reference to the java task instance */
522 task->data = (void*)jtask_new_global_ref(jtask,env);
525 jxbt_throw_jni(env,"global ref allocation failed");
528 JNIEXPORT void JNICALL
529 Java_simgrid_msg_Msg_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask_arg, jstring jname,
530 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
532 m_task_t task; /* the native parallel task to create */
533 const char* name; /* the name of the task */
534 jobject jtask; /* the global reference to the java parallel task instance */
537 double* computeDurations;
538 double* messageSizes;
539 jdouble *jcomputeDurations;
540 jdouble *jmessageSizes;
546 if (!jcomputeDurations_arg) {
547 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
551 if(!jmessageSizes_arg){
552 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
557 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
561 host_count = (int)(*env)->GetArrayLength(env,jhosts);
564 hosts = (m_host_t*)calloc(host_count,sizeof(m_host_t));
565 computeDurations = (double*)calloc(host_count,sizeof(double));
566 messageSizes = (double*)calloc(host_count,sizeof(double));
568 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
569 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
571 for(index = 0; index < host_count; index++) {
572 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
573 hosts[index] = jhost_get_native(env,jhost);
574 computeDurations[index] = jcomputeDurations[index];
575 messageSizes[index] = jmessageSizes[index];
578 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
579 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
582 /* get the C string from the java string*/
583 name = (*env)->GetStringUTFChars(env, jname, 0);
585 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
587 (*env)->ReleaseStringUTFChars(env, jname, name);
589 /* allocate a new global reference to the java task instance */
590 jtask = jparallel_task_ref(env,jtask_arg);
592 /* associate the java task object and the native task */
593 jparallel_task_bind(jtask,task,env);
595 task->data = (void*)jtask;
598 jxbt_throw_jni(env,"global ref allocation failed");
601 JNIEXPORT jobject JNICALL
602 Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
605 m_task_t task = jtask_to_native_task(jtask,env);
608 jxbt_throw_notbound(env,"task",jtask);
612 process = MSG_task_get_sender(task);
613 return SIMIX_process_get_jprocess(process->simdata->s_process);
616 JNIEXPORT jobject JNICALL
617 Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
621 task = jparallel_task_to_native_parallel_task(jtask,env);
624 jxbt_throw_notbound(env,"task",jtask);
628 process = MSG_task_get_sender(task);
630 return SIMIX_process_get_jprocess(process->simdata->s_process);
633 JNIEXPORT jobject JNICALL
634 Java_simgrid_msg_Msg_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
636 m_task_t task = jtask_to_native_task(jtask,env);
639 jxbt_throw_notbound(env,"task",jtask);
643 host = MSG_task_get_source(task);
646 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
650 return (jobject)host->data;
653 JNIEXPORT jobject JNICALL
654 Java_simgrid_msg_Msg_parallelTaskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
655 m_task_t task = jparallel_task_to_native_parallel_task(jtask,env);
659 jxbt_throw_notbound(env,"task",jtask);
663 host = MSG_task_get_source(task);
666 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
670 return (jobject)host->data;
674 JNIEXPORT jstring JNICALL
675 Java_simgrid_msg_Msg_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
676 m_task_t task = jtask_to_native_task(jtask,env);
679 jxbt_throw_notbound(env,"task",jtask);
683 return (*env)->NewStringUTF(env,task->name);
686 JNIEXPORT jstring JNICALL
687 Java_simgrid_msg_Msg_parallelTaskGetName(JNIEnv* env, jclass cls, jobject jtask) {
688 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
691 jxbt_throw_notbound(env,"task",jtask);
695 return (*env)->NewStringUTF(env,ptask->name);
698 JNIEXPORT void JNICALL
699 Java_simgrid_msg_Msg_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
700 m_task_t ptask = jtask_to_native_task(jtask,env);
703 jxbt_throw_notbound(env,"task",jtask);
707 if(MSG_OK != MSG_task_cancel(ptask))
708 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
711 JNIEXPORT void JNICALL
712 Java_simgrid_msg_Msg_parallelTaskCancel(JNIEnv* env, jclass cls, jobject jtask) {
713 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
716 jxbt_throw_notbound(env,"task",jtask);
720 if(MSG_OK != MSG_task_cancel(ptask))
721 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
725 JNIEXPORT jdouble JNICALL
726 Java_simgrid_msg_Msg_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
727 m_task_t ptask = jtask_to_native_task(jtask,env);
730 jxbt_throw_notbound(env,"task",jtask);
733 return (jdouble)MSG_task_get_compute_duration(ptask);
736 JNIEXPORT jdouble JNICALL
737 Java_simgrid_msg_Msg_parallelTaskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
738 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
741 jxbt_throw_notbound(env,"task",jtask);
744 return (jdouble)MSG_task_get_compute_duration(ptask);
747 JNIEXPORT jdouble JNICALL
748 Java_simgrid_msg_Msg_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
749 m_task_t ptask = jtask_to_native_task(jtask,env);
752 jxbt_throw_notbound(env,"task",jtask);
755 return (jdouble)MSG_task_get_remaining_computation(ptask);
758 JNIEXPORT jdouble JNICALL
759 Java_simgrid_msg_Msg_paralleTaskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
760 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
763 jxbt_throw_notbound(env,"task",jtask);
766 return (jdouble)MSG_task_get_remaining_computation(ptask);
769 JNIEXPORT void JNICALL
770 Java_simgrid_msg_Msg_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
771 m_task_t task = jtask_to_native_task(jtask,env);
774 jxbt_throw_notbound(env,"task",jtask);
777 MSG_task_set_priority(task,(double)priority);
780 JNIEXPORT void JNICALL
781 Java_simgrid_msg_Msg_parallelTaskSetPriority(JNIEnv* env, jclass cls,
782 jobject jtask, jdouble priority) {
783 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
786 jxbt_throw_notbound(env,"task",jtask);
789 MSG_task_set_priority(ptask,(double)priority);
793 JNIEXPORT void JNICALL
794 Java_simgrid_msg_Msg_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
796 /* get the native task */
797 m_task_t task = jtask_to_native_task(jtask_arg,env);
801 jxbt_throw_notbound(env,"task",jtask);
804 jtask = (jobject)task->data;
806 if(MSG_OK != MSG_task_destroy(task))
807 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
809 /* delete the global reference to the java task object */
810 jtask_delete_global_ref(jtask,env);
813 JNIEXPORT void JNICALL
814 Java_simgrid_msg_Msg_parallelTaskDestroy(JNIEnv* env, jclass cls, jobject jtask) {
815 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
818 jxbt_throw_notbound(env,"task",jtask);
822 /* unbind jobj & native one */
823 jparallel_task_bind(jtask,0,env);
825 /* delete the global reference to the java parallel task object */
826 jparallel_task_unref(env,jtask);
828 /* free allocated memory */
829 if(ptask->simdata->comm_amount)
830 free(ptask->simdata->comm_amount);
832 if(ptask->simdata->host_list)
833 free(ptask->simdata->host_list);
835 if(MSG_OK != MSG_task_destroy(ptask))
836 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
839 JNIEXPORT void JNICALL
840 Java_simgrid_msg_Msg_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
841 m_task_t task = jtask_to_native_task(jtask,env);
844 jxbt_throw_notbound(env,"task",jtask);
848 if(MSG_OK != MSG_task_execute(task))
849 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
852 JNIEXPORT void JNICALL
853 Java_simgrid_msg_Msg_parallelTaskExecute(JNIEnv* env, jclass cls, jobject jtask) {
854 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
857 jxbt_throw_notbound(env,"task",jtask);
861 if(MSG_OK != MSG_parallel_task_execute(ptask))
862 jxbt_throw_native(env, xbt_strdup("MSG_parallel_task_execute() failed"));
865 /***************************************************************************************
866 * The MSG channel connected functions implementation. *
867 ***************************************************************************************/
869 JNIEXPORT jobject JNICALL
870 Java_simgrid_msg_Msg_channelGet(JNIEnv* env, jclass cls, jobject jchannel) {
871 m_task_t task = NULL;
873 if(MSG_OK != MSG_task_get(&task,(int)jchannel_get_id(jchannel,env))) {
874 jxbt_throw_native(env, xbt_strdup("MSG_task_get() failed"));
878 return (jobject)task->data;
881 JNIEXPORT jobject JNICALL
882 Java_simgrid_msg_Msg_channelGetWithTimeout(JNIEnv* env, jclass cls,
883 jobject jchannel, jdouble jtimeout) {
884 m_task_t task = NULL;
885 int id = (int)jchannel_get_id(jchannel,env);
887 if(MSG_OK != MSG_task_get_with_time_out(&task,id,(double)jtimeout)) {
888 jxbt_throw_native(env, xbt_strdup("MSG_task_get_with_time_out() failed"));
892 return (jobject)task->data;
895 JNIEXPORT jobject JNICALL
896 Java_simgrid_msg_Msg_channelGetFromHost(JNIEnv* env, jclass cls,
897 jobject jchannel, jobject jhost) {
898 m_host_t host = jhost_get_native(env,jhost);
899 m_task_t task = NULL;
900 int id = (int)jchannel_get_id(jchannel,env);
903 jxbt_throw_notbound(env,"host",jhost);
906 if(MSG_OK != MSG_task_get_from_host(&task,id,host)){
907 jxbt_throw_native(env, xbt_strdup("MSG_task_get_from_host() failed"));
911 jxbt_throw_notbound(env,"task",task);
915 return (jobject)task->data;
918 JNIEXPORT jboolean JNICALL
919 Java_simgrid_msg_Msg_channelHasPendingCommunication(JNIEnv* env, jclass cls, jobject jchannel) {
920 int id = (int)jchannel_get_id(jchannel,env);
922 return (jboolean)MSG_task_Iprobe(id);
925 JNIEXPORT jint JNICALL
926 Java_simgrid_msg_Msg_channelGetCommunicatingProcess(JNIEnv* env, jclass cls, jobject jchannel) {
927 int id =jchannel_get_id(jchannel,env);
929 return (jint)MSG_task_probe_from(id);
932 JNIEXPORT jint JNICALL
933 Java_simgrid_msg_Msg_channelGetHostWaitingTasks(JNIEnv* env, jclass cls,
934 jobject jchannel, jobject jhost) {
935 int id = (int)jchannel_get_id(jchannel,env);
936 m_host_t host = jhost_get_native(env,jhost);
939 jxbt_throw_notbound(env,"host",jhost);
943 return (jint)MSG_task_probe_from_host(id,host);
946 JNIEXPORT void JNICALL
947 Java_simgrid_msg_Msg_channelPut(JNIEnv* env, jclass cls,
948 jobject jchannel, jobject jtask, jobject jhost) {
950 if(MSG_OK != MSG_task_put(jtask_to_native_task(jtask,env),
951 jhost_get_native(env,jhost),
952 (int)jchannel_get_id(jchannel,env)))
953 jxbt_throw_native(env, xbt_strdup("MSG_task_put() failed"));
956 JNIEXPORT void JNICALL
957 Java_simgrid_msg_Msg_channelPutWithTimeout(JNIEnv* env, jclass cls,
958 jobject jchannel, jobject jtask, jobject jhost,
960 m_task_t task = jtask_to_native_task(jtask,env);
961 int id = (int)jchannel_get_id(jchannel,env);
962 m_host_t host = jhost_get_native(env,jhost);
965 jxbt_throw_notbound(env,"host",jhost);
969 jxbt_throw_notbound(env,"task",jtask);
973 if(MSG_OK != MSG_task_put_with_timeout(task,host,id,(double)jtimeout))
974 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
977 JNIEXPORT void JNICALL
978 Java_simgrid_msg_Msg_channelPutBounded(JNIEnv* env, jclass cls,
979 jobject jchannel, jobject jtask, jobject jhost,
981 m_task_t task = jtask_to_native_task(jtask,env);
982 int id = (int)jchannel_get_id(jchannel,env);
983 m_host_t host = jhost_get_native(env,jhost);
986 jxbt_throw_notbound(env,"host",jhost);
990 jxbt_throw_notbound(env,"task",jtask);
994 if(MSG_OK != MSG_task_put_bounded(task,host,id,(double)jmaxRate))
995 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
998 JNIEXPORT jint JNICALL
999 Java_simgrid_msg_Msg_channelWait(JNIEnv* env, jclass cls, jobject jchannel, jdouble timeout) {
1001 int id = (int)jchannel_get_id(jchannel,env);
1003 if(MSG_OK != MSG_channel_select_from(id,(double)timeout,&PID)) {
1004 jxbt_throw_native(env, xbt_strdup("MSG_channel_select_from() failed"));
1011 JNIEXPORT jint JNICALL
1012 Java_simgrid_msg_Msg_channelGetNumber(JNIEnv* env, jclass cls) {
1013 return (jint)MSG_get_channel_number();
1016 JNIEXPORT void JNICALL
1017 Java_simgrid_msg_Msg_channelSetNumber(JNIEnv* env , jclass cls,jint channelNumber) {
1018 MSG_set_channel_number((int)channelNumber);
1021 JNIEXPORT jint JNICALL
1022 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
1023 return (jint)MSG_get_errno();
1026 JNIEXPORT jdouble JNICALL
1027 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
1028 return (jdouble)MSG_get_clock();
1032 JNIEXPORT void JNICALL
1033 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
1042 argc = (int)(*env)->GetArrayLength(env,jargs);
1046 argv = (char**)calloc(argc,sizeof(char*));
1048 argv[0] = strdup("java");
1050 for(index = 0; index < argc -1; index++) {
1051 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
1053 tmp = (*env)->GetStringUTFChars(env, jval, 0);
1055 argv[index +1] = strdup(tmp);
1057 (*env)->ReleaseStringUTFChars(env, jval, tmp);
1060 MSG_global_init(&argc,argv);
1062 for(index = 0; index < argc; index++)
1068 __current_thread_id = GetCurrentThreadId();
1070 __current_thread_id = pthread_self();
1075 JNIEXPORT void JNICALL
1076 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
1077 xbt_fifo_item_t item = NULL;
1078 m_host_t host = NULL;
1081 /* Run everything */
1082 if(MSG_OK != MSG_main())
1083 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1085 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
1086 SIMIX_display_process_status();
1087 exit(0); /* FIXME */
1089 DEBUG0("Clean java world");
1090 /* Cleanup java hosts */
1091 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
1092 jhost = (jobject)host->data;
1095 jhost_unref(env,jhost);
1097 DEBUG0("Clean native world");
1098 /* cleanup native stuff */
1099 if(MSG_OK != MSG_clean())
1100 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1104 JNIEXPORT jint JNICALL
1105 Java_simgrid_msg_Msg_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
1106 return (jint)MSG_process_killall((int)jresetPID);
1109 JNIEXPORT void JNICALL
1110 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
1112 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1114 MSG_create_environment(platformFile);
1116 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1119 JNIEXPORT void JNICALL
1120 Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) {
1121 m_process_t m_process = jprocess_to_native_process(jprocess,env);
1122 smx_process_t s_process;
1124 xbt_os_mutex_t ctx_mutex, creation_mutex;
1125 xbt_os_cond_t ctx_cond, creation_cond;
1127 DEBUG3("Msg_waitSignal(m_process=%p %s/%s)",
1128 m_process,m_process->name,m_process->simdata->m_host->name);
1130 jxbt_throw_notbound(env,"process",jprocess);
1134 s_process = m_process->simdata->s_process;
1136 if (s_process == NULL) {
1137 jxbt_throw_notbound(env,"SIMIX process",jprocess);
1141 ctx_mutex = SIMIX_process_get_jmutex(s_process);
1142 ctx_cond = SIMIX_process_get_jcond(s_process);
1144 creation_mutex = xbt_creation_mutex_get();
1145 creation_cond = xbt_creation_cond_get();
1147 xbt_os_mutex_lock(creation_mutex);
1148 xbt_os_mutex_lock(ctx_mutex);
1149 xbt_os_cond_signal( creation_cond );
1150 xbt_os_mutex_unlock( creation_mutex );
1151 xbt_os_cond_wait(ctx_cond, ctx_mutex);
1152 xbt_os_mutex_unlock(ctx_mutex);
1155 JNIEXPORT void JNICALL
1156 Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
1157 m_process_t process = jprocess_to_native_process(jprocess,env);
1160 jxbt_throw_notbound(env,"process",jprocess);
1163 MSG_process_kill(process);
1166 JNIEXPORT void JNICALL
1167 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
1168 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1170 MSG_paje_output(pajeFile);
1172 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1176 JNIEXPORT void JNICALL
1177 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
1178 const char* s = (*env)->GetStringUTFChars(env,js,0);
1180 (*env)->ReleaseStringUTFChars(env, js, s);
1183 JNIEXPORT jobjectArray JNICALL
1184 Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) {
1186 jobjectArray jtable;
1191 int count = xbt_fifo_size(msg_global->host);
1192 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
1194 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
1200 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
1203 jxbt_throw_jni(env,"Hosts table allocation failed");
1207 for(index = 0; index < count; index++) {
1208 host = table[index];
1209 jhost = (jobject)(host->data);
1212 jname = (*env)->NewStringUTF(env,host->name);
1214 jhost = Java_simgrid_msg_Msg_hostGetByName(env,cls_arg,jname);
1215 /* FIXME: leak of jname ? */
1218 (*env)->SetObjectArrayElement(env, jtable, index, jhost);