Use immutable collections for thread safety.

This commit is contained in:
akwizgran
2011-11-29 11:01:09 +00:00
parent 42430272f4
commit 7bf2ee64a8
15 changed files with 62 additions and 46 deletions

View File

@@ -10,6 +10,7 @@ import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.util.ByteUtils;
// This class is not thread-safe
class ConnectionWindowImpl implements ConnectionWindow {
private final CryptoComponent crypto;

View File

@@ -6,7 +6,9 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -58,11 +60,10 @@ abstract class StreamConnection implements DatabaseListener {
protected final ContactId contactId;
protected final StreamTransportConnection connection;
// These fields must only be accessed with this's lock held
private int writerFlags = 0;
private Collection<MessageId> offered = null;
private Collection<MessageId> requested = null;
private Offer incomingOffer = null;
private int writerFlags = 0; // Locking: this
private Collection<MessageId> offered = null; // Locking: this
private LinkedList<MessageId> requested = null; // Locking: this
private Offer incomingOffer = null; // Locking: this
StreamConnection(ConnectionReaderFactory connReaderFactory,
ConnectionWriterFactory connWriterFactory, DatabaseComponent db,
@@ -143,15 +144,15 @@ abstract class StreamConnection implements DatabaseListener {
}
// Work out which messages were requested
BitSet b = r.getBitmap();
Collection<MessageId> req = new LinkedList<MessageId>();
Collection<MessageId> seen = new ArrayList<MessageId>();
LinkedList<MessageId> req = new LinkedList<MessageId>();
List<MessageId> seen = new ArrayList<MessageId>();
int i = 0;
for(MessageId m : off) {
if(b.get(i++)) req.add(m);
else seen.add(m);
}
// Mark the unrequested messages as seen
db.setSeen(contactId, seen);
db.setSeen(contactId, Collections.unmodifiableList(seen));
// Store the requested message IDs and notify the writer
synchronized(this) {
if(requested != null)