Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add resume/suspend. Make restart/pause deprecated
[simgrid.git] / org / simgrid / msg / Process.java
index 85d822f..b726b8d 100644 (file)
@@ -55,19 +55,31 @@ public abstract class Process implements Runnable {
         * access to it. It is set automatically during the build of the object.
         */
        public long bind;
-
+       /**
+        * Indicates if the process is started
+        */
+       boolean started;
        /**
         * Even if this attribute is public you must never access to it.
         * It is used to compute the id of an MSG process.
         */
        public static long nextProcessId = 0;
-
+       
        /**
         * Even if this attribute is public you must never access to it.
         * It is compute automatically during the creation of the object. 
         * The native functions use this identifier to synchronize the process.
         */
        public long id;
+       
+       /**
+        * Start time of the process
+        */
+       public double startTime = 0;
+       /**
+        * Kill time of the process
+        */
+       public double killTime = -1;
 
     public Hashtable<String,String> properties;
 
@@ -91,10 +103,7 @@ public abstract class Process implements Runnable {
        /** The arguments of the method function of the process. */     
        public Vector<String> args;
 
-       /* process synchronization tools */
        
-    private boolean nativeStop = false;
-
        /**
         * Default constructor (used in ApplicationHandler to initialize it)
         */
@@ -155,10 +164,32 @@ public abstract class Process implements Runnable {
         * @param host                  The host of the process to create.
         * @param name                  The name of the process.
         * @param args                  The arguments of main method of the process.
+        */     
+       public Process(Host host, String name, String[]args) {
+               this();
+               this.host = host;
+               if (name == null)
+                       throw new NullPointerException("Process name cannot be NULL");
+               this.name = name;
+
+               this.args = new Vector<String>();
+               if (null != args)
+                       this.args.addAll(Arrays.asList(args));
+               
+               this.properties = new Hashtable<String,String>();
+       }       
+       /**
+        * Constructs a new process from a host and his name, the arguments of here method function are
+        * specified by the parameter args.
+        *
+        * @param host                  The host of the process to create.
+        * @param name                  The name of the process.
+        * @param args                  The arguments of main method of the process.
+        * @param startTime             Start time of the process
+        * @param killTime              Kill time of the process
         *
         */
-       public Process(Host host, String name, String[]args) {
-               /* This is the constructor called by all others */
+       public Process(Host host, String name, String[]args, double startTime, double killTime) {
                this();
                this.host = host;
                if (name == null)
@@ -171,6 +202,8 @@ public abstract class Process implements Runnable {
                
                this.properties = new Hashtable<String,String>();
                
+               this.startTime = startTime;
+               this.killTime = killTime;               
        }
        /**
         * The natively implemented method to create an MSG process.
@@ -188,48 +221,40 @@ public abstract class Process implements Runnable {
         *                      
         */ 
        public static native int killAll(int resetPID);
-       /**
-        * This method sets a flag to indicate that this thread must be killed. End user must use static method kill
-        *
-        * @return                              
-        *                      
-        */ 
-       public void nativeStop() {
-               nativeStop = true;
-       }
-       /**
-        * getter for the flag that indicates that this thread must be killed
-        *
-        * @return                              
-        *                      
-        */ 
-       public boolean getNativeStop() {
-               return nativeStop;
-       }
 
        /**
         * This method kill a process.
-        * @param process  the process to be killed.
         *
         */
-       public void kill() {
-                nativeStop();
-                Msg.info("Process " + msgName() + " will be killed.");                                                  
-       }
-
+       public native void kill();
        /**
         * Suspends the process by suspending the task on which it was
         * waiting for the completion.
-        *
         */
-       public native void pause();
+       public native void suspend();
+       /**
+        * Suspends the process by suspending the task on which it was
+        * waiting for the completion.
+        * DEPRECATED: use suspend instead.
+        */
+       @Deprecated
+       public void pause() {
+               suspend();
+       }
        /**
         * Resumes a suspended process by resuming the task on which it was
         * waiting for the completion.
-        *
-        *
+        * DEPRECATED: use resume instead.
         */ 
-       public native void restart();
+       @Deprecated
+       public void restart() {
+               resume();
+       }
+       /**
+        * Resumes a suspended process by resuming the task on which it was
+        * waiting for the completion.
+        */
+       public native void resume();    
        /**
         * Tests if a process is suspended.
         *
@@ -278,6 +303,12 @@ public abstract class Process implements Runnable {
        public int getPPID()  {
                return ppid;
        }
+       /**
+        * Set the kill time of the process
+        * @param killTime the time when the process is killed
+        */
+       public native void setKillTime(double killTime);
+       
        /**
         * This static method returns the currently running process.
         *
@@ -285,11 +316,6 @@ public abstract class Process implements Runnable {
         *
         */ 
        public static native Process currentProcess();
-       /**
-        * Kills a MSG process
-        * @param process Valid java process to kill
-        */
-       final static native void kill(Process process); 
        /**
         * Migrates a process to another host.
         *
@@ -297,14 +323,14 @@ public abstract class Process implements Runnable {
         * @param host                  The host where to migrate the process.
         *
         */
-       public native static void migrate(Process process, Host host);  
+       public native void migrate(Host host);  
        /**
         * Makes the current process sleep until millis millisecondes have elapsed.
         * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
         * FIXME: Not optimal, maybe we should have two native functions.
         * @param millis the length of time to sleep in milliseconds.
         */
-       public static void sleep(long millis)  {
+       public static void sleep(long millis) throws HostFailureException  {
                sleep(millis,0);
        }
        /**
@@ -315,12 +341,12 @@ public abstract class Process implements Runnable {
         * @param millis the length of time to sleep in milliseconds.
         * @param nanos additionnal nanoseconds to sleep.
         */
-       public native static void sleep(long millis, int nanos);
+       public native static void sleep(long millis, int nanos) throws HostFailureException;
        /**
         * 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);    
+       public native void waitFor(double seconds) throws HostFailureException;    
        /**
      *
      */
@@ -331,16 +357,16 @@ public abstract class Process implements Runnable {
                        Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
                                        "] args[" + i + "]=" + (String) (this.args.get(i)));
        }
-    /**
-     * Exit the process
-     */
-    public native void exit();
     /**
      * This method actually creates and run the process.
+     * It is a noop if the process is already launched.
      * @throws HostNotFoundException
      */
-    public void start() throws HostNotFoundException {
-       create(host.getName());
+    public final void start() throws HostNotFoundException {
+       if (!started) {
+               started = true;
+               create(host.getName());
+       }
     }
     
        /**
@@ -358,35 +384,14 @@ public abstract class Process implements Runnable {
                        }
 
                        this.main(args);
-                       exit();
                } catch(MsgException e) {
                        e.printStackTrace();
                        Msg.info("Unexpected behavior. Stopping now");
                        System.exit(1);
                }
-                catch(ProcessKilled pk) {
-                       if (nativeStop) {
-                               try {
-                                       exit();
-                               } catch (ProcessKilled pk2) {
-                                       /* Ignore that other exception that *will* occur all the time. 
-                                        * This is because the C mechanic gives the control to the now-killed process 
-                                        * so that it does some garbage collecting on its own. When it does so here, 
-                                        * the Java thread checks when starting if it's supposed to be killed (to inform 
-                                        * the C world). To avoid the infinite loop or anything similar, we ignore that 
-                                        * exception now. This should be ok since we ignore only a very specific exception 
-                                        * class and not a generic (such as any RuntimeException).
-                                        */
-                                       //System.err.println(currentThread().getName()+": I ignore that other exception");                                      
-                               }
-                               //Msg.info(" Process " + ((Process) Thread.currentThread()).msgName() + " has been killed.");                                           
-                       }
-                       else {
-                               pk.printStackTrace();
-                               Msg.info("Unexpected behavior. Stopping now");
-                               System.exit(1);
-                       }
-               }       
+                catch(ProcessKilledError pk) {
+                        
+                }      
        }
 
        /**