mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Fixed race condition when closing redundant sockets.
When more than one invitation socket is opened, Alice should pick which one to use and Bob should use whichever one Alice picks. This fixes a race condition where each party picked a different socket and closed the other.
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
package org.briarproject.util;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class LatchedReference<T> {
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
private final AtomicReference<T> reference = new AtomicReference<T>();
|
||||
|
||||
public boolean isSet() {
|
||||
return reference.get() != null;
|
||||
}
|
||||
|
||||
public boolean set(T t) {
|
||||
if (t == null) throw new IllegalArgumentException();
|
||||
if (reference.compareAndSet(null, t)) {
|
||||
latch.countDown();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public T waitForReference(long timeout) throws InterruptedException {
|
||||
latch.await(timeout, MILLISECONDS);
|
||||
return reference.get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user