comparison src/main/java/alice/codesegment/InputDataSegment.java @ 345:8f71c3e6f11d

Change directory structure Maven standard
author sugi
date Wed, 16 Apr 2014 18:26:07 +0900
parents
children aadea6a59376
comparison
equal deleted inserted replaced
344:9f97ec18f8c5 345:8f71c3e6f11d
1 package alice.codesegment;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 import alice.datasegment.Command;
6 import alice.datasegment.CommandType;
7 import alice.datasegment.DataSegment;
8 import alice.datasegment.ReceiveLocalData;
9 import alice.datasegment.ReceiveRemoteData;
10 import alice.datasegment.Receiver;
11
12 /**
13 * InputDataSegment Manager
14 * keep tracking unbound/bound count
15 * @author kazz
16 *
17 */
18 public class InputDataSegment {
19
20 public CodeSegment cs;
21 private AtomicInteger count = new AtomicInteger(1); // 1 for no input data segments
22 private AtomicInteger keyCount = new AtomicInteger(0); // number of DataSegments
23 public InputDataSegment(CodeSegment cs) {
24 this.cs = cs;
25 }
26
27 public void init(){
28 count = new AtomicInteger(1);
29 keyCount = new AtomicInteger(0);
30 }
31
32 public void quickPeek(Receiver receiver) {
33 cs.list.add(receiver);
34 if (receiver.managerKey==null){
35 DataSegment.getLocal().peek(receiver, cs);
36 } else {
37 DataSegment.get(receiver.managerKey).quickPeek(receiver ,cs);
38 }
39 }
40
41 public void quickTake(Receiver receiver) {
42 cs.list.add(receiver);
43 if (receiver.managerKey==null){
44 DataSegment.getLocal().quickTake(receiver, cs);
45 } else {
46 DataSegment.get(receiver.managerKey).quickTake(receiver ,cs);
47 }
48 }
49
50 public void peek(Receiver receiver) {
51 cs.list.add(receiver);
52 if (receiver.managerKey==null){
53 DataSegment.getLocal().peek(receiver, cs);
54 } else {
55 DataSegment.get(receiver.managerKey).peek(receiver, cs);
56 }
57 }
58
59
60 public void take(Receiver receiver) {
61 cs.list.add(receiver);
62 if (receiver.managerKey==null){
63 DataSegment.getLocal().take(receiver, cs);
64 } else {
65 DataSegment.get(receiver.managerKey).take(receiver, cs);
66 }
67 }
68
69 public void reply(Receiver receiver, Command reply) {
70 receiver.index = reply.index;
71 receiver.from = reply.reverseKey;
72 if (reply.reverseKey==null){
73 receiver.setData(new ReceiveRemoteData(reply.val));
74 } else if (!reply.reverseKey.equals("local")) {
75 receiver.setData(new ReceiveRemoteData(reply.val));
76 } else {
77 receiver.setData(new ReceiveLocalData(reply.obj));
78 }
79 receive();
80 }
81
82 public void register() {
83 count.getAndIncrement();
84 keyCount.getAndIncrement();
85 }
86
87 public void setKey() {
88 if (keyCount.decrementAndGet() == 0) {
89 receive();
90 }
91 }
92
93 public void receive() {
94 if (count.decrementAndGet() == 0) {
95 CodeSegmentManager.submit(cs);
96 }
97 }
98
99 /**
100 * InputDataSegment factory
101 * @param type PEEK or TAKE
102 * @return Receiver of DataSegment reply
103 */
104 public Receiver create(CommandType type) {
105 return new Receiver(this, type);
106 }
107
108 public void recommand(Receiver receiver) {
109 // TODO why only local?
110 DataSegment.getLocal().recommand(receiver, cs);
111 }
112
113 public void setCounter(int cnt){
114 count.set(cnt);
115 }
116 }