Changeset 4081


Ignore:
Timestamp:
Nov 9, 2009, 2:36:48 PM (11 years ago)
Author:
yura
Message:
  • Library Parallel.
Location:
to-imperative/trunk/java/java
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/java/java/org/refal/plus/objects/MailBox.java

    r4080 r4081  
    33package org.refal.plus.objects;
    44
    5 import java.util.ArrayList;
     5import java.util.concurrent.LinkedBlockingQueue;
    66
    77import org.refal.plus.Expr;
    8 import org.refal.plus.Symbol;
    98
    10 public final class Vector extends ArrayList<Expr> {
    11     private static final long serialVersionUID = 1856679299604048781L;
    12    
    13     public void init (Expr expr) throws ClassCastException {
    14         for (Comparable<?> term : expr)
    15             if (term instanceof Expr)
    16                 add((Expr) term);
    17             else
    18                 addAll((Vector) Symbol.getObject(term));
    19     }
    20 
    21     public Vector subVector (int s, int e) {
    22         Vector vector = new Vector();
    23         vector.addAll(subList(s, e));
    24         return vector;
    25     }
     9public final class MailBox extends LinkedBlockingQueue<Expr> {
     10    private static final long serialVersionUID = 1964658309366552579L;
    2611
    2712    public Expr toExpr () {
  • to-imperative/trunk/java/java/refal/plus/Parallel.java

    r4080 r4081  
    44
    55import org.refal.plus.Expr;
     6import org.refal.plus.Func;
    67import org.refal.plus.RefalException;
    78import org.refal.plus.RefalFormat;
     
    910import org.refal.plus.Symbol;
    1011
    11 public final class Box {
     12public final class Parallel {
    1213    /** Don't let anyone instantiate this class */
    13     private Box () {}
     14    private Parallel () {}
    1415
    15     static org.refal.plus.objects.Box getBox (Expr expr) throws ClassCastException {
    16         return (org.refal.plus.objects.Box) Symbol.getObject(expr);
     16    static org.refal.plus.objects.MailBox getMailBox (Expr expr) throws ClassCastException {
     17        return (org.refal.plus.objects.MailBox) Symbol.getObject(expr);
    1718    }
    1819
    19     @SuppressWarnings("all")
    20     @RefalFormat("$func Box e.expr = s.box;")
    21     public static void Box (Expr expr, Result box) {
    22         org.refal.plus.objects.Box b = new org.refal.plus.objects.Box();
    23         b.expr = expr;
    24         Symbol.setObject(b, box);
    25     }
    26 
    27     @RefalFormat("$func Get s.box = e.expr;")
    28     public static void Get (Expr box, Result expr) throws RefalException {
     20    @RefalFormat("$func ParApply s.func e.arg = ;")
     21    public static void ParApply (Expr func, Expr arg) throws RefalException {
    2922        try {
    30             expr.assign(getBox(box).expr);
    31         } catch (ClassCastException _) {
    32             throw new RefalException("refal.plus.Box.Get", "Invalid argument");
     23            final Func f = (Func) Symbol.getObject(func);
     24            final Expr a = arg;
     25            new Thread () {
     26                public void run () {
     27                    try {
     28                        f.eval(a, new Result());
     29                    } catch (RefalException e) {
     30                        // TODO Auto-generated catch block
     31                    }
     32                }
     33            }.start();
     34        } catch (ClassCastException e) {
     35            throw new RefalException("refal.plus.Parallel.ParApply", "Invalid argument");
    3336        }
    3437    }
    3538
    36     @RefalFormat("$func Store s.box e.expr = ;")
    37     public static void Store (Expr box, Expr expr) throws RefalException {
    38         assert box.length() == 1;
     39    @RefalFormat("$func MailBox = s.box;")
     40    public static void MailBox (Result box) {
     41        Symbol.setObject(new org.refal.plus.objects.MailBox(), box);
     42    }
     43
     44    @RefalFormat("$func? IRecv s.box = e.expr ;")
     45    public static boolean IRecv (Expr box, Result expr) throws RefalException {
    3946        try {
    40             getBox(box).expr = expr;
     47            Expr msg = getMailBox(box).poll();
     48            if (msg == null)
     49                return false;
     50            expr.assign(msg);
     51            return true;
    4152        } catch (ClassCastException _) {
    42             throw new RefalException("refal.plus.Box.Store", "Invalid argument");
     53            throw new RefalException("refal.plus.Parallel.IRecv", "Invalid argument");
    4354        }
    4455    }
    4556
    46     @RefalFormat("$func Put s.box e.expr = ;")
    47     public static void Put (Expr box, Expr expr) throws RefalException {
    48         org.refal.plus.objects.Box b = getBox(box);
    49         b.expr = Expr.concat(b.expr, expr);
     57    @RefalFormat("$func Recv s.box = e.expr;")
     58    public static void Recv (Expr box, Result expr) throws RefalException {
     59        try {
     60            expr.assign(getMailBox(box).take());
     61        } catch (ClassCastException _) {
     62            throw new RefalException("refal.plus.Parallel.Recv", "Invalid argument");
     63        } catch (InterruptedException _) {
     64            throw new RefalException("refal.plus.Parallel.Recv", "Interrupted");
     65        }
    5066    }
    5167
     68    @RefalFormat("$func Send s.box e.expr = ;")
     69    public static void Send (Expr box, Expr expr) throws RefalException {
     70        assert box.length() == 1;
     71        try {
     72            getMailBox(box).add(expr);
     73        } catch (ClassCastException _) {
     74            throw new RefalException("refal.plus.Parallel.Send", "Invalid argument");
     75        }
     76    }
    5277}
Note: See TracChangeset for help on using the changeset viewer.