Skip to content

Commit 1716c02

Browse files
committed
Merge branch 'master' of github.com:OpenHFT/Java-Thread-Affinity
2 parents 4ada7ab + d6f8552 commit 1716c02

4 files changed

Lines changed: 13 additions & 18 deletions

File tree

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public class AffinityLock implements Closeable {
6262
PROCESSORS = processors;
6363
BASE_AFFINITY = Affinity.getAffinity();
6464
RESERVED_AFFINITY = getReservedAffinity0();
65-
LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS));
66-
if (cpuLayout != null)
67-
LOCK_INVENTORY.set(cpuLayout);
65+
LOCK_INVENTORY = new LockInventory(cpuLayout == null ? new NoCpuLayout(PROCESSORS) : cpuLayout);
6866
}
6967

7068
/**

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/AffinityLockTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
/**
3939
* @author peter.lawrey
4040
*/
41-
@SuppressWarnings("ALL")
4241
public class AffinityLockTest {
4342
private static final Logger logger = LoggerFactory.getLogger(AffinityLockTest.class);
4443

@@ -177,7 +176,7 @@ public void testIssue19() {
177176
System.out.println("AffinityLock.PROCESSORS=" + AffinityLock.PROCESSORS);
178177

179178
AffinityLock al = AffinityLock.acquireLock();
180-
List<AffinityLock> locks = new ArrayList<AffinityLock>();
179+
List<AffinityLock> locks = new ArrayList<>();
181180
locks.add(al);
182181
for (int i = 0; i < 256; i++)
183182
locks.add(al = al.acquireLock(AffinityStrategies.DIFFERENT_SOCKET,
@@ -199,8 +198,7 @@ public void testAffinity() throws InterruptedException {
199198
// System.out.println("Started");
200199
logger.info("Started");
201200
displayStatus();
202-
final AffinityLock al = AffinityLock.acquireLock();
203-
try {
201+
try (AffinityLock al = AffinityLock.acquireLock()) {
204202
System.out.println("Main locked");
205203
displayStatus();
206204
Thread t = new Thread(new Runnable() {
@@ -216,8 +214,6 @@ public void run() {
216214
t.join();
217215
System.out.println("Thread-0 unlocked");
218216
displayStatus();
219-
} finally {
220-
al.close();
221217
}
222218
System.out.println("All unlocked");
223219
displayStatus();

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)