diff src/alice/datasegment/DataSegmentKey.java @ 17:bb075e103cd3

bug fix for take()
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Sun, 15 Jan 2012 15:18:01 +0900
parents e3f1b21718b0
children 72dd27d952b0
line wrap: on
line diff
--- a/src/alice/datasegment/DataSegmentKey.java	Sun Jan 15 12:17:30 2012 +0900
+++ b/src/alice/datasegment/DataSegmentKey.java	Sun Jan 15 15:18:01 2012 +0900
@@ -1,6 +1,7 @@
 package alice.datasegment;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -40,19 +41,18 @@
 							DataSegmentValue dsv = new DataSegmentValue(index, cmd.val); 
 							dataList.add(dsv);
 							// run waiting peek and take
-							LinkedList<Command> removeList = new LinkedList<Command>();
-							for (Command waitCmd : waitList) {
+							boolean takeFlag = true;
+							for (Iterator<Command> iter = waitList.iterator(); iter.hasNext() && takeFlag; ) {
+								Command waitCmd = iter.next();
 								if (waitCmd.index < index) {
 									waitCmd.replyQueue.put(new Command(CommandType.REPLY, null, null, cmd.val, index, waitCmd.seq, null, null));
-									removeList.add(waitCmd);
+									iter.remove();
 									if (waitCmd.type == CommandType.TAKE) { // delete data, if it run take cmd.
 										dataList.remove(dsv);
 										break;
 									}
 								}
-							}
-							for (Command rmCmd : removeList) {
-								waitList.remove(rmCmd);
+								
 							}
 							break;
 						case PEEK:
@@ -73,14 +73,18 @@
 								waitList.add(cmd);
 								break;
 							}
-							for (DataSegmentValue data : dataList) {
+							boolean waitFlag = true;
+							for (Iterator<DataSegmentValue> iter = dataList.iterator(); iter.hasNext(); ) {
+								DataSegmentValue data = iter.next();
 								if (data.index > cmd.index) {
 									cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null));
-									dataList.remove(data);
+									iter.remove();
+									waitFlag = false;
 									break;
 								}
 							}
-							waitList.add(cmd);
+							if (waitFlag)
+								waitList.add(cmd);
 							break;
 						case REMOVE:
 							// TODO: implements later