Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Port JavaContext to C++
authorGabriel Corona <gabriel.corona@loria.fr>
Mon, 7 Dec 2015 13:56:30 +0000 (14:56 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Mon, 7 Dec 2015 14:26:09 +0000 (15:26 +0100)
40 files changed:
examples/java/async/async.tesh
examples/java/bittorrent/bittorrent.tesh
examples/java/chord/chord.tesh
examples/java/cloud/cloud.tesh
examples/java/cloud/migration/migration.tesh
examples/java/commTime/commtime.tesh
examples/java/kademlia/kademlia.tesh
examples/java/master_slave_bypass/bypass.tesh
examples/java/master_slave_kill/kill.tesh
examples/java/masterslave/masterslave.tesh
examples/java/migration/migration.tesh
examples/java/mutualExclusion/mutualexclusion.tesh
examples/java/pingPong/pingpong.tesh
examples/java/priority/priority.tesh
examples/java/reservationSurfPlugin/reservation_surf_plugin.tesh
examples/java/startKillTime/startKillTime.tesh
examples/java/surfCpuModel/surf_cpu_model.tesh
examples/java/surfPlugin/surf_plugin.tesh
examples/java/suspend/suspend.tesh
examples/java/tracing/tracingPingPong.tesh
examples/scala/master_slave_bypass/bypass.tesh
examples/scala/master_slave_kill/kill.tesh
examples/scala/masterslave/masterslave.tesh
include/simgrid/simix.hpp
src/bindings/java/JavaContext.cpp [new file with mode: 0644]
src/bindings/java/JavaContext.hpp [new file with mode: 0644]
src/bindings/java/jmsg.cpp [moved from src/bindings/java/jmsg.c with 73% similarity]
src/bindings/java/jmsg.h
src/bindings/java/jmsg_host.c
src/bindings/java/jmsg_host.h
src/bindings/java/jmsg_process.cpp [moved from src/bindings/java/jmsg_process.c with 86% similarity]
src/bindings/java/jmsg_process.h
src/bindings/java/jmsg_storage.c
src/bindings/java/jmsg_task.c
src/bindings/java/jmsg_vm.c
src/bindings/java/jxbt_utilities.h
src/bindings/java/smx_context_java.c [deleted file]
src/bindings/java/smx_context_java.h [deleted file]
teshsuite/java/sleep_host_off/sleep_host_off.tesh
tools/cmake/DefinePackages.cmake

index 8a487e1..1b4b2a7 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 ! timeout 30
 $ java -classpath ${classpath:=.} async/AsyncTest ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/async/asyncDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (10:async.Slave@Robert) Receiving on 'slave_8'
 > [  0.000000] (11:async.Slave@Sirois) Receiving on 'slave_9'
 > [  0.000000] (12:async.Slave@Monique) Receiving on 'slave_10'
index 89c28c2..2a27a0a 100644 (file)
@@ -4,7 +4,7 @@
 ! timeout 15
 
 $ java -classpath ${classpath:=.} bittorrent/Bittorrent ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/bittorrent/bittorrent.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [5000.832370] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
 > [Boivin:bittorrent.Peer:(2) 0.000000] [jmsg/INFO] Hi, I'm joining the network with id 2
 > [Boivin:bittorrent.Peer:(2) 5000.048881] [jmsg/INFO] Here is my current status: 1111111111
index 4168a5c..01dc891 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} chord/Chord ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/chord/chord.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [652.540924] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
 > [Boivin:chord.Node:(7) 0.000000] [jmsg/INFO] Joining the ring with id 8 knowing node 1
 > [Gatien:chord.Node:(1) 0.000000] [jmsg/INFO] Joining the ring with id 48 knowing node 1
index 64a3c4b..d7386cf 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} cloud/Cloud ${srcdir:=.}/../platforms/platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (0:@) Start2  hosts
 > [  0.000000] (0:@) Create VM(VM00)@PM(Intel) with 0 mounted disks
 > [  0.000000] (0:@) Create VM(VM01)@PM(Provost) with 0 mounted disks
index 387a1ef..34b2b21 100644 (file)
@@ -1,7 +1,7 @@
 #! tesh
 
 $ java -classpath ${classpath:=.} cloud/migration/Main ${srcdir:=.}/../platforms/two_hosts_platform.xml ${srcdir:=.}/cloud/migration/deploy_simple.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [host0:cloud/migration/Test:(1) 0.000000] [jmsg/INFO] This example evaluates the migration time of a VM in presence of collocated VMs on the source and the dest nodes
 > [host0:cloud/migration/Test:(1) 0.000000] [jmsg/INFO] The migrated VM has a memory intensity rate of 70% of the network BW and a cpu load of 90% "(see cloudcom 2013 paper "Adding a Live Migration Model Into SimGrid" for further information) 
 > [host0:cloud/migration/Test:(1) 0.000000] [jmsg/INFO] Load of collocated VMs fluctuate between 0 and 90% in order to create a starvation issue and see whether it impacts or not the migration time
index cdd101c..d0f709c 100644 (file)
@@ -4,7 +4,7 @@
 ! timeout 15
 
 $ java -classpath ${classpath:=.} commTime/CommTimeTest ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/commTime/commTimeDeployment.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [7016.452828] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
 > [Bellemarre:commTime.Slave:(15) 0.000000] [jmsg/INFO] Receiving on 'slave_13'
 > [Bellemarre:commTime.Slave:(15) 7006.341189] [jmsg/INFO] Received Finalize. I'm done. See you!
index d9a0acf..3adbb56 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} kademlia/Kademlia ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/kademlia/kademlia.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [900.000000] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
 > [Boivin:kademlia.Node:(2) 0.000000] [jmsg/INFO] Hi, I'm going to join the network with the id 1!
 > [Boivin:kademlia.Node:(2) 900.000000] [jmsg/INFO] 8/8 FIND_NODE have succedded.
index 01efba0..98a38b0 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} master_slave_bypass/MsBypass ${srcdir:=.}/../platforms/platform.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Boivin:process1:(1) 0.000000] [jmsg/INFO] Master Hello!
 > [Boivin:process1:(1) 0.000000] [jmsg/INFO] Create process on host 'Jacquelin'
 > [Jacquelin:process2:(2) 0.000000] [jmsg/INFO] Slave Hello!
