Author: toad
Date: 2008-03-24 23:34:18 +0000 (Mon, 24 Mar 2008)
New Revision: 18757

Modified:
   trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java
Log:
Simplify locking.

Modified: trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java      
2008-03-24 23:31:11 UTC (rev 18756)
+++ trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java      
2008-03-24 23:34:18 UTC (rev 18757)
@@ -109,15 +109,11 @@
                                        Logger.minor(this, "No blocks to 
remove");
                                return null;
                        }
-               }
                for(int i=0;i<10;i++) {
                        Object ret;
                        int x;
-                       synchronized(segment) {
                                x = ctx.random.nextInt(blockNums.size());
                                ret = (Integer) blockNums.remove(x);
-                       }
-                       // LOCKING: keys is safe to check, but segment isn't.
                        Key key = 
segment.getBlockNodeKey(((Integer)ret).intValue());
                        if(key == null) {
                                if(segment.isFinishing() || 
segment.isFinished()) return null;
@@ -131,10 +127,7 @@
                                // Double check that it hasn't been found.
                                key = 
segment.getBlockNodeKey(((Integer)ret).intValue());
                                if(key == null) {
-                                       // A race condition is possible but 
should only rarely cause a Key is null for block <block>.
-                                       synchronized(segment) {
                                                blockNums.add(ret);
-                                       }
                                }
                                continue;
                        }
@@ -143,24 +136,22 @@
                        return ret;
                }
                return null;
+               }
        }

        public boolean hasValidKeys(KeysFetchingLocally keys) {
+               synchronized(segment) {
                for(int i=0;i<10;i++) {
                        Object ret;
                        int x;
-                       synchronized(segment) {
                                if(blockNums.isEmpty()) return false;
                                x = ctx.random.nextInt(blockNums.size());
                                ret = (Integer) blockNums.get(x);
-                       }
                        // LOCKING: keys is safe to check, but segment isn't.
                        Key key = 
segment.getBlockNodeKey(((Integer)ret).intValue());
                        if(key == null) {
                                Logger.error(this, "Key is null for block 
"+ret+" for "+this+" in hasValidKeys()");
-                               synchronized(segment) {
                                        blockNums.remove(x);
-                               }
                                continue;
                        }
                        if(keys.hasKey(key)) {
@@ -169,6 +160,7 @@
                        return true;
                }
                return false;
+               }
        }

        public boolean ignoreStore() {
@@ -380,9 +372,7 @@
                        if(!blockNums.isEmpty()) return;
                        if(logMINOR)
                                Logger.minor(this, "Definitely removing from 
parent: "+this);
-               }
                if(!segment.maybeRemoveSeg(this)) return;
-               synchronized(segment) {
                        cancelled = true;
                }
                unregister();
@@ -392,6 +382,7 @@
                if(logMINOR) Logger.minor(this, "onGotKey("+key+")");
                // Find and remove block if it is on this subsegment. However 
it may have been
                // removed already.
+               int blockNo;
                synchronized(segment) {
                        for(int i=0;i<blockNums.size();i++) {
                                Integer token = (Integer) blockNums.get(i);
@@ -402,8 +393,8 @@
                                        break;
                                }
                        }
+               blockNo = segment.getBlockNumber(key);
                }
-               int blockNo = segment.getBlockNumber(key);
                if(blockNo == -1) {
                        Logger.minor(this, "No block found for key "+key+" on 
"+this);
                        return;


Reply via email to