// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 13/07/2009 23:05:05
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   One2AnyChannel.java

package jcsp.lang;

import java.io.Serializable;
import jcsp.util.ChannelDataStore;

// Referenced classes of package jcsp.lang:
//            ProcessInterruptedError, One2AnyChannelX, ChannelInput, ChannelOutput

public class One2AnyChannel
    implements ChannelInput, ChannelOutput, Serializable
{

    public One2AnyChannel()
    {
        rwMonitor = new Object();
        empty = true;
    }

    public Object read()
    {
        synchronized(readMonitor)
        {
            synchronized(rwMonitor)
            {
                if(empty)
                {
                    empty = false;
                    try
                    {
                        rwMonitor.wait();
                    }
                    catch(InterruptedException interruptedexception)
                    {
                        throw new ProcessInterruptedError("*** Thrown from One2AnyChannel.read ()\n" + interruptedexception.toString());
                    }
                } else
                {
                    empty = true;
                }
                rwMonitor.notify();
                Object obj2 = hold;
                return obj2;
            }
        }
    }

    public void write(Object obj)
    {
        synchronized(rwMonitor)
        {
            hold = obj;
            if(empty)
            {
                empty = false;
            } else
            {
                empty = true;
                rwMonitor.notify();
            }
            try
            {
                rwMonitor.wait();
            }
            catch(InterruptedException interruptedexception)
            {
                throw new ProcessInterruptedError("*** Thrown from One2AnyChannel.write (Object)\n" + interruptedexception.toString());
            }
        }
    }

    public static One2AnyChannel[] create(int i)
    {
        One2AnyChannel aone2anychannel[] = new One2AnyChannel[i];
        for(int j = 0; j < i; j++)
            aone2anychannel[j] = new One2AnyChannel();

        return aone2anychannel;
    }

    public static One2AnyChannel create(ChannelDataStore channeldatastore)
    {
        return new One2AnyChannelX(channeldatastore);
    }

    public static One2AnyChannel[] create(int i, ChannelDataStore channeldatastore)
    {
        One2AnyChannel aone2anychannel[] = new One2AnyChannel[i];
        for(int j = 0; j < i; j++)
            aone2anychannel[j] = new One2AnyChannelX(channeldatastore);

        return aone2anychannel;
    }

    protected Object rwMonitor;
    private Object hold;
    private boolean empty;
    protected final Object readMonitor = new Object();
}
