X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/11b97013a9a29279a4a98b614156acafffcb8ed8..e3b238eaf75f5bbe14b203615c1bdc4f4b5247ed:/org/simgrid/msg/Process.java diff --git a/org/simgrid/msg/Process.java b/org/simgrid/msg/Process.java index 16a471ceb9..264a843cf3 100644 --- a/org/simgrid/msg/Process.java +++ b/org/simgrid/msg/Process.java @@ -14,6 +14,8 @@ 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 @@ -46,7 +48,7 @@ import java.util.Vector; * */ -public abstract class Process extends Thread { +public abstract class Process implements Runnable { /** * This attribute represents a bind between a java process object and * a native process. Even if this attribute is public you must never @@ -67,47 +69,37 @@ public abstract class Process extends Thread { */ public long id; - /** - * - */ public Hashtable properties; /** * The name of the process. */ protected String name; - /** - * - * @return - */ - public String msgName() { - return this.name; - } + /** + * The PID of the process + */ + protected int pid = -1; + /** + * The PPID of the process + */ + protected int ppid = -1; + /** + * The host of the process + */ + protected Host host = null; + /** The arguments of the method function of the process. */ public Vector args; - - /* process synchronization tools */ - /** - * - */ - /** - * - */ - protected Sem schedBegin, schedEnd; - private boolean nativeStop = false; - + /** * Default constructor (used in ApplicationHandler to initialize it) */ protected Process() { - super(); this.id = nextProcessId++; this.name = null; this.bind = 0; this.args = new Vector(); this.properties = null; - schedBegin = new Sem(0); - schedEnd = new Sem(0); } @@ -164,7 +156,7 @@ public abstract class Process extends Thread { public Process(Host host, String name, String[]args) { /* This is the constructor called by all others */ this(); - + this.host = host; if (name == null) throw new NullPointerException("Process name cannot be NULL"); this.name = name; @@ -172,11 +164,15 @@ public abstract class Process extends Thread { this.args = new Vector(); if (null != args) this.args.addAll(Arrays.asList(args)); - - MsgNative.processCreate(this, host); + + this.properties = new Hashtable(); + } - - + /** + * The natively implemented method to create an MSG process. + * @param host A valid (binded) host where create the process. + */ + protected native void create(String hostName) throws HostNotFoundException; /** * This method kills all running process of the simulation. * @@ -187,95 +183,46 @@ public abstract class Process extends Thread { * @return The function returns the PID of the next created process. * */ - public static int killAll(int resetPID) { - return MsgNative.processKillAll(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; - } - /** - * checks if the flag that indicates that this thread must be killed is set to true; if true, starts to kill it. End users should not have to deal with it - * If you develop a new MSG native call, please include a call to interruptedStop() at the beginning of your method code, so as the process can be killed if he call - * your method. - * - * @return - * - */ - public static void ifInterruptedStop() { - if ( (Thread.currentThread() instanceof Process) &&((Process) Thread.currentThread()).getNativeStop()) { - throw new RuntimeException("Interrupted"); - } - } - + public static native int killAll(int resetPID); /** * 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 void pause() { - Process.ifInterruptedStop(); - MsgNative.processSuspend(this); - } + public native void pause(); /** * Resumes a suspended process by resuming the task on which it was * waiting for the completion. * * */ - public void restart() { - Process.ifInterruptedStop(); - MsgNative.processResume(this); - } + public native void restart(); /** * Tests if a process is suspended. * * @return The method returns true if the process is suspended. * Otherwise the method returns false. */ - public boolean isSuspended() { - Process.ifInterruptedStop(); - return MsgNative.processIsSuspended(this); + public native boolean isSuspended(); + /** + * Returns the name of the process + */ + public String msgName() { + return this.name; } /** - * Returns the host of a process. - * + * Returns the host of the process. * @return The host instance of the process. - * - * */ public Host getHost() { - Process.ifInterruptedStop(); - return MsgNative.processGetHost(this); + return this.host; } /** * This static method gets a process from a PID. @@ -286,10 +233,7 @@ public abstract class Process extends Thread { * * @exception NativeException on error in the native SimGrid code */ - public static Process fromPID(int PID) throws NativeException { - Process.ifInterruptedStop(); - return MsgNative.processFromPID(PID); - } + public static native Process fromPID(int PID) throws NativeException; /** * This method returns the PID of the process. * @@ -297,8 +241,7 @@ public abstract class Process extends Thread { * */ public int getPID() { - Process.ifInterruptedStop(); - return MsgNative.processGetPID(this); + return pid; } /** * This method returns the PID of the parent of a process. @@ -307,8 +250,7 @@ public abstract class Process extends Thread { * */ public int getPPID() { - Process.ifInterruptedStop(); - return MsgNative.processGetPPID(this); + return ppid; } /** * This static method returns the currently running process. @@ -316,10 +258,7 @@ public abstract class Process extends Thread { * @return The current process. * */ - public static Process currentProcess() { - Process.ifInterruptedStop(); - return MsgNative.processSelf(); - } + public static native Process currentProcess(); /** * Migrates a process to another host. * @@ -327,26 +266,34 @@ public abstract class Process extends Thread { * @param host The host where to migrate the process. * */ - public static void migrate(Process process, Host host) { - Process.ifInterruptedStop(); - MsgNative.processMigrate(process, host); + public native static void migrate(Process process, 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) { + sleep(millis,0); } + /** + * Makes the current process sleep until millis milliseconds and nanos nanoseconds + * have elapsed. + * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds. + * Overloads Thread.sleep. + * @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); /** * Makes the current process sleep until time seconds have elapsed. - * * @param seconds The time the current process must sleep. - * - * @exception HostFailureException on error in the native SimGrid code */ - public static void waitFor(double seconds) throws HostFailureException { - Process.ifInterruptedStop(); - MsgNative.processWaitFor(seconds); - } - /** + public native void waitFor(double seconds); + /** * */ public void showArgs() { - Process.ifInterruptedStop(); Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" + this.args.size()); for (int i = 0; i < this.args.size(); i++) @@ -354,29 +301,21 @@ public abstract class Process extends Thread { "] args[" + i + "]=" + (String) (this.args.get(i))); } /** - * Let the simulated process sleep for the given amount of millisecond in the simulated world. - * - * You don't want to use sleep instead, because it would freeze your simulation - * run without any impact on the simulated world. - * @param millis + * This method actually creates and run the process. + * @throws HostNotFoundException */ - public native void simulatedSleep(double seconds); - + public void start() throws HostNotFoundException { + create(host.getName()); + } + /** * This method runs the process. Il calls the method function that you must overwrite. */ public void run() { - String[]args = null; /* 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 */ - - try { - schedBegin.acquire(); - } catch(InterruptedException e) { - } - try { args = new String[this.args.size()]; if (this.args.size() > 0) { @@ -384,26 +323,14 @@ public abstract class Process extends Thread { } this.main(args); - MsgNative.processExit(this); - schedEnd.release(); } catch(MsgException e) { e.printStackTrace(); Msg.info("Unexpected behavior. Stopping now"); System.exit(1); } - catch(RuntimeException re) { - if (nativeStop) - { - MsgNative.processExit(this); - Msg.info(" Process " + ((Process) Thread.currentThread()).msgName() + " has been killed."); - schedEnd.release(); - } - else { - re.printStackTrace(); - Msg.info("Unexpected behavior. Stopping now"); - System.exit(1); - } - } + catch(ProcessKilledException pk) { + + } } /** @@ -414,106 +341,12 @@ public abstract class Process extends Thread { */ public abstract void main(String[]args) throws MsgException; - - /** - * - */ - public void unschedule() { - //Process.ifInterruptedStop(); - try { - schedEnd.release(); - schedBegin.acquire(); - } catch (InterruptedException e) { - } - } - - /** - * - */ - public void schedule() { - //System.err.println("Scheduling process in Java"); - //Process.ifInterruptedStop(); - try { - schedBegin.release(); - schedEnd.acquire(); - } catch(InterruptedException e) { - System.err.println("Got an interuption while scheduling process in Java"); - e.printStackTrace(); - } - } - - /** Send the given task in the mailbox associated with the specified alias (waiting at most given time) - * @param mailbox - * @param task - * @param timeout - * @throws TimeoutException - * @throws HostFailureException - * @throws TransferFailureException */ - public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - MsgNative.taskSend(mailbox, task, timeout); - } - - /** Send the given task in the mailbox associated with the specified alias - * @param mailbox - * @param task - * @throws TimeoutException - * @throws HostFailureException - * @throws TransferFailureException */ - public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - MsgNative.taskSend(mailbox, task, -1); - } - - /** Receive a task on mailbox associated with the specified mailbox - * @param mailbox - * @return - * @throws TransferFailureException - * @throws HostFailureException - * @throws TimeoutException - */ - public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - return MsgNative.taskReceive(mailbox, -1.0, null); - } - - /** Receive a task on mailbox associated with the specified alias (waiting at most given time) - * @param mailbox - * @param timeout - * @return - * @throws TransferFailureException - * @throws HostFailureException - * @throws TimeoutException - */ - public Task taskReceive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - return MsgNative.taskReceive(mailbox, timeout, null); - } - - /** Receive a task on mailbox associated with the specified alias from given sender - * @param mailbox - * @param host - * @param timeout - * @return - * @throws TransferFailureException - * @throws HostFailureException - * @throws TimeoutException - */ - public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - return MsgNative.taskReceive(mailbox, timeout, host); - } - - /** Receive a task on mailbox associated with the specified alias from given sender - * @param mailbox - * @param host - * @return - * @throws TransferFailureException - * @throws HostFailureException - * @throws TimeoutException - */ - public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException { - Process.ifInterruptedStop(); - return MsgNative.taskReceive(mailbox, -1.0, host); + + /** + * Class initializer, to initialize various JNI stuff + */ + public static native void nativeInit(); + static { + nativeInit(); } }