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;
129 /* add the process to the list of the processes of the simulation */
130 xbt_fifo_unshift(msg_global->process_list, process);
134 JNIEXPORT void JNICALL
135 Java_simgrid_msg_Msg_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
136 m_process_t process = jprocess_to_native_process(jprocess,env);
139 jxbt_throw_notbound(env,"process",jprocess);
143 /* try to suspend the process */
144 if(MSG_OK != MSG_process_suspend(process))
145 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
148 JNIEXPORT void JNICALL
149 Java_simgrid_msg_Msg_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
150 m_process_t process = jprocess_to_native_process(jprocess,env);
153 jxbt_throw_notbound(env,"process",jprocess);
157 /* try to resume the process */
158 if(MSG_OK != MSG_process_resume(process))
159 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
162 JNIEXPORT jboolean JNICALL
163 Java_simgrid_msg_Msg_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
164 m_process_t process = jprocess_to_native_process(jprocess,env);
167 jxbt_throw_notbound(env,"process",jprocess);
171 /* true is the process is suspended, false otherwise */
172 return (jboolean)MSG_process_is_suspended(process);
175 JNIEXPORT void JNICALL
176 Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
177 /* get the native instances from the java ones */
178 m_process_t process = jprocess_to_native_process(jprocess,env);
181 jxbt_throw_notbound(env,"process",jprocess);
185 /* delete the global reference */
186 jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env);
188 /* kill the native process (this wrapper is call by the destructor of the java
191 MSG_process_kill(process);
194 JNIEXPORT jobject JNICALL
195 Java_simgrid_msg_Msg_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
196 /* get the native instances from the java ones */
197 m_process_t process = jprocess_to_native_process(jprocess,env);
201 jxbt_throw_notbound(env,"process",jprocess);
205 host = MSG_process_get_host(process);
208 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
212 /* return the global reference to the java host instance */
213 return (jobject)host->data;
217 JNIEXPORT jobject JNICALL
218 Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) {
219 m_process_t process = MSG_process_from_PID(PID);
222 jxbt_throw_native(env, bprintf("MSG_process_from_PID(%d) failed",PID));
226 if(!SIMIX_process_get_jprocess(process->simdata->s_process)) {
227 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
231 return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process);
235 JNIEXPORT jint JNICALL
236 Java_simgrid_msg_Msg_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
237 m_process_t process = jprocess_to_native_process(jprocess,env);
240 jxbt_throw_notbound(env,"process",jprocess);
244 return (jint)MSG_process_get_PID(process);
248 JNIEXPORT jint JNICALL
249 Java_simgrid_msg_Msg_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
250 m_process_t process = jprocess_to_native_process(jprocess,env);
253 jxbt_throw_notbound(env,"process",jprocess);
257 return (jint)MSG_process_get_PPID(process);
260 JNIEXPORT jobject JNICALL
261 Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) {
262 m_process_t process = MSG_process_self();
266 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
270 jprocess = SIMIX_process_get_jprocess(process->simdata->s_process);
273 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
279 JNIEXPORT jint JNICALL
280 Java_simgrid_msg_Msg_processSelfPID(JNIEnv* env, jclass cls) {
281 return (jint)MSG_process_self_PID();
285 JNIEXPORT jint JNICALL
286 Java_simgrid_msg_Msg_processSelfPPID(JNIEnv* env, jclass cls) {
287 return (jint)MSG_process_self_PPID();
290 JNIEXPORT void JNICALL
291 Java_simgrid_msg_Msg_processChangeHost(JNIEnv* env, jclass cls, jobject jprocess, jobject jhost){
292 m_host_t host = jhost_get_native(env,jhost);
293 m_process_t process = jprocess_to_native_process(jprocess,env);
296 jxbt_throw_notbound(env,"process",jprocess);
301 jxbt_throw_notbound(env,"host",jhost);
305 /* try to change the host of the process */
306 if(MSG_OK != MSG_process_change_host(process,host))
307 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
310 JNIEXPORT void JNICALL
311 Java_simgrid_msg_Msg_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
312 if(MSG_OK != MSG_process_sleep((double)seconds))
313 jxbt_throw_native(env,
314 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
318 /***************************************************************************************
319 * The MSG host connected functions implementation. *
320 ***************************************************************************************/
322 JNIEXPORT jobject JNICALL
323 Java_simgrid_msg_Msg_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
324 m_host_t host; /* native host */
325 jobject jhost; /* global reference to the java host instance returned */
327 /* get the C string from the java string*/
328 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
330 /* get the host by name (the hosts are created during the grid resolution) */
331 host = MSG_get_host_by_name(name);
332 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
334 (*env)->ReleaseStringUTFChars(env, jname, name);
336 if(!host) {/* invalid name */
337 jxbt_throw_host_not_found(env,name);
341 if(!host->data) { /* native host not associated yet with java host */
343 /* instanciate a new java host */
344 jhost = jhost_new_instance(env);
347 jxbt_throw_jni(env,"java host instantiation failed");
351 /* get a global reference to the newly created host */
352 jhost = jhost_ref(env,jhost);
355 jxbt_throw_jni(env,"new global ref allocation failed");
359 /* bind the java host and the native host */
360 jhost_bind(jhost,host,env);
362 /* the native host data field is set with the global reference to the
363 * java host returned by this function
365 host->data = (void*)jhost;
368 /* return the global reference to the java host instance */
369 return (jobject)host->data;
372 JNIEXPORT jstring JNICALL
373 Java_simgrid_msg_Msg_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
374 m_host_t host = jhost_get_native(env,jhost);
377 jxbt_throw_notbound(env,"host",jhost);
381 return (*env)->NewStringUTF(env,host->name);
384 JNIEXPORT jint JNICALL
385 Java_simgrid_msg_Msg_hostGetNumber(JNIEnv* env, jclass cls) {
386 return (jint)MSG_get_host_number();
389 JNIEXPORT jobject JNICALL
390 Java_simgrid_msg_Msg_hostSelf(JNIEnv* env, jclass cls) {
393 m_host_t host = MSG_host_self();
396 /* the native host not yet associated with the java host instance */
398 /* instanciate a new java host instance */
399 jhost = jhost_new_instance(env);
402 jxbt_throw_jni(env,"java host instantiation failed");
406 /* get a global reference to the newly created host */
407 jhost = jhost_ref(env,jhost);
410 jxbt_throw_jni(env,"global ref allocation failed");
414 /* Bind & store it */
415 jhost_bind(jhost,host,env);
416 host->data = (void*)jhost;
418 jhost = (jobject)host->data;
424 JNIEXPORT jdouble JNICALL
425 Java_simgrid_msg_Msg_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
426 m_host_t host = jhost_get_native(env,jhost);
429 jxbt_throw_notbound(env,"host",jhost);
433 return (jdouble)MSG_get_host_speed(host);
436 JNIEXPORT jint JNICALL
437 Java_simgrid_msg_Msg_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
438 m_host_t host = jhost_get_native(env,jhost);
441 jxbt_throw_notbound(env,"host",jhost);
445 return (jint)MSG_get_host_msgload(host);
449 JNIEXPORT jboolean JNICALL
450 Java_simgrid_msg_Msg_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
451 m_host_t host = jhost_get_native(env,jhost);
454 jxbt_throw_notbound(env,"host",jhost);
458 return (jboolean)MSG_host_is_avail(host);
462 /***************************************************************************************
463 * The MSG task connected functions implementation. *
464 ***************************************************************************************/
466 JNIEXPORT void JNICALL
467 Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
468 jdouble jcomputeDuration, jdouble jmessageSize) {
469 m_task_t task; /* the native task to create */
470 const char* name; /* the name of the task */
472 if(jcomputeDuration < 0) {
473 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
474 (double)jcomputeDuration));
478 if(jmessageSize < 0) {
479 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
480 (double)jmessageSize));
485 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
489 /* get the C string from the java string*/
490 name = (*env)->GetStringUTFChars(env, jname, 0);
492 /* create the task */
493 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
495 (*env)->ReleaseStringUTFChars(env, jname, name);
497 /* bind & store the task */
498 jtask_bind(jtask,task,env);
500 /* allocate a new global reference to the java task instance */
501 task->data = (void*)jtask_new_global_ref(jtask,env);
504 jxbt_throw_jni(env,"global ref allocation failed");
507 JNIEXPORT void JNICALL
508 Java_simgrid_msg_Msg_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask_arg, jstring jname,
509 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
511 m_task_t task; /* the native parallel task to create */
512 const char* name; /* the name of the task */
513 jobject jtask; /* the global reference to the java parallel task instance */
516 double* computeDurations;
517 double* messageSizes;
518 jdouble *jcomputeDurations;
519 jdouble *jmessageSizes;
525 if (!jcomputeDurations_arg) {
526 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
530 if(!jmessageSizes_arg){
531 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
536 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
540 host_count = (int)(*env)->GetArrayLength(env,jhosts);
543 hosts = (m_host_t*)calloc(host_count,sizeof(m_host_t));
544 computeDurations = (double*)calloc(host_count,sizeof(double));
545 messageSizes = (double*)calloc(host_count,sizeof(double));
547 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
548 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
550 for(index = 0; index < host_count; index++) {
551 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
552 hosts[index] = jhost_get_native(env,jhost);
553 computeDurations[index] = jcomputeDurations[index];
554 messageSizes[index] = jmessageSizes[index];
557 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
558 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
561 /* get the C string from the java string*/
562 name = (*env)->GetStringUTFChars(env, jname, 0);
564 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
566 (*env)->ReleaseStringUTFChars(env, jname, name);
568 /* allocate a new global reference to the java task instance */
569 jtask = jparallel_task_ref(env,jtask_arg);
571 /* associate the java task object and the native task */
572 jparallel_task_bind(jtask,task,env);
574 task->data = (void*)jtask;
577 jxbt_throw_jni(env,"global ref allocation failed");
580 JNIEXPORT jobject JNICALL
581 Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
584 m_task_t task = jtask_to_native_task(jtask,env);
587 jxbt_throw_notbound(env,"task",jtask);
591 process = MSG_task_get_sender(task);
592 return SIMIX_process_get_jprocess(process->simdata->s_process);
595 JNIEXPORT jobject JNICALL
596 Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
600 task = jparallel_task_to_native_parallel_task(jtask,env);
603 jxbt_throw_notbound(env,"task",jtask);
607 process = MSG_task_get_sender(task);
609 return SIMIX_process_get_jprocess(process->simdata->s_process);
612 JNIEXPORT jobject JNICALL
613 Java_simgrid_msg_Msg_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
615 m_task_t task = jtask_to_native_task(jtask,env);
618 jxbt_throw_notbound(env,"task",jtask);
622 host = MSG_task_get_source(task);
625 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
629 return (jobject)host->data;
632 JNIEXPORT jobject JNICALL
633 Java_simgrid_msg_Msg_parallelTaskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
634 m_task_t task = jparallel_task_to_native_parallel_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;
653 JNIEXPORT jstring JNICALL
654 Java_simgrid_msg_Msg_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
655 m_task_t task = jtask_to_native_task(jtask,env);
658 jxbt_throw_notbound(env,"task",jtask);
662 return (*env)->NewStringUTF(env,task->name);
665 JNIEXPORT jstring JNICALL
666 Java_simgrid_msg_Msg_parallelTaskGetName(JNIEnv* env, jclass cls, jobject jtask) {
667 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
670 jxbt_throw_notbound(env,"task",jtask);
674 return (*env)->NewStringUTF(env,ptask->name);
677 JNIEXPORT void JNICALL
678 Java_simgrid_msg_Msg_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
679 m_task_t ptask = jtask_to_native_task(jtask,env);
682 jxbt_throw_notbound(env,"task",jtask);
686 if(MSG_OK != MSG_task_cancel(ptask))
687 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
690 JNIEXPORT void JNICALL
691 Java_simgrid_msg_Msg_parallelTaskCancel(JNIEnv* env, jclass cls, jobject jtask) {
692 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
695 jxbt_throw_notbound(env,"task",jtask);
699 if(MSG_OK != MSG_task_cancel(ptask))
700 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
704 JNIEXPORT jdouble JNICALL
705 Java_simgrid_msg_Msg_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
706 m_task_t ptask = jtask_to_native_task(jtask,env);
709 jxbt_throw_notbound(env,"task",jtask);
712 return (jdouble)MSG_task_get_compute_duration(ptask);
715 JNIEXPORT jdouble JNICALL
716 Java_simgrid_msg_Msg_parallelTaskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
717 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
720 jxbt_throw_notbound(env,"task",jtask);
723 return (jdouble)MSG_task_get_compute_duration(ptask);
726 JNIEXPORT jdouble JNICALL
727 Java_simgrid_msg_Msg_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
728 m_task_t ptask = jtask_to_native_task(jtask,env);
731 jxbt_throw_notbound(env,"task",jtask);
734 return (jdouble)MSG_task_get_remaining_computation(ptask);
737 JNIEXPORT jdouble JNICALL
738 Java_simgrid_msg_Msg_paralleTaskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
739 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
742 jxbt_throw_notbound(env,"task",jtask);
745 return (jdouble)MSG_task_get_remaining_computation(ptask);
748 JNIEXPORT void JNICALL
749 Java_simgrid_msg_Msg_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
750 m_task_t task = jtask_to_native_task(jtask,env);
753 jxbt_throw_notbound(env,"task",jtask);
756 MSG_task_set_priority(task,(double)priority);
759 JNIEXPORT void JNICALL
760 Java_simgrid_msg_Msg_parallelTaskSetPriority(JNIEnv* env, jclass cls,
761 jobject jtask, jdouble priority) {
762 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
765 jxbt_throw_notbound(env,"task",jtask);
768 MSG_task_set_priority(ptask,(double)priority);
772 JNIEXPORT void JNICALL
773 Java_simgrid_msg_Msg_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
775 /* get the native task */
776 m_task_t task = jtask_to_native_task(jtask_arg,env);
780 jxbt_throw_notbound(env,"task",jtask);
783 jtask = (jobject)task->data;
785 if(MSG_OK != MSG_task_destroy(task))
786 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
788 /* delete the global reference to the java task object */
789 jtask_delete_global_ref(jtask,env);
792 JNIEXPORT void JNICALL
793 Java_simgrid_msg_Msg_parallelTaskDestroy(JNIEnv* env, jclass cls, jobject jtask) {
794 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
797 jxbt_throw_notbound(env,"task",jtask);
801 /* unbind jobj & native one */
802 jparallel_task_bind(jtask,0,env);
804 /* delete the global reference to the java parallel task object */
805 jparallel_task_unref(env,jtask);
807 /* free allocated memory */
808 if(ptask->simdata->comm_amount)
809 free(ptask->simdata->comm_amount);
811 if(ptask->simdata->host_list)
812 free(ptask->simdata->host_list);
814 if(MSG_OK != MSG_task_destroy(ptask))
815 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
818 JNIEXPORT void JNICALL
819 Java_simgrid_msg_Msg_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
820 m_task_t task = jtask_to_native_task(jtask,env);
823 jxbt_throw_notbound(env,"task",jtask);
827 if(MSG_OK != MSG_task_execute(task))
828 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
831 JNIEXPORT void JNICALL
832 Java_simgrid_msg_Msg_parallelTaskExecute(JNIEnv* env, jclass cls, jobject jtask) {
833 m_task_t ptask = jparallel_task_to_native_parallel_task(jtask,env);
836 jxbt_throw_notbound(env,"task",jtask);
840 if(MSG_OK != MSG_parallel_task_execute(ptask))
841 jxbt_throw_native(env, xbt_strdup("MSG_parallel_task_execute() failed"));
844 /***************************************************************************************
845 * The MSG channel connected functions implementation. *
846 ***************************************************************************************/
848 JNIEXPORT jobject JNICALL
849 Java_simgrid_msg_Msg_channelGet(JNIEnv* env, jclass cls, jobject jchannel) {
850 m_task_t task = NULL;
852 if(MSG_OK != MSG_task_get(&task,(int)jchannel_get_id(jchannel,env))) {
853 jxbt_throw_native(env, xbt_strdup("MSG_task_get() failed"));
857 return (jobject)task->data;
860 JNIEXPORT jobject JNICALL
861 Java_simgrid_msg_Msg_channelGetWithTimeout(JNIEnv* env, jclass cls,
862 jobject jchannel, jdouble jtimeout) {
863 m_task_t task = NULL;
864 int id = (int)jchannel_get_id(jchannel,env);
866 if(MSG_OK != MSG_task_get_with_time_out(&task,id,(double)jtimeout)) {
867 jxbt_throw_native(env, xbt_strdup("MSG_task_get_with_time_out() failed"));
871 return (jobject)task->data;
874 JNIEXPORT jobject JNICALL
875 Java_simgrid_msg_Msg_channelGetFromHost(JNIEnv* env, jclass cls,
876 jobject jchannel, jobject jhost) {
877 m_host_t host = jhost_get_native(env,jhost);
878 m_task_t task = NULL;
879 int id = (int)jchannel_get_id(jchannel,env);
882 jxbt_throw_notbound(env,"host",jhost);
885 if(MSG_OK != MSG_task_get_from_host(&task,id,host)){
886 jxbt_throw_native(env, xbt_strdup("MSG_task_get_from_host() failed"));
890 jxbt_throw_notbound(env,"task",task);
894 return (jobject)task->data;
897 JNIEXPORT jboolean JNICALL
898 Java_simgrid_msg_Msg_channelHasPendingCommunication(JNIEnv* env, jclass cls, jobject jchannel) {
899 int id = (int)jchannel_get_id(jchannel,env);
901 return (jboolean)MSG_task_Iprobe(id);
904 JNIEXPORT jint JNICALL
905 Java_simgrid_msg_Msg_channelGetCommunicatingProcess(JNIEnv* env, jclass cls, jobject jchannel) {
906 int id =jchannel_get_id(jchannel,env);
908 return (jint)MSG_task_probe_from(id);
911 JNIEXPORT jint JNICALL
912 Java_simgrid_msg_Msg_channelGetHostWaitingTasks(JNIEnv* env, jclass cls,
913 jobject jchannel, jobject jhost) {
914 int id = (int)jchannel_get_id(jchannel,env);
915 m_host_t host = jhost_get_native(env,jhost);
918 jxbt_throw_notbound(env,"host",jhost);
922 return (jint)MSG_task_probe_from_host(id,host);
925 JNIEXPORT void JNICALL
926 Java_simgrid_msg_Msg_channelPut(JNIEnv* env, jclass cls,
927 jobject jchannel, jobject jtask, jobject jhost) {
929 if(MSG_OK != MSG_task_put(jtask_to_native_task(jtask,env),
930 jhost_get_native(env,jhost),
931 (int)jchannel_get_id(jchannel,env)))
932 jxbt_throw_native(env, xbt_strdup("MSG_task_put() failed"));
935 JNIEXPORT void JNICALL
936 Java_simgrid_msg_Msg_channelPutWithTimeout(JNIEnv* env, jclass cls,
937 jobject jchannel, jobject jtask, jobject jhost,
939 m_task_t task = jtask_to_native_task(jtask,env);
940 int id = (int)jchannel_get_id(jchannel,env);
941 m_host_t host = jhost_get_native(env,jhost);
944 jxbt_throw_notbound(env,"host",jhost);
948 jxbt_throw_notbound(env,"task",jtask);
952 if(MSG_OK != MSG_task_put_with_timeout(task,host,id,(double)jtimeout))
953 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
956 JNIEXPORT void JNICALL
957 Java_simgrid_msg_Msg_channelPutBounded(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_bounded(task,host,id,(double)jmaxRate))
974 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
977 JNIEXPORT jint JNICALL
978 Java_simgrid_msg_Msg_channelWait(JNIEnv* env, jclass cls, jobject jchannel, jdouble timeout) {
980 int id = (int)jchannel_get_id(jchannel,env);
982 if(MSG_OK != MSG_channel_select_from(id,(double)timeout,&PID)) {
983 jxbt_throw_native(env, xbt_strdup("MSG_channel_select_from() failed"));
990 JNIEXPORT jint JNICALL
991 Java_simgrid_msg_Msg_channelGetNumber(JNIEnv* env, jclass cls) {
992 return (jint)MSG_get_channel_number();
995 JNIEXPORT void JNICALL
996 Java_simgrid_msg_Msg_channelSetNumber(JNIEnv* env , jclass cls,jint channelNumber) {
997 MSG_set_channel_number((int)channelNumber);
1000 JNIEXPORT jint JNICALL
1001 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
1002 return (jint)MSG_get_errno();
1005 JNIEXPORT jdouble JNICALL
1006 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
1007 return (jdouble)MSG_get_clock();
1011 JNIEXPORT void JNICALL
1012 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
1021 argc = (int)(*env)->GetArrayLength(env,jargs);
1025 argv = (char**)calloc(argc,sizeof(char*));
1027 argv[0] = strdup("java");
1029 for(index = 0; index < argc -1; index++) {
1030 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
1032 tmp = (*env)->GetStringUTFChars(env, jval, 0);
1034 argv[index +1] = strdup(tmp);
1036 (*env)->ReleaseStringUTFChars(env, jval, tmp);
1039 MSG_global_init(&argc,argv);
1041 for(index = 0; index < argc; index++)
1047 __current_thread_id = GetCurrentThreadId();
1049 __current_thread_id = pthread_self();
1054 JNIEXPORT void JNICALL
1055 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
1056 xbt_fifo_item_t item = NULL;
1057 m_host_t host = NULL;
1060 /* Run everything */
1061 if(MSG_OK != MSG_main())
1062 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1064 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
1065 SIMIX_display_process_status();
1066 exit(0); /* FIXME */
1068 DEBUG0("Clean java world");
1069 /* Cleanup java hosts */
1070 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
1071 jhost = (jobject)host->data;
1074 jhost_unref(env,jhost);
1076 DEBUG0("Clean native world");
1077 /* cleanup native stuff */
1078 if(MSG_OK != MSG_clean())
1079 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
1083 JNIEXPORT jint JNICALL
1084 Java_simgrid_msg_Msg_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
1085 return (jint)MSG_process_killall((int)jresetPID);
1088 JNIEXPORT void JNICALL
1089 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
1091 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1093 MSG_create_environment(platformFile);
1095 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1098 JNIEXPORT void JNICALL
1099 Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) {
1100 m_process_t m_process = jprocess_to_native_process(jprocess,env);
1101 smx_process_t s_process;
1103 xbt_os_mutex_t ctx_mutex, creation_mutex;
1104 xbt_os_cond_t ctx_cond, creation_cond;
1106 DEBUG3("Msg_waitSignal(m_process=%p %s/%s)",
1107 m_process,m_process->name,m_process->simdata->m_host->name);
1109 jxbt_throw_notbound(env,"process",jprocess);
1113 s_process = m_process->simdata->s_process;
1115 if (s_process == NULL) {
1116 jxbt_throw_notbound(env,"SIMIX process",jprocess);
1120 ctx_mutex = SIMIX_process_get_jmutex(s_process);
1121 ctx_cond = SIMIX_process_get_jcond(s_process);
1123 creation_mutex = xbt_creation_mutex_get();
1124 creation_cond = xbt_creation_cond_get();
1126 xbt_os_mutex_lock(creation_mutex);
1127 xbt_os_mutex_lock(ctx_mutex);
1128 xbt_os_cond_signal( creation_cond );
1129 xbt_os_mutex_unlock( creation_mutex );
1130 xbt_os_cond_wait(ctx_cond, ctx_mutex);
1131 xbt_os_mutex_unlock(ctx_mutex);
1134 JNIEXPORT void JNICALL
1135 Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
1136 m_process_t process = jprocess_to_native_process(jprocess,env);
1139 jxbt_throw_notbound(env,"process",jprocess);
1142 MSG_process_kill(process);
1145 JNIEXPORT void JNICALL
1146 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
1147 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1149 MSG_paje_output(pajeFile);
1151 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1155 JNIEXPORT void JNICALL
1156 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
1157 const char* s = (*env)->GetStringUTFChars(env,js,0);
1159 (*env)->ReleaseStringUTFChars(env, js, s);
1162 JNIEXPORT jobjectArray JNICALL
1163 Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) {
1165 jobjectArray jtable;
1170 int count = xbt_fifo_size(msg_global->host);
1171 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
1173 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
1179 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
1182 jxbt_throw_jni(env,"Hosts table allocation failed");
1186 for(index = 0; index < count; index++) {
1187 host = table[index];
1188 jhost = (jobject)(host->data);
1191 jname = (*env)->NewStringUTF(env,host->name);
1193 jhost = Java_simgrid_msg_Msg_hostGetByName(env,cls_arg,jname);
1194 /* FIXME: leak of jname ? */
1197 (*env)->SetObjectArrayElement(env, jtable, index, jhost);