Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill ALL trailing whitespace (and deprecate a function in Link)
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
index 055939e..80c31a7 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2006-2018. 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. */
@@ -9,20 +9,20 @@ import java.util.Arrays;
 import java.util.ArrayList;
 
 /**
- * A process may be defined as a code, with some private data, executing 
+ * A process may be defined as a code, with some private data, executing
  * in a location (host). All the process used by your simulation must be
  * declared in the deployment file (XML format).
- * To create your own process you must inherit your own process from this 
- * class and override the method "main()". For example if you want to use 
+ * To create your own process you must inherit your own process from this
+ * class and override the method "main()". For example if you want to use
  * a process named Slave proceed as it :
  *
  * (1) import the class Process of the package simgrid.msg
  * import simgrid.msg.Process;
- * 
+ *
  * public class Slave extends simgrid.msg.Process {
  *
  *  (2) Override the method function
- * 
+ *
  *  \verbatim
  *     public void main(String[] args) {
  *             System.out.println("Hello MSG");
@@ -33,10 +33,10 @@ import java.util.ArrayList;
  * For the example, for the previous process Slave this file must contains a line :
  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
  * Slave. All the process of your simulation are automatically launched and managed by Msg.
- * A process use tasks to simulate communications or computations with another process. 
- * For more information see Task. For more information on host concept 
+ * A process use tasks to simulate communications or computations with another process.
+ * For more information see Task. For more information on host concept
  * see Host.
- * 
+ *
  */
 
 public abstract class Process implements Runnable {
@@ -70,7 +70,7 @@ public abstract class Process implements Runnable {
         * @param name                  The name of the process.
         *
         * @exception                   HostNotFoundException  if no host with this name exists.
-        *                      
+        *
         *
         */
        public Process(String hostname, String name) throws HostNotFoundException {
@@ -86,12 +86,12 @@ public abstract class Process implements Runnable {
         *
         * @exception                   HostNotFoundException  if no host with this name exists.
         *
-        */ 
+        */
        public Process(String hostname, String name, String[] args) throws HostNotFoundException {
                this(Host.getByName(hostname), name, args);
        }
        /**
-        * Constructs a new process from a host and his name. The method function of the 
+        * Constructs a new process from a host and his name. The method function of the
         * process doesn't have argument.
         *
         * @param host                  Where to create the process.
@@ -107,9 +107,9 @@ public abstract class Process implements Runnable {
         *
         * @param host                  Where to create the process.
         * @param name                  The name of the process.
-        * @param args                  The arguments of main method of the process.
+        * @param argsParam             The arguments of main method of the process.
         */     
-       public Process(Host host, String name, String[]args
+       public Process(Host host, String name, String[]argsParam)
        {
                if (host == null)
                        throw new IllegalArgumentException("Cannot create a process on the null host");
@@ -120,8 +120,8 @@ public abstract class Process implements Runnable {
                this.name = name;
 
                this.args = new ArrayList<>();
-               if (null != args)
-                       this.args.addAll(Arrays.asList(args));
+               if (null != argsParam)
+                       this.args.addAll(Arrays.asList(argsParam));
        }
        /**
         * Constructs a new process from a host and his name, the arguments of here method function are
@@ -148,14 +148,10 @@ public abstract class Process implements Runnable {
        /**
         * This method kills all running process of the simulation.
         *
-        * @param resetPID              Should we reset the PID numbers. A negative number means no reset
-        *                                              and a positive number will be used to set the PID of the next newly
-        *                                              created process.
-        *
         * @return                              The function returns the PID of the next created process.
         *                      
-        */ 
-       public static native int killAll(int resetPID);
+        */
+       public static native int killAll();
 
        /** Simply kills the receiving process.
         *
@@ -182,7 +178,7 @@ public abstract class Process implements Runnable {
        
        /**
         * Specify whether the process should restart when its host restarts after a failure
-        * 
+        *
         * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
         * Otherwise, it just disappears when the host stops.
         */
@@ -198,7 +194,7 @@ public abstract class Process implements Runnable {
        /**
         * Returns the host of the process.
         * @return                              The host instance of the process.
-        */ 
+        */
        public Host getHost() {
                return this.host;
        }
@@ -208,28 +204,33 @@ public abstract class Process implements Runnable {
         * @param pid                   The process identifier of the process to get.
         *
         * @return                              The process with the specified PID.
-        */ 
+        */
        public static native Process fromPID(int pid);
        /**
         * This method returns the PID of the process.
         *
         * @return                              The PID of the process.
         *
-        */ 
+        */
        public int getPID()  {
+               if (pid == -1) // Don't traverse the JNI barrier if you already have the answer
+                       pid = nativeGetPID();
                return pid;
        }
+       // This should not be used: the PID is supposed to be initialized from the C directly when the actor is created,
+       // but this sometimes fail, so let's play nasty but safe here.
+       private native int nativeGetPID();
        /**
         * This method returns the PID of the parent of a process.
         *
         * @return                              The PID of the parent of the process.
         *
-        */ 
+        */
        public int getPPID()  {
                return ppid;
        }
        /**
-        * Returns the value of a given process property. 
+        * Returns the value of a given process property.
         */
        public native String getProperty(String name);
 
@@ -244,7 +245,7 @@ public abstract class Process implements Runnable {
         *
         * @return                              The current process.
         *
-        */ 
+        */
        public static native Process getCurrentProcess();
        /**
         * Migrates a process to another host.
@@ -263,9 +264,9 @@ public abstract class Process implements Runnable {
                sleep(millis,0);
        }
        /**
-        * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
+        * Makes the current process sleep until millis milliseconds and nanos nanoseconds
         * have elapsed.
-        * Unlike {@link #waitFor(double)} which takes seconds, this method takes 
+        * Unlike {@link #waitFor(double)} which takes seconds, this method takes
         * milliseconds and nanoseconds.
         * Overloads Thread.sleep.
         * @param millis the length of time to sleep in milliseconds.
@@ -275,12 +276,11 @@ public abstract class Process implements Runnable {
        /**
         * Makes the current process sleep until time seconds have elapsed.
         * @param seconds               The time the current process must sleep.
-        */ 
-       public native void waitFor(double seconds) throws HostFailureException;    
+        */
+       public native void waitFor(double seconds) throws HostFailureException;
        /**
         * This method actually creates and run the process.
         * It is a noop if the process is already launched.
-        * @throws HostNotFoundException
         */
        public final void start() {
           if (bind == 0)
@@ -291,15 +291,11 @@ public abstract class Process implements Runnable {
        @Override
        public void run() {
 
-               String[] args = null;      /* do not fill it before the signal or this.args will be empty */
-
                try {
-                       args = new String[this.args.size()];
-                       if (!this.args.isEmpty()) {
-                               this.args.toArray(args);
-                       }
+                       String[] argsArray = new String[this.args.size()];
+                       this.args.toArray(argsArray);
 
-                       this.main(args);
+                       this.main(argsArray);
                }
                catch(MsgException e) {
                        e.printStackTrace();
@@ -335,7 +331,7 @@ public abstract class Process implements Runnable {
         * This static method returns the current amount of processes running
         *
         * @return                      The count of the running processes
-        */ 
+        */
        public static native int getCount();
 
 }