Skip to content

Commit d6f8552

Browse files
committed
Fixes OpenHFT#30 - added synchronization for lastAffinityLock
1 parent 9be0097 commit d6f8552

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

affinity/src/main/java/net/openhft/affinity/AffinityThreadFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jetbrains.annotations.Nullable;
2222

2323
import java.util.concurrent.ThreadFactory;
24+
import java.util.concurrent.atomic.AtomicReference;
2425

2526
/**
2627
* This is a ThreadFactory which assigns threads based the strategies provided.
@@ -56,17 +57,19 @@ public synchronized Thread newThread(@NotNull final Runnable r) {
5657
Thread t = new Thread(new Runnable() {
5758
@Override
5859
public void run() {
59-
AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock.acquireLock(strategies);
60-
try {
61-
if (al.cpuId() >= 0)
62-
lastAffinityLock = al;
60+
try (AffinityLock ignored = acquireLockBasedOnLast()) {
6361
r.run();
64-
} finally {
65-
al.release();
6662
}
6763
}
6864
}, name2);
6965
t.setDaemon(daemon);
7066
return t;
7167
}
68+
69+
private synchronized AffinityLock acquireLockBasedOnLast() {
70+
AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock.acquireLock(strategies);
71+
if (al.cpuId() >= 0)
72+
lastAffinityLock = al;
73+
return al;
74+
}
7275
}

affinity/src/test/java/net/openhft/affinity/impl/AbstractAffinityImplTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ public void setAffinityCompletesGracefully() {
7070
}
7171

7272
@Test
73-
@Ignore("TODO FIX")
7473
public void getAffinityReturnsValuePreviouslySet() {
7574
final IAffinity impl = getImpl();
76-
final int cores = CORES;
77-
for (int core = 0; core < cores; core++) {
75+
for (int core = 0; core < CORES; core++) {
7876
final BitSet mask = new BitSet();
7977
mask.set(core, true);
8078
getAffinityReturnsValuePreviouslySet(impl, mask);

0 commit comments

Comments
 (0)