index 022b338..9281484 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} master_slave_kill/MsKill ${srcdir:=.}/../platforms/platform.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Jacquelin:master:(1) 0.000000] [jmsg/INFO] Master Hello!
 > [Jacquelin:master:(1) 0.000000] [jmsg/INFO] Create process on host 'Boivin'
 > [Boivin:slave:(2) 0.000000] [jmsg/INFO] Slave Hello!
index 83b8fdb..2ff2848 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} masterslave/Masterslave ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/masterslave/masterslaveDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:masterslave.Master@Jacquelin) Hello! Got 7 slaves and 5 tasks to process
 > [  0.000000] (2:masterslave.Forwarder@Jackson) Receiving on 'slave_0'
 > [  0.000000] (3:masterslave.Forwarder@Casavant) Receiving on 'slave_1'
index 9139002..3497f43 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} migration/Migration ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/migration/migrationDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:migration.Emigrant@Jacquelin) I'll look for a new job on another machine where the grass is greener.
 > [  0.000000] (1:migration.Emigrant@Boivin) Yeah, found something to do
 > [  1.000000] (2:migration.Policeman@Boivin) Wait a bit before migrating the emigrant.
index c744230..1c03a20 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} mutualExclusion/MutexCentral ${srcdir:=.}/../platforms/small_platform.xml ${srcdir:=.}/mutualExclusion/mutex_centralized_deployment.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Jupiter:mutualExclusion.Node:(2) 0.000000] [jmsg/INFO] Send a request to the coordinator
 > [Fafard:mutualExclusion.Node:(3) 0.000000] [jmsg/INFO] Send a request to the coordinator
 > [Tremblay:mutualExclusion.Coordinator:(1) 0.019014] [jmsg/INFO] Got a request from mutualExclusion.Node. Queue empty: grant it
index 69c7dfe..93643a7 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} pingPong/PingPongTest ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/pingPong/pingPongDeployment.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [1.048882] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
 > [Boivin:pingPong.Receiver:(2) 0.000000] [jmsg/INFO] hello!
 > [Boivin:pingPong.Receiver:(2) 0.000000] [jmsg/INFO] try to get a task
index 32747db..19879a6 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} priority/Priority ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/priority/priorityDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:priority.Test@Fafard) Hello! Running a task of size 7.6296E7 with priority 1.0
 > [  0.000000] (2:priority.Test@Fafard) Hello! Running a task of size 7.6296E7 with priority 2.0
 > [  0.833332] (2:priority.Test@Fafard) Goodbye now!
index 80f637f..d7d52c9 100644 (file)
@@ -1,7 +1,7 @@
 #! tesh
 
 $ java -classpath ${classpath:=.} reservationSurfPlugin/TestPlugin ${srcdir:=.}/../platforms/small_platform.xml ${srcdir:=.}/reservationSurfPlugin/reservationSurfPluginDeployment.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Tremblay:reservationSurfPlugin.Sender:(1) 0.000000] [jmsg/INFO] helloo!
 > [Ginette:reservationSurfPlugin.Receiver:(2) 0.000000] [jmsg/INFO] helloo!
 > [0.000000] [jmsg/INFO] Trace: Communicate message of size 1.0 with rate -1.0 and bound 1.648342855615789E9 from Tremblay to Ginette
index dee888b..eadb5f6 100644 (file)
@@ -1,6 +1,6 @@
 #! tesh
 $ java -classpath ${classpath:=.} startKillTime/StartKillTime ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/startKillTime/deployment_start_kill.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Jacquelin:startKillTime.Master:(1) 0.000000] [jmsg/INFO] Hello!
 > [Boivin:startKillTime.Slave:(2) 1.000000] [jmsg/INFO] Hello!
 > [Jean_Yves:startKillTime.Slave:(3) 2.000000] [jmsg/INFO] Hello!
index a91d278..b855455 100644 (file)
@@ -2,7 +2,7 @@
 
 $ java -classpath ${classpath:=.} surfCpuModel/TestCpuModel ${srcdir:=.}/../platforms/small_platform.xml ${srcdir:=.}/surfCpuModel/surfCpuModelDeployment.xml --cfg=host/model:compound
 > [0.000000] [xbt_cfg/INFO] Configuration change: Set 'host/model' to 'compound'
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [0.000000] [jmsg/INFO] Initialize Cpu Constant Model
 > [0.000000] [jmsg/INFO] New Cpu(Tremblay, 9.8095E7, 1.0)
 > [0.000000] [jmsg/INFO] New Cpu(Jupiter, 7.6296E7, 1.0)
index f0f720d..423cb4a 100644 (file)
@@ -9,7 +9,7 @@
 ! output sort
 #
 $ java -classpath ${classpath:=.} surfPlugin/TestPlugin ${srcdir:=.}/../platforms/small_platform.xml ${srcdir:=.}/surfPlugin/surfPluginDeployment.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [0.000000] [jmsg/INFO] Trace: Link created __loopback__
 > [0.000000] [jmsg/INFO] Trace: Link state changed __loopback__
 > [0.000000] [jmsg/INFO] Trace: Cpu created Tremblay
index ec22ec7..37b148b 100644 (file)
@@ -2,7 +2,7 @@
 
 ! output sort 19
 $ java -classpath ${classpath:=.} suspend/Suspend ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/suspend/suspendDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:suspend.DreamMaster@Jacquelin) Let's create a lazy guy.
 > [  0.000000] (1:suspend.DreamMaster@Jacquelin) Let's wait a little bit...
 > [  0.000000] (2:Lazy@Jacquelin) Nobody's watching me ? Let's go to sleep.
index f86e589..4b0937c 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} tracing/TracingTest ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/tracing/tracingPingPongDeployment.xml --cfg=tracing:yes  --cfg=tracing/filename:simulation.trace --cfg=tracing/platform:yes
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [0.000000] [xbt_cfg/INFO] Configuration change: Set 'tracing' to 'yes'
 > [0.000000] [xbt_cfg/INFO] Configuration change: Set 'tracing/filename' to 'simulation.trace'
 > [0.000000] [xbt_cfg/INFO] Configuration change: Set 'tracing/platform' to 'yes'
