Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
As the ApplicationHandler class this file doesn't use the the template class Vector...
[simgrid.git] / src / java / simgrid / msg / Process.java
index a3921e6..f9113e0 100644 (file)
@@ -56,7 +56,7 @@ import java.util.*;
  * @since JDK1.5011
  */
  
-public class Process extends Thread
+public abstract class Process extends Thread
 {
     /**
      * This attribute represents a bind between a java process object and
@@ -90,6 +90,9 @@ public class Process extends Thread
      * The arguments of the method function of the process.    
      */
     public Vector args;
+    
+   /* process synchronisation tools */
+       protected Sem schedBegin, schedEnd;
        
     /**
      * Default constructor. (used in ApplicationHandler to initialize it)
@@ -100,6 +103,8 @@ public class Process extends Thread
        this.name = null;
        this.bind = 0;
        this.args = new Vector();
+       schedBegin = new Sem(0);
+       schedEnd = new Sem(0);
     }
 
        
@@ -170,10 +175,18 @@ public class Process extends Thread
 
        if (name==null)
            throw new NullPointerException("Process name cannot be NULL");
-
-       this.args.addAll(Arrays.asList(args));
+       
+       
+       this.args = new Vector();
+       
+       if(null != args)
+               this.args.addAll(Arrays.asList(args));
+               
        this.name = name;
        this.id = nextProcessId++;
+       
+       schedBegin = new Sem(0);
+       schedEnd = new Sem(0);
                
        Msg.processCreate(this,host);
     }
@@ -359,16 +372,28 @@ public class Process extends Thread
     public synchronized void run() {
                
        try {
-           String[] args; /* do not fill it before the signal or this.args will be empty */
+           String[] args = null; /* do not fill it before the signal or this.args will be empty */
            
-           waitSignal(); /* wait for other people to fill the process in */
-
-           args = new String[this.args.size()];
-           this.args.toArray(args);
+           //waitSignal(); /* wait for other people to fill the process in */
+           
+           
+           try {
+           schedBegin.acquire();
+       } catch(InterruptedException e) {
+       }
+       
+                       
+               
+               if(this.args.size() > 0)
+               {
+               
+               args = new String[this.args.size()];
+                       this.args.toArray(args);
+               }
 
-           Msg.info("["+this.name+"/"+this.getHost().getName()+"] Start");
            this.main(args);
            Msg.processExit(this);
+           schedEnd.release();
        } catch (MsgException e) {
            e.printStackTrace();
            Msg.info("Unexpected behavior. Stopping now");
@@ -382,17 +407,44 @@ public class Process extends Thread
      * @exception                      JniException on JNI madness
      */
     private void waitSignal() throws JniException{
-        Msg.waitSignal(this);        
+        Msg.waitSignal(this);
+        unschedule();        
     }
   
     /**
-     * The main function of the process (to override).
-     */
-
-    public void main(String[] args) throws MsgException {
-       
-       // TODO
-       // ALL DERIVED CLASS OF THIS CLASS MUST OVERRIDE THIS METHOD
-    }
+        * The main function of the process (to implement).
+        */
+    public abstract void main(String[] args) 
+    throws JniException, NativeException;
+    
+   
+   public void unschedule() {
+       
+               try {
+                       schedEnd.release();
+                       schedBegin.acquire();
+               } catch(InterruptedException e){
+                       
+               }
+       }
     
+     public void schedule() {
+       
+       try { 
+                       schedBegin.release();
+                       schedEnd.acquire();
+               } catch(InterruptedException e) {
+                       
+               }
+       }
+       
+       public void taskSend(Channel channel,Task task, Host host)  throws NativeException, JniException {
+               Msg.channelPut(channel,task,host);
+       }
+       
+       public Task taskReceive(Channel channel)  throws NativeException, JniException {
+               return Msg.channelGet(channel);
+       }                    
 }
+
+