Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #17 from mpoquet/master
[simgrid.git] / src / bindings / java / org / simgrid / msg / Task.java
index c76e911..46f568a 100644 (file)
@@ -25,16 +25,10 @@ public class Task {
 
        private double messageSize;
 
 
        private double messageSize;
 
-       static private Long idCpt = 0L;
-
-       private Long id;
-
        /** Default constructor (all fields to 0 or null) */
        public Task() {
                create(null, 0, 0);
                this.messageSize = 0;
        /** Default constructor (all fields to 0 or null) */
        public Task() {
                create(null, 0, 0);
                this.messageSize = 0;
-               setId(idCpt);
-               idCpt++;
        }
 
        /* *              * *
        }
 
        /* *              * *
@@ -48,17 +42,15 @@ public class Task {
         *
         * @param flopsAmount   A value of the processing amount (in flop) needed to process the task. 
         *                              If 0, then it cannot be executed with the execute() method.
         *
         * @param flopsAmount   A value of the processing amount (in flop) needed to process the task. 
         *                              If 0, then it cannot be executed with the execute() method.
-        *                              This value has to be >= 0.
+        *                              This value has to be ≥ 0.
         *
         * @param bytesAmount           A value of amount of data (in bytes) needed to transfert this task.
         *                              If 0, then it cannot be transfered with the get() and put() methods.
         *
         * @param bytesAmount           A value of amount of data (in bytes) needed to transfert this task.
         *                              If 0, then it cannot be transfered with the get() and put() methods.
-        *                              This value has to be >= 0.
+        *                              This value has to be ≥ 0.
         */ 
        public Task(String name, double flopsAmount, double bytesAmount) {
                create(name, flopsAmount, bytesAmount);
                this.messageSize = bytesAmount;
         */ 
        public Task(String name, double flopsAmount, double bytesAmount) {
                create(name, flopsAmount, bytesAmount);
                this.messageSize = bytesAmount;
-               setId(idCpt);
-               idCpt++;
        }
        /**
         * Construct an new parallel task with the specified processing amount and amount for each host
        }
        /**
         * Construct an new parallel task with the specified processing amount and amount for each host
@@ -72,7 +64,7 @@ public class Task {
        public Task(String name, Host[]hosts, double[]flopsAmount, double[]bytesAmount) {
                parallelCreate(name, hosts, flopsAmount, bytesAmount);
        }
        public Task(String name, Host[]hosts, double[]flopsAmount, double[]bytesAmount) {
                parallelCreate(name, hosts, flopsAmount, bytesAmount);
        }
-       
+
        /**
         * The natively implemented method to create a MSG task.
         *
        /**
         * The natively implemented method to create a MSG task.
         *
@@ -89,7 +81,7 @@ public class Task {
        private final native void create(String name,
                        double flopsAmount,
                        double bytesAmount)
        private final native void create(String name,
                        double flopsAmount,
                        double bytesAmount)
-       throws IllegalArgumentException;                
+                                       throws IllegalArgumentException;                
        /**
         * The natively implemented method to create a MSG parallel task.
         *
        /**
         * The natively implemented method to create a MSG parallel task.
         *
@@ -104,21 +96,21 @@ public class Task {
                        Host[]hosts,
                        double[]flopsAmount,
                        double[]bytesAmount)
                        Host[]hosts,
                        double[]flopsAmount,
                        double[]bytesAmount)
-       throws NullPointerException, IllegalArgumentException;
+                                       throws NullPointerException, IllegalArgumentException;
        /* *                   * *
         * * Getters / Setters * *
         * *                   * */
        /* *                   * *
         * * Getters / Setters * *
         * *                   * */
-    /** Gets the name of a task */
+       /** Gets the name of a task */
        public String getName() {
                return name;
        }
        public String getName() {
                return name;
        }
-       
+
        /** Gets the sender of the task (or null if not sent yet) */
        public native Process getSender();
        /** Gets the sender of the task (or null if not sent yet) */
        public native Process getSender();
-       
+
        /** Gets the source of the task (or null if not sent yet). */
        public native Host getSource();   
        /** Gets the source of the task (or null if not sent yet). */
        public native Host getSource();   
-       
+
        /** Gets the remaining amount of flops to execute in this task
         * 
         * If it's ongoing, you get the exact amount at the present time. If it's already done, it's 0.
        /** Gets the remaining amount of flops to execute in this task
         * 
         * If it's ongoing, you get the exact amount at the present time. If it's already done, it's 0.
@@ -138,7 +130,7 @@ public class Task {
         * @param priority      The new priority of the task.
         */ 
        public native void setPriority(double priority);
         * @param priority      The new priority of the task.
         */ 
        public native void setPriority(double priority);
-       
+
        /** Set the computation amount needed to process the task
         * 
         * Warning if the execution is already started and ongoing, this call does nothing.
        /** Set the computation amount needed to process the task
         * 
         * Warning if the execution is already started and ongoing, this call does nothing.
@@ -157,94 +149,95 @@ public class Task {
         * *                     * */
        /**
         * Executes a task on the location on which the current process is running.
         * *                     * */
        /**
         * Executes a task on the location on which the current process is running.
-     *
-     * @throws HostFailureException
-     * @throws TaskCancelledException
-     */
+        *
+        * @throws HostFailureException
+        * @throws TaskCancelledException
+        */
        public native void execute() throws HostFailureException,TaskCancelledException;
        public native void execute() throws HostFailureException,TaskCancelledException;
-       
+
        /** Bound a computation to a certain load */
        public native void setBound(double load); 
 
        /** Cancels a task. */ 
        public native void cancel();
 
        /** Bound a computation to a certain load */
        public native void setBound(double load); 
 
        /** Cancels a task. */ 
        public native void cancel();
 
-       /** Deletes a task.
-        *
-        * @exception                   NativeException if the destruction failed.
-        */ 
-       protected void finalize() throws NativeException {
-               destroy();
+       /** Deletes a task once the garbage collector reclaims it */
+       @Override
+       protected void finalize() {
+               try {
+                       // Exceptions in finalizers lead to bad situations:
+                       // http://stackoverflow.com/questions/7644556/troubleshooting-a-java-memory-leak-finalization
+                       nativeFinalize();
+                       bind=0; // to avoid segfaults if the impossible happens yet again making this task surviving its finalize()
+               } catch (Throwable e) {
+                       e.printStackTrace();
+               }
        }
        }
-       /**
-        * The natively implemented method to destroy a MSG task.
-        */
-       protected native void destroy();
+       protected native void nativeFinalize();
        /* *                       * *
         * * Communication-related * *
         * *                       * */
 
        /* *                       * *
         * * Communication-related * *
         * *                       * */
 
-       /** Send the task asynchronously on the mailbox identified by the specified name
+       /** Send the task asynchronously on the specified mailbox
         *  with no way to retrieve whether the communication succeeded or not
         * 
         */
        public native void dsendBounded(String mailbox, double maxrate);
 
 
         *  with no way to retrieve whether the communication succeeded or not
         * 
         */
        public native void dsendBounded(String mailbox, double maxrate);
 
 
-       /** Send the task asynchronously on the mailbox identified by the specified name
+       /** Send the task asynchronously on the specified mailbox
         *  with no way to retrieve whether the communication succeeded or not
         * 
         */
        public native void dsend(String mailbox);
         *  with no way to retrieve whether the communication succeeded or not
         * 
         */
        public native void dsend(String mailbox);
-       
+
        /**
        /**
-        * Sends the task on the mailbox identified by the specified name 
+        * Sends the task on the specified mailbox 
         *
         *
-     * @param mailbox
-     * @throws TimeoutException
+        * @param mailbox where to send the message
+        * @throws TimeoutException
         * @throws HostFailureException 
         * @throws TransferFailureException 
         */
         * @throws HostFailureException 
         * @throws TransferFailureException 
         */
-       public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
+       public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException, NativeException {
                send(mailbox, -1);
        } 
 
        /**
                send(mailbox, -1);
        } 
 
        /**
-        * Sends the task on the mailbox identified by the specified name (wait at most \a timeout seconds)
+        * Sends the task on the specified mailbox (wait at most \a timeout seconds)
         *
         *
-     * @param mailbox
-     * @param timeout
-     * @exception  NativeException if the retrieval fails.
+        * @param mailbox where to send the message
+        * @param timeout
+        * @exception  NativeException if the retrieval fails.
         * @throws TimeoutException 
         * @throws HostFailureException 
         * @throws TransferFailureException 
         */
         * @throws TimeoutException 
         * @throws HostFailureException 
         * @throws TransferFailureException 
         */
-       public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
-       /**
-        * Sends the task on the mailbox identified by the specified alias  (capping the sending rate to \a maxrate) 
+       public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException, NativeException;
+
+       /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate) 
         *
         *
-     * @param alias
-     * @param maxrate 
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
+        * @param mailbox where to send the message
+        * @param maxrate 
+        * @throws TransferFailureException
+        * @throws HostFailureException
+        * @throws TimeoutException
         */
         */
-       public void sendBounded(String alias, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
-              sendBounded(alias,-1,maxrate);
-        }
+       public void sendBounded(String mailbox, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
+               sendBounded(mailbox,-1,maxrate);
+       }
 
 
 
 
-/**
-        * Sends the task on the mailbox identified by the specified alias  (capping the sending rate to \a maxrate) with a timeout
+       /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate) with a timeout
         *
         *
-     * @param alias
-     * @param timeout
-     * @param maxrate 
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
+        * @param mailbox where to send the message
+        * @param timeout
+        * @param maxrate 
+        * @throws TransferFailureException
+        * @throws HostFailureException
+        * @throws TimeoutException
         */
         */
-       public native void sendBounded(String alias, double timeout, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException;
+       public native void sendBounded(String mailbox, double timeout, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException;
 
 
        /**
 
 
        /**
@@ -256,7 +249,7 @@ public class Task {
         * Sends the task on the mailbox asynchronously (capping the sending rate to \a maxrate)
         */
        public native Comm isendBounded(String mailbox, double maxrate);
         * Sends the task on the mailbox asynchronously (capping the sending rate to \a maxrate)
         */
        public native Comm isendBounded(String mailbox, double maxrate);
-       
+
 
        /**
         * Starts listening for receiving a task from an asynchronous communication
 
        /**
         * Starts listening for receiving a task from an asynchronous communication
@@ -266,7 +259,7 @@ public class Task {
        /**
         * Retrieves next task from the mailbox identified by the specified name
         *
        /**
         * Retrieves next task from the mailbox identified by the specified name
         *
-     * @param mailbox
+        * @param mailbox
         */
 
        public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
         */
 
        public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
@@ -276,8 +269,8 @@ public class Task {
        /**
         * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
         *
        /**
         * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
         *
-     * @param mailbox
-     * @param timeout
+        * @param mailbox
+        * @param timeout
         */
        public static Task receive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
                return receive(mailbox, timeout, null);
         */
        public static Task receive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
                return receive(mailbox, timeout, null);
@@ -286,8 +279,8 @@ public class Task {
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias 
         *
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias 
         *
-     * @param mailbox
-     * @param host
+        * @param mailbox
+        * @param host
         */
 
        public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
         */
 
        public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
@@ -297,9 +290,9 @@ public class Task {
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
         *
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
         *
-     * @param mailbox
-     * @param timeout 
-     * @param host
+        * @param mailbox
+        * @param timeout 
+        * @param host
         */
        public native static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException;
 
         */
        public native static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException;
 
@@ -311,7 +304,7 @@ public class Task {
        /**
         * Retrieves next task from the mailbox identified by the specified name with a capped rate
         *
        /**
         * Retrieves next task from the mailbox identified by the specified name with a capped rate
         *
-     * @param mailbox
+        * @param mailbox
         */
 
        public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
         */
 
        public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
@@ -321,8 +314,8 @@ public class Task {
        /**
         * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate
         *
        /**
         * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate
         *
-     * @param mailbox
-     * @param timeout
+        * @param mailbox
+        * @param timeout
         */
        public static Task receiveBounded(String mailbox, double timeout, double rate) throws  TransferFailureException, HostFailureException, TimeoutException {
                return receiveBounded(mailbox, timeout, null, rate);
         */
        public static Task receiveBounded(String mailbox, double timeout, double rate) throws  TransferFailureException, HostFailureException, TimeoutException {
                return receiveBounded(mailbox, timeout, null, rate);
@@ -331,8 +324,8 @@ public class Task {
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate
         *
        /**
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate
         *
-     * @param mailbox
-     * @param host
+        * @param mailbox
+        * @param host
         */
 
        public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
         */
 
        public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
@@ -343,28 +336,28 @@ public class Task {
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
         * with a capped rate
         *
         * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
         * with a capped rate
         *
-     * @param mailbox
-     * @param timeout 
-     * @param host
+        * @param mailbox
+        * @param timeout 
+        * @param host
         */
        public native static Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException;
 
         */
        public native static Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException;
 
-       
-       
+
+
        /**
         * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
        /**
         * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
-     */
+        */
        public native static int listenFrom(String mailbox);
        /**
         * Listen whether there is a waiting task on the mailbox identified by the specified alias
        public native static int listenFrom(String mailbox);
        /**
         * Listen whether there is a waiting task on the mailbox identified by the specified alias
-     */
+        */
        public native static boolean listen(String mailbox);
 
        /**
         * Counts the number of tasks waiting to be received on the \a mailbox identified by the specified alia and sended by the specified \a host.
        public native static boolean listen(String mailbox);
 
        /**
         * Counts the number of tasks waiting to be received on the \a mailbox identified by the specified alia and sended by the specified \a host.
-     */
+        */
        public native static int listenFromHost(String alias, Host host);
        public native static int listenFromHost(String alias, Host host);
-       
+
        /**
         * Class initializer, to initialize various JNI stuff
         */
        /**
         * Class initializer, to initialize various JNI stuff
         */
@@ -377,12 +370,4 @@ public class Task {
        public double getMessageSize() {
                return this.messageSize;
        }
        public double getMessageSize() {
                return this.messageSize;
        }
-
-       public Long getId() {
-               return id;
-       }
-
-       public void setId(Long id) {
-               this.id = id;
-       }
 }
 }