Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ensures that the native libraries are always loaded before trying to access them
[simgrid.git] / org / simgrid / msg / Process.java
index 264a843..edde2bf 100644 (file)
@@ -1,21 +1,16 @@
 /*
- * $Id$
- *
- * Copyright 2006,2007 Martin Quinson, Malek Cherier           
+ * Copyright 2006-2012 The SimGrid team
  * All right 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. 
+ * (GNU LGPL) which comes with this package.
  */
 
 package org.simgrid.msg;
  
 import java.util.Arrays;
-import java.util.Hashtable;
 import java.util.Vector;
-import java.lang.Runnable;
-import java.util.concurrent.Semaphore;
 
 /**
  * A process may be defined as a code, with some private data, executing 
@@ -54,23 +49,33 @@ public abstract class Process implements Runnable {
         * a native process. Even if this attribute is public you must never
         * access to it. It is set automatically during the build of the object.
         */
-       public long bind;
-
+       private 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;
-
-    public Hashtable<String,String> properties;
-
+       
+       /**
+        * Start time of the process
+        */
+       public double startTime = 0;
+       /**
+        * Kill time of the process
+        */
+       public double killTime = -1;
+       
        /**
         * The name of the process.                                                     
         */
@@ -90,6 +95,7 @@ public abstract class Process implements Runnable {
 
        /** The arguments of the method function of the process. */     
        public Vector<String> args;
+
        
        /**
         * Default constructor (used in ApplicationHandler to initialize it)
@@ -99,7 +105,6 @@ public abstract class Process implements Runnable {
                this.name = null;
                this.bind = 0;
                this.args = new Vector<String>();
-               this.properties = null;
        }
 
 
@@ -151,10 +156,30 @@ 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));
+       }       
+       /**
+        * 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)
@@ -164,9 +189,9 @@ public abstract class Process implements Runnable {
                this.args = new Vector<String>();
                if (null != args)
                        this.args.addAll(Arrays.asList(args));
-               
-               this.properties = new Hashtable<String,String>();
-               
+                               
+               this.startTime = startTime;
+               this.killTime = killTime;               
        }
        /**
         * The natively implemented method to create an MSG process.
@@ -187,23 +212,36 @@ public abstract class Process implements Runnable {
 
        /**
         * This method kill a process.
-        * @param process  the process to 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();
        /**
-        * Resumes a suspended process by resuming the task on which it was
+        * Suspends the process by suspending the task on which it was
         * waiting for the completion.
-        *
-        *
-        */ 
+        * DEPRECATED: use suspend instead.
+        */
+       @Deprecated
+       public void pause() {
+               suspend();
+       }
+       /**
+        * Sets the "auto-restart" flag of the process.
+        */
+       public native void setAutoRestart(boolean autoRestart);
+       /**
+        * Restarts the process from the beginning
+        */
        public native void restart();
+       /**
+        * 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.
         *
@@ -252,6 +290,17 @@ public abstract class Process implements Runnable {
        public int getPPID()  {
                return ppid;
        }
+       /**
+        * @brief Returns the value of a given process property. 
+        */
+       public native String getProperty(String name);
+       
+       /**
+        * 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.
         *
@@ -266,14 +315,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);
        }
        /**
@@ -284,12 +333,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;    
        /**
      *
      */
@@ -302,10 +351,14 @@ public abstract class Process implements Runnable {
        }
     /**
      * 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());
+       }
     }
     
        /**
@@ -328,9 +381,10 @@ public abstract class Process implements Runnable {
                        Msg.info("Unexpected behavior. Stopping now");
                        System.exit(1);
                }
-                catch(ProcessKilledException pk) {
-
+                catch(ProcessKilledError pk) {
+                        
                 }      
+               exit();
        }
 
        /**
@@ -341,12 +395,20 @@ public abstract class Process implements Runnable {
      */
        public abstract void main(String[]args) throws MsgException;
 
-    
+       public native void exit();    
        /**
         * Class initializer, to initialize various JNI stuff
         */
        public static native void nativeInit();
        static {
+               Msg.nativeInit();
                nativeInit();
        }
+       /**
+        * This static method returns the current amount of processes running
+        *
+        * @return                      The count of the running processes
+        */ 
+       public native static int getCount();
+
 }