index aa26972..a38a098 100644 (file)
@@ -2,7 +2,7 @@
 
 ! output sort 19
 $ java -classpath ${classpath:=.} master_slave_bypass/MsBypass ${srcdir:=.}/../platforms/small_platform.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Jupiter:process1:(1) 0.000000] [jmsg/INFO] Master Hello!
 > [Jupiter:process1:(1) 0.000000] [jmsg/INFO] Create process on host 'Tremblay'
 > [Tremblay:process2:(2) 0.000000] [jmsg/INFO] Slave Hello!
index 12d0c9b..c06a0b8 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} master_slave_kill/MsKill ${srcdir:=.}/../platforms/small_platform.xml
-> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up.
+> [0.000000] [jmsg/INFO] Using regular java threads.
 > [Jupiter:master:(1) 0.000000] [jmsg/INFO] Master Hello!
 > [Jupiter:master:(1) 0.000000] [jmsg/INFO] Create process on host 'alice'
 > [Tremblay:slave:(2) 0.000000] [jmsg/INFO] Slave Hello!
index 83b8fdb..2ff2848 100644 (file)
@@ -3,7 +3,7 @@
 ! output sort 19
 
 $ java -classpath ${classpath:=.} masterslave/Masterslave ${srcdir:=.}/../platforms/platform.xml ${srcdir:=.}/masterslave/masterslaveDeployment.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:masterslave.Master@Jacquelin) Hello! Got 7 slaves and 5 tasks to process
 > [  0.000000] (2:masterslave.Forwarder@Jackson) Receiving on 'slave_0'
 > [  0.000000] (3:masterslave.Forwarder@Casavant) Receiving on 'slave_1'
