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 "simix/private.h"
15 #include "java/jxbt_context.h"
17 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_parallel_task.h"
21 #include "jmsg_channel.h"
22 #include "jxbt_utilities.h"
24 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
26 /* header for windows */
35 static JavaVM * __java_vm = NULL;
39 static DWORD __current_thread_id = 0;
41 int is_main_thread() {
42 return (GetCurrentThreadId() == __current_thread_id);
47 static pthread_t __current_thread_id = 0;
49 int is_main_thread() {
50 return (pthread_self() == __current_thread_id);
60 get_current_thread_env(void) {
63 (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL);
70 * The MSG process connected functions implementation.
73 JNIEXPORT void JNICALL
74 Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg, jobject jhost) {
75 jobject jprocess; /* the global reference to the java process instance */
76 jstring jname; /* the name of the java process instance */
77 const char* name; /* the C name of the process */
78 m_process_t process; /* the native process to create */
81 DEBUG4("Java_simgrid_msg_Msg_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
82 env,cls,jprocess_arg,jhost);
83 /* get the name of the java process */
84 jname = jprocess_get_name(jprocess_arg,env);
87 jxbt_throw_null(env,xbt_strdup("Internal error: Process name cannot be NULL"));
91 /* allocate the data of the simulation */
92 process = xbt_new0(s_m_process_t,1);
93 process->simdata = xbt_new0(s_simdata_process_t,1);
95 /* create a global java process instance */
96 jprocess = jprocess_new_global_ref(jprocess_arg,env);
99 free(process->simdata);
101 jxbt_throw_jni(env, "Can't get a global ref to the java process");
105 /* bind the java process instance to the native process */
106 jprocess_bind(jprocess,process,env);
108 /* build the C name of the process */
109 name = (*env)->GetStringUTFChars(env, jname, 0);
110 process->name = xbt_strdup(name);
111 (*env)->ReleaseStringUTFChars(env, jname, name);
113 process->simdata->m_host = jhost_get_native(env,jhost);
114 if( ! (process->simdata->m_host) ) { /* not binded */
115 free(process->simdata);
118 jxbt_throw_notbound(env,"host",jhost);
121 process->simdata->PID = msg_global->PID++;
123 /* create a new context */
124 DEBUG8("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
125 process->name,process->simdata->m_host->name,
126 process->simdata->PID,process,
127 process->simdata, process->simdata->m_host, process->simdata->m_host->simdata,
130 SIMIX_jprocess_create(process->name,
131 process->simdata->m_host->simdata->smx_host,
132 /*data*/ (void*)process,
134 &process->simdata->s_process);
135 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
138 if (SIMIX_process_self()) { /* someone created me */
139 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
141 process->simdata->PPID = -1;
144 process->simdata->last_errno = MSG_OK;
146 /* add the process to the list of the processes of the simulation */
147 xbt_fifo_unshift(msg_global->process_list, process);
151 JNIEXPORT void JNICALL
152 Java_simgrid_msg_Msg_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
153 m_process_t process = jprocess_to_native_process(jprocess,env);
156 jxbt_throw_notbound(env,"process",jprocess);
160 /* try to suspend the process */
161 if(MSG_OK != MSG_process_suspend(process))
162 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
165 JNIEXPORT void JNICALL
166 Java_simgrid_msg_Msg_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
167 m_process_t process = jprocess_to_native_process(jprocess,env);
170 jxbt_throw_notbound(env,"process",jprocess);
174 /* try to resume the process */
175 if(MSG_OK != MSG_process_resume(process))
176 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
179 JNIEXPORT jboolean JNICALL
180 Java_simgrid_msg_Msg_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
181 m_process_t process = jprocess_to_native_process(jprocess,env);
184 jxbt_throw_notbound(env,"process",jprocess);
188 /* true is the process is suspended, false otherwise */
189 return (jboolean)MSG_process_is_suspended(process);
192 JNIEXPORT void JNICALL
193 Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
194 /* get the native instances from the java ones */
195 m_process_t process = jprocess_to_native_process(jprocess,env);
198 jxbt_throw_notbound(env,"process",jprocess);
202 /* delete the global reference */
203 jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env);
205 /* kill the native process (this wrapper is call by the destructor of the java
208 MSG_process_kill(process);
211 JNIEXPORT jobject JNICALL
212 Java_simgrid_msg_Msg_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
213 /* get the native instances from the java ones */
214 m_process_t process = jprocess_to_native_process(jprocess,env);
218 jxbt_throw_notbound(env,"process",jprocess);
222 host = MSG_process_get_host(process);
225 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
229 /* return the global reference to the java host instance */
230 return (jobject)host->data;
234 JNIEXPORT jobject JNICALL
235 Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) {
236 m_process_t process = MSG_process_from_PID(PID);
239 jxbt_throw_native(env, bprintf("MSG_process_from_PID(%d) failed",PID));
243 if(!SIMIX_process_get_jprocess(process->simdata->s_process)) {
244 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
248 return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process);
252 JNIEXPORT jint JNICALL
253 Java_simgrid_msg_Msg_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
254 m_process_t process = jprocess_to_native_process(jprocess,env);
257 jxbt_throw_notbound(env,"process",jprocess);
261 return (jint)MSG_process_get_PID(process);
265 JNIEXPORT jint JNICALL
266 Java_simgrid_msg_Msg_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
267 m_process_t process = jprocess_to_native_process(jprocess,env);
270 jxbt_throw_notbound(env,"process",jprocess);
274 return (jint)MSG_process_get_PPID(process);
277 JNIEXPORT jobject JNICALL
278 Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) {
279 m_process_t process = MSG_process_self();
283 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
287 jprocess = SIMIX_process_get_jprocess(process->simdata->s_process);
290 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
296 JNIEXPORT jint JNICALL
297 Java_simgrid_msg_Msg_processSelfPID(JNIEnv* env, jclass cls) {
298 return (jint)MSG_process_self_PID();
302 JNIEXPORT jint JNICALL
303 Java_simgrid_msg_Msg_processSelfPPID(JNIEnv* env, jclass cls) {
304 return (jint)MSG_process_self_PPID();
307 JNIEXPORT void JNICALL
308 Java_simgrid_msg_Msg_processChangeHost(JNIEnv* env, jclass cls, jobject jprocess, jobject jhost){
309 m_host_t host = jhost_get_native(env,jhost);
310 m_process_t process = jprocess_to_native_process(jprocess,env);
313 jxbt_throw_notbound(env,"process",jprocess);
318 jxbt_throw_notbound(env,"host",jhost);
322 /* try to change the host of the process */
323 if(MSG_OK != MSG_process_change_host(process,host))
324 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
327 JNIEXPORT void JNICALL
328 Java_simgrid_msg_Msg_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
329 if(MSG_OK != MSG_process_sleep((double)seconds))
330 jxbt_throw_native(env,
331 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
335 /***************************************************************************************
336 * The MSG host connected functions implementation. *
337 ***************************************************************************************/
339 JNIEXPORT jobject JNICALL
340 Java_simgrid_msg_Msg_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
341 m_host_t host; /* native host */
342 jobject jhost; /* global reference to the java host instance returned */
344 /* get the C string from the java string*/
345 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
347 /* get the host by name (the hosts are created during the grid resolution) */
348 host = MSG_get_host_by_name(name);
349 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
351 (*env)->ReleaseStringUTFChars(env, jname, name);
353 if(!host) {/* invalid name */
354 jxbt_throw_host_not_found(env,name);
358 if(!host->data) { /* native host not associated yet with java host */
360 /* instanciate a new java host */
361 jhost = jhost_new_instance(env);
364 jxbt_throw_jni(env,"java host instantiation failed");
368 /* get a global reference to the newly created host */
369 jhost = jhost_ref(env,jhost);
372 jxbt_throw_jni(env,"new global ref allocation failed");
376 /* bind the java host and the native host */
377 jhost_bind(jhost,host,env);
379 /* the native host data field is set with the global reference to the
380 * java host returned by this function
382 host->data = (void*)jhost;
385 /* return the global reference to the java host instance */
386 return (jobject)host->data;
389 JNIEXPORT jstring JNICALL
390 Java_simgrid_msg_Msg_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
391 m_host_t host = jhost_get_native(env,jhost);
394 jxbt_throw_notbound(env,"host",jhost);
398 return (*env)->NewStringUTF(env,host->name);
401 JNIEXPORT jint JNICALL
402 Java_simgrid_msg_Msg_hostGetNumber(JNIEnv* env, jclass cls) {
403 return (jint)MSG_get_host_number();
406 JNIEXPORT jobject JNICALL
407 Java_simgrid_msg_Msg_hostSelf(JNIEnv* env, jclass cls) {
410 m_host_t host = MSG_host_self();
413 /* the native host not yet associated with the java host instance */
415 /* instanciate a new java host instance */
416 jhost = jhost_new_instance(env);
419 jxbt_throw_jni(env,"java host instantiation failed");
423 /* get a global reference to the newly created host */
424 jhost = jhost_ref(env,jhost);
427 jxbt_throw_jni(env,"global ref allocation failed");
431 /* Bind & store it */
432 jhost_bind(jhost,host,env);
433 host->data = (void*)jhost;
435 jhost = (jobject)host->data;
441 JNIEXPORT jdouble JNICALL
442 Java_simgrid_msg_Msg_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
443 m_host_t host = jhost_get_native(env,jhost);
446 jxbt_throw_notbound(env,"host",jhost);
450 return (jdouble)MSG_get_host_speed(host);
453 JNIEXPORT jint JNICALL
454 Java_simgrid_msg_Msg_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
455 m_host_t host = jhost_get_native(env,jhost);
458 jxbt_throw_notbound(env,"host",jhost);
462 return (jint)MSG_get_host_msgload(host);
466 JNIEXPORT jboolean JNICALL
467 Java_simgrid_msg_Msg_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
468 m_host_t host = jhost_get_native(env,jhost);
471 jxbt_throw_notbound(env,"host",jhost);
475 return (jboolean)MSG_host_is_avail(host);
479 /***************************************************************************************
480 * The MSG task connected functions implementation. *
481 ***************************************************************************************/
483 JNIEXPORT void JNICALL
484 Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
485 jdouble jcomputeDuration, jdouble jmessageSize) {
486 m_task_t task; /* the native task to create */
487 const char* name; /* the name of the task */
489 if(jcomputeDuration < 0) {
490 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
491 (double)jcomputeDuration));
495 if(jmessageSize < 0) {
496 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
497 (double)jmessageSize));
502 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
506 /* get the C string from the java string*/
507 name = (*env)->GetStringUTFChars(env, jname, 0);
509 /* create the task */
510 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
512 (*env)->ReleaseStringUTFChars(env, jname, name);
514 /* bind & store the task */
515 jtask_bind(jtask,task,env);
517 /* allocate a new global reference to the java task instance */
518 task->data = (void*)jtask_new_global_ref(jtask,env);
521 jxbt_throw_jni(env,"global ref allocation failed");
525 JNIEXPORT void JNICALL
526 Java_simgrid_msg_Msg_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask_arg, jstring jname,
527 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
529 m_task_t task; /* the native parallel task to create */
530 const char* name; /* the name of the task */
531 jobject jtask; /* the global reference to the java parallel task instance */
534 double* computeDurations;
535 double* messageSizes;
536 jdouble *jcomputeDurations;
537 jdouble *jmessageSizes;
543 if (!jcomputeDurations_arg) {
544 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
548 if(!jmessageSizes_arg){
549 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
554 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
558 host_count = (int)(*env)->GetArrayLength(env,jhosts);
561 hosts = (m_host_t*)calloc(host_count,sizeof(m_host_t));
562 computeDurations = (double*)calloc(host_count,sizeof(double));
563 messageSizes = (double*)calloc(host_count,sizeof(double));
565 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
566 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
568 for(index = 0; index < host_count; index++) {
569 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
570 hosts[index] = jhost_get_native(env,jhost);
571 computeDurations[index] = jcomputeDurations[index];
572 messageSizes[index] = jmessageSizes[index];
575 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
576 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
579 /* get the C string from the java string*/
580 name = (*env)->GetStringUTFChars(env, jname, 0);
582 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
584 (*env)->ReleaseStringUTFChars(env, jname, name);
586 /* allocate a new global reference to the java task instance */
587 jtask = jparallel_task_ref(env,jtask_arg);
589 /* associate the java task object and the native task */
590 jparallel_task_bind(jtask,task,env);
592 task->data = (void*)jtask;
595 jxbt_throw_jni(env,"global ref allocation failed");
598 JNIEXPORT jobject JNICALL
599 Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
602 m_task_t task = jtask_to_native_task(jtask,env);
605 jxbt_throw_notbound(env,"task",jtask);
609 process = MSG_task_get_sender(task);
610 return SIMIX_process_get_jprocess(process->simdata->s_process);
613 JNIEXPORT jobject JNICALL
614 Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
618 task = jparallel_task_to_native_parallel_task(jtask,env);
621 jxbt_throw_notbound(env,"task",jtask);
625 process = MSG_task_get_sender(task);
627 return SIMIX_process_get_jprocess(process->simdata->s_process);
630 JNIEXPORT jobject JNICALL
631 Java_simgrid_msg_Msg_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
633 m_task_t task = jtask_to_native_task(jtask,env);
636 jxbt_throw_notbound(env,"task",jtask);
640 host = MSG_task_get_source(task);
643 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
647 return (jobject)host->data;
650 JNIEXPORT jobject JNICALL
651 Java_simgrid_msg_Msg_parallelTaskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
652 m_task_t task = jparallel_task_to_native_parallel_task(jtask,env);
656 jxbt_throw_notbound(env,"task",jtask);
660 host = MSG_task_get_source(task);
663 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
667 return (jobject)host->data;
671 JNIEXPORT jstring JNICALL
672 Java_simgrid_msg_Msg_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
673 m_task_t task = jtask_to_native_task(jtask,env);
676 jxbt_throw_notbound(env,"task",jtask);
680 return (*env)->NewStringUTF(env,task->name);
683 JNIEXPORT jstring JNICALL
684 Java_simgrid_msg_Msg_parallelTaskGetName(JNIEnv* env, jclass cls, jobject jtask) {
685 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
688 jxbt_throw_notbound(env,"task",jtask);
692 return (*env)->NewStringUTF(env,ptask->name);
695 JNIEXPORT void JNICALL
696 Java_simgrid_msg_Msg_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
697 m_task_t ptask = jtask_to_native_task(jtask,env);
700 jxbt_throw_notbound(env,"task",jtask);
704 if(MSG_OK != MSG_task_cancel(ptask))
705 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
708 JNIEXPORT void JNICALL
709 Java_simgrid_msg_Msg_parallelTaskCancel(JNIEnv* env, jclass cls, jobject jtask) {
710 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
713 jxbt_throw_notbound(env,"task",jtask);
717 if(MSG_OK != MSG_task_cancel(ptask))
718 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
722 JNIEXPORT jdouble JNICALL
723 Java_simgrid_msg_Msg_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
724 m_task_t ptask = jtask_to_native_task(jtask,env);
727 jxbt_throw_notbound(env,"task",jtask);
730 return (jdouble)MSG_task_get_compute_duration(ptask);
733 JNIEXPORT jdouble JNICALL
734 Java_simgrid_msg_Msg_parallelTaskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
735 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
738 jxbt_throw_notbound(env,"task",jtask);
741 return (jdouble)MSG_task_get_compute_duration(ptask);
744 JNIEXPORT jdouble JNICALL
745 Java_simgrid_msg_Msg_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
746 m_task_t ptask = jtask_to_native_task(jtask,env);
749 jxbt_throw_notbound(env,"task",jtask);
752 return (jdouble)MSG_task_get_remaining_computation(ptask);
755 JNIEXPORT jdouble JNICALL
756 Java_simgrid_msg_Msg_paralleTaskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
757 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
760 jxbt_throw_notbound(env,"task",jtask);
763 return (jdouble)MSG_task_get_remaining_computation(ptask);
766 JNIEXPORT void JNICALL
767 Java_simgrid_msg_Msg_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
768 m_task_t task = jtask_to_native_task(jtask,env);
771 jxbt_throw_notbound(env,"task",jtask);
774 MSG_task_set_priority(task,(double)priority);
777 JNIEXPORT void JNICALL
778 Java_simgrid_msg_Msg_parallelTaskSetPriority(JNIEnv* env, jclass cls,
779 jobject jtask, jdouble priority) {
780 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
783 jxbt_throw_notbound(env,"task",jtask);
786 MSG_task_set_priority(ptask,(double)priority);
790 JNIEXPORT void JNICALL
791 Java_simgrid_msg_Msg_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
793 /* get the native task */
794 m_task_t task = jtask_to_native_task(jtask_arg,env);
798 jxbt_throw_notbound(env,"task",task);
801 jtask = (jobject)task->data;
803 if(MSG_OK != MSG_task_destroy(task))
804 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
806 /* delete the global reference to the java task object */
807 jtask_delete_global_ref(jtask,env);
810 JNIEXPORT void JNICALL
811 Java_simgrid_msg_Msg_parallelTaskDestroy(JNIEnv* env, jclass cls, jobject jtask) {
812 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
815 jxbt_throw_notbound(env,"task",jtask);
819 /* unbind jobj & native one */
820 jparallel_task_bind(jtask,0,env);
822 /* delete the global reference to the java parallel task object */
823 jparallel_task_unref(env,jtask);
825 /* free allocated memory */
826 if(ptask->simdata->comm_amount)
827 free(ptask->simdata->comm_amount);
829 if(ptask->simdata->host_list)
830 free(ptask->simdata->host_list);
832 if(MSG_OK != MSG_task_destroy(ptask))
833 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
836 JNIEXPORT void JNICALL
837 Java_simgrid_msg_Msg_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
838 m_task_t task = jtask_to_native_task(jtask,env);
841 jxbt_throw_notbound(env,"task",jtask);
845 if(MSG_OK != MSG_task_execute(task))
846 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
849 JNIEXPORT void JNICALL
850 Java_simgrid_msg_Msg_parallelTaskExecute(JNIEnv* env, jclass cls, jobject jtask) {
851 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
854 jxbt_throw_notbound(env,"task",jtask);
858 if(MSG_OK != MSG_parallel_task_execute(ptask))
859 jxbt_throw_native(env, xbt_strdup("MSG_parallel_task_execute() failed"));
862 /***************************************************************************************
863 * The MSG channel connected functions implementation. *
864 ***************************************************************************************/
866 JNIEXPORT jobject JNICALL
867 Java_simgrid_msg_Msg_channelGet(JNIEnv* env, jclass cls, jobject jchannel) {
868 m_task_t task = NULL;
870 if(MSG_OK != MSG_task_get(&task,(int)jchannel_get_id(jchannel,env))) {
871 jxbt_throw_native(env, xbt_strdup("MSG_task_get() failed"));
875 return (jobject)task->data;
878 JNIEXPORT jobject JNICALL
879 Java_simgrid_msg_Msg_channelGetWithTimeout(JNIEnv* env, jclass cls,
880 jobject jchannel, jdouble jtimeout) {
881 m_task_t task = NULL;
882 int id = (int)jchannel_get_id(jchannel,env);
884 if(MSG_OK != MSG_task_get_with_time_out(&task,id,(double)jtimeout)) {
885 jxbt_throw_native(env, xbt_strdup("MSG_task_get_with_time_out() failed"));
889 return (jobject)task->data;
892 JNIEXPORT jobject JNICALL
893 Java_simgrid_msg_Msg_channelGetFromHost(JNIEnv* env, jclass cls,
894 jobject jchannel, jobject jhost) {
895 m_host_t host = jhost_get_native(env,jhost);
896 m_task_t task = NULL;
897 int id = (int)jchannel_get_id(jchannel,env);
900 jxbt_throw_notbound(env,"host",jhost);
903 if(MSG_OK != MSG_task_get_from_host(&task,id,host)){
904 jxbt_throw_native(env, xbt_strdup("MSG_task_get_from_host() failed"));
908 jxbt_throw_notbound(env,"task",task);
912 return (jobject)task->data;
915 JNIEXPORT jboolean JNICALL
916 Java_simgrid_msg_Msg_channelHasPendingCommunication(JNIEnv* env, jclass cls, jobject jchannel) {
917 int id = (int)jchannel_get_id(jchannel,env);
919 return (jboolean)MSG_task_Iprobe(id);
922 JNIEXPORT jint JNICALL
923 Java_simgrid_msg_Msg_channelGetCommunicatingProcess(JNIEnv* env, jclass cls, jobject jchannel) {
924 int id =jchannel_get_id(jchannel,env);
926 return (jint)MSG_task_probe_from(id);
929 JNIEXPORT jint JNICALL
930 Java_simgrid_msg_Msg_channelGetHostWaitingTasks(JNIEnv* env, jclass cls,
931 jobject jchannel, jobject jhost) {
932 int id = (int)jchannel_get_id(jchannel,env);
933 m_host_t host = jhost_get_native(env,jhost);
936 jxbt_throw_notbound(env,"host",jhost);
940 return (jint)MSG_task_probe_from_host(id,host);
943 JNIEXPORT void JNICALL
944 Java_simgrid_msg_Msg_channelPut(JNIEnv* env, jclass cls,
945 jobject jchannel, jobject jtask, jobject jhost) {
947 if(MSG_OK != MSG_task_put(jtask_to_native_task(jtask,env),
948 jhost_get_native(env,jhost),
949 (int)jchannel_get_id(jchannel,env)))
950 jxbt_throw_native(env, xbt_strdup("MSG_task_put() failed"));
953 JNIEXPORT void JNICALL
954 Java_simgrid_msg_Msg_channelPutWithTimeout(JNIEnv* env, jclass cls,
955 jobject jchannel, jobject jtask, jobject jhost,
957 m_task_t task = jtask_to_native_task(jtask,env);
958 int id = (int)jchannel_get_id(jchannel,env);
959 m_host_t host = jhost_get_native(env,jhost);
962 jxbt_throw_notbound(env,"host",jhost);
966 jxbt_throw_notbound(env,"task",jtask);
970 if(MSG_OK != MSG_task_put_with_timeout(task,host,id,(double)jtimeout))
971 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
974 JNIEXPORT void JNICALL
975 Java_simgrid_msg_Msg_channelPutBounded(JNIEnv* env, jclass cls,
976 jobject jchannel, jobject jtask, jobject jhost,
978 m_task_t task = jtask_to_native_task(jtask,env);
979 int id = (int)jchannel_get_id(jchannel,env);
980 m_host_t host = jhost_get_native(env,jhost);
983 jxbt_throw_notbound(env,"host",jhost);
987 jxbt_throw_notbound(env,"task",jtask);
991 if(MSG_OK != MSG_task_put_bounded(task,host,id,(double)jmaxRate))
992 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
995 JNIEXPORT jint JNICALL
996 Java_simgrid_msg_Msg_channelWait(JNIEnv* env, jclass cls, jobject jchannel, jdouble timeout) {
998 int id = (int)jchannel_get_id(jchannel,env);
1000 if(MSG_OK != MSG_channel_select_from(id,(double)timeout,&PID)) {
1001 jxbt_throw_native(env, xbt_strdup("MSG_channel_select_from() failed"));
1008 JNIEXPORT jint JNICALL
1009 Java_simgrid_msg_Msg_channelGetNumber(JNIEnv* env, jclass cls) {
1010 return (jint)MSG_get_channel_number();
1013 JNIEXPORT void JNICALL
1014 Java_simgrid_msg_Msg_channelSetNumber(JNIEnv* env , jclass cls,jint channelNumber) {
1015 MSG_set_channel_number((int)channelNumber);
1018 JNIEXPORT jint JNICALL
1019 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
1020 return (jint)MSG_get_errno();
1023 JNIEXPORT jdouble JNICALL
1024 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
1025 return (jdouble)MSG_get_clock();
1029 JNIEXPORT void JNICALL
1030 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
1039 argc = (int)(*env)->GetArrayLength(env,jargs);
1043 argv = (char**)calloc(argc,sizeof(char*));
1045 argv[0] = strdup("java");
1047 for(index = 0; index < argc -1; index++) {
1048 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
1050 tmp = (*env)->GetStringUTFChars(env, jval, 0);
1052 argv[index +1] = strdup(tmp);
1054 (*env)->ReleaseStringUTFChars(env, jval, tmp);
1057 MSG_global_init(&argc,argv);
1059 for(index = 0; index < argc; index++)
1065 __current_thread_id = GetCurrentThreadId();
1067 __current_thread_id = pthread_self();
1070 (*env)->GetJavaVM(env,&__java_vm);
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.");
1086 DEBUG0("Clean java world");
1087 /* Cleanup java hosts */
1088 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
1089 jhost = (jobject)host->data;
1092 jhost_unref(env,jhost);
1095 DEBUG0("Clean native world");
1096 /* cleanup native stuff */
1097 if(MSG_OK != MSG_clean())
1098 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1102 JNIEXPORT jint JNICALL
1103 Java_simgrid_msg_Msg_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
1104 return (jint)MSG_process_killall((int)jresetPID);
1107 JNIEXPORT void JNICALL
1108 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
1110 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1112 MSG_create_environment(platformFile);
1114 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1117 JNIEXPORT void JNICALL
1118 Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) {
1122 JNIEXPORT void JNICALL
1123 Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
1125 m_process_t process = jprocess_to_native_process(jprocess,env);
1128 jxbt_throw_notbound(env,"process",jprocess);
1132 jcontext_exit(process->simdata->s_process->simdata->context,0,get_current_thread_env());
1135 JNIEXPORT void JNICALL
1136 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
1137 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1139 MSG_paje_output(pajeFile);
1141 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1145 JNIEXPORT void JNICALL
1146 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
1147 const char* s = (*env)->GetStringUTFChars(env,js,0);
1149 (*env)->ReleaseStringUTFChars(env, js, s);
1152 JNIEXPORT jobjectArray JNICALL
1153 Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) {
1155 jobjectArray jtable;
1160 int count = xbt_fifo_size(msg_global->host);
1161 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
1163 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
1169 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
1172 jxbt_throw_jni(env,"Hosts table allocation failed");
1176 for(index = 0; index < count; index++) {
1177 host = table[index];
1178 jhost = (jobject)(host->data);
1181 jname = (*env)->NewStringUTF(env,host->name);
1183 jhost = Java_simgrid_msg_Msg_hostGetByName(env,cls_arg,jname);
1184 /* FIXME: leak of jname ? */
1187 (*env)->SetObjectArrayElement(env, jtable, index, jhost);