view src/alice/codesegment/InputDataSegment.java @ 192:a8b0d44bbe68 working

not use value object in bitonic
author e095732
date Thu, 07 Mar 2013 22:20:54 +0900
parents a85ff8dc16c1
children f151dea22b2c
line wrap: on
line source

package alice.codesegment;

import java.util.concurrent.atomic.AtomicInteger;

import alice.datasegment.CommandType;
import alice.datasegment.DataSegment;
import alice.datasegment.DataSegmentValue;
import alice.datasegment.Receiver;

/**
 * InputDataSegment Manager
 * 			keep tracking unbound/bound count
 * @author kazz
 *
 */
public class InputDataSegment {
	
	public CodeSegment cs;
	private AtomicInteger count = new AtomicInteger(1); // 1 for no input data segments
	private AtomicInteger keyCount = new AtomicInteger(0); // number of DataSegments
	
	public InputDataSegment(CodeSegment cs) {
		this.cs = cs;
	}
	
	public void peek(Receiver receiver, String managerKey, String key) {
		peek(receiver, managerKey, key, 0);
	}
	
	public void peek(Receiver receiver, String managerKey, String key, int index) {
		DataSegment.get(managerKey).peek(receiver, key, index, cs);
	}

	public void peek(Receiver receiver, String key) {
		peek(receiver, key, 0);
	}
	
	public void peek(Receiver receiver, String key, int index) {
		DataSegment.getLocal().peek(receiver, key, index, cs);
	}

	public void take(Receiver receiver, String managerKey, String key) {
		take(receiver, managerKey, key, 0);
	}
	
	public void take(Receiver receiver, String managerKey, String key, int index) {
		DataSegment.get(managerKey).take(receiver, key, index, cs);
	}

	public void take(Receiver receiver, String key) {
		take(receiver, key, 0);
	}
	
	public void take(Receiver receiver, String key, int index) {
		DataSegment.getLocal().take(receiver, key, index, cs);
	}

	public void reply(Receiver receiver, DataSegmentValue val) {
		receiver.index = val.index;
		receiver.val = val.val;
		receiver.from = val.from;
		receiver.obj = val.obj;
		receive();
	}

	public void regist() {
		count.getAndIncrement();
		keyCount.getAndIncrement();
	}
	
	public void setKey() {
		if (keyCount.decrementAndGet() == 0) {
			receive();
		}
	}
	
	public void receive() {
		if (count.decrementAndGet() == 0) {
			CodeSegmentManager.submit(cs);
		}
	}
	
	/**
	 * InputDataSegment factory
	 * @param type PEEK or TAKE
	 * @return Receiver of DataSegment reply 
	 */
	public Receiver create(CommandType type) {
		return new Receiver(this, type);
	}

}