index 2565808..8888fb8 100644 (file)
@@ -49,10 +49,11 @@ protected:
 };
 
 class Context {
-private:
+protected:
   xbt_main_func_t code_ = nullptr;
   int argc_ = 0;
   char **argv_ = nullptr;
+private:
   void_pfn_smxprocess_t cleanup_func_ = nullptr;
   smx_process_t process_ = nullptr;
 public:
diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp
new file mode 100644 (file)
index 0000000..f371d6f
--- /dev/null
@@ -0,0 +1,190 @@
+/* context_java - implementation of context switching for java threads */
+
+/* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include <xbt/function_types.h>
+#include <simgrid/simix.h>
+#include <xbt/ex.h>
+#include "JavaContext.hpp"
+#include "jxbt_utilities.h"
+#include "xbt/dynar.h"
+#include "../../simix/smx_private.h"
+extern JavaVM *__java_vm;
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
+
+namespace simgrid {
+namespace java {
+
+simgrid::simix::ContextFactory* java_factory()
+{
+  XBT_INFO("Using regular java threads.");
+  return new JavaContextFactory();
+}
+
+JavaContextFactory::JavaContextFactory()
+  : ContextFactory("JavaContextFactory")
+{
+}
+
+JavaContextFactory::~JavaContextFactory()
+{
+}
+
+JavaContext* JavaContextFactory::self()
+{
+  return (JavaContext*) xbt_os_thread_get_extra_data();
+}
+
+JavaContext* JavaContextFactory::create_context(
+  xbt_main_func_t code, int argc, char ** argv,
+  void_pfn_smxprocess_t cleanup, smx_process_t process)
+{
+  return this->new_context<JavaContext>(code, argc, argv, cleanup, process);
+}
+
+void JavaContextFactory::run_all()
+{
+  xbt_dynar_t processes = SIMIX_process_get_runnable();
+  smx_process_t process;
+  unsigned int cursor;
+  xbt_dynar_foreach(processes, cursor, process) {
+    static_cast<JavaContext*>(SIMIX_process_get_context(process))->resume();
+  }
+}
+
+
+JavaContext::JavaContext(xbt_main_func_t code,
+        int argc, char **argv,
+        void_pfn_smxprocess_t cleanup_func,
+        smx_process_t process)
+  : Context(code, argc, argv, cleanup_func, process)
+{
+  static int thread_amount=0;
+  thread_amount++;
+
+  /* If the user provided a function for the process then use it
+     otherwise is the context for maestro */
+  if (code) {
+    if (argc == 0)
+      this->jprocess = (jobject) code;
+    else
+      this->jprocess = nullptr;
+    this->begin = xbt_os_sem_init(0);
+    this->end = xbt_os_sem_init(0);
+
+    TRY {
+       this->thread = xbt_os_thread_create(
+         nullptr, JavaContext::wrapper, this, nullptr);
+    }
+    CATCH_ANONYMOUS {
+      RETHROWF("Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
+               "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
+               thread_amount);
+    }
+  } else {
+    this->thread = nullptr;
+    xbt_os_thread_set_extra_data(this);
+  }
+}
+
+JavaContext::~JavaContext()
+{
+  if (this->thread) {
+    // We are not in maestro context
+    xbt_os_thread_join(this->thread, nullptr);
+    xbt_os_sem_destroy(this->begin);
+    xbt_os_sem_destroy(this->end);
+  }
+}
+
+void* JavaContext::wrapper(void *data)
+{
+  JavaContext* context = (JavaContext*)data;
+  xbt_os_thread_set_extra_data(context);
+  //Attach the thread to the JVM
+
+  JNIEnv *env;
+  XBT_ATTRIB_UNUSED jint error =
+    __java_vm->AttachCurrentThread((void **) &env, NULL);
+  xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
+  context->jenv = get_current_thread_env();
+  //Wait for the first scheduling round to happen.
+  xbt_os_sem_acquire(context->begin);
+  //Create the "Process" object if needed.
+  if (context->argc_ > 0)
+    (*context)();
+  else {
+    smx_process_t process = SIMIX_process_self();
+    env->SetLongField(context->jprocess, jprocess_field_Process_bind,
+                         (intptr_t)process);
+  }
+
+  // Adrien, ugly path, just to bypass creation of context at low levels
+  // (i.e such as for the VM migration for instance)
+  if (context->jprocess != nullptr){
+    xbt_assert((context->jprocess != nullptr), "Process not created...");
+    //wait for the process to be able to begin
+    //TODO: Cache it
+    jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
+    jdouble startTime =  env->GetDoubleField(context->jprocess, jprocess_field_Process_startTime);
+    if (startTime > MSG_get_clock()) {
+      MSG_process_sleep(startTime - MSG_get_clock());
+    }
+    //Execution of the "run" method.
+    jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
+    xbt_assert( (id != nullptr), "Method not found...");
+    env->CallVoidMethod(context->jprocess, id);
+  }
+  context->stop();
+  return nullptr;
+}
+
+void JavaContext::stop()
+{
+  /* I am the current process and I am dying */
+  if (this->iwannadie) {
+    this->iwannadie = 0;
+    JNIEnv *env = get_current_thread_env();
+    XBT_DEBUG("Gonna launch Killed Error");
+    // TODO Adrien, if the process has not been created at the java layer, why should we raise the exception/error at the java level (this happens
+    // for instance during the migration process that creates at the C level two processes: one on the SRC node and one on the DST node, if the DST process is killed.
+    // it is not required to raise an exception at the JAVA level, the low level should be able to manage such an issue correctly but this is not the case right now unfortunately ...
+    // TODO it will be nice to have the name of the process to help the end-user to know which Process has been killed
+   // jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process %s killed :) (file smx_context_java.c)", MSG_process_get_name( (msg_process_t)context) ));
+    jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError",
+      bprintf("Process %s killed :) (file JavaContext.cpp)",
+      simcall_process_get_name((smx_process_t) SIMIX_context_get_process(this))) );
+    XBT_DEBUG("Trigger a cancel error at the C level");
+    THROWF(cancel_error, 0, "process cancelled");
+  } else {
+    Context::stop();
+    /* detach the thread and kills it */
+    JNIEnv *env = this->jenv;
+    env->DeleteGlobalRef(this->jprocess);
+    XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
+    xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
+    xbt_os_sem_release(this->end);
+    xbt_os_thread_exit(NULL);
+  }
+}
+
+void JavaContext::suspend()
+{
+  xbt_os_sem_release(this->end);
+  xbt_os_sem_acquire(this->begin);
+}
+
+// FIXME: inline those functions
+void JavaContext::resume()
+{
+  xbt_os_sem_release(this->begin);
+  xbt_os_sem_acquire(this->end);
+}
+
+}
+}
diff --git a/src/bindings/java/JavaContext.hpp b/src/bindings/java/JavaContext.hpp
new file mode 100644 (file)
index 0000000..fbad882
--- /dev/null
@@ -0,0 +1,69 @@
+/* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef SIMGRID_JAVA_JAVA_CONTEXT_HPP
+#define SIMGRID_JAVA_JAVA_CONTEXT_HPP
+
+#include <jni.h>
+
+#include <xbt/misc.h>
+#include <simgrid/simix.h>
+#include <xbt/xbt_os_thread.h>
+
+#include "src/simix/smx_private.hpp"
+
+#include "jmsg.h"
+#include "jmsg_process.h"
+
+namespace simgrid {
+namespace java {
+
+class JavaContext;
+class JavacontextFactory;
+
+class JavaContext : public simgrid::simix::Context {
+public:
+  // The java process instance bound with the msg process structure:
+  jobject jprocess;
+  // JNI interface pointer associated to this thread:
+  JNIEnv *jenv;
+  xbt_os_thread_t thread;
+  // Sempahore used to schedule/yield the process:
+  xbt_os_sem_t begin;
+  // Semaphore used to schedule/unschedule the process:
+  xbt_os_sem_t end;
+public:
+  friend class JavaContextFactory;
+  JavaContext(xbt_main_func_t code,
+          int argc, char **argv,
+          void_pfn_smxprocess_t cleanup_func,
+          smx_process_t process);
+  ~JavaContext() override;
+  void stop() override;
+  void suspend() override;
+  void resume();
+private:
+  static void* wrapper(void *data);
+};
+
+class JavaContextFactory : public simgrid::simix::ContextFactory {
+public:
+  JavaContextFactory();
+  ~JavaContextFactory() override;
+  JavaContext* self() override;
+  JavaContext* create_context(
+    xbt_main_func_t, int, char **, void_pfn_smxprocess_t,
+    smx_process_t process
+    ) override;
+  void run_all() override;
+};
+
+XBT_PRIVATE simgrid::simix::ContextFactory* java_factory();
+
+}
+}
+
+#endif                          /* !_XBT_CONTEXT_JAVA_H */
similarity index 73%
rename from src/bindings/java/jmsg.c
rename to src/bindings/java/jmsg.cpp
index d588664..ad979be 100644 (file)
@@ -10,8 +10,7 @@
 #include <simgrid/simix.h>
 #include <surf/surfxml_parse.h>
 #include <locale.h>
-
-#include "smx_context_java.h"
+#include <src/simix/smx_private.h>
 
 #include "jmsg_process.h"
 
@@ -24,6 +23,8 @@
 
 #include "jmsg.h"
 
+#include "JavaContext.hpp"
+
 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
 #ifndef JNIEXPORT
 #define JNIEXPORT
@@ -33,6 +34,8 @@
 #endif
 /* end of eclipse-mandated pimple */
 
+SG_BEGIN_DECL()
+
 int JAVA_HOST_LEVEL;
 int JAVA_STORAGE_LEVEL;
 
@@ -51,7 +54,7 @@ JNIEnv *get_current_thread_env(void)
 {
   JNIEnv *env;
 
-  (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
+  __java_vm->AttachCurrentThread((void **) &env, NULL);
   return env;
 }
 
@@ -105,28 +108,28 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   XBT_LOG_CONNECT(jmsg);
   XBT_LOG_CONNECT(jtrace);
 
-  (*env)->GetJavaVM(env, &__java_vm);
+  env->GetJavaVM(&__java_vm);
 
-  smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
-  jthrowable exc = (*env)->ExceptionOccurred(env);
+  simgrid::simix::factory_initializer = simgrid::java::java_factory;
+  jthrowable exc = env->ExceptionOccurred();
   if (exc) {
-    (*env)->ExceptionClear(env);
+    env->ExceptionClear();
   }
 
   setlocale(LC_NUMERIC,"C");
 
   if (jargs)
-    argc = (int) (*env)->GetArrayLength(env, jargs);
+    argc = (int) env->GetArrayLength(jargs);
 
   argc++;
   argv = xbt_new(char *, argc + 1);
   argv[0] = xbt_strdup("java");
 
   for (index = 0; index < argc - 1; index++) {
-    jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
-    tmp = (*env)->GetStringUTFChars(env, jval, 0);
+    jval = (jstring) env->GetObjectArrayElement(jargs, index);
+    tmp = env->GetStringUTFChars(jval, 0);
     argv[index + 1] = xbt_strdup(tmp);
-    (*env)->ReleaseStringUTFChars(env, jval, tmp);
+    env->ReleaseStringUTFChars(jval, tmp);
   }
   argv[argc] = NULL;
 
@@ -139,11 +142,6 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
     free(argv[index]);
 
   free(argv);
-
-  if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
-    XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
-  else
-    xbt_die("Unknown context factory. Please report bug.");
 }
 
 JNIEXPORT void JNICALL
@@ -192,11 +190,11 @@ Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
 {
 
   const char *platformFile =
-      (*env)->GetStringUTFChars(env, jplatformFile, 0);
+      env->GetStringUTFChars(jplatformFile, 0);
 
   MSG_create_environment(platformFile);
 
-  (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
+  env->ReleaseStringUTFChars(jplatformFile, platformFile);
 }
 
 JNIEXPORT jobject JNICALL
@@ -221,44 +219,44 @@ Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_DEBUG("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_VERB("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_INFO("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_WARN("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_ERROR("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
 {
-  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  const char *s = env->GetStringUTFChars(js, 0);
   XBT_CRITICAL("%s", s);
-  (*env)->ReleaseStringUTFChars(env, js, s);
+  env->ReleaseStringUTFChars(js, s);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
@@ -266,7 +264,7 @@ Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
 {
 
   const char *deploymentFile =
-      (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
+      env->GetStringUTFChars(jdeploymentFile, 0);
 
   SIMIX_function_register_default(create_jprocess);
   MSG_launch_application(deploymentFile);
@@ -280,37 +278,41 @@ static int create_jprocess(int argc, char *argv[]) {
   JNIEnv *env = get_current_thread_env();
   //Change the "." in class name for "/".
   xbt_str_subst(argv[0],'.','/',0);
-  jclass class_Process = (*env)->FindClass(env, argv[0]);
+  jclass class_Process = env->FindClass(argv[0]);
   xbt_str_subst(argv[0],'/','.',0);
   //Retrieve the methodID for the constructor
   xbt_assert((class_Process != NULL), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
-  jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
+  jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
 
   //Retrieve the name of the process.
-  jstring jname = (*env)->NewStringUTF(env, argv[0]);
+  jstring jname = env->NewStringUTF(argv[0]);
   //Build the arguments
-  jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
-  (*env)->FindClass(env,"java/lang/String"),
-  (*env)->NewStringUTF(env,"")));
+  jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
+    env->FindClass("java/lang/String"),
+    env->NewStringUTF(""));
   int i;
   for (i = 1; i < argc; i++)
-      (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
+      env->SetObjectArrayElement(args,i - 1,
+        env->NewStringUTF(argv[i]));
   //Retrieve the host for the process.
-  jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
+  jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
   //creates the process
-  jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
+  jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
   xbt_assert((jprocess != NULL), "Process allocation failed.");
-  jprocess = (*env)->NewGlobalRef(env, jprocess);
+  jprocess = env->NewGlobalRef(jprocess);
   //bind the process to the context
   msg_process_t process = MSG_process_self();
-  smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
+  simgrid::java::JavaContext* context =
+    (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
   context->jprocess = jprocess;
-/* sets the PID and the PPID of the process */
-(*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
-(*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
+  /* sets the PID and the PPID of the process */
+  env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
+  env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
   jprocess_bind(jprocess, process, env);
 
   return 0;
 }
+
+SG_END_DECL()
index f101f73..bc8ad54 100644 (file)
 #include <simgrid/msg.h>
 #include <jni.h>
 
-extern int JAVA_HOST_LEVEL;
-extern xbt_lib_t host_lib;
+SG_BEGIN_DECL()
 
+extern int JAVA_HOST_LEVEL;
 extern int JAVA_STORAGE_LEVEL;
-extern xbt_lib_t storage_lib;
 
 JavaVM *get_java_VM(void);
 JNIEnv *get_current_thread_env(void);
@@ -68,4 +67,6 @@ JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
                                        jstring jdeploymentFile);
 
+SG_END_DECL()
+
 #endif                          /* !MSG4JAVA_H */
index 4b82931..c173e33 100644 (file)
@@ -12,7 +12,7 @@
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
 #include "jmsg_storage.h"
-
+#include <surf/surf_routing.h>
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
index 0e03b94..4a82c34 100644 (file)
@@ -12,6 +12,8 @@
 #include <jni.h>
 #include "simgrid/msg.h"
 
+SG_BEGIN_DECL()
+
 /**
  * This function returns a new java host instance.
  *
@@ -224,3 +226,4 @@ Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject
 
 #endif                          /*!MSG_JHOST_H */
 
+SG_END_DECL()
similarity index 86%
rename from src/bindings/java/jmsg_process.c
rename to src/bindings/java/jmsg_process.cpp
index a7b3a3f..f8daf99 100644 (file)
 #include "jmsg.h"
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
-#include "smx_context_java.h"
+#include "JavaContext.hpp"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
+extern "C" {
+
 jfieldID jprocess_field_Process_bind;
 jfieldID jprocess_field_Process_host;
 jfieldID jprocess_field_Process_killTime;
@@ -30,50 +32,52 @@ Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
 
 jobject native_to_java_process(msg_process_t process)
 {
-  return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
+  simgrid::java::JavaContext* context =
+    (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
+  return context->jprocess;
 }
 
 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
 {
-  return (*env)->NewGlobalRef(env, jprocess);
+  return env->NewGlobalRef(jprocess);
 }
 
 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
 {
-  (*env)->DeleteGlobalRef(env, jprocess);
+  env->DeleteGlobalRef(jprocess);
 }
 
 void jprocess_join(jobject jprocess, JNIEnv * env)
 {
        msg_process_t process = jprocess_to_native_process(jprocess,env);
-       smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
+       simgrid::java::JavaContext* context =
+    (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
        xbt_os_thread_join(context->thread,NULL);
 }
 
 msg_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
 {
   return
-    (msg_process_t)(intptr_t)(*env)->GetLongField(env, jprocess,
+    (msg_process_t)(intptr_t)env->GetLongField(jprocess,
                                                   jprocess_field_Process_bind);
 }
 
 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
 {
-  (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind,
+  env->SetLongField(jprocess, jprocess_field_Process_bind,
                        (intptr_t)process);
 }
 
 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
 {
   return
-    (intptr_t)(*env)->GetLongField(env, jprocess, jprocess_field_Process_id);
+    (intptr_t)env->GetLongField(jprocess, jprocess_field_Process_id);
 }
 
 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
 {
-  jstring jname = (jstring) (*env)->GetObjectField(env, jprocess, jprocess_field_Process_name);
-  return (*env)->NewGlobalRef(env, jname);
-
+  jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
+  return (jstring) env->NewGlobalRef(jname);
 }
 
 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
@@ -83,11 +87,11 @@ jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
   if (!id)
     return JNI_FALSE;
 
-  return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
+  return env->GetLongField(jprocess, id) ? JNI_TRUE : JNI_FALSE;
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
-  jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
+  jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
 
   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
@@ -115,7 +119,7 @@ Java_org_simgrid_msg_Process_create(JNIEnv * env,
   msg_process_t process;          /* the native process to create                         */
   msg_host_t host;                /* Where that process lives */
 
-  hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
+  hostname = env->GetStringUTFChars((jstring) jhostname, 0);
 
   /* get the name of the java process */
   jname = jprocess_get_name(jprocess_arg, env);
@@ -141,11 +145,11 @@ Java_org_simgrid_msg_Process_create(JNIEnv * env,
   }
 
   /* build the C name of the process */
-  name = (*env)->GetStringUTFChars(env, jname, 0);
+  name = env->GetStringUTFChars(jname, 0);
   name = xbt_strdup(name);
 
   /* Retrieve the kill time from the process */
-  jdouble jkill = (*env)->GetDoubleField(env, jprocess, jprocess_field_Process_killTime);
+  jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
   /* Actually build the MSG process */
   process = MSG_process_create_with_environment(name,
                                                (xbt_main_func_t) jprocess,
@@ -159,16 +163,16 @@ Java_org_simgrid_msg_Process_create(JNIEnv * env,
 
   /* release our reference to the process name (variable name becomes invalid) */
   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
-  //(*env)->ReleaseStringUTFChars(env, jname, name);
-  (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
+  //env->ReleaseStringUTFChars(jname, name);
+  env->ReleaseStringUTFChars((jstring) jhostname, hostname);
 
   /* sets the PID and the PPID of the process */
-  (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
-  (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
+  env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
+  env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
   /* sets the Host of the process */
-  jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
+  jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL, (jstring)jhostname);
 
-  (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
+  env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
 }
 
 JNIEXPORT jint JNICALL
@@ -206,16 +210,16 @@ Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject
     jxbt_throw_notbound(env, "process", jprocess);
     return NULL;
   }
-  const char *name = (*env)->GetStringUTFChars(env, jname, 0);
+  const char *name = env->GetStringUTFChars((jstring)jname, 0);
 
   const char *property = MSG_process_get_property_value(process, name);
   if (!property) {
     return NULL;
   }
 
-  jobject jproperty = (*env)->NewStringUTF(env, property);
+  jobject jproperty = env->NewStringUTF(property);
 
-  (*env)->ReleaseStringUTFChars(env, jname, name);
+  env->ReleaseStringUTFChars((jstring)jname, name);
 
   return jproperty;
 }
@@ -339,7 +343,7 @@ Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint
 
     //jmsg_throw_status(env,rv);
 
-    // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only 
+    // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
     // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
     // is a cancelled_error, see bindings/java/smx_context_java.c, function void smx_ctx_java_stop(smx_context_t context) and src/msg/msg_gos.c
     // function  msg_error_t MSG_process_sleep(double nb_sec)
@@ -353,7 +357,7 @@ Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
 {
   msg_error_t rv;
   rv = MSG_process_sleep((double)jseconds);
-  if ((*env)->ExceptionOccurred(env))
+  if (env->ExceptionOccurred())
     return;
   if (rv != MSG_OK) {
     XBT_DEBUG("Status NOK");
@@ -399,7 +403,7 @@ Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
     return;
   }
   /* change the host java side */
-  (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
+  env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
@@ -411,3 +415,5 @@ JNIEXPORT jint JNICALL
 Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
   return (jint) MSG_process_get_number();
 }
+
+}
index 974ff4a..7cd3c65 100644 (file)
@@ -13,6 +13,8 @@
 #include <simgrid/msg.h>
 #include <simgrid/simix.h>
 
+SG_BEGIN_DECL();
+
 //Cached java fields
 extern jfieldID jprocess_field_Process_bind;
 extern jfieldID jprocess_field_Process_host;
@@ -264,5 +266,6 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime
 JNIEXPORT jint JNICALL
 Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls);
 
+SG_END_DECL();
 
 #endif                          /* !MSG_JPROCESS_H */
index d3fd8db..aadd0ab 100644 (file)
@@ -6,7 +6,9 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "xbt/str.h"
+#include <xbt/str.h>
+#include <surf/surf_routing.h>
+
 #include "simgrid/msg.h"
 #include "jmsg.h"
 #include "jmsg_storage.h"
index b8048f0..f9352b2 100644 (file)
@@ -8,8 +8,6 @@
 
 #include "jmsg.h"
 
-#include "smx_context_java.h"
-
 #include "jmsg_host.h"
 #include "jmsg_task.h"
 
@@ -19,6 +17,8 @@
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
+jobject native_to_java_process(msg_process_t process);
+
 static jmethodID jtask_method_Comm_constructor;
 
 static jfieldID jtask_field_Task_bind;
index 54130c1..3dd3397 100644 (file)
@@ -12,6 +12,8 @@
 #include "jmsg_process.h"
 #include "jxbt_utilities.h"
 #include "simgrid/msg.h"
+#include <surf/surf_routing.h>
+
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
 static jfieldID jvm_field_bind;
index 015a4f7..972c112 100644 (file)
 /* JNI GETTERS */
 /* *********** */
 
+SG_BEGIN_DECL()
+
 /* Search a class and throw an exception if not found */
 jclass jxbt_get_class(JNIEnv * env, const char *name);
 
 /* Search a method in a class and throw an exception if not found
    (it's ok to to pass a NULL class: it's a noop) */
-jmethodID jxbt_get_jmethod(JNIEnv * env, jclass class,
+jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls,
                            const char *name, const char *signature);
 
 /* Like the jxbt_get_class() but get a static method */
@@ -30,7 +32,7 @@ jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls,
 
 /* Search a field in a class and throw an exception if not found
    (it's ok to to pass a NULL class: it's a noop) */
-jfieldID jxbt_get_jfield(JNIEnv * env, jclass class,
+jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
                          const char *name, const char *signature);
 
 
@@ -96,3 +98,5 @@ void jxbt_throw_task_cancelled(JNIEnv * env, char *details);
 /** Thrown when looking for a storage from name does not lead to anything */
 void jxbt_throw_storage_not_found(JNIEnv * env, const char *invalid_name);
 #endif                          /* ! JXBT_UTILITY_H */
+
+SG_END_DECL()
diff --git a/src/bindings/java/smx_context_java.c b/src/bindings/java/smx_context_java.c
deleted file mode 100644 (file)
index 6d7206d..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-/* context_java - implementation of context switching for java threads */
-
-/* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
- * All rights reserved.                                                     */
-
-/* This program is free software; you can redistribute it and/or modify it
- * under the terms of the license (GNU LGPL) which comes with this package. */
-
-#include <xbt/function_types.h>
-#include <simgrid/simix.h>
-#include <xbt/ex.h>
-#include "smx_context_java.h"
-#include "jxbt_utilities.h"
-#include "xbt/dynar.h"
-#include "../../simix/smx_private.h"
-extern JavaVM *__java_vm;
-
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
-
-static smx_context_t
-smx_ctx_java_factory_create_context(xbt_main_func_t code,
-                                    int argc, char **argv,
-                                    void_pfn_smxprocess_t cleanup_func,
-                                    smx_process_t process);
-
-static void smx_ctx_java_free(smx_context_t context);
-static void smx_ctx_java_suspend(smx_context_t context);
-static void smx_ctx_java_resume(smx_context_t new_context);
-static void smx_ctx_java_runall(void);
-static void* smx_ctx_java_thread_run(void *data);
-void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
-{
-  /* instantiate the context factory */
-  smx_ctx_base_factory_init(factory);
-
-  (*factory)->create_context = smx_ctx_java_factory_create_context;
-  /* Leave default behavior of (*factory)->finalize */
-  (*factory)->free = smx_ctx_java_free;
-  (*factory)->stop = smx_ctx_java_stop;
-  (*factory)->suspend = smx_ctx_java_suspend;
-  (*factory)->runall = smx_ctx_java_runall;
-  (*factory)->name = "ctx_java_factory";
-  //(*factory)->finalize = smx_ctx_base_factory_finalize;
-  (*factory)->self = smx_ctx_java_self;
-  (*factory)->get_process = smx_ctx_base_get_process;
-}
-smx_context_t smx_ctx_java_self(void)
-{
-       return (smx_context_t)xbt_os_thread_get_extra_data();
-}
-
-static smx_context_t
-smx_ctx_java_factory_create_context(xbt_main_func_t code,
-                                    int argc, char **argv,
-                                    void_pfn_smxprocess_t cleanup_func,
-                                    smx_process_t process)
-{
-  static int thread_amount=0;
-  smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
-  thread_amount++;
-  /* If the user provided a function for the process then use it
-     otherwise is the context for maestro */
-  if (code) {
-    if (argc == 0) {
-      context->jprocess = (jobject) code;
-    }
-    else {
-      context->jprocess = NULL;
-    }
-    context->super.cleanup_func = cleanup_func;
-    context->begin = xbt_os_sem_init(0);
-    context->end = xbt_os_sem_init(0);
-
-    context->super.argc = argc;
-    context->super.argv = argv;
-    context->super.code = code;
-
-    TRY {        
-       context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
-    }
-    CATCH_ANONYMOUS {
-      RETHROWF("Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
-               "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
-               thread_amount);
-    }
-  } else {
-       context->thread = NULL;
-    xbt_os_thread_set_extra_data(context);
-  }
-  context->super.process = process;
-  
-  return (smx_context_t) context;
-}
-
-static void* smx_ctx_java_thread_run(void *data) {
-  smx_ctx_java_t context = (smx_ctx_java_t)data;
-  xbt_os_thread_set_extra_data(context);
-  //Attach the thread to the JVM
-  JNIEnv *env;
-  XBT_ATTRIB_UNUSED jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
-  xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
-  context->jenv = get_current_thread_env();
-  //Wait for the first scheduling round to happen.
-  xbt_os_sem_acquire(context->begin);
-  //Create the "Process" object if needed.
-  if (context->super.argc > 0) {
-    context->super.code(context->super.argc, context->super.argv);
-  }
-  else {
-    smx_process_t process = SIMIX_process_self();
-    (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind,
-                         (intptr_t)process);
-  }
-
-  // Adrien, ugly path, just to bypass creation of context at low levels
-  // (i.e such as for the VM migration for instance)
-  if(context->jprocess != NULL){
-       xbt_assert((context->jprocess != NULL), "Process not created...");
-       //wait for the process to be able to begin
-       //TODO: Cache it
-       jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
-       jdouble startTime =  (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
-       if (startTime > MSG_get_clock()) {
-               MSG_process_sleep(startTime - MSG_get_clock());
-       }
-       //Execution of the "run" method.
-       jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
-       xbt_assert( (id != NULL), "Method not found...");
-       (*env)->CallVoidMethod(env, context->jprocess, id);
-  }
-  smx_ctx_java_stop((smx_context_t)context);
-
-  return NULL;
-}
-
-static void smx_ctx_java_free(smx_context_t context)
-{
-  if (context) {
-    smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
-    if (ctx_java->thread) { /* We are not in maestro context */
-      xbt_os_thread_join(ctx_java->thread, NULL);
-      xbt_os_sem_destroy(ctx_java->begin);
-      xbt_os_sem_destroy(ctx_java->end);
-    }
-  }
-  smx_ctx_base_free(context);
-}
-
-
-void smx_ctx_java_stop(smx_context_t context)
-{
-  smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
-  /* I am the current process and I am dying */
-  if (context->iwannadie) {
-    context->iwannadie = 0;
-    JNIEnv *env = get_current_thread_env();
-    XBT_DEBUG("Gonna launch Killed Error");
-    // TODO Adrien, if the process has not been created at the java layer, why should we raise the exception/error at the java level (this happens
-    // for instance during the migration process that creates at the C level two processes: one on the SRC node and one on the DST node, if the DST process is killed. 
-    // it is not required to raise an exception at the JAVA level, the low level should be able to manage such an issue correctly but this is not the case right now unfortunately ...
-    // TODO it will be nice to have the name of the process to help the end-user to know which Process has been killed
-   // jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process %s killed :) (file smx_context_java.c)", MSG_process_get_name( (msg_process_t)context) ));
-    jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process %s killed :) (file smx_context_java.c)", simcall_process_get_name((smx_process_t) SIMIX_context_get_process(context))) );
-    XBT_DEBUG("Trigger a cancel error at the C level");
-    THROWF(cancel_error, 0, "process cancelled");
-  } else {
-    smx_ctx_base_stop(context);
-    /* detach the thread and kills it */
-    JNIEnv *env = ctx_java->jenv;
-    (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
-    XBT_ATTRIB_UNUSED jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
-    xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
-    xbt_os_sem_release(((smx_ctx_java_t)context)->end);
-    xbt_os_thread_exit(NULL);
-  }
-}
-
-static void smx_ctx_java_suspend(smx_context_t context)
-{
-  smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
-  xbt_os_sem_release(ctx_java->end);
-  xbt_os_sem_acquire(ctx_java->begin);
-}
-
-// FIXME: inline those functions
-static void smx_ctx_java_resume(smx_context_t new_context)
-{
-  smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
-  xbt_os_sem_release(ctx_java->begin);
-  xbt_os_sem_acquire(ctx_java->end);
-}
-
-static void smx_ctx_java_runall(void)
-{
-  xbt_dynar_t processes = SIMIX_process_get_runnable();
-  smx_process_t process;
-  unsigned int cursor;
-  xbt_dynar_foreach(processes, cursor, process) {
-    smx_ctx_java_resume(SIMIX_process_get_context(process));
-  }
-}
diff --git a/src/bindings/java/smx_context_java.h b/src/bindings/java/smx_context_java.h
deleted file mode 100644 (file)
index fbeee13..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
- * All rights reserved.                                                     */
-
-/* This program is free software; you can redistribute it and/or modify it
- * under the terms of the license (GNU LGPL) which comes with this package. */
-
-#ifndef _XBT_CONTEXT_JAVA_H
-#define _XBT_CONTEXT_JAVA_H
-
-#include <xbt/misc.h>
-#include <simgrid/simix.h>
-#include <xbt/xbt_os_thread.h>
-
-#include "jmsg.h"
-#include "jmsg_process.h"
-
-SG_BEGIN_DECL()
-
-typedef struct s_smx_ctx_java {
-  s_smx_ctx_base_t super;       /* Fields of super implementation */
-  jobject jprocess;             /* the java process instance bound with the msg process structure */
-  JNIEnv *jenv;                 /* jni interface pointer associated to this thread */
-  xbt_os_thread_t thread;
-  xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
-  xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
-} s_smx_ctx_java_t, *smx_ctx_java_t;
-
-void SIMIX_ctx_java_factory_init(smx_context_factory_t *factory);
-void smx_ctx_java_stop(smx_context_t context);
-smx_context_t smx_ctx_java_self(void);
-SG_END_DECL()
-
-#endif                          /* !_XBT_CONTEXT_JAVA_H */
index 949ad0a..fa4ff1c 100644 (file)
@@ -1,5 +1,5 @@
 $ java -classpath ${classpath:=.} sleep_host_off.Main ${srcdir:=.}/../../examples/platforms/small_platform.xml ${srcdir:=.}/sleep_host_off/sleep_host_off_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (0:@) Using regular java threads. Coroutines could speed your simulation up.
+> [  0.000000] (0:@) Using regular java threads.
 > [  0.000000] (1:sleep_host_off.SleepHostOff@Tremblay) **** **** **** ***** ***** Test Sleep ***** ***** **** **** ****
 > [  0.000000] (1:sleep_host_off.SleepHostOff@Tremblay) Test sleep: Create a process on Jupiter, the process simply make periodic sleep, turn off Jupiter
 > [  0.000000] (2:sleep@Jupiter) I'm not dead
index a28a827..03adb42 100644 (file)
@@ -495,7 +495,7 @@ set(JSURF_JAVA_GENERATED_SRC
 )
 
 set(JMSG_C_SRC
-  src/bindings/java/jmsg.c
+  src/bindings/java/jmsg.cpp
   src/bindings/java/jmsg.h
   src/bindings/java/jmsg_as.c
   src/bindings/java/jmsg_as.h
@@ -505,7 +505,7 @@ set(JMSG_C_SRC
   src/bindings/java/jmsg_file.h
   src/bindings/java/jmsg_host.c
   src/bindings/java/jmsg_host.h
-  src/bindings/java/jmsg_process.c
+  src/bindings/java/jmsg_process.cpp
   src/bindings/java/jmsg_process.h
   src/bindings/java/jmsg_rngstream.c
   src/bindings/java/jmsg_rngstream.h
@@ -517,8 +517,8 @@ set(JMSG_C_SRC
   src/bindings/java/jmsg_vm.h
   src/bindings/java/jxbt_utilities.c
   src/bindings/java/jxbt_utilities.h
-  src/bindings/java/smx_context_java.c
-  src/bindings/java/smx_context_java.h
+  src/bindings/java/JavaContext.cpp
+  src/bindings/java/JavaContext.hpp
   src/bindings/java/jmsg_storage.c
   src/bindings/java/jmsg_storage.h
 )